Skip to content

Commit

Permalink
refactor: query code points in a specific keyboard zone
Browse files Browse the repository at this point in the history
  • Loading branch information
aradzie committed Apr 2, 2024
1 parent 896edf7 commit 339fa1d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
22 changes: 22 additions & 0 deletions packages/keybr-keyboard/lib/keyboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ test("data", (t) => {
[...keyboard.getCodePoints({ dead: false, shift: false, alt: false })],
[/* a */ 0x0061],
);
t.deepEqual(
[
...keyboard.getCodePoints({
dead: false,
shift: false,
alt: false,
zone: "left",
}),
],
[/* a */ 0x0061],
);
t.deepEqual(
[
...keyboard.getCodePoints({
dead: false,
shift: false,
alt: false,
zone: "right",
}),
],
[],
);

t.is(keyboard.getCharacters("unknown"), null);
t.deepEqual(keyboard.getCharacters("KeyA"), key1);
Expand Down
23 changes: 12 additions & 11 deletions packages/keybr-keyboard/lib/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,31 @@ export class Keyboard {
dead = true,
shift = true,
alt = true,
zone = null,
}: {
readonly dead?: boolean;
readonly shift?: boolean;
readonly alt?: boolean;
readonly zone?: ZoneId | null;
} = {}): WeightedCodePointSet {
const list: CodePoint[] = [];
const weights = new Map<CodePoint, number>();
for (const combo of this.combos.values()) {
const shape = this.getShape(combo.id);
if (
(combo.prefix == null || dead) &&
(!combo.shift || shift) &&
(!combo.alt || alt)
(!combo.alt || alt) &&
(zone == null || shape?.inZone(zone))
) {
list.push(combo.codePoint);
const shape = this.shapes.get(combo.id);
if (shape != null) {
switch (shape.row) {
case "home":
weights.set(combo.codePoint, 1);
break;
case "top":
weights.set(combo.codePoint, 2);
break;
}
switch (shape?.row) {
case "home":
weights.set(combo.codePoint, 1);
break;
case "top":
weights.set(combo.codePoint, 2);
break;
}
}
}
Expand Down

0 comments on commit 339fa1d

Please sign in to comment.