Make key class
This commit is contained in:
parent
620d4bc03e
commit
62a86c5253
8 changed files with 193 additions and 167 deletions
|
@ -1,5 +1,5 @@
|
|||
import * as dom from '../shared/utils/dom';
|
||||
import Key, * as keys from './domains/Key';
|
||||
import Key from './domains/Key';
|
||||
|
||||
const cancelKey = (e: KeyboardEvent): boolean => {
|
||||
if (e.key === 'Escape') {
|
||||
|
@ -66,7 +66,7 @@ export default class InputDriver {
|
|||
return;
|
||||
}
|
||||
|
||||
let key = keys.fromKeyboardEvent(e);
|
||||
let key = Key.fromKeyboardEvent(e);
|
||||
for (let listener of this.onKeyListeners) {
|
||||
let stop = listener(key);
|
||||
if (stop) {
|
||||
|
|
|
@ -35,7 +35,7 @@ export class FollowMasterClientImpl implements FollowMasterClient {
|
|||
this.postMessage({
|
||||
type: messages.FOLLOW_KEY_PRESS,
|
||||
key: key.key,
|
||||
ctrlKey: key.ctrlKey || false,
|
||||
ctrlKey: key.ctrl || false,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,3 @@
|
|||
export default interface Key {
|
||||
key: string;
|
||||
shiftKey?: boolean;
|
||||
ctrlKey?: boolean;
|
||||
altKey?: boolean;
|
||||
metaKey?: boolean;
|
||||
}
|
||||
|
||||
const modifiedKeyName = (name: string): string => {
|
||||
if (name === ' ') {
|
||||
return 'Space';
|
||||
|
@ -18,55 +10,84 @@ const modifiedKeyName = (name: string): string => {
|
|||
return name;
|
||||
};
|
||||
|
||||
export const 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;
|
||||
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;
|
||||
}
|
||||
|
||||
return {
|
||||
key: modifiedKeyName(e.key),
|
||||
shiftKey: shift,
|
||||
ctrlKey: e.ctrlKey,
|
||||
altKey: e.altKey,
|
||||
metaKey: e.metaKey,
|
||||
};
|
||||
};
|
||||
|
||||
export const fromMapKey = (key: string): Key => {
|
||||
if (key.startsWith('<') && key.endsWith('>')) {
|
||||
let inner = key.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();
|
||||
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 {
|
||||
key: base,
|
||||
shiftKey: inner.includes('S-'),
|
||||
ctrlKey: inner.includes('C-'),
|
||||
altKey: inner.includes('A-'),
|
||||
metaKey: inner.includes('M-'),
|
||||
};
|
||||
}
|
||||
return {
|
||||
key: key,
|
||||
shiftKey: key.toLowerCase() !== key,
|
||||
ctrlKey: false,
|
||||
altKey: false,
|
||||
metaKey: false,
|
||||
};
|
||||
};
|
||||
|
||||
export const equals = (e1: Key, e2: Key): boolean => {
|
||||
return e1.key === e2.key &&
|
||||
e1.ctrlKey === e2.ctrlKey &&
|
||||
e1.metaKey === e2.metaKey &&
|
||||
e1.altKey === e2.altKey &&
|
||||
e1.shiftKey === e2.shiftKey;
|
||||
};
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import Key, * as keyUtils from './Key';
|
||||
import Key from './Key';
|
||||
|
||||
export default class KeySequence {
|
||||
private keys: Key[];
|
||||
|
@ -24,7 +24,7 @@ export default class KeySequence {
|
|||
return false;
|
||||
}
|
||||
for (let i = 0; i < o.keys.length; ++i) {
|
||||
if (!keyUtils.equals(this.keys[i], o.keys[i])) {
|
||||
if (!this.keys[i].equals(o.keys[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ export const fromMapKeys = (keys: string): KeySequence => {
|
|||
|
||||
return fromMapKeysRecursive(
|
||||
remainings.slice(nextPos),
|
||||
mappedKeys.concat([keyUtils.fromMapKey(remainings.slice(0, nextPos))])
|
||||
mappedKeys.concat([Key.fromMapKey(remainings.slice(0, nextPos))])
|
||||
);
|
||||
};
|
||||
|
||||
|
|
Reference in a new issue