@rulecad/keyboard-layout 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -1
- package/keyboard-layout.d.mts +12 -0
- package/keyboard-layout.mjs +56 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @rulecad/keyboard-layout
|
|
2
2
|
|
|
3
3
|
A keyboard layout as plain JSON that everything else can read: the plate, the keycaps, the case and the circuit board
|
|
4
|
-
all take their positions from one file. No dependencies; browsers and Node
|
|
4
|
+
all take their positions from one file. No dependencies; browsers and Node 20+.
|
|
5
5
|
|
|
6
6
|
```json
|
|
7
7
|
{
|
|
@@ -27,3 +27,16 @@ depth(layout) // 2 rows
|
|
|
27
27
|
- Rows run back to front and start at the same left edge. `x` and `y` are each key's centre in key units, with the
|
|
28
28
|
layout centred on 0 (x to the right, y to the back). Multiply by your pitch (19.05 mm for Cherry MX).
|
|
29
29
|
- `project(layout, fields)` keeps only the fields something reads, so you can tell whether an edit matters to it.
|
|
30
|
+
- `{ "gap": 0.5 }` in a row is space between keys, such as the groups of a function row.
|
|
31
|
+
|
|
32
|
+
## From keyboard-layout-editor.com
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { fromKLE } from '@rulecad/keyboard-layout';
|
|
36
|
+
|
|
37
|
+
const { layout, notes } = fromKLE(rawData); // its JSON download, or the text from its "Raw data" tab
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Rows, widths, gaps and legends come across, and a shifted legend ("!" over "1") becomes `legend2`. What this format
|
|
41
|
+
has no place for (rotated keys, rows nudged up or down, tall or stepped keys such as ISO Enter) is listed in `notes`,
|
|
42
|
+
and those keys keep their place in the row. With the `cad` command: `npx cad import-kle layout.json keyboard/mine`.
|
package/keyboard-layout.d.mts
CHANGED
|
@@ -4,6 +4,18 @@
|
|
|
4
4
|
* @returns {Key[]}
|
|
5
5
|
*/
|
|
6
6
|
export function keys(layout: Layout): Key[];
|
|
7
|
+
/**
|
|
8
|
+
* A layout from keyboard-layout-editor.com: its JSON download, or the "Raw data" text. Rows, widths and the gaps
|
|
9
|
+
* between keys come across; what this format has no place for (rotated keys, rows nudged up or down, tall or
|
|
10
|
+
* stepped keys such as ISO Enter) is listed in notes, and those keys keep their place in the row.
|
|
11
|
+
* A key's label is its main legend; a second legend (the shifted symbol, "!" over "1") becomes legend2.
|
|
12
|
+
* @param {string | any[]} kle
|
|
13
|
+
* @returns {{ layout: Layout, notes: string[] }}
|
|
14
|
+
*/
|
|
15
|
+
export function fromKLE(kle: string | any[]): {
|
|
16
|
+
layout: Layout;
|
|
17
|
+
notes: string[];
|
|
18
|
+
};
|
|
7
19
|
export function width(layout: Layout): number;
|
|
8
20
|
export function depth(layout: Layout): number;
|
|
9
21
|
export function project(layout: Layout, fields?: string[]): object[];
|
package/keyboard-layout.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Keyboard layouts: where the keys go, as plain JSON that a plate, keycaps, a case and a circuit board can all read:
|
|
2
2
|
// { "title": "…", "note": "…", "defaults": { "anchor": "mc" },
|
|
3
3
|
// "rows": [ { "row": "R1", "keys": ["Esc", ["Tab", 1.25], { "label": "F", "legend": "F", "homing": true }] }, … ] }
|
|
4
|
+
// A { "gap": 0.25 } in a row is space between keys (a function row's groups). fromKLE() reads keyboard-layout-editor.com.
|
|
4
5
|
// Rows run back to front and start at the same left edge, as on a keyboard. A key is a label (1u), [label, units], or
|
|
5
6
|
// an object with label, u and any other properties. A row can be a plain list of keys, or an object whose other
|
|
6
7
|
// properties apply to every key in it (row: "R1"); defaults apply to every key. A key's own properties win. A value of
|
|
@@ -14,6 +15,8 @@
|
|
|
14
15
|
// A key as { label, u, …properties }
|
|
15
16
|
const key = (k) => (typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k });
|
|
16
17
|
const rowOf = (r) => (Array.isArray(r) ? { keys: r } : r);
|
|
18
|
+
// A gap between keys: { "gap": 0.5 } in a row, in key units (no key there, but the keys after it move along)
|
|
19
|
+
const isGap = (k) => k && typeof k === 'object' && !Array.isArray(k) && k.gap != null && k.label == null;
|
|
17
20
|
|
|
18
21
|
// keys(layout) -> [{ row, col, label, u, units, x, y, …properties }]: each key's centre in key units, the layout
|
|
19
22
|
// centred on 0 (x to the right, y to the back). units is u, under its older name.
|
|
@@ -27,11 +30,12 @@ export function keys(layout) {
|
|
|
27
30
|
const across = width(layout), out = [];
|
|
28
31
|
rows.forEach((r, row) => {
|
|
29
32
|
const { keys: list = [], ...shared } = r;
|
|
30
|
-
let x = -across / 2;
|
|
31
|
-
list.forEach((k
|
|
33
|
+
let x = -across / 2, col = 0;
|
|
34
|
+
list.forEach((k) => {
|
|
35
|
+
if (isGap(k)) { x += k.gap; return; }
|
|
32
36
|
const props = { ...defaults, ...shared, ...key(k) };
|
|
33
37
|
for (const [n, v] of Object.entries(props)) if (v === '@label') props[n] = props.label; // e.g. legend: the key's label
|
|
34
|
-
out.push({ ...props, row: props.row ?? row, index: row, col
|
|
38
|
+
out.push({ ...props, row: props.row ?? row, index: row, col: col++, units: props.u, x: x + props.u / 2, y: (rows.length - 1) / 2 - row });
|
|
35
39
|
x += props.u;
|
|
36
40
|
});
|
|
37
41
|
});
|
|
@@ -40,7 +44,7 @@ export function keys(layout) {
|
|
|
40
44
|
|
|
41
45
|
// width(layout): the widest row, in key units; depth(layout): the number of rows
|
|
42
46
|
/** The widest row, in key units. @param {Layout} layout @returns {number} */
|
|
43
|
-
export const width = (layout) => Math.max(...(layout.rows || []).map(rowOf).map((r) => (r.keys || []).reduce((s, k) => s + key(k).u, 0)));
|
|
47
|
+
export const width = (layout) => Math.max(...(layout.rows || []).map(rowOf).map((r) => (r.keys || []).reduce((s, k) => s + (isGap(k) ? k.gap : key(k).u), 0)));
|
|
44
48
|
/** The number of rows. @param {Layout} layout @returns {number} */
|
|
45
49
|
export const depth = (layout) => (layout.rows || []).length;
|
|
46
50
|
|
|
@@ -48,3 +52,51 @@ export const depth = (layout) => (layout.rows || []).length;
|
|
|
48
52
|
// only positions matter) isn't a change to it. Positions always count.
|
|
49
53
|
/** Only the fields something reads, key by key (positions always). @param {Layout} layout @param {string[]} [fields] @returns {object[]} */
|
|
50
54
|
export const project = (layout, fields = []) => keys(layout).map((k) => Object.fromEntries(['index', 'col', 'u', ...fields].map((f) => [f, k[f] ?? null])));
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A layout from keyboard-layout-editor.com: its JSON download, or the "Raw data" text. Rows, widths and the gaps
|
|
58
|
+
* between keys come across; what this format has no place for (rotated keys, rows nudged up or down, tall or
|
|
59
|
+
* stepped keys such as ISO Enter) is listed in notes, and those keys keep their place in the row.
|
|
60
|
+
* A key's label is its main legend; a second legend (the shifted symbol, "!" over "1") becomes legend2.
|
|
61
|
+
* @param {string | any[]} kle
|
|
62
|
+
* @returns {{ layout: Layout, notes: string[] }}
|
|
63
|
+
*/
|
|
64
|
+
export function fromKLE(kle) {
|
|
65
|
+
const data = typeof kle === 'string' ? parseRaw(kle) : kle;
|
|
66
|
+
if (!Array.isArray(data)) throw new Error("this isn't a keyboard-layout-editor layout (a list of rows)");
|
|
67
|
+
const notes = new Set(), rows = [];
|
|
68
|
+
let title;
|
|
69
|
+
for (const row of data) {
|
|
70
|
+
if (!Array.isArray(row)) { if (row && typeof row === 'object') title = row.name; continue; } // the layout's own settings
|
|
71
|
+
const keys = [];
|
|
72
|
+
let next = {};
|
|
73
|
+
for (const item of row) {
|
|
74
|
+
if (item && typeof item === 'object') {
|
|
75
|
+
next = { ...next, ...item };
|
|
76
|
+
if (item.r || item.rx || item.ry) notes.add('rotated keys are placed as if straight');
|
|
77
|
+
if (item.y) notes.add('rows nudged up or down are placed one row apart');
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (next.x) keys.push({ gap: next.x });
|
|
81
|
+
if ((next.h && next.h !== 1) || next.w2 || next.h2 || next.x2 || next.y2) notes.add(`${legendOf(item).label || 'a key'} is tall or stepped: placed as ${next.w || 1}u wide`);
|
|
82
|
+
const { label, legend2 } = legendOf(item), u = next.w || 1;
|
|
83
|
+
keys.push(legend2 ? { label, u, legend2 } : u === 1 ? label : [label, u]);
|
|
84
|
+
next = {};
|
|
85
|
+
}
|
|
86
|
+
rows.push(keys);
|
|
87
|
+
}
|
|
88
|
+
return { layout: { ...(title ? { title } : {}), rows }, notes: [...notes] };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// "Raw data" from keyboard-layout-editor.com is JSON with its object keys unquoted and the outer brackets left off
|
|
92
|
+
function parseRaw(text) {
|
|
93
|
+
try { return JSON.parse(text); } catch {}
|
|
94
|
+
const quoted = `[${text}]`.replace(/([{,]\s*)([A-Za-z_][\w]*)\s*:/g, '$1"$2":');
|
|
95
|
+
try { return JSON.parse(quoted); } catch { throw new Error("this isn't keyboard-layout-editor data: paste its Raw data, or use its JSON download"); }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// A key's legends: "Q", or "!\n1" (top-left shifted, bottom-left main), up to twelve positions
|
|
99
|
+
function legendOf(text) {
|
|
100
|
+
const all = String(text).split('\n').map((l) => l.trim()).filter(Boolean);
|
|
101
|
+
return all.length > 1 ? { label: all[all.length - 1], legend2: all[0] } : { label: all[0] || '' };
|
|
102
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/keyboard-layout",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A plain JSON keyboard layout format: rows of keys with widths and any per-key properties.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"mechanical-keyboard"
|
|
21
21
|
],
|
|
22
22
|
"engines": {
|
|
23
|
-
"node": ">=
|
|
23
|
+
"node": ">=20"
|
|
24
24
|
},
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"repository": {
|