@astl/editor 0.0.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.
Files changed (161) hide show
  1. package/CHANGELOG.md +107 -0
  2. package/LICENSE +202 -0
  3. package/README.md +43 -0
  4. package/dist/authoring.d.ts +284 -0
  5. package/dist/authoring.d.ts.map +1 -0
  6. package/dist/authoring.js +566 -0
  7. package/dist/authoring.js.map +1 -0
  8. package/dist/define.d.ts +8 -0
  9. package/dist/define.d.ts.map +1 -0
  10. package/dist/define.js +35 -0
  11. package/dist/define.js.map +1 -0
  12. package/dist/edit.d.ts +45 -0
  13. package/dist/edit.d.ts.map +1 -0
  14. package/dist/edit.js +150 -0
  15. package/dist/edit.js.map +1 -0
  16. package/dist/elements/base.d.ts +31 -0
  17. package/dist/elements/base.d.ts.map +1 -0
  18. package/dist/elements/base.js +75 -0
  19. package/dist/elements/base.js.map +1 -0
  20. package/dist/elements/color-control.d.ts +25 -0
  21. package/dist/elements/color-control.d.ts.map +1 -0
  22. package/dist/elements/color-control.js +139 -0
  23. package/dist/elements/color-control.js.map +1 -0
  24. package/dist/elements/help-form.d.ts +31 -0
  25. package/dist/elements/help-form.d.ts.map +1 -0
  26. package/dist/elements/help-form.js +371 -0
  27. package/dist/elements/help-form.js.map +1 -0
  28. package/dist/elements/insert-menu.d.ts +79 -0
  29. package/dist/elements/insert-menu.d.ts.map +1 -0
  30. package/dist/elements/insert-menu.js +362 -0
  31. package/dist/elements/insert-menu.js.map +1 -0
  32. package/dist/elements/inspector.d.ts +24 -0
  33. package/dist/elements/inspector.d.ts.map +1 -0
  34. package/dist/elements/inspector.js +846 -0
  35. package/dist/elements/inspector.js.map +1 -0
  36. package/dist/elements/issue-list.d.ts +18 -0
  37. package/dist/elements/issue-list.d.ts.map +1 -0
  38. package/dist/elements/issue-list.js +85 -0
  39. package/dist/elements/issue-list.js.map +1 -0
  40. package/dist/elements/issues.d.ts +24 -0
  41. package/dist/elements/issues.d.ts.map +1 -0
  42. package/dist/elements/issues.js +81 -0
  43. package/dist/elements/issues.js.map +1 -0
  44. package/dist/elements/picture-dialog.d.ts +40 -0
  45. package/dist/elements/picture-dialog.d.ts.map +1 -0
  46. package/dist/elements/picture-dialog.js +433 -0
  47. package/dist/elements/picture-dialog.js.map +1 -0
  48. package/dist/elements/picture-field.d.ts +33 -0
  49. package/dist/elements/picture-field.d.ts.map +1 -0
  50. package/dist/elements/picture-field.js +692 -0
  51. package/dist/elements/picture-field.js.map +1 -0
  52. package/dist/elements/preview.d.ts +34 -0
  53. package/dist/elements/preview.d.ts.map +1 -0
  54. package/dist/elements/preview.js +165 -0
  55. package/dist/elements/preview.js.map +1 -0
  56. package/dist/elements/style-form.d.ts +30 -0
  57. package/dist/elements/style-form.d.ts.map +1 -0
  58. package/dist/elements/style-form.js +640 -0
  59. package/dist/elements/style-form.js.map +1 -0
  60. package/dist/elements/style-load.d.ts +26 -0
  61. package/dist/elements/style-load.d.ts.map +1 -0
  62. package/dist/elements/style-load.js +50 -0
  63. package/dist/elements/style-load.js.map +1 -0
  64. package/dist/elements/style-popups.d.ts +39 -0
  65. package/dist/elements/style-popups.d.ts.map +1 -0
  66. package/dist/elements/style-popups.js +173 -0
  67. package/dist/elements/style-popups.js.map +1 -0
  68. package/dist/elements/style-rules.d.ts +21 -0
  69. package/dist/elements/style-rules.d.ts.map +1 -0
  70. package/dist/elements/style-rules.js +274 -0
  71. package/dist/elements/style-rules.js.map +1 -0
  72. package/dist/elements/styles.d.ts +9 -0
  73. package/dist/elements/styles.d.ts.map +1 -0
  74. package/dist/elements/styles.js +161 -0
  75. package/dist/elements/styles.js.map +1 -0
  76. package/dist/elements/toolbar.d.ts +65 -0
  77. package/dist/elements/toolbar.d.ts.map +1 -0
  78. package/dist/elements/toolbar.js +1270 -0
  79. package/dist/elements/toolbar.js.map +1 -0
  80. package/dist/help.d.ts +33 -0
  81. package/dist/help.d.ts.map +1 -0
  82. package/dist/help.js +111 -0
  83. package/dist/help.js.map +1 -0
  84. package/dist/index.d.ts +32 -0
  85. package/dist/index.d.ts.map +1 -0
  86. package/dist/index.js +17 -0
  87. package/dist/index.js.map +1 -0
  88. package/dist/inspect.d.ts +251 -0
  89. package/dist/inspect.d.ts.map +1 -0
  90. package/dist/inspect.js +428 -0
  91. package/dist/inspect.js.map +1 -0
  92. package/dist/messages/en.json +252 -0
  93. package/dist/messages/help/en.json +18 -0
  94. package/dist/messages/help/pl.json +18 -0
  95. package/dist/messages/pl.json +252 -0
  96. package/dist/messages/style/en.json +67 -0
  97. package/dist/messages/style/pl.json +67 -0
  98. package/dist/messages.d.ts +5 -0
  99. package/dist/messages.d.ts.map +1 -0
  100. package/dist/messages.js +9 -0
  101. package/dist/messages.js.map +1 -0
  102. package/dist/pages.d.ts +28 -0
  103. package/dist/pages.d.ts.map +1 -0
  104. package/dist/pages.js +147 -0
  105. package/dist/pages.js.map +1 -0
  106. package/dist/scan.d.ts +82 -0
  107. package/dist/scan.d.ts.map +1 -0
  108. package/dist/scan.js +329 -0
  109. package/dist/scan.js.map +1 -0
  110. package/dist/style-messages.d.ts +7 -0
  111. package/dist/style-messages.d.ts.map +1 -0
  112. package/dist/style-messages.js +12 -0
  113. package/dist/style-messages.js.map +1 -0
  114. package/dist/style-model.d.ts +50 -0
  115. package/dist/style-model.d.ts.map +1 -0
  116. package/dist/style-model.js +76 -0
  117. package/dist/style-model.js.map +1 -0
  118. package/dist/style-placing.d.ts +38 -0
  119. package/dist/style-placing.d.ts.map +1 -0
  120. package/dist/style-placing.js +98 -0
  121. package/dist/style-placing.js.map +1 -0
  122. package/dist/textarea.d.ts +17 -0
  123. package/dist/textarea.d.ts.map +1 -0
  124. package/dist/textarea.js +175 -0
  125. package/dist/textarea.js.map +1 -0
  126. package/package.json +54 -4
  127. package/src/authoring.ts +896 -0
  128. package/src/define.ts +34 -0
  129. package/src/edit.ts +195 -0
  130. package/src/elements/base.ts +93 -0
  131. package/src/elements/color-control.ts +159 -0
  132. package/src/elements/help-form.ts +404 -0
  133. package/src/elements/insert-menu.ts +481 -0
  134. package/src/elements/inspector.ts +941 -0
  135. package/src/elements/issue-list.ts +103 -0
  136. package/src/elements/issues.ts +89 -0
  137. package/src/elements/picture-dialog.ts +461 -0
  138. package/src/elements/picture-field.ts +770 -0
  139. package/src/elements/preview.ts +186 -0
  140. package/src/elements/style-form.ts +751 -0
  141. package/src/elements/style-load.ts +64 -0
  142. package/src/elements/style-popups.ts +257 -0
  143. package/src/elements/style-rules.ts +294 -0
  144. package/src/elements/styles.ts +162 -0
  145. package/src/elements/toolbar.ts +1386 -0
  146. package/src/help.ts +156 -0
  147. package/src/index.ts +81 -0
  148. package/src/inspect.ts +774 -0
  149. package/src/messages/en.json +252 -0
  150. package/src/messages/help/en.json +18 -0
  151. package/src/messages/help/pl.json +18 -0
  152. package/src/messages/pl.json +252 -0
  153. package/src/messages/style/en.json +67 -0
  154. package/src/messages/style/pl.json +67 -0
  155. package/src/messages.ts +11 -0
  156. package/src/pages.ts +158 -0
  157. package/src/scan.ts +382 -0
  158. package/src/style-messages.ts +13 -0
  159. package/src/style-model.ts +125 -0
  160. package/src/style-placing.ts +117 -0
  161. package/src/textarea.ts +182 -0
