Define Key and KeySequence

jh-changes
Shin'ya Ueoka 6 years ago
parent 2ec912c262
commit a5518dce3d
  1. 6
      src/content/InputDriver.ts
  2. 4
      src/content/actions/index.ts
  3. 4
      src/content/actions/input.ts
  4. 2
      src/content/client/FollowMasterClient.ts
  5. 2
      src/content/components/common/follow.ts
  6. 12
      src/content/components/common/index.ts
  7. 87
      src/content/components/common/keymapper.ts
  8. 6
      src/content/components/common/mark.ts
  9. 2
      src/content/controllers/KeymapController.ts
  10. 4
      src/content/controllers/MarkKeyController.ts
  11. 47
      src/content/domains/Key.ts
  12. 64
      src/content/domains/KeySequence.ts
  13. 4
      src/content/reducers/input.ts
  14. 11
      src/content/repositories/KeymapRepository.ts
  15. 35
      src/content/usecases/KeymapUseCase.ts
  16. 69
      test/content/domains/Key.test.ts
  17. 72
      test/content/domains/KeySequence.test.ts

@ -1,5 +1,5 @@
import * as dom from '../shared/utils/dom';
import * as keys from '../shared/utils/keys';
import Key, * as keys from './domains/Key';
const cancelKey = (e: KeyboardEvent): boolean => {
return e.key === 'Escape' || e.key === '[' && e.ctrlKey;
@ -8,7 +8,7 @@ const cancelKey = (e: KeyboardEvent): boolean => {
export default class InputDriver {
private pressed: {[key: string]: string} = {};
private onKeyListeners: ((key: keys.Key) => boolean)[] = [];
private onKeyListeners: ((key: Key) => boolean)[] = [];
constructor(target: HTMLElement) {
this.pressed = {};
@ -19,7 +19,7 @@ export default class InputDriver {
target.addEventListener('keyup', this.onKeyUp.bind(this));
}
onKey(cb: (key: keys.Key) => boolean) {
onKey(cb: (key: Key) => boolean) {
this.onKeyListeners.push(cb);
}

@ -1,5 +1,5 @@
import Redux from 'redux';
import * as keyUtils from '../../shared/utils/keys';
import Key from '../domains/Key';
// User input
export const INPUT_KEY_PRESS = 'input.key.press';
@ -25,7 +25,7 @@ export const NOOP = 'noop';
export interface InputKeyPressAction extends Redux.Action {
type: typeof INPUT_KEY_PRESS;
key: keyUtils.Key;
key: Key;
}
export interface InputClearKeysAction extends Redux.Action {

@ -1,7 +1,7 @@
import * as actions from './index';
import * as keyUtils from '../../shared/utils/keys';
import Key from '../domains/Key';
const keyPress = (key: keyUtils.Key): actions.InputAction => {
const keyPress = (key: Key): actions.InputAction => {
return {
type: actions.INPUT_KEY_PRESS,
key,

@ -1,5 +1,5 @@
import * as messages from '../../shared/messages';
import { Key } from '../../shared/utils/keys';
import Key from '../domains/Key';
export default interface FollowMasterClient {
startFollow(newTab: boolean, background: boolean): void;

@ -1,7 +1,7 @@
import MessageListener from '../../MessageListener';
import { LinkHint, InputHint } from '../../presenters/Hint';
import * as messages from '../../../shared/messages';
import { Key } from '../../../shared/utils/keys';
import Key from '../../domains/Key';
import TabsClient, { TabsClientImpl } from '../../client/TabsClient';
import FollowMasterClient, { FollowMasterClientImpl }
from '../../client/FollowMasterClient';

@ -1,11 +1,11 @@
import InputDriver from './../../InputDriver';
import FollowComponent from './follow';
import MarkComponent from './mark';
import KeymapperComponent from './keymapper';
// import KeymapperComponent from './keymapper';
import * as messages from '../../../shared/messages';
import MessageListener from '../../MessageListener';
import * as blacklists from '../../../shared/blacklists';
import * as keys from '../../../shared/utils/keys';
import Key from '../../domains/Key';
import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
import SettingUseCase from '../../usecases/SettingUseCase';
@ -18,11 +18,11 @@ export default class Common {
const input = new InputDriver(win.document.body);
const follow = new FollowComponent();
const mark = new MarkComponent(store);
const keymapper = new KeymapperComponent(store);
// const keymapper = new KeymapperComponent(store);
input.onKey((key: keys.Key) => follow.key(key));
input.onKey((key: keys.Key) => mark.key(key));
input.onKey((key: keys.Key) => keymapper.key(key));
input.onKey((key: Key) => follow.key(key));
input.onKey((key: Key) => mark.key(key));
// input.onKey((key: Key) => keymapper.key(key));
this.reloadSettings();

@ -1,87 +0,0 @@
import * as inputActions from '../../actions/input';
import * as operationActions from '../../actions/operation';
import * as operations from '../../../shared/operations';
import * as keyUtils from '../../../shared/utils/keys';
import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
import { SettingRepositoryImpl } from '../../repositories/SettingRepository';
import { Keymaps } from '../../../shared/Settings';
type KeymapEntityMap = Map<keyUtils.Key[], operations.Operation>;
let addonEnabledUseCase = new AddonEnabledUseCase();
let settingRepository = new SettingRepositoryImpl();
const reservedKeymaps: Keymaps = {
'<Esc>': { type: operations.CANCEL },
'<C-[>': { type: operations.CANCEL },
};
const mapStartsWith = (
mapping: keyUtils.Key[],
keys: keyUtils.Key[],
): boolean => {
if (mapping.length < keys.length) {
return false;
}
for (let i = 0; i < keys.length; ++i) {
if (!keyUtils.equals(mapping[i], keys[i])) {
return false;
}
}
return true;
};
export default class KeymapperComponent {
private store: any;
constructor(store: any) {
this.store = store;
}
key(key: keyUtils.Key): boolean {
this.store.dispatch(inputActions.keyPress(key));
let input = this.store.getState().input;
let keymaps = this.keymapEntityMap();
let matched = Array.from(keymaps.keys()).filter(
(mapping: keyUtils.Key[]) => {
return mapStartsWith(mapping, input.keys);
});
if (!addonEnabledUseCase.getEnabled()) {
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
// the addon disabled
matched = matched.filter((keys) => {
let type = (keymaps.get(keys) as operations.Operation).type;
return type === operations.ADDON_ENABLE ||
type === operations.ADDON_TOGGLE_ENABLED;
});
}
if (matched.length === 0) {
this.store.dispatch(inputActions.clearKeys());
return false;
} else if (matched.length > 1 ||
matched.length === 1 && input.keys.length < matched[0].length) {
return true;
}
let operation = keymaps.get(matched[0]) as operations.Operation;
let act = operationActions.exec(operation);
this.store.dispatch(act);
this.store.dispatch(inputActions.clearKeys());
return true;
}
private keymapEntityMap(): KeymapEntityMap {
let keymaps = {
...settingRepository.get().keymaps,
...reservedKeymaps,
};
let entries = Object.entries(keymaps).map((entry) => {
return [
keyUtils.fromMapKeys(entry[0]),
entry[1],
];
}) as [keyUtils.Key[], operations.Operation][];
return new Map<keyUtils.Key[], operations.Operation>(entries);
}
}

@ -1,12 +1,12 @@
import * as markActions from '../../actions/mark';
import * as consoleFrames from '../..//console-frames';
import * as keyUtils from '../../../shared/utils/keys';
import Key from '../../domains/Key';
import MarkUseCase from '../../usecases/MarkUseCase';
let markUseCase = new MarkUseCase();
const cancelKey = (key: keyUtils.Key): boolean => {
const cancelKey = (key: Key): boolean => {
return key.key === 'Esc' || key.key === '[' && Boolean(key.ctrlKey);
};
@ -18,7 +18,7 @@ export default class MarkComponent {
}
// eslint-disable-next-line max-statements
key(key: keyUtils.Key) {
key(key: Key) {
let { mark: markState } = this.store.getState();
if (!markState.setMode && !markState.jumpMode) {

@ -8,7 +8,7 @@ import FocusUseCase from '../usecases/FocusUseCase';
import ClipboardUseCase from '../usecases/ClipboardUseCase';
import BackgroundClient from '../client/BackgroundClient';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import { Key } from '../../shared/utils/keys';
import Key from '../domains/Key';
export default class KeymapController {
private keymapUseCase: KeymapUseCase;

@ -1,6 +1,6 @@
import MarkUseCase from '../usecases/MarkUseCase';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import * as keys from '../../shared/utils/keys';
import Key from '../domains/Key';
export default class MarkKeyController {
private markUseCase: MarkUseCase;
@ -15,7 +15,7 @@ export default class MarkKeyController {
this.markKeyUseCase = markKeyUseCase;
}
press(key: keys.Key): boolean {
press(key: Key): boolean {
if (this.markKeyUseCase.isSetMode()) {
this.markUseCase.set(key.key);
this.markKeyUseCase.disableSetMode();

@ -1,9 +1,11 @@
export interface Key {
key: string;
shiftKey: boolean | undefined;
ctrlKey: boolean | undefined;
altKey: boolean | undefined;
metaKey: boolean | undefined;
export default interface Key {
key: string;
shiftKey?: boolean;
ctrlKey?: boolean;
altKey?: boolean;
metaKey?: boolean;
// eslint-disable-next-line semi
}
const modifiedKeyName = (name: string): string => {
@ -18,7 +20,7 @@ const modifiedKeyName = (name: string): string => {
return name;
};
const fromKeyboardEvent = (e: KeyboardEvent): Key => {
export const fromKeyboardEvent = (e: KeyboardEvent): Key => {
let key = modifiedKeyName(e.key);
let shift = e.shiftKey;
if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) {
@ -36,7 +38,7 @@ const fromKeyboardEvent = (e: KeyboardEvent): Key => {
};
};
const fromMapKey = (key: string): Key => {
export const fromMapKey = (key: string): Key => {
if (key.startsWith('<') && key.endsWith('>')) {
let inner = key.slice(1, -1);
let shift = inner.includes('S-');
@ -63,37 +65,10 @@ const fromMapKey = (key: string): Key => {
};
};
const fromMapKeys = (keys: string): Key[] => {
const fromMapKeysRecursive = (
remainings: string, mappedKeys: Key[],
): Key[] => {
if (remainings.length === 0) {
return mappedKeys;
}
let nextPos = 1;
if (remainings.startsWith('<')) {
let ltPos = remainings.indexOf('>');
if (ltPos > 0) {
nextPos = ltPos + 1;
}
}
return fromMapKeysRecursive(
remainings.slice(nextPos),
mappedKeys.concat([fromMapKey(remainings.slice(0, nextPos))])
);
};
return fromMapKeysRecursive(keys, []);
};
const equals = (e1: Key, e2: Key): boolean => {
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;
};
export { fromKeyboardEvent, fromMapKey, fromMapKeys, equals };

@ -0,0 +1,64 @@
import Key, * as keyUtils from './Key';
export default class KeySequence {
private keys: Key[];
private constructor(keys: Key[]) {
this.keys = keys;
}
static from(keys: Key[]): KeySequence {
return new KeySequence(keys);
}
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 (!keyUtils.equals(this.keys[i], o.keys[i])) {
return false;
}
}
return true;
}
getKeyArray(): Key[] {
return this.keys;
}
}
export const fromMapKeys = (keys: string): KeySequence => {
const fromMapKeysRecursive = (
remainings: string, mappedKeys: Key[],
): Key[] => {
if (remainings.length === 0) {
return mappedKeys;
}
let nextPos = 1;
if (remainings.startsWith('<')) {
let ltPos = remainings.indexOf('>');
if (ltPos > 0) {
nextPos = ltPos + 1;
}
}
return fromMapKeysRecursive(
remainings.slice(nextPos),
mappedKeys.concat([keyUtils.fromMapKey(remainings.slice(0, nextPos))])
);
};
let data = fromMapKeysRecursive(keys, []);
return KeySequence.from(data);
};

@ -1,8 +1,8 @@
import * as actions from '../actions';
import * as keyUtils from '../../shared/utils/keys';
import Key from '../domains/Key';
export interface State {
keys: keyUtils.Key[],
keys: Key[],
}
const defaultState: State = {

@ -1,23 +1,24 @@
import { Key } from '../../shared/utils/keys';
import Key from '../domains/Key';
import KeySequence from '../domains/KeySequence';
export default interface KeymapRepository {
enqueueKey(key: Key): Key[];
enqueueKey(key: Key): KeySequence;
clear(): void;
// eslint-disable-next-line semi
}
let current: Key[] = [];
let current: KeySequence = KeySequence.from([]);
export class KeymapRepositoryImpl {
enqueueKey(key: Key): Key[] {
enqueueKey(key: Key): KeySequence {
current.push(key);
return current;
}
clear(): void {
current = [];
current = KeySequence.from([]);
}
}

@ -7,29 +7,16 @@ import AddonEnabledRepository, { AddonEnabledRepositoryImpl }
import * as operations from '../../shared/operations';
import { Keymaps } from '../../shared/Settings';
import * as keyUtils from '../../shared/utils/keys';
import Key from '../domains/Key';
import KeySequence, * as keySequenceUtils from '../domains/KeySequence';
type KeymapEntityMap = Map<keyUtils.Key[], operations.Operation>;
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
const reservedKeymaps: Keymaps = {
'<Esc>': { type: operations.CANCEL },
'<C-[>': { type: operations.CANCEL },
};
const mapStartsWith = (
mapping: keyUtils.Key[],
keys: keyUtils.Key[],
): boolean => {
if (mapping.length < keys.length) {
return false;
}
for (let i = 0; i < keys.length; ++i) {
if (!keyUtils.equals(mapping[i], keys[i])) {
return false;
}
}
return true;
};
export default class KeymapUseCase {
private repository: KeymapRepository;
@ -48,13 +35,13 @@ export default class KeymapUseCase {
this.addonEnabledRepository = addonEnabledRepository;
}
nextOp(key: keyUtils.Key): operations.Operation | null {
let keys = this.repository.enqueueKey(key);
nextOp(key: Key): operations.Operation | null {
let sequence = this.repository.enqueueKey(key);
let keymaps = this.keymapEntityMap();
let matched = Array.from(keymaps.keys()).filter(
(mapping: keyUtils.Key[]) => {
return mapStartsWith(mapping, keys);
(mapping: KeySequence) => {
return mapping.startsWith(sequence);
});
if (!this.addonEnabledRepository.get()) {
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
@ -70,7 +57,7 @@ export default class KeymapUseCase {
this.repository.clear();
return null;
} else if (matched.length > 1 ||
matched.length === 1 && keys.length < matched[0].length) {
matched.length === 1 && sequence.length() < matched[0].length()) {
// More than one operations are matched
return null;
}
@ -91,10 +78,10 @@ export default class KeymapUseCase {
};
let entries = Object.entries(keymaps).map((entry) => {
return [
keyUtils.fromMapKeys(entry[0]),
keySequenceUtils.fromMapKeys(entry[0]),
entry[1],
];
}) as [keyUtils.Key[], operations.Operation][];
return new Map<keyUtils.Key[], operations.Operation>(entries);
}) as [KeySequence, operations.Operation][];
return new Map<KeySequence, operations.Operation>(entries);
}
}

@ -1,11 +1,12 @@
import * as keys from 'shared/utils/keys';
import Key, * as keys from '../../../src/content/domains/Key';
import { expect } from 'chai'
describe("keys util", () => {
describe("Key", () => {
describe('fromKeyboardEvent', () => {
it('returns from keyboard input Ctrl+X', () => {
let k = keys.fromKeyboardEvent({
key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true
});
let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true,
}));
expect(k.key).to.equal('x');
expect(k.shiftKey).to.be.false;
expect(k.ctrlKey).to.be.true;
@ -14,9 +15,9 @@ describe("keys util", () => {
});
it('returns from keyboard input Shift+Esc', () => {
let k = keys.fromKeyboardEvent({
let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true
});
}));
expect(k.key).to.equal('Esc');
expect(k.shiftKey).to.be.true;
expect(k.ctrlKey).to.be.false;
@ -26,9 +27,9 @@ describe("keys util", () => {
it('returns from keyboard input Ctrl+$', () => {
// $ required shift pressing on most keyboards
let k = keys.fromKeyboardEvent({
let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false
});
}));
expect(k.key).to.equal('$');
expect(k.shiftKey).to.be.false;
expect(k.ctrlKey).to.be.true;
@ -37,9 +38,9 @@ describe("keys util", () => {
});
it('returns from keyboard input Crtl+Space', () => {
let k = keys.fromKeyboardEvent({
let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false
});
}));
expect(k.key).to.equal('Space');
expect(k.shiftKey).to.be.false;
expect(k.ctrlKey).to.be.true;
@ -122,43 +123,15 @@ describe("keys util", () => {
});
});
describe('fromMapKeys', () => {
it('returns mapped keys for Shift+Esc', () => {
let keyArray = keys.fromMapKeys('<S-Esc>');
expect(keyArray).to.have.lengthOf(1);
expect(keyArray[0].key).to.equal('Esc');
expect(keyArray[0].shiftKey).to.be.true;
});
it('returns mapped keys for a<C-B><A-C>d<M-e>', () => {
let keyArray = keys.fromMapKeys('a<C-B><A-C>d<M-e>');
expect(keyArray).to.have.lengthOf(5);
expect(keyArray[0].key).to.equal('a');
expect(keyArray[1].ctrlKey).to.be.true;
expect(keyArray[1].key).to.equal('b');
expect(keyArray[2].altKey).to.be.true;
expect(keyArray[2].key).to.equal('c');
expect(keyArray[3].key).to.equal('d');
expect(keyArray[4].metaKey).to.be.true;
expect(keyArray[4].key).to.equal('e');
});
})
describe('equals', () => {
expect(keys.equals({
key: 'x',
ctrlKey: true,
}, {
key: 'x',
ctrlKey: true,
})).to.be.true;
expect(keys.equals({
key: 'X',
shiftKey: true,
}, {
key: 'x',
ctrlKey: true,
})).to.be.false;
expect(keys.equals(
{ key: 'x', ctrlKey: true, },
{ key: 'x', ctrlKey: true, },
)).to.be.true;
expect(keys.equals(
{ key: 'X', shiftKey: true, },
{ key: 'x', ctrlKey: true, },
)).to.be.false;
});
});

@ -0,0 +1,72 @@
import KeySequence, * as utils from '../../../src/content/domains/KeySequence';
import { expect } from 'chai'
describe("KeySequence", () => {
describe('#push', () => {
it('append a key to the sequence', () => {
let seq = KeySequence.from([]);
seq.push({ key: 'g' });
seq.push({ key: 'u', shiftKey: true });
let array = seq.getKeyArray();
expect(array[0]).to.deep.equal({ key: 'g' });
expect(array[1]).to.deep.equal({ key: 'u', shiftKey: true });
})
});
describe('#startsWith', () => {
it('returns true if the key sequence starts with param', () => {
let seq = KeySequence.from([
{ key: 'g' },
{ key: 'u', shiftKey: true },
]);
expect(seq.startsWith(KeySequence.from([
]))).to.be.true;
expect(seq.startsWith(KeySequence.from([
{ key: 'g' },
]))).to.be.true;
expect(seq.startsWith(KeySequence.from([
{ key: 'g' }, { key: 'u', shiftKey: true },
]))).to.be.true;
expect(seq.startsWith(KeySequence.from([
{ key: 'g' }, { key: 'u', shiftKey: true }, { key: 'x' },
]))).to.be.false;
expect(seq.startsWith(KeySequence.from([
{ key: 'h' },
]))).to.be.false;
})
it('returns true if the empty sequence starts with an empty sequence', () => {
let seq = KeySequence.from([]);
expect(seq.startsWith(KeySequence.from([]))).to.be.true;
expect(seq.startsWith(KeySequence.from([
{ key: 'h' },
]))).to.be.false;
})
});
describe('#fromMapKeys', () => {
it('returns mapped keys for Shift+Esc', () => {
let keyArray = utils.fromMapKeys('<S-Esc>').getKeyArray();
expect(keyArray).to.have.lengthOf(1);
expect(keyArray[0].key).to.equal('Esc');
expect(keyArray[0].shiftKey).to.be.true;
});
it('returns mapped keys for a<C-B><A-C>d<M-e>', () => {
let keyArray = utils.fromMapKeys('a<C-B><A-C>d<M-e>').getKeyArray();
expect(keyArray).to.have.lengthOf(5);
expect(keyArray[0].key).to.equal('a');
expect(keyArray[1].ctrlKey).to.be.true;
expect(keyArray[1].key).to.equal('b');
expect(keyArray[2].altKey).to.be.true;
expect(keyArray[2].key).to.equal('c');
expect(keyArray[3].key).to.equal('d');
expect(keyArray[4].metaKey).to.be.true;
expect(keyArray[4].key).to.equal('e');
});
})
});