Skip to content

Commit 339fa1d

Browse files
committed
refactor: query code points in a specific keyboard zone
1 parent 896edf7 commit 339fa1d

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

packages/keybr-keyboard/lib/keyboard.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ test("data", (t) => {
6060
[...keyboard.getCodePoints({ dead: false, shift: false, alt: false })],
6161
[/* a */ 0x0061],
6262
);
63+
t.deepEqual(
64+
[
65+
...keyboard.getCodePoints({
66+
dead: false,
67+
shift: false,
68+
alt: false,
69+
zone: "left",
70+
}),
71+
],
72+
[/* a */ 0x0061],
73+
);
74+
t.deepEqual(
75+
[
76+
...keyboard.getCodePoints({
77+
dead: false,
78+
shift: false,
79+
alt: false,
80+
zone: "right",
81+
}),
82+
],
83+
[],
84+
);
6385

6486
t.is(keyboard.getCharacters("unknown"), null);
6587
t.deepEqual(keyboard.getCharacters("KeyA"), key1);

packages/keybr-keyboard/lib/keyboard.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,31 @@ export class Keyboard {
8282
dead = true,
8383
shift = true,
8484
alt = true,
85+
zone = null,
8586
}: {
8687
readonly dead?: boolean;
8788
readonly shift?: boolean;
8889
readonly alt?: boolean;
90+
readonly zone?: ZoneId | null;
8991
} = {}): WeightedCodePointSet {
9092
const list: CodePoint[] = [];
9193
const weights = new Map<CodePoint, number>();
9294
for (const combo of this.combos.values()) {
95+
const shape = this.getShape(combo.id);
9396
if (
9497
(combo.prefix == null || dead) &&
9598
(!combo.shift || shift) &&
96-
(!combo.alt || alt)
99+
(!combo.alt || alt) &&
100+
(zone == null || shape?.inZone(zone))
97101
) {
98102
list.push(combo.codePoint);
99-
const shape = this.shapes.get(combo.id);
100-
if (shape != null) {
101-
switch (shape.row) {
102-
case "home":
103-
weights.set(combo.codePoint, 1);
104-
break;
105-
case "top":
106-
weights.set(combo.codePoint, 2);
107-
break;
108-
}
103+
switch (shape?.row) {
104+
case "home":
105+
weights.set(combo.codePoint, 1);
106+
break;
107+
case "top":
108+
weights.set(combo.codePoint, 2);
109+
break;
109110
}
110111
}
111112
}

0 commit comments

Comments
 (0)