parent
2ec912c262
commit
a5518dce3d
17 changed files with 208 additions and 223 deletions
@ -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); |
||||
} |
||||
} |
@ -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,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([]); |
||||
} |
||||
} |
||||
|
@ -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 new issue