@rulecad/keyboard-layout 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +28 -2
- package/keyboard-layout.d.mts +36 -0
- package/keyboard-layout.mjs +50 -0
- package/package.json +33 -4
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
|
-
#
|
|
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 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,36 @@
|
|
|
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
|
+
export function width(layout: Layout): number;
|
|
8
|
+
export function depth(layout: Layout): number;
|
|
9
|
+
export function project(layout: Layout, fields?: string[]): object[];
|
|
10
|
+
/**
|
|
11
|
+
* a key: "Q", ["Tab", 1.5] or an object
|
|
12
|
+
*/
|
|
13
|
+
export type KeySpec = string | [string, number] | {
|
|
14
|
+
label: string;
|
|
15
|
+
u?: number;
|
|
16
|
+
[property: string]: any;
|
|
17
|
+
};
|
|
18
|
+
export type Layout = {
|
|
19
|
+
rows: (KeySpec[] | {
|
|
20
|
+
keys: KeySpec[];
|
|
21
|
+
[property: string]: any;
|
|
22
|
+
})[];
|
|
23
|
+
defaults?: object;
|
|
24
|
+
title?: string;
|
|
25
|
+
};
|
|
26
|
+
export type Key = {
|
|
27
|
+
label: string;
|
|
28
|
+
u: number;
|
|
29
|
+
units: number;
|
|
30
|
+
row: string | number;
|
|
31
|
+
index: number;
|
|
32
|
+
col: number;
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
[property: string]: any;
|
|
36
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
/** @typedef {string | [string, number] | { label: string, u?: number, [property: string]: any }} KeySpec a key: "Q", ["Tab", 1.5] or an object */
|
|
11
|
+
/** @typedef {{ rows: (KeySpec[] | { keys: KeySpec[], [property: string]: any })[], defaults?: object, title?: string }} Layout */
|
|
12
|
+
/** @typedef {{ label: string, u: number, units: number, row: string | number, index: number, col: number, x: number, y: number, [property: string]: any }} Key */
|
|
13
|
+
|
|
14
|
+
// A key as { label, u, …properties }
|
|
15
|
+
const key = (k) => (typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k });
|
|
16
|
+
const rowOf = (r) => (Array.isArray(r) ? { keys: r } : r);
|
|
17
|
+
|
|
18
|
+
// keys(layout) -> [{ row, col, label, u, units, x, y, …properties }]: each key's centre in key units, the layout
|
|
19
|
+
// centred on 0 (x to the right, y to the back). units is u, under its older name.
|
|
20
|
+
/**
|
|
21
|
+
* Each key with its centre in key units (the layout centred on 0, x right, y to the back) and its properties.
|
|
22
|
+
* @param {Layout} layout
|
|
23
|
+
* @returns {Key[]}
|
|
24
|
+
*/
|
|
25
|
+
export function keys(layout) {
|
|
26
|
+
const rows = (layout.rows || []).map(rowOf), defaults = layout.defaults || {};
|
|
27
|
+
const across = width(layout), out = [];
|
|
28
|
+
rows.forEach((r, row) => {
|
|
29
|
+
const { keys: list = [], ...shared } = r;
|
|
30
|
+
let x = -across / 2;
|
|
31
|
+
list.forEach((k, col) => {
|
|
32
|
+
const props = { ...defaults, ...shared, ...key(k) };
|
|
33
|
+
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, units: props.u, x: x + props.u / 2, y: (rows.length - 1) / 2 - row });
|
|
35
|
+
x += props.u;
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// width(layout): the widest row, in key units; depth(layout): the number of rows
|
|
42
|
+
/** 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)));
|
|
44
|
+
/** The number of rows. @param {Layout} layout @returns {number} */
|
|
45
|
+
export const depth = (layout) => (layout.rows || []).length;
|
|
46
|
+
|
|
47
|
+
// project(layout, fields): only the fields something reads, key by key, so a change to anything else (a legend, when
|
|
48
|
+
// only positions matter) isn't a change to it. Positions always count.
|
|
49
|
+
/** Only the fields something reads, key by key (positions always). @param {Layout} layout @param {string[]} [fields] @returns {object[]} */
|
|
50
|
+
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,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/keyboard-layout",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A plain JSON keyboard layout format: rows of keys with widths and any per-key properties.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"types": "./keyboard-layout.d.mts",
|
|
8
|
+
"default": "./keyboard-layout.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"keyboard-layout.mjs",
|
|
12
|
+
"keyboard-layout.d.mts",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"keyboard",
|
|
19
|
+
"layout",
|
|
20
|
+
"mechanical-keyboard"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/JoniBach/rulecad.git",
|
|
29
|
+
"directory": "packages/keyboard-layout"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node --test"
|
|
33
|
+
},
|
|
34
|
+
"types": "./keyboard-layout.d.mts"
|
|
35
|
+
}
|