Define Key and KeySequence
This commit is contained in:
parent
2ec912c262
commit
a5518dce3d
17 changed files with 207 additions and 222 deletions
|
@ -1,5 +1,5 @@
|
||||||
import * as dom from '../shared/utils/dom';
|
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 => {
|
const cancelKey = (e: KeyboardEvent): boolean => {
|
||||||
return e.key === 'Escape' || e.key === '[' && e.ctrlKey;
|
return e.key === 'Escape' || e.key === '[' && e.ctrlKey;
|
||||||
|
@ -8,7 +8,7 @@ const cancelKey = (e: KeyboardEvent): boolean => {
|
||||||
export default class InputDriver {
|
export default class InputDriver {
|
||||||
private pressed: {[key: string]: string} = {};
|
private pressed: {[key: string]: string} = {};
|
||||||
|
|
||||||
private onKeyListeners: ((key: keys.Key) => boolean)[] = [];
|
private onKeyListeners: ((key: Key) => boolean)[] = [];
|
||||||
|
|
||||||
constructor(target: HTMLElement) {
|
constructor(target: HTMLElement) {
|
||||||
this.pressed = {};
|
this.pressed = {};
|
||||||
|
@ -19,7 +19,7 @@ export default class InputDriver {
|
||||||
target.addEventListener('keyup', this.onKeyUp.bind(this));
|
target.addEventListener('keyup', this.onKeyUp.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
onKey(cb: (key: keys.Key) => boolean) {
|
onKey(cb: (key: Key) => boolean) {
|
||||||
this.onKeyListeners.push(cb);
|
this.onKeyListeners.push(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import Redux from 'redux';
|
import Redux from 'redux';
|
||||||
import * as keyUtils from '../../shared/utils/keys';
|
import Key from '../domains/Key';
|
||||||
|
|
||||||
// User input
|
// User input
|
||||||
export const INPUT_KEY_PRESS = 'input.key.press';
|
export const INPUT_KEY_PRESS = 'input.key.press';
|
||||||
|
@ -25,7 +25,7 @@ export const NOOP = 'noop';
|
||||||
|
|
||||||
export interface InputKeyPressAction extends Redux.Action {
|
export interface InputKeyPressAction extends Redux.Action {
|
||||||
type: typeof INPUT_KEY_PRESS;
|
type: typeof INPUT_KEY_PRESS;
|
||||||
key: keyUtils.Key;
|
key: Key;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InputClearKeysAction extends Redux.Action {
|
export interface InputClearKeysAction extends Redux.Action {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as actions from './index';
|
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 {
|
return {
|
||||||
type: actions.INPUT_KEY_PRESS,
|
type: actions.INPUT_KEY_PRESS,
|
||||||
key,
|
key,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import * as messages from '../../shared/messages';
|
import * as messages from '../../shared/messages';
|
||||||
import { Key } from '../../shared/utils/keys';
|
import Key from '../domains/Key';
|
||||||
|
|
||||||
export default interface FollowMasterClient {
|
export default interface FollowMasterClient {
|
||||||
startFollow(newTab: boolean, background: boolean): void;
|
startFollow(newTab: boolean, background: boolean): void;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import MessageListener from '../../MessageListener';
|
import MessageListener from '../../MessageListener';
|
||||||
import { LinkHint, InputHint } from '../../presenters/Hint';
|
import { LinkHint, InputHint } from '../../presenters/Hint';
|
||||||
import * as messages from '../../../shared/messages';
|
import * as messages from '../../../shared/messages';
|
||||||
import { Key } from '../../../shared/utils/keys';
|
import Key from '../../domains/Key';
|
||||||
import TabsClient, { TabsClientImpl } from '../../client/TabsClient';
|
import TabsClient, { TabsClientImpl } from '../../client/TabsClient';
|
||||||
import FollowMasterClient, { FollowMasterClientImpl }
|
import FollowMasterClient, { FollowMasterClientImpl }
|
||||||
from '../../client/FollowMasterClient';
|
from '../../client/FollowMasterClient';
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import InputDriver from './../../InputDriver';
|
import InputDriver from './../../InputDriver';
|
||||||
import FollowComponent from './follow';
|
import FollowComponent from './follow';
|
||||||
import MarkComponent from './mark';
|
import MarkComponent from './mark';
|
||||||
import KeymapperComponent from './keymapper';
|
// import KeymapperComponent from './keymapper';
|
||||||
import * as messages from '../../../shared/messages';
|
import * as messages from '../../../shared/messages';
|
||||||
import MessageListener from '../../MessageListener';
|
import MessageListener from '../../MessageListener';
|
||||||
import * as blacklists from '../../../shared/blacklists';
|
import * as blacklists from '../../../shared/blacklists';
|
||||||
import * as keys from '../../../shared/utils/keys';
|
import Key from '../../domains/Key';
|
||||||
|
|
||||||
import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
|
import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
|
||||||
import SettingUseCase from '../../usecases/SettingUseCase';
|
import SettingUseCase from '../../usecases/SettingUseCase';
|
||||||
|
@ -18,11 +18,11 @@ export default class Common {
|
||||||
const input = new InputDriver(win.document.body);
|
const input = new InputDriver(win.document.body);
|
||||||
const follow = new FollowComponent();
|
const follow = new FollowComponent();
|
||||||
const mark = new MarkComponent(store);
|
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: Key) => follow.key(key));
|
||||||
input.onKey((key: keys.Key) => mark.key(key));
|
input.onKey((key: Key) => mark.key(key));
|
||||||
input.onKey((key: keys.Key) => keymapper.key(key));
|
// input.onKey((key: Key) => keymapper.key(key));
|
||||||
|
|
||||||
this.reloadSettings();
|
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 markActions from '../../actions/mark';
|
||||||
import * as consoleFrames from '../..//console-frames';
|
import * as consoleFrames from '../..//console-frames';
|
||||||
import * as keyUtils from '../../../shared/utils/keys';
|
import Key from '../../domains/Key';
|
||||||
|
|
||||||
import MarkUseCase from '../../usecases/MarkUseCase';
|
import MarkUseCase from '../../usecases/MarkUseCase';
|
||||||
|
|
||||||
let markUseCase = new 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);
|
return key.key === 'Esc' || key.key === '[' && Boolean(key.ctrlKey);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ export default class MarkComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line max-statements
|
// eslint-disable-next-line max-statements
|
||||||
key(key: keyUtils.Key) {
|
key(key: Key) {
|
||||||
let { mark: markState } = this.store.getState();
|
let { mark: markState } = this.store.getState();
|
||||||
|
|
||||||
if (!markState.setMode && !markState.jumpMode) {
|
if (!markState.setMode && !markState.jumpMode) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import FocusUseCase from '../usecases/FocusUseCase';
|
||||||
import ClipboardUseCase from '../usecases/ClipboardUseCase';
|
import ClipboardUseCase from '../usecases/ClipboardUseCase';
|
||||||
import BackgroundClient from '../client/BackgroundClient';
|
import BackgroundClient from '../client/BackgroundClient';
|
||||||
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
|
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
|
||||||
import { Key } from '../../shared/utils/keys';
|
import Key from '../domains/Key';
|
||||||
|
|
||||||
export default class KeymapController {
|
export default class KeymapController {
|
||||||
private keymapUseCase: KeymapUseCase;
|
private keymapUseCase: KeymapUseCase;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import MarkUseCase from '../usecases/MarkUseCase';
|
import MarkUseCase from '../usecases/MarkUseCase';
|
||||||
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
|
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
|
||||||
import * as keys from '../../shared/utils/keys';
|
import Key from '../domains/Key';
|
||||||
|
|
||||||
export default class MarkKeyController {
|
export default class MarkKeyController {
|
||||||
private markUseCase: MarkUseCase;
|
private markUseCase: MarkUseCase;
|
||||||
|
@ -15,7 +15,7 @@ export default class MarkKeyController {
|
||||||
this.markKeyUseCase = markKeyUseCase;
|
this.markKeyUseCase = markKeyUseCase;
|
||||||
}
|
}
|
||||||
|
|
||||||
press(key: keys.Key): boolean {
|
press(key: Key): boolean {
|
||||||
if (this.markKeyUseCase.isSetMode()) {
|
if (this.markKeyUseCase.isSetMode()) {
|
||||||
this.markUseCase.set(key.key);
|
this.markUseCase.set(key.key);
|
||||||
this.markKeyUseCase.disableSetMode();
|
this.markKeyUseCase.disableSetMode();
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
export interface Key {
|
export default interface Key {
|
||||||
key: string;
|
key: string;
|
||||||
shiftKey: boolean | undefined;
|
shiftKey?: boolean;
|
||||||
ctrlKey: boolean | undefined;
|
ctrlKey?: boolean;
|
||||||
altKey: boolean | undefined;
|
altKey?: boolean;
|
||||||
metaKey: boolean | undefined;
|
metaKey?: boolean;
|
||||||
|
|
||||||
|
// eslint-disable-next-line semi
|
||||||
}
|
}
|
||||||
|
|
||||||
const modifiedKeyName = (name: string): string => {
|
const modifiedKeyName = (name: string): string => {
|
||||||
|
@ -18,7 +20,7 @@ const modifiedKeyName = (name: string): string => {
|
||||||
return name;
|
return name;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fromKeyboardEvent = (e: KeyboardEvent): Key => {
|
export const fromKeyboardEvent = (e: KeyboardEvent): Key => {
|
||||||
let key = modifiedKeyName(e.key);
|
let key = modifiedKeyName(e.key);
|
||||||
let shift = e.shiftKey;
|
let shift = e.shiftKey;
|
||||||
if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) {
|
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('>')) {
|
if (key.startsWith('<') && key.endsWith('>')) {
|
||||||
let inner = key.slice(1, -1);
|
let inner = key.slice(1, -1);
|
||||||
let shift = inner.includes('S-');
|
let shift = inner.includes('S-');
|
||||||
|
@ -63,37 +65,10 @@ const fromMapKey = (key: string): Key => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const fromMapKeys = (keys: string): Key[] => {
|
export const equals = (e1: Key, e2: Key): boolean => {
|
||||||
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 => {
|
|
||||||
return e1.key === e2.key &&
|
return e1.key === e2.key &&
|
||||||
e1.ctrlKey === e2.ctrlKey &&
|
e1.ctrlKey === e2.ctrlKey &&
|
||||||
e1.metaKey === e2.metaKey &&
|
e1.metaKey === e2.metaKey &&
|
||||||
e1.altKey === e2.altKey &&
|
e1.altKey === e2.altKey &&
|
||||||
e1.shiftKey === e2.shiftKey;
|
e1.shiftKey === e2.shiftKey;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { fromKeyboardEvent, fromMapKey, fromMapKeys, equals };
|
|
64
src/content/domains/KeySequence.ts
Normal file
64
src/content/domains/KeySequence.ts
Normal file
|
@ -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 actions from '../actions';
|
||||||
import * as keyUtils from '../../shared/utils/keys';
|
import Key from '../domains/Key';
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
keys: keyUtils.Key[],
|
keys: Key[],
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultState: State = {
|
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 {
|
export default interface KeymapRepository {
|
||||||
enqueueKey(key: Key): Key[];
|
enqueueKey(key: Key): KeySequence;
|
||||||
|
|
||||||
clear(): void;
|
clear(): void;
|
||||||
|
|
||||||
// eslint-disable-next-line semi
|
// eslint-disable-next-line semi
|
||||||
}
|
}
|
||||||
|
|
||||||
let current: Key[] = [];
|
let current: KeySequence = KeySequence.from([]);
|
||||||
|
|
||||||
export class KeymapRepositoryImpl {
|
export class KeymapRepositoryImpl {
|
||||||
|
|
||||||
enqueueKey(key: Key): Key[] {
|
enqueueKey(key: Key): KeySequence {
|
||||||
current.push(key);
|
current.push(key);
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
clear(): void {
|
clear(): void {
|
||||||
current = [];
|
current = KeySequence.from([]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,29 +7,16 @@ import AddonEnabledRepository, { AddonEnabledRepositoryImpl }
|
||||||
|
|
||||||
import * as operations from '../../shared/operations';
|
import * as operations from '../../shared/operations';
|
||||||
import { Keymaps } from '../../shared/Settings';
|
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 = {
|
const reservedKeymaps: Keymaps = {
|
||||||
'<Esc>': { type: operations.CANCEL },
|
'<Esc>': { type: operations.CANCEL },
|
||||||
'<C-[>': { 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 {
|
export default class KeymapUseCase {
|
||||||
private repository: KeymapRepository;
|
private repository: KeymapRepository;
|
||||||
|
@ -48,13 +35,13 @@ export default class KeymapUseCase {
|
||||||
this.addonEnabledRepository = addonEnabledRepository;
|
this.addonEnabledRepository = addonEnabledRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextOp(key: keyUtils.Key): operations.Operation | null {
|
nextOp(key: Key): operations.Operation | null {
|
||||||
let keys = this.repository.enqueueKey(key);
|
let sequence = this.repository.enqueueKey(key);
|
||||||
|
|
||||||
let keymaps = this.keymapEntityMap();
|
let keymaps = this.keymapEntityMap();
|
||||||
let matched = Array.from(keymaps.keys()).filter(
|
let matched = Array.from(keymaps.keys()).filter(
|
||||||
(mapping: keyUtils.Key[]) => {
|
(mapping: KeySequence) => {
|
||||||
return mapStartsWith(mapping, keys);
|
return mapping.startsWith(sequence);
|
||||||
});
|
});
|
||||||
if (!this.addonEnabledRepository.get()) {
|
if (!this.addonEnabledRepository.get()) {
|
||||||
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
|
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
|
||||||
|
@ -70,7 +57,7 @@ export default class KeymapUseCase {
|
||||||
this.repository.clear();
|
this.repository.clear();
|
||||||
return null;
|
return null;
|
||||||
} else if (matched.length > 1 ||
|
} 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
|
// More than one operations are matched
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -91,10 +78,10 @@ export default class KeymapUseCase {
|
||||||
};
|
};
|
||||||
let entries = Object.entries(keymaps).map((entry) => {
|
let entries = Object.entries(keymaps).map((entry) => {
|
||||||
return [
|
return [
|
||||||
keyUtils.fromMapKeys(entry[0]),
|
keySequenceUtils.fromMapKeys(entry[0]),
|
||||||
entry[1],
|
entry[1],
|
||||||
];
|
];
|
||||||
}) as [keyUtils.Key[], operations.Operation][];
|
}) as [KeySequence, operations.Operation][];
|
||||||
return new Map<keyUtils.Key[], operations.Operation>(entries);
|
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', () => {
|
describe('fromKeyboardEvent', () => {
|
||||||
it('returns from keyboard input Ctrl+X', () => {
|
it('returns from keyboard input Ctrl+X', () => {
|
||||||
let k = keys.fromKeyboardEvent({
|
let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', {
|
||||||
key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true
|
key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true,
|
||||||
});
|
}));
|
||||||
expect(k.key).to.equal('x');
|
expect(k.key).to.equal('x');
|
||||||
expect(k.shiftKey).to.be.false;
|
expect(k.shiftKey).to.be.false;
|
||||||
expect(k.ctrlKey).to.be.true;
|
expect(k.ctrlKey).to.be.true;
|
||||||
|
@ -14,9 +15,9 @@ describe("keys util", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns from keyboard input Shift+Esc', () => {
|
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
|
key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true
|
||||||
});
|
}));
|
||||||
expect(k.key).to.equal('Esc');
|
expect(k.key).to.equal('Esc');
|
||||||
expect(k.shiftKey).to.be.true;
|
expect(k.shiftKey).to.be.true;
|
||||||
expect(k.ctrlKey).to.be.false;
|
expect(k.ctrlKey).to.be.false;
|
||||||
|
@ -26,9 +27,9 @@ describe("keys util", () => {
|
||||||
|
|
||||||
it('returns from keyboard input Ctrl+$', () => {
|
it('returns from keyboard input Ctrl+$', () => {
|
||||||
// $ required shift pressing on most keyboards
|
// $ 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
|
key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false
|
||||||
});
|
}));
|
||||||
expect(k.key).to.equal('$');
|
expect(k.key).to.equal('$');
|
||||||
expect(k.shiftKey).to.be.false;
|
expect(k.shiftKey).to.be.false;
|
||||||
expect(k.ctrlKey).to.be.true;
|
expect(k.ctrlKey).to.be.true;
|
||||||
|
@ -37,9 +38,9 @@ describe("keys util", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns from keyboard input Crtl+Space', () => {
|
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
|
key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false
|
||||||
});
|
}));
|
||||||
expect(k.key).to.equal('Space');
|
expect(k.key).to.equal('Space');
|
||||||
expect(k.shiftKey).to.be.false;
|
expect(k.shiftKey).to.be.false;
|
||||||
expect(k.ctrlKey).to.be.true;
|
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', () => {
|
describe('equals', () => {
|
||||||
expect(keys.equals({
|
expect(keys.equals(
|
||||||
key: 'x',
|
{ key: 'x', ctrlKey: true, },
|
||||||
ctrlKey: true,
|
{ key: 'x', ctrlKey: true, },
|
||||||
}, {
|
)).to.be.true;
|
||||||
key: 'x',
|
|
||||||
ctrlKey: true,
|
|
||||||
})).to.be.true;
|
|
||||||
|
|
||||||
expect(keys.equals({
|
expect(keys.equals(
|
||||||
key: 'X',
|
{ key: 'X', shiftKey: true, },
|
||||||
shiftKey: true,
|
{ key: 'x', ctrlKey: true, },
|
||||||
}, {
|
)).to.be.false;
|
||||||
key: 'x',
|
|
||||||
ctrlKey: true,
|
|
||||||
})).to.be.false;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
72
test/content/domains/KeySequence.test.ts
Normal file
72
test/content/domains/KeySequence.test.ts
Normal file
|
@ -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');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
});
|
Reference in a new issue