Make KeySequence class
This commit is contained in:
parent
62a86c5253
commit
b496cea582
5 changed files with 63 additions and 74 deletions
|
@ -1,14 +1,9 @@
|
|||
import Key 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);
|
||||
constructor(
|
||||
public readonly keys: Key[],
|
||||
) {
|
||||
}
|
||||
|
||||
push(key: Key): number {
|
||||
|
@ -31,34 +26,29 @@ export default class KeySequence {
|
|||
return true;
|
||||
}
|
||||
|
||||
getKeyArray(): Key[] {
|
||||
return this.keys;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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([Key.fromMapKey(remainings.slice(0, nextPos))])
|
||||
);
|
||||
};
|
||||
|
||||
let data = fromMapKeysRecursive(keys, []);
|
||||
return KeySequence.from(data);
|
||||
};
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ export default interface KeymapRepository {
|
|||
clear(): void;
|
||||
}
|
||||
|
||||
let current: KeySequence = KeySequence.from([]);
|
||||
let current: KeySequence = new KeySequence([]);
|
||||
|
||||
export class KeymapRepositoryImpl {
|
||||
|
||||
|
@ -17,6 +17,6 @@ export class KeymapRepositoryImpl {
|
|||
}
|
||||
|
||||
clear(): void {
|
||||
current = KeySequence.from([]);
|
||||
current = new KeySequence([]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import AddonEnabledRepository from '../repositories/AddonEnabledRepository';
|
|||
import * as operations from '../../shared/operations';
|
||||
import { Keymaps } from '../../shared/Settings';
|
||||
import Key from '../domains/Key';
|
||||
import KeySequence, * as keySequenceUtils from '../domains/KeySequence';
|
||||
import KeySequence from '../domains/KeySequence';
|
||||
|
||||
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
|
||||
|
||||
|
@ -71,7 +71,7 @@ export default class KeymapUseCase {
|
|||
};
|
||||
let entries = Object.entries(keymaps).map((entry) => {
|
||||
return [
|
||||
keySequenceUtils.fromMapKeys(entry[0]),
|
||||
KeySequence.fromMapKeys(entry[0]),
|
||||
entry[1],
|
||||
];
|
||||
}) as [KeySequence, operations.Operation][];
|
||||
|
|
Reference in a new issue