Skip to content

Commit

Permalink
fix: add repr method for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
gera2ld committed Mar 8, 2024
1 parent 504c70b commit 845d0b5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { modifiers } from './constants';
import {
addKeyNode,
createKeyNode,
getKeyNode,
removeKeyNode,
reprNodeTree,
} from './node';
import { Subject } from './subject';
import {
IKeyNode,
IShortcut,
Expand All @@ -6,10 +15,7 @@ import {
IShortcutOptions,
IShortcutServiceOptions,
} from './types';
import { normalizeSequence, parseCondition, buildKey } from './util';
import { addKeyNode, createKeyNode, getKeyNode, removeKeyNode } from './node';
import { modifiers } from './constants';
import { Subject } from './subject';
import { buildKey, normalizeSequence, parseCondition } from './util';

export * from './constants';
export * from './types';
Expand Down Expand Up @@ -238,6 +244,10 @@ export class KeyboardService {
}
this._timer = window.setTimeout(this._reset, this.options.sequenceTimeout);
};

repr() {
return reprNodeTree(this._root);
}
}

let service: KeyboardService;
Expand Down
30 changes: 24 additions & 6 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export function createKeyNode(): IKeyNode {
}

export function addKeyNode(
parent: IKeyNode,
root: IKeyNode,
sequence: string[],
shortcut: IShortcut,
) {
let node: IKeyNode = parent;
let node: IKeyNode = root;
for (const key of sequence) {
let child = node.children.get(key);
if (!child) {
Expand All @@ -24,8 +24,8 @@ export function addKeyNode(
node.shortcuts.add(shortcut);
}

export function getKeyNode(parent: IKeyNode, sequence: string[]) {
let node: IKeyNode | undefined = parent;
export function getKeyNode(root: IKeyNode, sequence: string[]) {
let node: IKeyNode | undefined = root;
for (const key of sequence) {
node = node.children.get(key);
if (!node) break;
Expand All @@ -34,11 +34,11 @@ export function getKeyNode(parent: IKeyNode, sequence: string[]) {
}

export function removeKeyNode(
parent: IKeyNode,
root: IKeyNode,
sequence: string[],
shortcut?: IShortcut,
) {
let node: IKeyNode | undefined = parent;
let node: IKeyNode | undefined = root;
const ancestors = [node];
for (const key of sequence) {
node = node.children.get(key);
Expand All @@ -56,3 +56,21 @@ export function removeKeyNode(
i -= 1;
}
}

export function reprNodeTree(root: IKeyNode) {
const result: string[] = [];
const reprChildren = (node: IKeyNode, level = 0) => {
for (const [key, child] of node.children.entries()) {
result.push(
[
' '.repeat(level),
key,
child.shortcuts.size ? ` (${child.shortcuts.size})` : '',
].join(''),
);
reprChildren(child, level + 1);
}
};
reprChildren(root);
return result.join('\n');
}

0 comments on commit 845d0b5

Please sign in to comment.