@sdeck/plugin 0.2.0 → 0.3.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/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +157 -1
- package/dist/index.js.map +9 -8
- package/dist/permissions.d.ts +1 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/plugin.d.ts +45 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/settings.d.ts +24 -3
- package/dist/settings.d.ts.map +1 -1
- package/dist/text.d.ts +44 -0
- package/dist/text.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +12 -0
- package/src/permissions.ts +6 -0
- package/src/plugin.ts +47 -0
- package/src/settings.ts +46 -6
- package/src/text.ts +192 -0
package/dist/settings.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"settings.d.ts","sourceRoot":"","sources":["../src/settings.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,KAAK,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAE3C,UAAU,SAAS;IACjB,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,iCAAiC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;OAGG;IACH,SAAS,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,SAAS,EAAE,CAAC,CAAC,CAAC;CACxE;AAED,MAAM,WAAW,SAAU,SAAQ,SAAS;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,oEAAoE;IACpE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AA6E;AAC7E,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,EAAE,GAAG,SAAS,CAetE;AAID,sEAAsE;AACtE,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,SAAS,CAG7D;AA4BD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,cAAc,EACtD,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,GACpD,aAAa,CAAC,CAAC,CAAC,CAIlB;AAED,0FAA0F;AAC1F,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,GAAG,cAAc,CAErE;AAED,+EAA+E;AAC/E,wBAAgB,YAAY,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,EAAE,CAI7D;AAED,8DAA8D;AAC9D,wBAAgB,cAAc,CAC5B,KAAK,EAAE,YAAY,EACnB,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC1C,OAAO,CAMT;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,cAAc,EACtB,OAAO,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM,GAC9C,MAAM,EAAE,CA2CV"}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Choosing a text size without laying the text out. */
|
|
2
|
+
/** A stretch of height text can be in, such as one row of keys. */
|
|
3
|
+
export interface TextRow {
|
|
4
|
+
top: number;
|
|
5
|
+
height: number;
|
|
6
|
+
/** Where it starts and how wide it is, when that is not all of the width. */
|
|
7
|
+
left?: number;
|
|
8
|
+
width?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface TextLayoutOptions {
|
|
11
|
+
width: number;
|
|
12
|
+
/** Each line goes into one of these, whole: pass an overlay's `rows`. */
|
|
13
|
+
rows: readonly TextRow[];
|
|
14
|
+
/** The smallest size to go down to, even if the text then does not fit (default 8). */
|
|
15
|
+
min?: number;
|
|
16
|
+
/** The largest size (default: whatever fits). */
|
|
17
|
+
max?: number;
|
|
18
|
+
/** Line height, as a multiple of the size (default 1.2). */
|
|
19
|
+
lineHeight?: number;
|
|
20
|
+
/** Where lines go when there is room to spare (default `middle`). */
|
|
21
|
+
position?: "top" | "middle" | "bottom";
|
|
22
|
+
}
|
|
23
|
+
export interface TextLayout {
|
|
24
|
+
size: number;
|
|
25
|
+
/** The lines: where they are in the text, and the place each one has. */
|
|
26
|
+
lines: {
|
|
27
|
+
text: string;
|
|
28
|
+
start: number;
|
|
29
|
+
end: number;
|
|
30
|
+
top: number;
|
|
31
|
+
left: number;
|
|
32
|
+
width: number;
|
|
33
|
+
}[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Breaks text into lines and finds the largest font size at which they fit. It goes by how wide
|
|
37
|
+
* characters of the built-in font usually are, so leave a little room at the sides.
|
|
38
|
+
*/
|
|
39
|
+
export declare function layoutText(text: string, options: TextLayoutOptions): TextLayout;
|
|
40
|
+
/** The largest font size at which `text`, wrapped between words, fits a box. */
|
|
41
|
+
export declare function fitText(text: string, { height, ...options }: Omit<TextLayoutOptions, "rows" | "position"> & {
|
|
42
|
+
height: number;
|
|
43
|
+
}): number;
|
|
44
|
+
//# sourceMappingURL=text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../src/text.ts"],"names":[],"mappings":"AAAA,wDAAwD;AAiExD,mEAAmE;AACnE,MAAM,WAAW,OAAO;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,yEAAyE;IACzE,IAAI,EAAE,SAAS,OAAO,EAAE,CAAC;IACzB,uFAAuF;IACvF,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,4DAA4D;IAC5D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,QAAQ,CAAC;CACxC;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,GAAG,EAAE,MAAM,CAAC;QACZ,GAAG,EAAE,MAAM,CAAC;QACZ,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;KACf,EAAE,CAAC;CACL;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,GAAG,UAAU,CA8E/E;AAED,gFAAgF;AAChF,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,EAAE,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,UAAU,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GACxF,MAAM,CAER"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeck/plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "SDK for sdeck plugins: actions that run headless or draw with React, live state, triggers and option sources.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"test": "bun test"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@sdeck/app": "0.
|
|
29
|
-
"@sdeck/engine": "0.
|
|
30
|
-
"@sdeck/schema": "0.
|
|
28
|
+
"@sdeck/app": "0.3.0",
|
|
29
|
+
"@sdeck/engine": "0.3.0",
|
|
30
|
+
"@sdeck/schema": "0.3.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": ">=19.3"
|
package/src/index.ts
CHANGED
|
@@ -49,6 +49,8 @@ export {
|
|
|
49
49
|
type Onboarding,
|
|
50
50
|
type OnboardingStep,
|
|
51
51
|
type OptionSource,
|
|
52
|
+
type Overlay,
|
|
53
|
+
type OverlayProps,
|
|
52
54
|
PLUGIN_API_VERSION,
|
|
53
55
|
type PluginCommand,
|
|
54
56
|
type PluginContext,
|
|
@@ -77,6 +79,7 @@ export {
|
|
|
77
79
|
type ImageField,
|
|
78
80
|
type InferSettings,
|
|
79
81
|
isFieldVisible,
|
|
82
|
+
liveSettings,
|
|
80
83
|
type NumberField,
|
|
81
84
|
type OptionGroup,
|
|
82
85
|
optionGroups,
|
|
@@ -88,6 +91,15 @@ export {
|
|
|
88
91
|
type SettingField,
|
|
89
92
|
type SettingsSchema,
|
|
90
93
|
type SettingValue,
|
|
94
|
+
type SlotsField,
|
|
95
|
+
slotList,
|
|
91
96
|
type TextField,
|
|
92
97
|
} from "./settings.ts";
|
|
93
98
|
export { Shared } from "./shared.ts";
|
|
99
|
+
export {
|
|
100
|
+
fitText,
|
|
101
|
+
layoutText,
|
|
102
|
+
type TextLayout,
|
|
103
|
+
type TextLayoutOptions,
|
|
104
|
+
type TextRow,
|
|
105
|
+
} from "./text.ts";
|
package/src/permissions.ts
CHANGED
|
@@ -14,6 +14,7 @@ export type Permission =
|
|
|
14
14
|
| "variables"
|
|
15
15
|
| "state"
|
|
16
16
|
| "actions"
|
|
17
|
+
| "overlay"
|
|
17
18
|
| "native";
|
|
18
19
|
|
|
19
20
|
/** A permission as the user reads it. */
|
|
@@ -87,6 +88,11 @@ export const PERMISSION_TEXTS: Readonly<Record<Permission, PermissionText>> = {
|
|
|
87
88
|
action: "run other plugins' actions",
|
|
88
89
|
powerful: true,
|
|
89
90
|
},
|
|
91
|
+
overlay: {
|
|
92
|
+
name: "Draw over buttons",
|
|
93
|
+
description: "Show images and text on top of any of your buttons.",
|
|
94
|
+
action: "draw over your buttons",
|
|
95
|
+
},
|
|
90
96
|
native: {
|
|
91
97
|
name: "Native code",
|
|
92
98
|
description: "Load compiled code, which sdeck cannot limit in any way.",
|
package/src/plugin.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type SelectOption,
|
|
10
10
|
type SettingsSchema,
|
|
11
11
|
} from "./settings.ts";
|
|
12
|
+
import type { TextRow } from "./text.ts";
|
|
12
13
|
|
|
13
14
|
/** Bumped when the plugin contract changes incompatibly. Hosts refuse plugins built for another. */
|
|
14
15
|
export const PLUGIN_API_VERSION = 2;
|
|
@@ -41,6 +42,17 @@ export interface PluginContext {
|
|
|
41
42
|
set(id: string, value: unknown): void;
|
|
42
43
|
/** Keys of other plugins need the `state` permission. */
|
|
43
44
|
get(key: string): unknown;
|
|
45
|
+
/**
|
|
46
|
+
* Calls `listener` whenever one of `keys` changes, and keeps them up to date meanwhile (`get`
|
|
47
|
+
* alone does not). Returns a function that stops. Same permission as `get`.
|
|
48
|
+
*/
|
|
49
|
+
watch(keys: readonly string[], listener: () => void): () => void;
|
|
50
|
+
};
|
|
51
|
+
/** Pictures and text on top of the user's buttons. Needs the `overlay` permission. */
|
|
52
|
+
readonly overlays: {
|
|
53
|
+
/** Shows `overlay` on every deck, replacing this plugin's overlay called `id`. */
|
|
54
|
+
show(id: string, overlay: Overlay): void;
|
|
55
|
+
hide(id: string): void;
|
|
44
56
|
};
|
|
45
57
|
/** Emits `<namespace>:<event>` on the event bus (workflows can trigger on it). */
|
|
46
58
|
emit(event: string, payload?: unknown): void;
|
|
@@ -51,6 +63,41 @@ export interface PluginContext {
|
|
|
51
63
|
};
|
|
52
64
|
}
|
|
53
65
|
|
|
66
|
+
/** Something drawn as one picture across several slots. The last one shown is on top. */
|
|
67
|
+
export interface Overlay {
|
|
68
|
+
/**
|
|
69
|
+
* The slots it covers: `key:0`, `sensor:1`, `window` (as a `slots` setting gives them). Leaving
|
|
70
|
+
* it out, or an empty list, covers the entire deck. Slots a deck does not have are skipped, and
|
|
71
|
+
* touch sensors show no text.
|
|
72
|
+
*/
|
|
73
|
+
slots?: readonly string[];
|
|
74
|
+
/** Shown on this page only (default: every page). */
|
|
75
|
+
page?: string;
|
|
76
|
+
/**
|
|
77
|
+
* What happens between slots, where a deck shows nothing. `keep`: the picture runs on behind
|
|
78
|
+
* the gaps, as through a window. `close`: slots are drawn as if they touched, so nothing is
|
|
79
|
+
* lost, which suits text. Default `keep`.
|
|
80
|
+
*/
|
|
81
|
+
gaps?: "keep" | "close";
|
|
82
|
+
/**
|
|
83
|
+
* Draws it, at the size of the whole overlay. Every slot draws all of it and shows its own
|
|
84
|
+
* part, so it must look the same in each: keep state outside, or in `@sdeck/plugin` hooks.
|
|
85
|
+
* Transparent parts show the buttons; `null` draws nothing.
|
|
86
|
+
*/
|
|
87
|
+
content: ComponentType<OverlayProps>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface OverlayProps {
|
|
91
|
+
/** The size of the whole overlay. On a Neo these are pixels, and a key is 96×96. */
|
|
92
|
+
width: number;
|
|
93
|
+
height: number;
|
|
94
|
+
/**
|
|
95
|
+
* The rows of keys, or the window, it lies over, from the top, and how wide each is. Text reads best when no line is cut by the gap
|
|
96
|
+
* between two of them: `layoutText` places it so.
|
|
97
|
+
*/
|
|
98
|
+
rows: readonly TextRow[];
|
|
99
|
+
}
|
|
100
|
+
|
|
54
101
|
/** How a plugin's status reads: fine, needing attention, or failing. */
|
|
55
102
|
export type StatusLevel = "ok" | "info" | "error";
|
|
56
103
|
|
package/src/settings.ts
CHANGED
|
@@ -31,6 +31,11 @@ export interface TextField extends FieldBase {
|
|
|
31
31
|
source?: string;
|
|
32
32
|
/** Other fields the suggestions depend on, passed to the source. */
|
|
33
33
|
dependsOn?: readonly string[];
|
|
34
|
+
/**
|
|
35
|
+
* `{{templates}}` in it reach `run` as they were written, for drawing with `useTemplate` so the
|
|
36
|
+
* text follows their values. Ones that read the run itself (`event`, `loop`…) are filled in.
|
|
37
|
+
*/
|
|
38
|
+
live?: boolean;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
export interface NumberField extends FieldBase {
|
|
@@ -88,12 +93,25 @@ export interface SelectField extends FieldBase {
|
|
|
88
93
|
export interface PageField extends FieldBase {
|
|
89
94
|
type: "page";
|
|
90
95
|
default?: string;
|
|
96
|
+
/** Offers choosing no page (an empty value), under this name: `Every page`. */
|
|
97
|
+
none?: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Keys, touch sensors and the window, picked on a picture of the deck: `key:0`, `sensor:1`,
|
|
102
|
+
* `window`. An empty list stands for the entire deck.
|
|
103
|
+
*/
|
|
104
|
+
export interface SlotsField extends FieldBase {
|
|
105
|
+
type: "slots";
|
|
106
|
+
default?: readonly string[];
|
|
91
107
|
}
|
|
92
108
|
|
|
93
109
|
/** An image: an `http(s)` URL or a `data:` URI (the UI accepts a URL or a file). */
|
|
94
110
|
export interface ImageField extends FieldBase {
|
|
95
111
|
type: "image";
|
|
96
112
|
default?: string;
|
|
113
|
+
/** As on a text field: `{{spotify.art}}` reaches `run` as written. */
|
|
114
|
+
live?: boolean;
|
|
97
115
|
}
|
|
98
116
|
|
|
99
117
|
/** A named set the user made of options, from a `groups` field. */
|
|
@@ -122,6 +140,7 @@ export type SettingField =
|
|
|
122
140
|
| ColorField
|
|
123
141
|
| SelectField
|
|
124
142
|
| PageField
|
|
143
|
+
| SlotsField
|
|
125
144
|
| ImageField
|
|
126
145
|
| GroupsField
|
|
127
146
|
| SecretField;
|
|
@@ -135,14 +154,16 @@ export type FieldValue<F extends SettingField> = F extends NumberField
|
|
|
135
154
|
? boolean
|
|
136
155
|
: F extends GroupsField
|
|
137
156
|
? readonly OptionGroup[]
|
|
138
|
-
: F extends
|
|
139
|
-
?
|
|
140
|
-
|
|
141
|
-
:
|
|
142
|
-
|
|
157
|
+
: F extends SlotsField
|
|
158
|
+
? readonly string[]
|
|
159
|
+
: F extends SelectField
|
|
160
|
+
? F extends { source: string }
|
|
161
|
+
? string
|
|
162
|
+
: F["options"][number]["value"]
|
|
163
|
+
: string;
|
|
143
164
|
|
|
144
165
|
/** A value of any field. */
|
|
145
|
-
export type SettingValue = Primitive | readonly OptionGroup[];
|
|
166
|
+
export type SettingValue = Primitive | readonly OptionGroup[] | readonly string[];
|
|
146
167
|
|
|
147
168
|
/** The settings object an action receives for a schema. */
|
|
148
169
|
export type InferSettings<S extends SettingsSchema> = {
|
|
@@ -161,6 +182,7 @@ export function defaultValue(field: SettingField): SettingValue {
|
|
|
161
182
|
case "color":
|
|
162
183
|
return field.default ?? "#000000";
|
|
163
184
|
case "groups":
|
|
185
|
+
case "slots":
|
|
164
186
|
return field.default ?? [];
|
|
165
187
|
case "select":
|
|
166
188
|
return field.default ?? field.options[0]?.value ?? "";
|
|
@@ -187,6 +209,14 @@ export function optionGroups(value: unknown): OptionGroup[] | undefined {
|
|
|
187
209
|
});
|
|
188
210
|
}
|
|
189
211
|
|
|
212
|
+
const SLOT = /^(window|(key|sensor):\d{1,3})$/;
|
|
213
|
+
|
|
214
|
+
/** The slots in a stored value, or `undefined` if it isn't a list. */
|
|
215
|
+
export function slotList(value: unknown): string[] | undefined {
|
|
216
|
+
if (!Array.isArray(value)) return undefined;
|
|
217
|
+
return [...new Set(value.filter((v): v is string => typeof v === "string" && SLOT.test(v)))];
|
|
218
|
+
}
|
|
219
|
+
|
|
190
220
|
function coerce(field: SettingField, value: unknown): SettingValue {
|
|
191
221
|
switch (field.type) {
|
|
192
222
|
case "number": {
|
|
@@ -202,6 +232,8 @@ function coerce(field: SettingField, value: unknown): SettingValue {
|
|
|
202
232
|
: defaultValue(field);
|
|
203
233
|
case "groups":
|
|
204
234
|
return optionGroups(value) ?? defaultValue(field);
|
|
235
|
+
case "slots":
|
|
236
|
+
return slotList(value) ?? defaultValue(field);
|
|
205
237
|
case "select":
|
|
206
238
|
if (typeof value !== "string") return defaultValue(field);
|
|
207
239
|
if (field.source !== undefined) return value;
|
|
@@ -229,6 +261,13 @@ export function behaviorFields(schema: SettingsSchema): SettingsSchema {
|
|
|
229
261
|
return Object.fromEntries(Object.entries(schema).filter(([, field]) => !field.appearance));
|
|
230
262
|
}
|
|
231
263
|
|
|
264
|
+
/** The settings whose templates are left for the action to keep up to date. */
|
|
265
|
+
export function liveSettings(schema: SettingsSchema): string[] {
|
|
266
|
+
return Object.entries(schema).flatMap(([key, field]) =>
|
|
267
|
+
"live" in field && field.live ? [key] : [],
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
232
271
|
/** Whether `field` is shown for these (resolved) settings. */
|
|
233
272
|
export function isFieldVisible(
|
|
234
273
|
field: SettingField,
|
|
@@ -257,6 +296,7 @@ export function checkSchema(
|
|
|
257
296
|
"color",
|
|
258
297
|
"select",
|
|
259
298
|
"page",
|
|
299
|
+
"slots",
|
|
260
300
|
"image",
|
|
261
301
|
"groups",
|
|
262
302
|
"secret",
|
package/src/text.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/** Choosing a text size without laying the text out. */
|
|
2
|
+
|
|
3
|
+
/** How wide characters are in the built-in font, in ems. A little generous, so text fits. */
|
|
4
|
+
function emWidth(char: string): number {
|
|
5
|
+
if (/[ᄀ-ᇿ⺀-鿿가-豈--⦆]/.test(char)) return 1;
|
|
6
|
+
if (/\p{Extended_Pictographic}/u.test(char)) return 1.25;
|
|
7
|
+
if (/\p{Mark}/u.test(char)) return 0;
|
|
8
|
+
if (char === " ") return 0.27;
|
|
9
|
+
if (/[ijl.,:;'|!’‘·]/.test(char)) return 0.29;
|
|
10
|
+
if (/[Ifrt()[\]{}/\\"“”-]/.test(char)) return 0.41;
|
|
11
|
+
if (/[mwMW@%—]/.test(char)) return 0.93;
|
|
12
|
+
if (/[\p{Lu}\d]/u.test(char)) return 0.69;
|
|
13
|
+
return 0.6;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const textWidth = (text: string) => [...text].reduce((sum, c) => sum + emWidth(c), 0);
|
|
17
|
+
|
|
18
|
+
/** Chinese, Japanese and Korean characters, between which a line can break. */
|
|
19
|
+
const CJK = "\\u2e80-\\u9fff\\uac00-\\ud7af\\uff00-\\uff60";
|
|
20
|
+
const TOKENS = new RegExp(`\\n|[^\\S\\n]+|[${CJK}]|[^\\s${CJK}]+`, "g");
|
|
21
|
+
|
|
22
|
+
interface Range {
|
|
23
|
+
start: number;
|
|
24
|
+
end: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Breaks text into lines, each at most as many ems wide as `room` says for its number.
|
|
29
|
+
* `undefined` when a word is too long for its line.
|
|
30
|
+
*/
|
|
31
|
+
function wrap(text: string, room: (line: number) => number): Range[] | undefined {
|
|
32
|
+
const lines: Range[] = [];
|
|
33
|
+
let line: Range | undefined;
|
|
34
|
+
let used = 0;
|
|
35
|
+
for (const match of text.matchAll(TOKENS)) {
|
|
36
|
+
const [token] = match;
|
|
37
|
+
const start = match.index;
|
|
38
|
+
const end = start + token.length;
|
|
39
|
+
if (token === "\n") {
|
|
40
|
+
lines.push(line ?? { start, end: start });
|
|
41
|
+
line = undefined;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const width = textWidth(token);
|
|
45
|
+
if (token.trim() === "") {
|
|
46
|
+
used += width;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (line && used + width > room(lines.length)) {
|
|
50
|
+
lines.push(line);
|
|
51
|
+
line = undefined;
|
|
52
|
+
}
|
|
53
|
+
if (line) {
|
|
54
|
+
line.end = end;
|
|
55
|
+
used += width;
|
|
56
|
+
} else {
|
|
57
|
+
if (width > room(lines.length)) return undefined;
|
|
58
|
+
line = { start, end };
|
|
59
|
+
used = width;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (line) lines.push(line);
|
|
63
|
+
return lines;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A stretch of height text can be in, such as one row of keys. */
|
|
67
|
+
export interface TextRow {
|
|
68
|
+
top: number;
|
|
69
|
+
height: number;
|
|
70
|
+
/** Where it starts and how wide it is, when that is not all of the width. */
|
|
71
|
+
left?: number;
|
|
72
|
+
width?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface TextLayoutOptions {
|
|
76
|
+
width: number;
|
|
77
|
+
/** Each line goes into one of these, whole: pass an overlay's `rows`. */
|
|
78
|
+
rows: readonly TextRow[];
|
|
79
|
+
/** The smallest size to go down to, even if the text then does not fit (default 8). */
|
|
80
|
+
min?: number;
|
|
81
|
+
/** The largest size (default: whatever fits). */
|
|
82
|
+
max?: number;
|
|
83
|
+
/** Line height, as a multiple of the size (default 1.2). */
|
|
84
|
+
lineHeight?: number;
|
|
85
|
+
/** Where lines go when there is room to spare (default `middle`). */
|
|
86
|
+
position?: "top" | "middle" | "bottom";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface TextLayout {
|
|
90
|
+
size: number;
|
|
91
|
+
/** The lines: where they are in the text, and the place each one has. */
|
|
92
|
+
lines: {
|
|
93
|
+
text: string;
|
|
94
|
+
start: number;
|
|
95
|
+
end: number;
|
|
96
|
+
top: number;
|
|
97
|
+
left: number;
|
|
98
|
+
width: number;
|
|
99
|
+
}[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Breaks text into lines and finds the largest font size at which they fit. It goes by how wide
|
|
104
|
+
* characters of the built-in font usually are, so leave a little room at the sides.
|
|
105
|
+
*/
|
|
106
|
+
export function layoutText(text: string, options: TextLayoutOptions): TextLayout {
|
|
107
|
+
const { width, rows, min = 8, lineHeight = 1.2, position = "middle" } = options;
|
|
108
|
+
const tallest = Math.max(0, ...rows.map((r) => r.height));
|
|
109
|
+
const max = Math.max(min, Math.floor(options.max ?? tallest / lineHeight));
|
|
110
|
+
/** The places for lines, by row. */
|
|
111
|
+
const slots = (size: number) =>
|
|
112
|
+
rows.flatMap((row, index) => {
|
|
113
|
+
const count = Math.floor((row.height + 0.01) / (size * lineHeight));
|
|
114
|
+
return Array.from({ length: count }, () => ({
|
|
115
|
+
index,
|
|
116
|
+
left: row.left ?? 0,
|
|
117
|
+
width: row.width ?? width,
|
|
118
|
+
}));
|
|
119
|
+
});
|
|
120
|
+
/** The lines from the `skip`th place on, if they fit. */
|
|
121
|
+
const fill = (size: number, skip = 0) => {
|
|
122
|
+
const places = slots(size).slice(skip);
|
|
123
|
+
const lines = wrap(text, (i) => (places[i]?.width ?? 0) / size);
|
|
124
|
+
return lines && lines.length <= places.length ? lines : undefined;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
let size = max;
|
|
128
|
+
if (!fill(max)) {
|
|
129
|
+
size = min;
|
|
130
|
+
for (let high = max; high - size > 1; ) {
|
|
131
|
+
const middle = Math.floor((size + high) / 2);
|
|
132
|
+
if (fill(middle)) size = middle;
|
|
133
|
+
else high = middle;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const places = slots(size);
|
|
138
|
+
const last = rows.at(-1);
|
|
139
|
+
if (last && rows.length > 1 && places.length === 0) {
|
|
140
|
+
// Taller than any row, as asked: then it has to run across them.
|
|
141
|
+
const top = rows[0]?.top ?? 0;
|
|
142
|
+
const whole = { top, height: last.top + last.height - top };
|
|
143
|
+
return layoutText(text, { ...options, rows: [whole] });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let skip = 0;
|
|
147
|
+
let lines = fill(size);
|
|
148
|
+
if (lines && position !== "top") {
|
|
149
|
+
const spare = places.length - lines.length;
|
|
150
|
+
// Further down the rows can be narrower, and need more lines.
|
|
151
|
+
for (let wanted = position === "bottom" ? spare : Math.floor(spare / 2); wanted > 0; wanted--) {
|
|
152
|
+
const moved = fill(size, wanted);
|
|
153
|
+
if (!moved) continue;
|
|
154
|
+
lines = moved;
|
|
155
|
+
skip = wanted;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// What does not fit even at the smallest size runs out at the bottom and the sides.
|
|
160
|
+
lines ??=
|
|
161
|
+
wrap(text, (i) => (places[i]?.width ?? width) / size) ?? wrap(text, () => Infinity) ?? [];
|
|
162
|
+
|
|
163
|
+
const height = size * lineHeight;
|
|
164
|
+
const used = places.slice(skip, skip + lines.length);
|
|
165
|
+
const placed: TextLayout["lines"] = [];
|
|
166
|
+
for (const [i, line] of lines.entries()) {
|
|
167
|
+
const place = used[i];
|
|
168
|
+
const row = place && rows[place.index];
|
|
169
|
+
const before = placed.at(-1);
|
|
170
|
+
let top = before ? before.top + height : (rows[0]?.top ?? 0);
|
|
171
|
+
if (row && used[i - 1]?.index !== place.index) {
|
|
172
|
+
const free = row.height - used.filter((p) => p.index === place.index).length * height;
|
|
173
|
+
top = row.top + (position === "top" ? 0 : position === "bottom" ? free : free / 2);
|
|
174
|
+
}
|
|
175
|
+
placed.push({
|
|
176
|
+
...line,
|
|
177
|
+
text: text.slice(line.start, line.end),
|
|
178
|
+
top,
|
|
179
|
+
left: place?.left ?? 0,
|
|
180
|
+
width: place?.width ?? width,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return { size, lines: placed };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The largest font size at which `text`, wrapped between words, fits a box. */
|
|
187
|
+
export function fitText(
|
|
188
|
+
text: string,
|
|
189
|
+
{ height, ...options }: Omit<TextLayoutOptions, "rows" | "position"> & { height: number },
|
|
190
|
+
): number {
|
|
191
|
+
return layoutText(text, { ...options, rows: [{ top: 0, height }] }).size;
|
|
192
|
+
}
|