diff options
Diffstat (limited to 'src/content/domains')
-rw-r--r-- | src/content/domains/Key.ts | 93 | ||||
-rw-r--r-- | src/content/domains/KeySequence.ts | 54 |
2 files changed, 0 insertions, 147 deletions
diff --git a/src/content/domains/Key.ts b/src/content/domains/Key.ts deleted file mode 100644 index 669edfc..0000000 --- a/src/content/domains/Key.ts +++ /dev/null @@ -1,93 +0,0 @@ -const modifiedKeyName = (name: string): string => { - if (name === ' ') { - return 'Space'; - } - if (name.length === 1) { - return name; - } else if (name === 'Escape') { - return 'Esc'; - } - return name; -}; - -export default class Key { - public readonly key: string; - - public readonly shift: boolean; - - public readonly ctrl: boolean; - - public readonly alt: boolean; - - public readonly meta: boolean; - - constructor({ key, shift, ctrl, alt, meta }: { - key: string; - shift: boolean; - ctrl: boolean; - alt: boolean; - meta: boolean; - }) { - this.key = key; - this.shift = shift; - this.ctrl = ctrl; - this.alt = alt; - this.meta = meta; - } - - static fromMapKey(str: string): Key { - if (str.startsWith('<') && str.endsWith('>')) { - let inner = str.slice(1, -1); - let shift = inner.includes('S-'); - let base = inner.slice(inner.lastIndexOf('-') + 1); - if (shift && base.length === 1) { - base = base.toUpperCase(); - } else if (!shift && base.length === 1) { - base = base.toLowerCase(); - } - return new Key({ - key: base, - shift: shift, - ctrl: inner.includes('C-'), - alt: inner.includes('A-'), - meta: inner.includes('M-'), - }); - } - - return new Key({ - key: str, - shift: str.toLowerCase() !== str, - ctrl: false, - alt: false, - meta: false, - }); - } - - static fromKeyboardEvent(e: KeyboardEvent): Key { - let key = modifiedKeyName(e.key); - let shift = e.shiftKey; - if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) { - // make shift false for symbols to enable key bindings by symbold keys. - // But this limits key bindings by symbol keys with Shift - // (such as Shift+$>. - shift = false; - } - - return new Key({ - key: modifiedKeyName(e.key), - shift: shift, - ctrl: e.ctrlKey, - alt: e.altKey, - meta: e.metaKey, - }); - } - - equals(key: Key) { - return this.key === key.key && - this.ctrl === key.ctrl && - this.meta === key.meta && - this.alt === key.alt && - this.shift === key.shift; - } -} - diff --git a/src/content/domains/KeySequence.ts b/src/content/domains/KeySequence.ts deleted file mode 100644 index abae61a..0000000 --- a/src/content/domains/KeySequence.ts +++ /dev/null @@ -1,54 +0,0 @@ -import Key from './Key'; - -export default class KeySequence { - constructor( - public readonly keys: Key[], - ) { - } - - push(key: Key): number { - return this.keys.push(key); - } - - length(): number { - return this.keys.length; - } - - startsWith(o: KeySequence): boolean { - if (this.keys.length < o.keys.length) { - return false; - } - for (let i = 0; i < o.keys.length; ++i) { - if (!this.keys[i].equals(o.keys[i])) { - return false; - } - } - return true; - } - - static fromMapKeys(keys: string): KeySequence { - const fromMapKeysRecursive = ( - remaining: string, mappedKeys: Key[], - ): Key[] => { - if (remaining.length === 0) { - return mappedKeys; - } - - let nextPos = 1; - if (remaining.startsWith('<')) { - let ltPos = remaining.indexOf('>'); - if (ltPos > 0) { - nextPos = ltPos + 1; - } - } - - return fromMapKeysRecursive( - remaining.slice(nextPos), - mappedKeys.concat([Key.fromMapKey(remaining.slice(0, nextPos))]) - ); - }; - - let data = fromMapKeysRecursive(keys, []); - return new KeySequence(data); - } -} |