@rulecad/keyboard-layout 0.1.0 → 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/keyboard-layout.d.mts +36 -0
- package/keyboard-layout.mjs +12 -0
- package/package.json +8 -3
|
@@ -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
|
+
};
|
package/keyboard-layout.mjs
CHANGED
|
@@ -7,12 +7,21 @@
|
|
|
7
7
|
// "@label" is the key's label: "defaults": { "legend": "@label" } gives every key its label as its legend.
|
|
8
8
|
// No imports and no file access: the format alone, for browsers and Node alike.
|
|
9
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
|
+
|
|
10
14
|
// A key as { label, u, …properties }
|
|
11
15
|
const key = (k) => (typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k });
|
|
12
16
|
const rowOf = (r) => (Array.isArray(r) ? { keys: r } : r);
|
|
13
17
|
|
|
14
18
|
// keys(layout) -> [{ row, col, label, u, units, x, y, …properties }]: each key's centre in key units, the layout
|
|
15
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
|
+
*/
|
|
16
25
|
export function keys(layout) {
|
|
17
26
|
const rows = (layout.rows || []).map(rowOf), defaults = layout.defaults || {};
|
|
18
27
|
const across = width(layout), out = [];
|
|
@@ -30,9 +39,12 @@ export function keys(layout) {
|
|
|
30
39
|
}
|
|
31
40
|
|
|
32
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} */
|
|
33
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} */
|
|
34
45
|
export const depth = (layout) => (layout.rows || []).length;
|
|
35
46
|
|
|
36
47
|
// project(layout, fields): only the fields something reads, key by key, so a change to anything else (a legend, when
|
|
37
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[]} */
|
|
38
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,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/keyboard-layout",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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":
|
|
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
|
}
|