package/src/inspect.ts ADDED
@@ -0,0 +1,774 @@
1
+ /**
2
+ * Inspector models: what an inspector shows for one component element. Fields come from the EditorDescriptor, or
3
+ * are derived from the manifest; controls follow the AttributeSpec types. Every component also gets `variant` (when
4
+ * its manifest has variants) and `style-template` (when templates are offered for it), after those fields. Plain
5
+ * data, so any UI can render them.
6
+ */
7
+ import {
8
+ type AttributeSpec,
9
+ allAttributes,
10
+ type ComponentManifest,
11
+ type EditorDescriptor,
12
+ type ElementNode,
13
+ type InspectorField,
14
+ type MessageParams,
15
+ type Node,
16
+ STYLE_TEMPLATE_ATTRIBUTE,
17
+ textContent,
18
+ } from '@astl/core';
19
+
20
+ export interface InspectorOption {
21
+ readonly value: string;
22
+ readonly label: string;
23
+ }
24
+
25
+ interface FieldTexts {
26
+ /** Translated label. */
27
+ readonly label: string;
28
+ /** Translated help text, or null. */
29
+ readonly help: string | null;
30
+ }
31
+
32
+ export interface TextFieldModel extends FieldTexts {
33
+ readonly kind: 'attribute';
34
+ readonly control: 'text';
35
+ readonly name: string;
36
+ /** The written value; '' when absent. */
37
+ readonly value: string;
38
+ readonly required: boolean;
39
+ /** A `url` attribute's files to upload (the field's `accept`), or null: the address is typed. */
40
+ readonly accept: string | null;
41
+ }
42
+
43
+ export interface NumberFieldModel extends FieldTexts {
44
+ readonly kind: 'attribute';
45
+ readonly control: 'number';
46
+ readonly name: string;
47
+ /** The written value, else the manifest default; '' when neither. */
48
+ readonly value: string;
49
+ readonly min: number | null;
50
+ readonly max: number | null;
51
+ /** `1` for integers. */
52
+ readonly step: 'any' | '1';
53
+ readonly required: boolean;
54
+ }
55
+
56
+ export interface CheckboxFieldModel extends FieldTexts {
57
+ readonly kind: 'attribute';
58
+ readonly control: 'checkbox';
59
+ readonly name: string;
60
+ readonly checked: boolean;
61
+ }
62
+
63
+ export interface SelectFieldModel extends FieldTexts {
64
+ readonly kind: 'attribute';
65
+ readonly control: 'select';
66
+ readonly name: string;
67
+ /** The written value, else the default; '' (the "not set" option) when neither. */
68
+ readonly value: string;
69
+ readonly options: readonly InspectorOption[];
70
+ }
71
+
72
+ export interface TextListFieldModel extends FieldTexts {
73
+ readonly kind: 'text-list';
74
+ readonly tag: string;
75
+ readonly values: readonly string[];
76
+ /** null = unbounded. */
77
+ readonly max: number | null;
78
+ /** Each value in a text area that keeps its line breaks. */
79
+ readonly multiline: boolean;
80
+ }
81
+
82
+ export interface ChildFlagModel {
83
+ /** Position among the children with this tag, from 0 (what `child-flag` changes use). */
84
+ readonly index: number;
85
+ readonly id: string | null;
86
+ /** The child's text, or a placeholder when it is empty. */
87
+ readonly label: string;
88
+ readonly checked: boolean;
89
+ }
90
+
91
+ export interface ChildFlagFieldModel extends FieldTexts {
92
+ readonly kind: 'child-flag';
93
+ readonly tag: string;
94
+ readonly attribute: string;
95
+ readonly children: readonly ChildFlagModel[];
96
+ }
97
+
98
+ export interface ChildTextModel {
99
+ /** Position among the children with this tag, from 0 (what `child-attributes` changes use). */
100
+ readonly index: number;
101
+ readonly id: string | null;
102
+ /** The child's text, or a placeholder when it is empty. */
103
+ readonly label: string;
104
+ /** The written value; '' when absent. */
105
+ readonly value: string;
106
+ }
107
+
108
+ export interface ChildTextFieldModel extends FieldTexts {
109
+ readonly kind: 'child-text';
110
+ readonly tag: string;
111
+ readonly attribute: string;
112
+ readonly children: readonly ChildTextModel[];
113
+ }
114
+
115
+ export interface ChildChoiceModel {
116
+ /** Position among the children with this tag, from 0 (what `child-choice` changes use). */
117
+ readonly index: number;
118
+ readonly id: string | null;
119
+ /** The child's text, or a placeholder when it is empty. */
120
+ readonly label: string;
121
+ /** The written value; '' when absent. */
122
+ readonly value: string;
123
+ /**
124
+ * The choices of this child. Enum source: the attribute's values, the same for every child (an absent or unknown
125
+ * value matches none). Child source: each option child's id with its text, plus the "not set" option when the
126
+ * value is absent and the written value itself when it names no option (so the control shows what the source says).
127
+ */
128
+ readonly options: readonly InspectorOption[];
129
+ }
130
+
131
+ export interface ChildChoiceFieldModel extends FieldTexts {
132
+ readonly kind: 'child-choice';
133
+ readonly tag: string;
134
+ readonly attribute: string;
135
+ /** Radios for the enum values of the attribute, a select for the ids of other children (`options`). */
136
+ readonly control: 'radios' | 'select';
137
+ readonly children: readonly ChildChoiceModel[];
138
+ }
139
+
140
+ /** A column of a `child-table` field: one child attribute, its control picked by the attribute's type. */
141
+ export interface ChildTableColumnModel {
142
+ readonly attribute: string;
143
+ /** Translated label. */
144
+ readonly label: string;
145
+ readonly control: 'text' | 'number' | 'select';
146
+ /** The choices of a select: the enum's values (a row whose value is none of them shows it, or "not set"). */
147
+ readonly options: readonly InspectorOption[];
148
+ readonly min: number | null;
149
+ readonly max: number | null;
150
+ /** `1` for integers. */
151
+ readonly step: 'any' | '1';
152
+ /** A required attribute is written empty rather than removed. */
153
+ readonly required: boolean;
154
+ }
155
+
156
+ export interface ChildTableRowModel {
157
+ /** Position among the children with this tag, from 0 (what `child-attributes` and `child-remove` use). */
158
+ readonly index: number;
159
+ readonly id: string | null;
160
+ /** The child's text, or a placeholder when it is empty. */
161
+ readonly label: string;
162
+ /** The written value of each column, '' when absent. */
163
+ readonly values: readonly string[];
164
+ }
165
+
166
+ export interface ChildTableFieldModel extends FieldTexts {
167
+ readonly kind: 'child-table';
168
+ readonly tag: string;
169
+ readonly columns: readonly ChildTableColumnModel[];
170
+ readonly rows: readonly ChildTableRowModel[];
171
+ /**
172
+ * The attributes of a new row (`child-add`): a fresh local id, `<tag without astl->-<n>`, and for each required
173
+ * attribute its first enum value, its minimum or ''.
174
+ */
175
+ readonly added: Readonly<Record<string, string>>;
176
+ /** The manifest's bounds on the number of children; null = unbounded. */
177
+ readonly min: number;
178
+ readonly max: number | null;
179
+ }
180
+
181
+ export type PictureShape = 'rect' | 'circle' | 'polygon';
182
+
183
+ /** One zone of a `picture-regions` field, in percent of the picture (absent or invalid numbers read as 0). */
184
+ export interface PictureRegionModel {
185
+ /** Position among the children with the field's tag, from 0 (what `child-*` changes use). */
186
+ readonly index: number;
187
+ readonly id: string;
188
+ readonly shape: PictureShape;
189
+ readonly x: number;
190
+ readonly y: number;
191
+ readonly width: number;
192
+ readonly height: number;
193
+ readonly r: number;
194
+ readonly points: readonly (readonly [number, number])[];
195
+ readonly label: string;
196
+ /** Whether the zone has the field's `flag` (a right place): drawn solid, the others dashed. */
197
+ readonly flagged: boolean;
198
+ }
199
+
200
+ /** One pin of a `picture-points` field, in percent of the picture. */
201
+ export interface PicturePointModel {
202
+ readonly index: number;
203
+ readonly id: string;
204
+ readonly x: number;
205
+ readonly y: number;
206
+ readonly label: string;
207
+ }
208
+
209
+ interface PictureFieldBase extends FieldTexts {
210
+ readonly tag: string;
211
+ /** The tag of the element with the picture: the URL policy is asked for its `srcAttribute`, as the engine does. */
212
+ readonly elementTag: string;
213
+ /** The element's attribute with the picture's address, and its value ('' when absent). */
214
+ readonly srcAttribute: string;
215
+ readonly src: string;
216
+ }
217
+
218
+ export interface PictureRegionsFieldModel extends PictureFieldBase {
219
+ readonly kind: 'picture-regions';
220
+ /**
221
+ * The shapes a zone may take: the values of the child's `shape` enum, or rectangles only when it has no `shape`
222
+ * (then no `shape` is written).
223
+ */
224
+ readonly shapes: readonly PictureShape[];
225
+ /**
226
+ * The boolean attribute of a `child-flag` field on the same child tag (a hotspot's right places), whose zones are
227
+ * drawn solid and the others dashed; null without one (every zone solid).
228
+ */
229
+ readonly flag: string | null;
230
+ readonly regions: readonly PictureRegionModel[];
231
+ }
232
+
233
+ export interface PicturePointsFieldModel extends PictureFieldBase {
234
+ readonly kind: 'picture-points';
235
+ readonly points: readonly PicturePointModel[];
236
+ }
237
+
238
+ export type InspectorFieldModel =
239
+ | TextFieldModel
240
+ | NumberFieldModel
241
+ | CheckboxFieldModel
242
+ | SelectFieldModel
243
+ | TextListFieldModel
244
+ | ChildFlagFieldModel
245
+ | ChildTextFieldModel
246
+ | ChildChoiceFieldModel
247
+ | ChildTableFieldModel
248
+ | PictureRegionsFieldModel
249
+ | PicturePointsFieldModel;
250
+
251
+ export interface InspectorModel {
252
+ readonly tag: string;
253
+ readonly id: string | null;
254
+ /** Translated menu label of the type (the tag when there is no descriptor). */
255
+ readonly label: string;
256
+ /** Icon name from the descriptor. */
257
+ readonly icon: string | null;
258
+ readonly fields: readonly InspectorFieldModel[];
259
+ }
260
+
261
+ /** Texts the model needs: messages of the manifest namespace (with a fallback) and kit strings. */
262
+ export interface InspectTexts {
263
+ /** A message of the manifest's namespace, or `fallback` when the catalogs lack it. */
264
+ readonly message: (key: string, fallback: string, params?: MessageParams) => string;
265
+ /** Label of the empty option of a select without a default. */
266
+ readonly unset: string;
267
+ /** Placeholder label of an empty child, `n` from 1. */
268
+ readonly untitled: (n: number) => string;
269
+ /** How picture fields number their zones and pins ("Zone 2", "Pin 2"), for a child of theirs without a label. */
270
+ readonly region: (n: number) => string;
271
+ readonly point: (n: number) => string;
272
+ /** Kit labels of the variant and style template fields, used when the manifest's catalogs have none. */
273
+ readonly variant: string;
274
+ readonly styleTemplate: string;
275
+ /** The style template option that writes nothing: the look around the element. */
276
+ readonly sameAsDocument: string;
277
+ }
278
+
279
+ const own = (el: ElementNode, name: string): string | null =>
280
+ Object.hasOwn(el.attrs, name) ? (el.attrs[name] ?? '') : null;
281
+
282
+ /**
283
+ * The children with `tag` in document order, wherever they are in the element: a rich block's inline children sit in
284
+ * its text (`updateElement` counts them the same way).
285
+ */
286
+ const childrenOf = (el: ElementNode, tag: string): ElementNode[] =>
287
+ el.children.flatMap((n) => (n.type !== 'element' ? [] : n.tag === tag ? [n] : childrenOf(n, tag)));
288
+
289
+ /**
290
+ * Fields for a manifest without descriptor fields: type attributes, then private text children (text lists),
291
+ * boolean and enum child attributes (child flags, child choices), then scoring and points. `id`, `v` and derived
292
+ * attributes are left out.
293
+ */
294
+ export function deriveFields(manifest: ComponentManifest): InspectorField[] {
295
+ const attribute = (spec: AttributeSpec): InspectorField => ({
296
+ kind: 'attribute',
297
+ name: spec.name,
298
+ label: `editor.${spec.name}`,
299
+ });
300
+ const editable = (spec: AttributeSpec) => spec.visibility !== 'derived';
301
+ const scoring = manifest.scoring === null ? [] : ['scoring', 'points'];
302
+ const common = allAttributes(manifest).filter((s) => scoring.includes(s.name));
303
+ return [
304
+ ...manifest.attributes.filter(editable).map(attribute),
305
+ ...manifest.children
306
+ .filter((c) => c.visibility === 'private' && c.content === 'text')
307
+ .map((c): InspectorField => ({ kind: 'text-list', tag: c.tag, label: `editor.${c.tag}` })),
308
+ ...manifest.children.flatMap((c) =>
309
+ c.attributes
310
+ .filter((a) => (a.type === 'boolean' || a.type === 'enum') && editable(a))
311
+ .map(
312
+ (a): InspectorField => ({
313
+ kind: a.type === 'boolean' ? 'child-flag' : 'child-choice',
314
+ tag: c.tag,
315
+ attribute: a.name,
316
+ label: `editor.${c.tag}.${a.name}`,
317
+ }),
318
+ ),
319
+ ),
320
+ ...common.map(attribute),
321
+ ];
322
+ }
323
+
324
+ function selectOptions(
325
+ manifest: ComponentManifest,
326
+ spec: AttributeSpec,
327
+ texts: InspectTexts,
328
+ ): InspectorOption[] {
329
+ const values =
330
+ spec.name === 'scoring' && manifest.scoring !== null
331
+ ? Object.keys(manifest.scoring.modes)
332
+ : [...(spec.values ?? [])];
333
+ return values.map((value) => ({
334
+ value,
335
+ label: texts.message(`editor.value.${spec.name}.${value}`, value),
336
+ }));
337
+ }
338
+
339
+ function defaultOf(manifest: ComponentManifest, spec: AttributeSpec): string | null {
340
+ if (spec.name === 'scoring' && manifest.scoring !== null) return manifest.scoring.default;
341
+ return spec.default === undefined ? null : String(spec.default);
342
+ }
343
+
344
+ function attributeModel(
345
+ el: ElementNode,
346
+ manifest: ComponentManifest,
347
+ field: Extract<InspectorField, { kind: 'attribute' }>,
348
+ texts: InspectTexts,
349
+ fallbackLabel: string = field.name,
350
+ find: (id: string) => ElementNode | null = () => null,
351
+ ): InspectorFieldModel | null {
352
+ const spec = allAttributes(manifest).find((s) => s.name === field.name);
353
+ if (spec === undefined) return null;
354
+ const name = spec.name;
355
+ const label = texts.message(field.label, fallbackLabel);
356
+ const help = field.help === undefined ? null : texts.message(field.help, '');
357
+ const base = { kind: 'attribute' as const, name, label, help: help === '' ? null : help };
358
+ const written = own(el, name);
359
+ const required = spec.required === true;
360
+ // What another element of the document holds (the words of a drop's bank), when the document has it.
361
+ const from = field.options === undefined ? null : find(own(el, field.options.from) ?? '');
362
+ if (from !== null && field.options !== undefined) {
363
+ const value = written ?? '';
364
+ return {
365
+ ...base,
366
+ control: 'select',
367
+ value,
368
+ options: childOptions(from, field.options.tag, texts)(value),
369
+ };
370
+ }
371
+ const isScoring = name === 'scoring' && manifest.scoring !== null;
372
+ if (spec.type === 'enum' || isScoring) {
373
+ const options = selectOptions(manifest, spec, texts);
374
+ const value = written ?? defaultOf(manifest, spec) ?? '';
375
+ if (value === '' && !options.some((o) => o.value === ''))
376
+ options.unshift({ value: '', label: texts.unset });
377
+ else if (!options.some((o) => o.value === value)) options.push({ value, label: value });
378
+ return { ...base, control: 'select', value, options };
379
+ }
380
+ if (spec.type === 'number' || spec.type === 'integer') {
381
+ return {
382
+ ...base,
383
+ control: 'number',
384
+ value: written ?? defaultOf(manifest, spec) ?? '',
385
+ min: spec.min ?? null,
386
+ max: spec.max ?? null,
387
+ step: spec.type === 'integer' ? '1' : 'any',
388
+ required,
389
+ };
390
+ }
391
+ if (spec.type === 'boolean') return { ...base, control: 'checkbox', checked: written !== null };
392
+ const accept = spec.type === 'url' ? (field.accept ?? null) : null;
393
+ return { ...base, control: 'text', value: written ?? '', required, accept };
394
+ }
395
+
396
+ /** The kinds of picture field among `fields`, by the child tag of their zones or pins. */
397
+ function pictureTags(
398
+ fields: readonly InspectorField[],
399
+ ): ReadonlyMap<string, 'picture-regions' | 'picture-points'> {
400
+ return new Map(
401
+ fields.flatMap((f) =>
402
+ f.kind === 'picture-regions' || f.kind === 'picture-points' ? [[f.tag, f.kind]] : [],
403
+ ),
404
+ );
405
+ }
406
+
407
+ /** The shapes the zones of `tag` take: their `shape` enum's values, or rectangles only without one. */
408
+ function shapesOf(manifest: ComponentManifest, tag: string): PictureShape[] {
409
+ const spec = manifest.children.find((c) => c.tag === tag)?.attributes.find((a) => a.name === 'shape');
410
+ if (spec === undefined) return ['rect'];
411
+ const shapes = (['rect', 'circle', 'polygon'] as const).filter((shape) => spec.values?.includes(shape));
412
+ return shapes.length > 0 ? shapes : ['rect'];
413
+ }
414
+
415
+ function fieldModel(
416
+ el: ElementNode,
417
+ manifest: ComponentManifest,
418
+ field: InspectorField,
419
+ texts: InspectTexts,
420
+ fields: readonly InspectorField[] = [field],
421
+ find: (id: string) => ElementNode | null = () => null,
422
+ ): InspectorFieldModel | null {
423
+ const help = field.help === undefined ? null : texts.message(field.help, '') || null;
424
+ // Zones and pins have no text: other fields list them as their picture field numbers them.
425
+ const picture = 'tag' in field ? pictureTags(fields).get(field.tag) : undefined;
426
+ const childLabel = (child: ElementNode, index: number): string => labelOf(child, index, texts, picture);
427
+ switch (field.kind) {
428
+ case 'attribute':
429
+ return attributeModel(el, manifest, field, texts, field.name, find);
430
+ case 'text-list':
431
+ return {
432
+ kind: 'text-list',
433
+ tag: field.tag,
434
+ label: texts.message(field.label, field.tag),
435
+ help,
436
+ values: childrenOf(el, field.tag).map(textContent),
437
+ max: manifest.children.find((c) => c.tag === field.tag)?.max ?? null,
438
+ multiline: field.multiline === true,
439
+ };
440
+ case 'child-flag': {
441
+ const children = childrenOf(el, field.tag).map((child, index) => ({
442
+ index,
443
+ id: own(child, 'id'),
444
+ label: childLabel(child, index),
445
+ checked: own(child, field.attribute) !== null,
446
+ }));
447
+ // How many children there are and how many have the flag, for messages that say so ("2 of 3 correct").
448
+ const counts = { count: children.length, checked: children.filter((c) => c.checked).length };
449
+ const fallback = `${field.tag} ${field.attribute}`;
450
+ const described = field.help === undefined ? null : texts.message(field.help, '', counts) || null;
451
+ return {
452
+ kind: 'child-flag',
453
+ tag: field.tag,
454
+ attribute: field.attribute,
455
+ label: texts.message(field.label, fallback, counts),
456
+ help: described,
457
+ children,
458
+ };
459
+ }
460
+ case 'child-text':
461
+ return {
462
+ kind: 'child-text',
463
+ tag: field.tag,
464
+ attribute: field.attribute,
465
+ label: texts.message(field.label, `${field.tag} ${field.attribute}`),
466
+ help,
467
+ children: childrenOf(el, field.tag).map((child, index) => ({
468
+ index,
469
+ id: own(child, 'id'),
470
+ label: childLabel(child, index),
471
+ value: own(child, field.attribute) ?? '',
472
+ })),
473
+ };
474
+ case 'child-choice': {
475
+ const source = choiceSource(el, manifest, field, texts);
476
+ if (source === null) return null;
477
+ return {
478
+ kind: 'child-choice',
479
+ tag: field.tag,
480
+ attribute: field.attribute,
481
+ label: texts.message(field.label, `${field.tag} ${field.attribute}`),
482
+ help,
483
+ control: source.control,
484
+ children: childrenOf(el, field.tag).map((child, index) => {
485
+ const value = own(child, field.attribute) ?? '';
486
+ const options = source.options(value);
487
+ return { index, id: own(child, 'id'), label: childLabel(child, index), value, options };
488
+ }),
489
+ };
490
+ }
491
+ case 'child-table':
492
+ return childTable(el, manifest, field, texts, help);
493
+ case 'picture-regions':
494
+ case 'picture-points': {
495
+ const base = {
496
+ tag: field.tag,
497
+ label: texts.message(field.label, field.tag),
498
+ help,
499
+ elementTag: el.tag,
500
+ srcAttribute: field.src,
501
+ src: own(el, field.src) ?? '',
502
+ };
503
+ const children = childrenOf(el, field.tag);
504
+ if (field.kind === 'picture-points') {
505
+ return {
506
+ ...base,
507
+ kind: 'picture-points',
508
+ points: children.map((child, index) => ({
509
+ index,
510
+ id: own(child, 'id') ?? '',
511
+ x: percent(own(child, 'x')),
512
+ y: percent(own(child, 'y')),
513
+ label: own(child, 'label') ?? '',
514
+ })),
515
+ };
516
+ }
517
+ const flag = fields.find((f) => f.kind === 'child-flag' && f.tag === field.tag);
518
+ const flagName = flag !== undefined && 'attribute' in flag ? flag.attribute : null;
519
+ return {
520
+ ...base,
521
+ kind: 'picture-regions',
522
+ shapes: shapesOf(manifest, field.tag),
523
+ flag: flagName,
524
+ regions: children.map((child, index) => {
525
+ const shape = own(child, 'shape');
526
+ return {
527
+ index,
528
+ id: own(child, 'id') ?? '',
529
+ shape: shape === 'circle' || shape === 'polygon' ? shape : 'rect',
530
+ x: percent(own(child, 'x')),
531
+ y: percent(own(child, 'y')),
532
+ width: percent(own(child, 'width')),
533
+ height: percent(own(child, 'height')),
534
+ r: percent(own(child, 'r')),
535
+ points: pointsOf(own(child, 'points')),
536
+ label: own(child, 'label') ?? '',
537
+ flagged: flagName !== null && own(child, flagName) !== null,
538
+ };
539
+ }),
540
+ };
541
+ }
542
+ }
543
+ }
544
+
545
+ function childTable(
546
+ el: ElementNode,
547
+ manifest: ComponentManifest,
548
+ field: Extract<InspectorField, { kind: 'child-table' }>,
549
+ texts: InspectTexts,
550
+ help: string | null,
551
+ ): ChildTableFieldModel | null {
552
+ const spec = manifest.children.find((c) => c.tag === field.tag);
553
+ if (spec === undefined) return null;
554
+ const columns = field.columns.flatMap((column): ChildTableColumnModel[] => {
555
+ const attr = spec.attributes.find((a) => a.name === column.attribute);
556
+ if (attr === undefined) return [];
557
+ const number = attr.type === 'integer' || attr.type === 'number';
558
+ return [
559
+ {
560
+ attribute: attr.name,
561
+ label: texts.message(column.label, attr.name),
562
+ control: attr.type === 'enum' ? 'select' : number ? 'number' : 'text',
563
+ options: (attr.values ?? []).map((value) => ({
564
+ value,
565
+ label: texts.message(`editor.value.${attr.name}.${value}`, value),
566
+ })),
567
+ min: attr.min ?? null,
568
+ max: attr.max ?? null,
569
+ step: attr.type === 'integer' ? '1' : 'any',
570
+ required: attr.required === true,
571
+ },
572
+ ];
573
+ });
574
+ const children = childrenOf(el, field.tag);
575
+ const prefix = field.tag.replace(/^astl-/, '');
576
+ const taken = new Set(children.map((child) => own(child, 'id')));
577
+ let n = children.length + 1;
578
+ while (taken.has(`${prefix}-${n}`)) n++;
579
+ const added: Record<string, string> = { id: `${prefix}-${n}` };
580
+ for (const attr of spec.attributes) {
581
+ if (attr.required !== true || attr.name === 'id') continue;
582
+ added[attr.name] = attr.values?.[0] ?? (attr.min === undefined ? '' : String(attr.min));
583
+ }
584
+ return {
585
+ kind: 'child-table',
586
+ tag: field.tag,
587
+ label: texts.message(field.label, field.tag),
588
+ help,
589
+ columns,
590
+ rows: children.map((child, index) => ({
591
+ index,
592
+ id: own(child, 'id'),
593
+ label: labelOf(child, index, texts),
594
+ values: columns.map((c) => own(child, c.attribute) ?? ''),
595
+ })),
596
+ added,
597
+ min: spec.min,
598
+ max: spec.max,
599
+ };
600
+ }
601
+
602
+ /** A coordinate in percent: a number from 0 to 100 (anything else reads as 0; out-of-range values are clamped). */
603
+ function percent(value: string | null): number {
604
+ const n = Number.parseFloat(value ?? '');
605
+ return Number.isFinite(n) ? Math.min(100, Math.max(0, n)) : 0;
606
+ }
607
+
608
+ /** Polygon points written as `x,y x,y ...`. */
609
+ export function pointsOf(value: string | null): (readonly [number, number])[] {
610
+ return (value ?? '')
611
+ .trim()
612
+ .split(/\s+/)
613
+ .filter((pair) => pair !== '')
614
+ .map((pair) => {
615
+ const [x = '', y = ''] = pair.split(',');
616
+ return [percent(x), percent(y)] as const;
617
+ });
618
+ }
619
+
620
+ /** Text with the alt text of its pictures, as a screen reader reads it: a child may be only a picture (a card). */
621
+ const said = (node: Node): string =>
622
+ node.type === 'text'
623
+ ? node.value
624
+ : node.tag === 'img'
625
+ ? ` ${node.attrs.alt ?? ''} `
626
+ : node.children.map(said).join('');
627
+
628
+ /**
629
+ * A child listed by what it says, or a numbered placeholder when it says nothing. A zone or a pin of a picture field
630
+ * (`picture`) says nothing: it is listed by its label, else as that field numbers it ("Zone 2").
631
+ */
632
+ function labelOf(
633
+ child: ElementNode,
634
+ index: number,
635
+ texts: InspectTexts,
636
+ picture?: 'picture-regions' | 'picture-points',
637
+ ): string {
638
+ const text = said(child).replace(/\s+/g, ' ').trim();
639
+ if (text !== '' || picture === undefined) return text || texts.untitled(index + 1);
640
+ const label = (own(child, 'label') ?? '').replace(/\s+/g, ' ').trim();
641
+ return label || (picture === 'picture-regions' ? texts.region(index + 1) : texts.point(index + 1));
642
+ }
643
+
644
+ /**
645
+ * The ids of the `tag` children of `parent` by their text, as the options of a select showing `value`: "not set"
646
+ * first while the value is absent, and a written value that names none kept at the end.
647
+ */
648
+ function childOptions(
649
+ parent: ElementNode,
650
+ tag: string,
651
+ texts: InspectTexts,
652
+ ): (value: string) => InspectorOption[] {
653
+ const choices = childrenOf(parent, tag).flatMap((option, index): InspectorOption[] => {
654
+ const id = own(option, 'id');
655
+ return id === null || id === '' ? [] : [{ value: id, label: labelOf(option, index, texts) }];
656
+ });
657
+ return (value) =>
658
+ value === ''
659
+ ? [{ value: '', label: texts.unset }, ...choices]
660
+ : choices.some((o) => o.value === value)
661
+ ? choices
662
+ : [...choices, { value, label: value }];
663
+ }
664
+
665
+ /**
666
+ * Where a child choice gets its choices. Without `options`: the attribute's enum values as radios, labelled by
667
+ * `editor.value.<attribute>.<value>` (null when the attribute is no enum). With `options`, a child tag: the ids of
668
+ * those children by their text, in a select.
669
+ */
670
+ function choiceSource(
671
+ el: ElementNode,
672
+ manifest: ComponentManifest,
673
+ field: Extract<InspectorField, { kind: 'child-choice' }>,
674
+ texts: InspectTexts,
675
+ ): { readonly control: 'radios' | 'select'; readonly options: (value: string) => InspectorOption[] } | null {
676
+ if (field.options === undefined) {
677
+ const spec = manifest.children
678
+ .find((c) => c.tag === field.tag)
679
+ ?.attributes.find((a) => a.name === field.attribute);
680
+ if (spec?.type !== 'enum') return null;
681
+ const values = (spec.values ?? []).map((value) => ({
682
+ value,
683
+ label: texts.message(`editor.value.${spec.name}.${value}`, value),
684
+ }));
685
+ return { control: 'radios', options: () => values };
686
+ }
687
+ return { control: 'select', options: childOptions(el, field.options, texts) };
688
+ }
689
+
690
+ /**
691
+ * A `style-template` select: `unset` (writes nothing), then the offered templates; a written value that is not
692
+ * offered stays visible, so it can be seen and removed.
693
+ */
694
+ export function styleTemplateField(
695
+ el: ElementNode,
696
+ label: string,
697
+ unset: string,
698
+ templates: readonly InspectorOption[],
699
+ ): SelectFieldModel {
700
+ const value = own(el, STYLE_TEMPLATE_ATTRIBUTE) ?? '';
701
+ const options = [{ value: '', label: unset }, ...templates];
702
+ if (!options.some((o) => o.value === value)) options.push({ value, label: value });
703
+ return {
704
+ kind: 'attribute',
705
+ control: 'select',
706
+ name: STYLE_TEMPLATE_ATTRIBUTE,
707
+ label,
708
+ help: null,
709
+ value,
710
+ options,
711
+ };
712
+ }
713
+
714
+ /** The variant the element shows: its `variant` when the manifest has that value, else the default; null without variants. */
715
+ function variantOf(el: ElementNode, manifest: ComponentManifest): string | null {
716
+ if (manifest.variants === null) return null;
717
+ const written = own(el, 'variant');
718
+ return written !== null && manifest.variants.values.includes(written) ? written : manifest.variants.default;
719
+ }
720
+
721
+ /** Whether a field applies to the variant the element shows (an attribute field with `variants` lists it). */
722
+ function forVariant(field: InspectorField, variant: string | null): boolean {
723
+ return field.kind !== 'attribute' || field.variants === undefined || field.variants.includes(variant ?? '');
724
+ }
725
+
726
+ /** The fields every component gets after its own: `variant` and `style-template` (unless already listed). */
727
+ function commonFields(
728
+ el: ElementNode,
729
+ manifest: ComponentManifest,
730
+ listed: readonly InspectorFieldModel[],
731
+ texts: InspectTexts,
732
+ templates: readonly InspectorOption[],
733
+ ): InspectorFieldModel[] {
734
+ const has = (name: string) => listed.some((f) => f.kind === 'attribute' && f.name === name);
735
+ const out: InspectorFieldModel[] = [];
736
+ if (manifest.variants !== null && !has('variant')) {
737
+ const field = { kind: 'attribute', name: 'variant', label: 'editor.variant' } as const;
738
+ const variant = attributeModel(el, manifest, field, texts, texts.variant);
739
+ if (variant !== null) out.push(variant);
740
+ }
741
+ const written = own(el, STYLE_TEMPLATE_ATTRIBUTE) !== null;
742
+ if ((templates.length > 0 || written) && !has(STYLE_TEMPLATE_ATTRIBUTE)) {
743
+ const label = texts.message(`editor.${STYLE_TEMPLATE_ATTRIBUTE}`, texts.styleTemplate);
744
+ out.push(styleTemplateField(el, label, texts.sameAsDocument, templates));
745
+ }
746
+ return out;
747
+ }
748
+
749
+ /**
750
+ * The inspector model of a parsed component element. `templates`: the style templates offered for its tag at the
751
+ * component scope, labelled. `find`: another element of the document by its id, for the fields that offer what it
752
+ * holds (none by default).
753
+ */
754
+ export function inspectorModel(
755
+ el: ElementNode,
756
+ manifest: ComponentManifest,
757
+ descriptor: EditorDescriptor | null,
758
+ texts: InspectTexts,
759
+ templates: readonly InspectorOption[] = [],
760
+ find: (id: string) => ElementNode | null = () => null,
761
+ ): InspectorModel {
762
+ const declared = descriptor?.fields ?? deriveFields(manifest);
763
+ const variant = variantOf(el, manifest);
764
+ const fields = declared
765
+ .filter((f) => forVariant(f, variant))
766
+ .flatMap((f) => fieldModel(el, manifest, f, texts, declared, find) ?? []);
767
+ return {
768
+ tag: el.tag,
769
+ id: own(el, 'id'),
770
+ label: descriptor === null ? el.tag : texts.message(descriptor.label, el.tag),
771
+ icon: descriptor?.icon ?? null,
772
+ fields: [...fields, ...commonFields(el, manifest, fields, texts, templates)],
773
+ };
774
+ }