@rulecad/keyboard-layout 0.1.0 → 0.1.2

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 CHANGED
@@ -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`.
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Each key with its centre in key units (the layout centred on 0, x right, y to the back) and its properties.
3
+ * @param {Layout} layout
4
+ * @returns {Key[]}
5
+ */
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
+ };
19
+ export function width(layout: Layout): number;
20
+ export function depth(layout: Layout): number;
21
+ export function project(layout: Layout, fields?: string[]): object[];
22
+ /**
23
+ * a key: "Q", ["Tab", 1.5] or an object
24
+ */
25
+ export type KeySpec = string | [string, number] | {
26
+ label: string;
27
+ u?: number;
28
+ [property: string]: any;
29
+ };
30
+ export type Layout = {
31
+ rows: (KeySpec[] | {
32
+ keys: KeySpec[];
33
+ [property: string]: any;
34
+ })[];
35
+ defaults?: object;
36
+ title?: string;
37
+ };
38
+ export type Key = {
39
+ label: string;
40
+ u: number;
41
+ units: number;
42
+ row: string | number;
43
+ index: number;
44
+ col: number;
45
+ x: number;
46
+ y: number;
47
+ [property: string]: any;
48
+ };
@@ -1,28 +1,41 @@
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
7
8
  // "@label" is the key's label: "defaults": { "legend": "@label" } gives every key its label as its legend.
8
9
  // No imports and no file access: the format alone, for browsers and Node alike.
9
10
 
11
+ /** @typedef {string | [string, number] | { label: string, u?: number, [property: string]: any }} KeySpec a key: "Q", ["Tab", 1.5] or an object */
12
+ /** @typedef {{ rows: (KeySpec[] | { keys: KeySpec[], [property: string]: any })[], defaults?: object, title?: string }} Layout */
13
+ /** @typedef {{ label: string, u: number, units: number, row: string | number, index: number, col: number, x: number, y: number, [property: string]: any }} Key */
14
+
10
15
  // A key as { label, u, …properties }
11
16
  const key = (k) => (typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k });
12
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;
13
20
 
14
21
  // keys(layout) -> [{ row, col, label, u, units, x, y, …properties }]: each key's centre in key units, the layout
15
22
  // centred on 0 (x to the right, y to the back). units is u, under its older name.
23
+ /**
24
+ * Each key with its centre in key units (the layout centred on 0, x right, y to the back) and its properties.
25
+ * @param {Layout} layout
26
+ * @returns {Key[]}
27
+ */
16
28
  export function keys(layout) {
17
29
  const rows = (layout.rows || []).map(rowOf), defaults = layout.defaults || {};
18
30
  const across = width(layout), out = [];
19
31
  rows.forEach((r, row) => {
20
32
  const { keys: list = [], ...shared } = r;
21
- let x = -across / 2;
22
- list.forEach((k, col) => {
33
+ let x = -across / 2, col = 0;
34
+ list.forEach((k) => {
35
+ if (isGap(k)) { x += k.gap; return; }
23
36
  const props = { ...defaults, ...shared, ...key(k) };
24
37
  for (const [n, v] of Object.entries(props)) if (v === '@label') props[n] = props.label; // e.g. legend: the key's label
25
- out.push({ ...props, row: props.row ?? row, index: row, col, units: props.u, x: x + props.u / 2, y: (rows.length - 1) / 2 - row });
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 });
26
39
  x += props.u;
27
40
  });
28
41
  });
@@ -30,9 +43,60 @@ export function keys(layout) {
30
43
  }
31
44
 
32
45
  // width(layout): the widest row, in key units; depth(layout): the number of rows
33
- export const width = (layout) => Math.max(...(layout.rows || []).map(rowOf).map((r) => (r.keys || []).reduce((s, k) => s + key(k).u, 0)));
46
+ /** The widest row, in key units. @param {Layout} layout @returns {number} */
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)));
48
+ /** The number of rows. @param {Layout} layout @returns {number} */
34
49
  export const depth = (layout) => (layout.rows || []).length;
35
50
 
36
51
  // project(layout, fields): only the fields something reads, key by key, so a change to anything else (a legend, when
37
52
  // only positions matter) isn't a change to it. Positions always count.
53
+ /** Only the fields something reads, key by key (positions always). @param {Layout} layout @param {string[]} [fields] @returns {object[]} */
38
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,11 +1,15 @@
1
1
  {
2
2
  "name": "@rulecad/keyboard-layout",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A plain JSON keyboard layout format: rows of keys with widths and any per-key properties.",
5
5
  "type": "module",
6
- "exports": "./keyboard-layout.mjs",
6
+ "exports": {
7
+ "types": "./keyboard-layout.d.mts",
8
+ "default": "./keyboard-layout.mjs"
9
+ },
7
10
  "files": [
8
11
  "keyboard-layout.mjs",
12
+ "keyboard-layout.d.mts",
9
13
  "README.md",
10
14
  "LICENSE"
11
15
  ],
@@ -26,5 +30,6 @@
26
30
  },
27
31
  "scripts": {
28
32
  "test": "node --test"
29
- }
33
+ },
34
+ "types": "./keyboard-layout.d.mts"
30
35
  }