@rulecad/keyboard-layout 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 JoniBach and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,29 @@
1
- # Temporary Holding Version
1
+ # @rulecad/keyboard-layout
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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 18+.
5
+
6
+ ```json
7
+ {
8
+ "defaults": { "legend": "@label" },
9
+ "rows": [
10
+ { "row": "R1", "keys": ["Esc", "Q", "W", "E"] },
11
+ { "row": "R2", "keys": [["Tab", 1.5], "A", { "label": "F", "homing": true }] }
12
+ ]
13
+ }
14
+ ```
15
+
16
+ ```js
17
+ import { keys, width, depth } from '@rulecad/keyboard-layout';
18
+
19
+ keys(layout) // [{ label: 'Esc', u: 1, row: 'R1', legend: 'Esc', x: -1.5, y: 0.5, … }, …]
20
+ width(layout) // 4: the widest row, in key units
21
+ depth(layout) // 2 rows
22
+ ```
23
+
24
+ - A key is a label (`"Q"`, 1u wide), `[label, units]`, or an object with `label`, `u` and any other properties.
25
+ - A row is a list of keys, or an object whose other properties apply to every key in it. `defaults` apply to every
26
+ key, and a key's own properties win. `"@label"` means the key's own label.
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
+ layout centred on 0 (x to the right, y to the back). Multiply by your pitch (19.05 mm for Cherry MX).
29
+ - `project(layout, fields)` keeps only the fields something reads, so you can tell whether an edit matters to it.
@@ -0,0 +1,38 @@
1
+ // Keyboard layouts: where the keys go, as plain JSON that a plate, keycaps, a case and a circuit board can all read:
2
+ // { "title": "…", "note": "…", "defaults": { "anchor": "mc" },
3
+ // "rows": [ { "row": "R1", "keys": ["Esc", ["Tab", 1.25], { "label": "F", "legend": "F", "homing": true }] }, … ] }
4
+ // 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
+ // an object with label, u and any other properties. A row can be a plain list of keys, or an object whose other
6
+ // properties apply to every key in it (row: "R1"); defaults apply to every key. A key's own properties win. A value of
7
+ // "@label" is the key's label: "defaults": { "legend": "@label" } gives every key its label as its legend.
8
+ // No imports and no file access: the format alone, for browsers and Node alike.
9
+
10
+ // A key as { label, u, …properties }
11
+ const key = (k) => (typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k });
12
+ const rowOf = (r) => (Array.isArray(r) ? { keys: r } : r);
13
+
14
+ // keys(layout) -> [{ row, col, label, u, units, x, y, …properties }]: each key's centre in key units, the layout
15
+ // centred on 0 (x to the right, y to the back). units is u, under its older name.
16
+ export function keys(layout) {
17
+ const rows = (layout.rows || []).map(rowOf), defaults = layout.defaults || {};
18
+ const across = width(layout), out = [];
19
+ rows.forEach((r, row) => {
20
+ const { keys: list = [], ...shared } = r;
21
+ let x = -across / 2;
22
+ list.forEach((k, col) => {
23
+ const props = { ...defaults, ...shared, ...key(k) };
24
+ 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 });
26
+ x += props.u;
27
+ });
28
+ });
29
+ return out;
30
+ }
31
+
32
+ // 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)));
34
+ export const depth = (layout) => (layout.rows || []).length;
35
+
36
+ // project(layout, fields): only the fields something reads, key by key, so a change to anything else (a legend, when
37
+ // only positions matter) isn't a change to it. Positions always count.
38
+ export const project = (layout, fields = []) => keys(layout).map((k) => Object.fromEntries(['index', 'col', 'u', ...fields].map((f) => [f, k[f] ?? null])));
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@rulecad/keyboard-layout",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A plain JSON keyboard layout format: rows of keys with widths and any per-key properties.",
5
+ "type": "module",
6
+ "exports": "./keyboard-layout.mjs",
7
+ "files": [
8
+ "keyboard-layout.mjs",
9
+ "README.md",
10
+ "LICENSE"
11
+ ],
12
+ "license": "MIT",
13
+ "keywords": [
14
+ "keyboard",
15
+ "layout",
16
+ "mechanical-keyboard"
17
+ ],
18
+ "engines": {
19
+ "node": ">=18"
20
+ },
21
+ "sideEffects": false,
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/JoniBach/rulecad.git",
25
+ "directory": "packages/keyboard-layout"
26
+ },
27
+ "scripts": {
28
+ "test": "node --test"
29
+ }
30
+ }