-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from suvash/add-traditional-layout-and-refactor
Add 'traditional' layout with tests and refactor the library to support multiple layouts
- Loading branch information
Showing
17 changed files
with
777 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Setup the project and run tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out code | ||
id: check-out-code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install packages and run tests | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
- run: npm install | ||
- run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { fetchLayout, defaultLayout } from "./layouts"; | ||
|
||
function wrapHandlerwithLayout(layout) { | ||
function isValidTarget(target) { | ||
return target.type === "text" || target.type === "textarea"; | ||
} | ||
|
||
const handler = function handleKeypress(event) { | ||
if (isValidTarget(event.target)) { | ||
const formattedKey = layout.formatKey(event.key); | ||
if (formattedKey) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
const selectionStart = event.target.selectionStart; | ||
const selectionEnd = event.target.selectionEnd; | ||
|
||
const beforeSelection = event.target.value.substring(0, selectionStart); | ||
const afterSelection = event.target.value.substring(selectionEnd); | ||
|
||
event.target.value = beforeSelection + formattedKey + afterSelection; | ||
const newSelectionPos = selectionStart + formattedKey.length; | ||
|
||
event.target.setSelectionRange(newSelectionPos, newSelectionPos); | ||
} | ||
} | ||
}; | ||
|
||
return handler; | ||
} | ||
|
||
export function interceptElementById(dirtyIdSelector, dirtyOptions) { | ||
const defaultOptions = { | ||
layout: defaultLayout, | ||
enable: true, | ||
}; | ||
|
||
const options = { ...defaultOptions, ...dirtyOptions }; | ||
const selector = String(dirtyIdSelector); | ||
const el = document.getElementById(selector); | ||
|
||
const layout = fetchLayout(options.layout); | ||
const handler = wrapHandlerwithLayout(layout); | ||
var enabled = false; | ||
|
||
if (options.enable) { | ||
enable(); | ||
} | ||
|
||
function enable() { | ||
el.addEventListener("keypress", handler); | ||
enabled = true; | ||
} | ||
|
||
function disable() { | ||
el.removeEventListener("keypress", handler); | ||
enabled = false; | ||
} | ||
|
||
function isEnabled() { | ||
return enabled; | ||
} | ||
|
||
return { | ||
el: el, | ||
enable: enable, | ||
disable: disable, | ||
isEnabled: isEnabled, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { fetchLayout, defaultLayout } from "./layouts"; | ||
|
||
export function format(dirtyStr, dirtyOptions) { | ||
const defaultOptions = { | ||
layout: defaultLayout, | ||
}; | ||
const text = String(dirtyStr); | ||
const options = { ...defaultOptions, ...dirtyOptions }; | ||
|
||
const layout = fetchLayout(options.layout); | ||
|
||
return Array.from(text, (c) => layout.formatKey(c) || c).join(""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.