@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
@@ -0,0 +1,751 @@
1
+ import type {
2
+ EditorLayoutEdit,
3
+ EditorLayoutShape,
4
+ EditorStyleChanges,
5
+ EditorStyleRule,
6
+ EditorStyleTarget,
7
+ EditorStyleTargetKind,
8
+ } from '@astl/core';
9
+ import {
10
+ cleanStyle,
11
+ MAX_STYLE_LENGTH,
12
+ mergeStyles,
13
+ STYLE_PROPERTIES,
14
+ styleText,
15
+ } from '@astl/core/declarations';
16
+ import {
17
+ type CSSResultGroup,
18
+ css,
19
+ html,
20
+ nothing,
21
+ type PropertyDeclarations,
22
+ type PropertyValues,
23
+ type TemplateResult,
24
+ } from 'lit';
25
+ import { live } from 'lit/directives/live.js';
26
+ import { styleMessages } from '../style-messages.js';
27
+ import { LAYOUT_SHAPES } from '../style-model.js';
28
+ import { PLACEMENTS, type Placement, positionChanges, positionOf, widthChanges } from '../style-placing.js';
29
+ import { AuthoringElement } from './base.js';
30
+ import { colorControl, colorStyles } from './color-control.js';
31
+ import { kitStyles } from './styles.js';
32
+
33
+ /** A field of the form, table-driven: one property, with the control its values ask for. */
34
+ type Field =
35
+ | {
36
+ readonly kind: 'number';
37
+ readonly property: string;
38
+ readonly unit: 'px' | '%' | '';
39
+ readonly min: number;
40
+ readonly max: number;
41
+ readonly step?: number;
42
+ }
43
+ | { readonly kind: 'select'; readonly property: string; readonly options: readonly string[] }
44
+ | { readonly kind: 'font'; readonly property: 'font-family' }
45
+ | { readonly kind: 'color'; readonly property: string }
46
+ | { readonly kind: 'position'; readonly property: 'position' };
47
+
48
+ type Section = 'text' | 'box' | 'spacing' | 'size';
49
+
50
+ const FONT: Field = { kind: 'font', property: 'font-family' };
51
+ const WIDTH: Field = { kind: 'number', property: 'width', unit: '%', min: 1, max: 100 };
52
+ const POSITION: Field = { kind: 'position', property: 'position' };
53
+
54
+ const TEXT_FIELDS: readonly Field[] = [
55
+ FONT,
56
+ { kind: 'number', property: 'font-size', unit: 'px', min: 10, max: 96 },
57
+ { kind: 'select', property: 'font-weight', options: ['normal', 'bold'] },
58
+ { kind: 'select', property: 'font-style', options: ['normal', 'italic'] },
59
+ { kind: 'color', property: 'color' },
60
+ { kind: 'select', property: 'text-align', options: ['start', 'center', 'end', 'justify'] },
61
+ { kind: 'number', property: 'line-height', unit: '', min: 0.8, max: 4, step: 0.05 },
62
+ ];
63
+ const BOX_FIELDS: readonly Field[] = [
64
+ { kind: 'color', property: 'background-color' },
65
+ { kind: 'number', property: 'border-width', unit: 'px', min: 0, max: 16 },
66
+ { kind: 'select', property: 'border-style', options: ['none', 'solid', 'dashed', 'dotted', 'double'] },
67
+ { kind: 'color', property: 'border-color' },
68
+ { kind: 'number', property: 'border-radius', unit: 'px', min: 0, max: 128 },
69
+ ];
70
+ const SPACING_FIELDS: readonly Field[] = [
71
+ { kind: 'number', property: 'padding', unit: 'px', min: 0, max: 64 },
72
+ { kind: 'number', property: 'margin-block-start', unit: 'px', min: 0, max: 128 },
73
+ { kind: 'number', property: 'margin-block-end', unit: 'px', min: 0, max: 128 },
74
+ ];
75
+
76
+ /** What each kind of element can sensibly change, in the order it comes to mind; the first section starts open. */
77
+ const sectionsOf = (
78
+ kind: EditorStyleTargetKind | 'tag' | 'named',
79
+ inFigure: boolean,
80
+ ): readonly (readonly [Section, readonly Field[]])[] => {
81
+ const text: [Section, readonly Field[]] = ['text', TEXT_FIELDS];
82
+ const box: [Section, readonly Field[]] = ['box', BOX_FIELDS];
83
+ const spacing: [Section, readonly Field[]] = ['spacing', SPACING_FIELDS];
84
+ const place: [Section, readonly Field[]] = ['size', [POSITION, WIDTH]];
85
+ switch (kind) {
86
+ case 'picture':
87
+ // A figure's picture fills the figure, which is what sits on the page.
88
+ return inFigure ? [box] : [place, box];
89
+ case 'figure':
90
+ return [place, box, spacing, text];
91
+ case 'layout':
92
+ return [
93
+ [
94
+ 'size',
95
+ [
96
+ { kind: 'number', property: 'gap', unit: 'px', min: 0, max: 64 },
97
+ {
98
+ kind: 'select',
99
+ property: 'align-items',
100
+ options: ['stretch', 'start', 'center', 'end'],
101
+ },
102
+ ],
103
+ ],
104
+ box,
105
+ spacing,
106
+ text,
107
+ ];
108
+ case 'column':
109
+ return [
110
+ ['size', [{ kind: 'number', property: 'flex-grow', unit: '', min: 0, max: 12 }]],
111
+ box,
112
+ spacing,
113
+ text,
114
+ ];
115
+ case 'component':
116
+ // Placed as a picture is: an exercise can float with text beside it.
117
+ return [text, box, spacing, place];
118
+ case 'named':
119
+ case 'block':
120
+ return [text, box, spacing, ['size', [WIDTH]]];
121
+ default:
122
+ return [text, box, spacing];
123
+ }
124
+ };
125
+
126
+ /** A refused declaration, with the reason a message says. */
127
+ interface Refusal {
128
+ readonly name: string;
129
+ readonly reason: 'property' | 'value' | 'text' | 'length';
130
+ }
131
+
132
+ /** Why the allow-list refused a declaration, from the name `cleanStyle` reports. */
133
+ function refusals(style: string, removed: readonly string[]): readonly Refusal[] {
134
+ if (style.length > MAX_STYLE_LENGTH) return [{ name: 'style', reason: 'length' }];
135
+ return removed.map((name) => ({
136
+ name,
137
+ reason: STYLE_PROPERTIES.includes(name)
138
+ ? 'value'
139
+ : /^-{0,2}[a-z][a-z-]*$/.test(name)
140
+ ? 'property'
141
+ : 'text',
142
+ }));
143
+ }
144
+
145
+ /** A number in the field's unit, or null for what the field cannot show (it says it as a placeholder). */
146
+ const numberIn = (value: string | undefined, unit: string): string | null => {
147
+ const match = /^(\d*\.?\d+)([a-z%]*)$/.exec(value ?? '');
148
+ if (match === null) return null;
149
+ const [, number, written] = match;
150
+ return written === unit || number === '0' || (unit === '' && written === '') ? (number as string) : null;
151
+ };
152
+
153
+ /**
154
+ * `<astl-style-form>`: the style of an element at the cursor (`target`, or what the cursor is innermost in), or of a
155
+ * rule of the document (`rule`): text, background and border, spacing, size and position, the named style, and "Own
156
+ * CSS", which says what the allow-list refused and why. "Save as a style" makes the element's own style a named one.
157
+ * Each commit of a field (a `change`) is one undo step, and the focus stays in the form. The adapter's `style`
158
+ * (`EditorStyles`) does the work; without it the form says so. Comes with `loadStyleElements`, on first use.
159
+ */
160
+ export class AstlStyleForm extends AuthoringElement {
161
+ static override properties: PropertyDeclarations = {
162
+ ...AuthoringElement.properties,
163
+ target: {},
164
+ rule: { attribute: false },
165
+ };
166
+
167
+ static override styles: CSSResultGroup = [
168
+ kitStyles,
169
+ colorStyles,
170
+ // A summary stays a list item, which keeps the marker that says it opens; its padding makes the line a target. The
171
+ // layout's commands go in a row from the start, wrapping when narrow; its question has a line of its own.
172
+ css`
173
+ .form {
174
+ display: flex;
175
+ flex-direction: column;
176
+ gap: var(--astl-space-2);
177
+ }
178
+ details {
179
+ border: var(--astl-outline-width) solid var(--astl-line);
180
+ border-radius: var(--astl-radius-m);
181
+ }
182
+ summary {
183
+ box-sizing: border-box;
184
+ min-block-size: var(--astl-target-min);
185
+ padding-block: calc((var(--astl-target-min) - 1lh) / 2);
186
+ padding-inline: var(--astl-space-3);
187
+ font-weight: 600;
188
+ cursor: pointer;
189
+ }
190
+ .fields {
191
+ display: grid;
192
+ grid-template-columns: repeat(auto-fit, minmax(min(9rem, 100%), 1fr));
193
+ gap: var(--astl-space-3);
194
+ padding: var(--astl-space-3);
195
+ padding-block-start: var(--astl-space-1);
196
+ }
197
+ .field {
198
+ display: flex;
199
+ flex-direction: column;
200
+ gap: var(--astl-space-1);
201
+ min-inline-size: 0;
202
+ }
203
+ .field.wide,
204
+ .fields > fieldset {
205
+ grid-column: 1 / -1;
206
+ }
207
+ fieldset {
208
+ margin: 0;
209
+ padding: 0;
210
+ border: 0;
211
+ min-inline-size: 0;
212
+ }
213
+ legend,
214
+ label {
215
+ padding: 0;
216
+ font-weight: 600;
217
+ }
218
+ legend {
219
+ margin-block-end: var(--astl-space-1);
220
+ }
221
+ .unit {
222
+ display: flex;
223
+ align-items: center;
224
+ gap: var(--astl-space-1);
225
+ }
226
+ .unit span {
227
+ flex: none;
228
+ color: var(--astl-ink-muted);
229
+ }
230
+ textarea {
231
+ font-family: var(--astl-font-mono, monospace);
232
+ font-size: var(--astl-size-small);
233
+ }
234
+ .refused {
235
+ margin: 0;
236
+ padding: var(--astl-space-2);
237
+ border-inline-start: var(--astl-space-1) solid var(--astl-danger);
238
+ background: var(--astl-danger-soft);
239
+ }
240
+ .refused ul {
241
+ gap: var(--astl-space-1);
242
+ }
243
+ .save {
244
+ display: grid;
245
+ grid-template-columns: minmax(0, 1fr) auto;
246
+ gap: var(--astl-space-2);
247
+ align-items: end;
248
+ }
249
+ .save label {
250
+ grid-column: 1 / -1;
251
+ }
252
+ .layout {
253
+ flex-direction: row;
254
+ flex-wrap: wrap;
255
+ align-items: center;
256
+ gap: var(--astl-space-2);
257
+ }
258
+ .layout [role='alert'] {
259
+ flex-basis: 100%;
260
+ }
261
+ `,
262
+ ];
263
+
264
+ /** What the form styles, of what the cursor is in; null: the innermost. */
265
+ declare target: EditorStyleTargetKind | null;
266
+ /** A rule to edit in place of an element: by its `name`, or `for` a tag. */
267
+ declare rule: Pick<EditorStyleRule, 'name' | 'for'> | null;
268
+
269
+ /** The refused declarations of the last edit: shown until the next one. */
270
+ #refused: readonly Refusal[] = [];
271
+ /** New refusals to bring into view: in a host's card or sheet the form is taller than its box. */
272
+ #revealRefused = false;
273
+ /** Colour fields whose typed colour was refused. */
274
+ #badColors = new Set<string>();
275
+ /** The field's text when it was refused, kept so the author can correct it. */
276
+ #draft: string | null = null;
277
+ #saveLabel = '';
278
+ /** What the form was last showing: moving to another element forgets the last refusals. */
279
+ #shown = '';
280
+ /** "Delete with content" was pressed on a layout that holds some: the form asks first. */
281
+ #askDelete = false;
282
+
283
+ constructor() {
284
+ super();
285
+ this.target = null;
286
+ this.rule = null;
287
+ }
288
+
289
+ protected override cursorChanged(): void {
290
+ this.requestUpdate();
291
+ }
292
+
293
+ protected override willUpdate(changed: PropertyValues<this>): void {
294
+ super.willUpdate(changed);
295
+ if (changed.has('authoring')) this.authoring?.addMessages(styleMessages);
296
+ const key =
297
+ this.rule === null ? (this.#element()?.kind ?? '') : `rule ${this.rule.name ?? this.rule.for}`;
298
+ if (key !== this.#shown) {
299
+ this.#askDelete = false;
300
+ this.#refused = [];
301
+ this.#draft = null;
302
+ this.#badColors.clear();
303
+ }
304
+ this.#shown = key;
305
+ }
306
+
307
+ protected override updated(changed: PropertyValues<this>): void {
308
+ super.updated(changed);
309
+ if (!this.#revealRefused) return;
310
+ this.#revealRefused = false;
311
+ const note = this.renderRoot.querySelector('#own-refused');
312
+ note?.closest('details')?.setAttribute('open', '');
313
+ note?.scrollIntoView({ block: 'nearest' });
314
+ }
315
+
316
+ #styles() {
317
+ return this.adapter?.style ?? null;
318
+ }
319
+
320
+ #element(): EditorStyleTarget | null {
321
+ const targets = this.#styles()?.state().targets ?? [];
322
+ return (this.target === null ? targets[0] : targets.find((one) => one.kind === this.target)) ?? null;
323
+ }
324
+
325
+ #currentRule(): EditorStyleRule | null {
326
+ const rule = this.rule;
327
+ if (rule === null) return null;
328
+ return (
329
+ this.#styles()
330
+ ?.rules()
331
+ .find((one) => (rule.name !== undefined ? one.name === rule.name : one.for === rule.for)) ?? {
332
+ ...(rule.name === undefined ? {} : { name: rule.name }),
333
+ ...(rule.for === undefined ? {} : { for: rule.for }),
334
+ style: '',
335
+ }
336
+ );
337
+ }
338
+
339
+ #write(changes: EditorStyleChanges): void {
340
+ const styles = this.#styles();
341
+ if (styles === null) return;
342
+ const rule = this.#currentRule();
343
+ if (rule !== null) {
344
+ const gone = new Set(Object.keys(changes));
345
+ const adding = cleanStyle(
346
+ Object.entries(changes)
347
+ .flatMap(([name, value]) => (value === null ? [] : [`${name}: ${value}`]))
348
+ .join('; '),
349
+ ).declarations;
350
+ const style = styleText(
351
+ mergeStyles(
352
+ cleanStyle(rule.style).declarations.filter(([name]) => !gone.has(name)),
353
+ adding,
354
+ ),
355
+ );
356
+ styles.setRule({ ...rule, style });
357
+ } else {
358
+ const element = this.#element();
359
+ if (element !== null) styles.setTarget(element.kind, changes);
360
+ }
361
+ this.#refused = [];
362
+ this.#draft = null;
363
+ this.requestUpdate();
364
+ }
365
+
366
+ /** One field's value: what the allow-list refuses is said, not written. */
367
+ #commit(property: string, value: string | null): void {
368
+ if (value !== null) {
369
+ const { removed } = cleanStyle(`${property}: ${value}`);
370
+ if (removed.length > 0) {
371
+ this.#refused = refusals('', removed);
372
+ this.#revealRefused = true;
373
+ this.requestUpdate();
374
+ return;
375
+ }
376
+ }
377
+ this.#write({ [property]: value });
378
+ }
379
+
380
+ /** The "Own CSS" field: what the allow-list keeps is the new style, what it refuses is said and never written. */
381
+ #applyOwn(text: string): void {
382
+ const styles = this.#styles();
383
+ if (styles === null) return;
384
+ const clean = cleanStyle(text);
385
+ const rule = this.#currentRule();
386
+ if (rule !== null) styles.setRule({ ...rule, style: clean.style });
387
+ else {
388
+ const element = this.#element();
389
+ if (element !== null) styles.replaceTarget(element.kind, clean.style);
390
+ }
391
+ this.#refused = refusals(text, clean.removed);
392
+ this.#revealRefused = this.#refused.length > 0;
393
+ // The field then shows what stays; the refusals say what went.
394
+ this.#draft = null;
395
+ this.requestUpdate();
396
+ }
397
+
398
+ #fieldLabel(field: Field): string {
399
+ return this.t(`style.field.${field.property}`);
400
+ }
401
+
402
+ #field(field: Field, declared: ReadonlyMap<string, string>, style: string, at: string): TemplateResult {
403
+ const id = `${at}-${field.property}`;
404
+ const label = this.#fieldLabel(field);
405
+ const value = declared.get(field.property);
406
+ switch (field.kind) {
407
+ case 'number': {
408
+ const shown = numberIn(value, field.unit);
409
+ return html`<div class="field" part="field">
410
+ <label for=${id} part="label">${label}</label>
411
+ <div class="unit">
412
+ <input
413
+ id=${id}
414
+ type="number"
415
+ inputmode="decimal"
416
+ data-property=${field.property}
417
+ min=${field.min}
418
+ max=${field.max}
419
+ step=${field.step ?? 1}
420
+ placeholder=${shown === null ? (value ?? '') : ''}
421
+ .value=${live(shown ?? '')}
422
+ @change=${(event: Event) => {
423
+ const text = (event.target as HTMLInputElement).value.trim();
424
+ // The field writes what the corner handle writes.
425
+ if (
426
+ field.property === 'width' &&
427
+ text !== '' &&
428
+ cleanStyle(`width: ${text}%`).removed.length === 0
429
+ )
430
+ this.#write(widthChanges(style, Number(text)));
431
+ else this.#commit(field.property, text === '' ? null : `${text}${field.unit}`);
432
+ }}
433
+ />
434
+ ${field.unit === '' ? nothing : html`<span aria-hidden="true">${field.unit}</span>`}
435
+ </div>
436
+ </div>`;
437
+ }
438
+ case 'select':
439
+ return html`<div class="field" part="field">
440
+ <label for=${id} part="label">${label}</label>
441
+ <select
442
+ id=${id}
443
+ data-property=${field.property}
444
+ .value=${live(value ?? '')}
445
+ @change=${(event: Event) => {
446
+ const next = (event.target as HTMLSelectElement).value;
447
+ this.#commit(field.property, next === '' ? null : next);
448
+ }}
449
+ >
450
+ <option value="">${this.t('toolbar.default')}</option>
451
+ ${field.options.map(
452
+ (option) =>
453
+ html`<option value=${option} ?selected=${option === value}>${this.t(`style.value.${option}`)}</option>`,
454
+ )}
455
+ </select>
456
+ </div>`;
457
+ case 'font': {
458
+ const fonts = this.authoring?.fonts ?? [];
459
+ const other = value !== undefined && !fonts.some((font) => font.stack === value);
460
+ return html`<div class="field wide" part="field">
461
+ <label for=${id} part="label">${label}</label>
462
+ <select
463
+ id=${id}
464
+ data-property=${field.property}
465
+ .value=${live(value ?? '')}
466
+ @change=${(event: Event) => {
467
+ const next = (event.target as HTMLSelectElement).value;
468
+ this.#commit(field.property, next === '' ? null : next);
469
+ }}
470
+ >
471
+ <option value="">${this.t('toolbar.default')}</option>
472
+ ${fonts.map(
473
+ (font) =>
474
+ html`<option value=${font.stack} ?selected=${font.stack === value}>${font.name}</option>`,
475
+ )}
476
+ ${other ? html`<option value=${value} selected>${value}</option>` : nothing}
477
+ </select>
478
+ </div>`;
479
+ }
480
+ case 'position': {
481
+ const position = positionOf(style);
482
+ return html`<div class="field wide" part="field">
483
+ <label for=${id} part="label">${label}</label>
484
+ <select
485
+ id=${id}
486
+ data-property="position"
487
+ .value=${live(position)}
488
+ @change=${(event: Event) => {
489
+ const next = (event.target as HTMLSelectElement).value as Placement;
490
+ this.#write(positionChanges(next, style, this.#element()?.kind === 'figure'));
491
+ }}
492
+ >
493
+ ${PLACEMENTS.map(
494
+ (one) =>
495
+ html`<option value=${one} ?selected=${one === position}>${this.t(`style.position.${one}`)}</option>`,
496
+ )}
497
+ </select>
498
+ </div>`;
499
+ }
500
+ case 'color':
501
+ return html`<fieldset part="field" data-property=${field.property}>
502
+ <legend part="label">${label}</legend>
503
+ ${colorControl({
504
+ id,
505
+ value: value ?? null,
506
+ t: (key, params) => this.t(key, params),
507
+ invalid: this.#badColors.has(field.property),
508
+ ground: field.property === 'background-color',
509
+ pick: (next) => {
510
+ this.#badColors.delete(field.property);
511
+ this.#commit(field.property, next);
512
+ },
513
+ refuse: () => {
514
+ this.#badColors.add(field.property);
515
+ this.requestUpdate();
516
+ },
517
+ })}
518
+ </fieldset>`;
519
+ }
520
+ }
521
+
522
+ #refusedList(): TemplateResult | typeof nothing {
523
+ if (this.#refused.length === 0) return nothing;
524
+ return html`<div class="refused" id="own-refused">
525
+ <ul>
526
+ ${this.#refused.map(
527
+ ({ name, reason }) =>
528
+ html`<li data-refused=${name}>${this.t(`style.refused.${reason}`, { name, max: MAX_STYLE_LENGTH })}</li>`,
529
+ )}
530
+ </ul>
531
+ </div>`;
532
+ }
533
+
534
+ /** The named style of the element, the ready styles the document does not have yet, and saving its own style as one. */
535
+ #named(element: EditorStyleTarget): TemplateResult {
536
+ const named = (this.#styles()?.rules() ?? []).filter((rule) => rule.name !== undefined);
537
+ const own = new Set(named.map((rule) => rule.name));
538
+ const ready = (this.authoring?.styles ?? []).filter((preset) => !own.has(preset.name));
539
+ return html`<details part="section" data-section="named">
540
+ <summary>${this.t('style.section.named')}</summary>
541
+ <div class="fields">
542
+ <div class="field wide" part="field">
543
+ <label for="named" part="label">${this.t('style.field.named')}</label>
544
+ <select
545
+ id="named"
546
+ .value=${live(element.className ?? '')}
547
+ @change=${(event: Event) => {
548
+ const next = (event.target as HTMLSelectElement).value;
549
+ const preset = ready.find((one) => one.name === next);
550
+ // A ready style brings its rule, labelled as the author reads it now, in the same undo step.
551
+ if (preset !== undefined)
552
+ this.#styles()?.setClass(element.kind, next, {
553
+ name: next,
554
+ label: preset.label,
555
+ style: preset.style,
556
+ });
557
+ else this.#styles()?.setClass(element.kind, next === '' ? null : next);
558
+ this.requestUpdate();
559
+ }}
560
+ >
561
+ <option value="">${this.t('style.named.none')}</option>
562
+ ${named.map(
563
+ (rule) =>
564
+ html`<option value=${rule.name ?? ''} ?selected=${rule.name === element.className}>${rule.label ?? rule.name}</option>`,
565
+ )}
566
+ ${
567
+ ready.length === 0
568
+ ? nothing
569
+ : html`<optgroup label=${this.t('toolbar.readyStyles')}>
570
+ ${ready.map(
571
+ (preset) =>
572
+ html`<option value=${preset.name} ?selected=${preset.name === element.className}>${preset.label}</option>`,
573
+ )}
574
+ </optgroup>`
575
+ }
576
+ </select>
577
+ </div>
578
+ <div class="save field wide">
579
+ <label for="save-label" part="label">${this.t('style.save.label')}</label>
580
+ <input
581
+ id="save-label"
582
+ type="text"
583
+ autocomplete="off"
584
+ .value=${this.#saveLabel}
585
+ @input=${(event: Event) => {
586
+ this.#saveLabel = (event.target as HTMLInputElement).value;
587
+ }}
588
+ @keydown=${(event: KeyboardEvent) => {
589
+ if (event.key === 'Enter') {
590
+ event.preventDefault();
591
+ this.#save(element);
592
+ }
593
+ }}
594
+ />
595
+ <button type="button" data-save @click=${() => this.#save(element)}>${this.t('style.save')}</button>
596
+ </div>
597
+ </div>
598
+ </details>`;
599
+ }
600
+
601
+ #save(element: EditorStyleTarget): void {
602
+ const label = this.#saveLabel.trim();
603
+ if (label === '') {
604
+ this.renderRoot.querySelector<HTMLInputElement>('#save-label')?.focus();
605
+ return;
606
+ }
607
+ if (this.#styles()?.saveRule(element.kind, label) !== null) this.#saveLabel = '';
608
+ this.requestUpdate();
609
+ }
610
+
611
+ #editLayout(edit: EditorLayoutEdit): void {
612
+ this.#askDelete = false;
613
+ this.#styles()?.editLayout?.(edit);
614
+ this.requestUpdate();
615
+ }
616
+
617
+ /**
618
+ * The layout's own commands, first in its form and open: another shape, its columns removed with their content kept,
619
+ * or deleted with it, which asks first when there is content to lose. Its style sections follow, closed.
620
+ */
621
+ #layout(): TemplateResult | typeof nothing {
622
+ const styles = this.#styles();
623
+ const state = styles?.layout?.() ?? null;
624
+ if (styles?.editLayout === undefined || state === null) return nothing;
625
+ const ask = this.#askDelete;
626
+ return html`<details open part="section" data-section="layout">
627
+ <summary>${this.t('layout.section')}</summary>
628
+ <div class="fields">
629
+ <div class="field wide" part="field">
630
+ <label for="layout-shape" part="label">${this.t('layout.change')}</label>
631
+ <select
632
+ id="layout-shape"
633
+ @change=${(event: Event) => {
634
+ const value = (event.target as HTMLSelectElement).value;
635
+ if (value !== '') this.#editLayout(value as EditorLayoutShape);
636
+ }}
637
+ >
638
+ ${state.shape === null ? html`<option value="" selected>${this.t('layout.other')}</option>` : nothing}
639
+ ${LAYOUT_SHAPES.map(
640
+ (shape) =>
641
+ html`<option value=${shape} ?selected=${shape === state.shape}>
642
+ ${this.t(`insert.layout.${shape}`)}
643
+ </option>`,
644
+ )}
645
+ </select>
646
+ </div>
647
+ <div class="layout wide field" role="group" aria-label=${this.t('layout.section')}>
648
+ ${
649
+ ask
650
+ ? html`<span role="alert">${this.t('layout.confirm')}</span>
651
+ <button type="button" @click=${() => this.#editLayout('delete')}>
652
+ ${this.t('layout.confirmDelete')}
653
+ </button>
654
+ <button
655
+ type="button"
656
+ data-cancel
657
+ @click=${() => {
658
+ this.#askDelete = false;
659
+ this.requestUpdate();
660
+ }}
661
+ >
662
+ ${this.t('layout.cancel')}
663
+ </button>`
664
+ : html`<button type="button" @click=${() => this.#editLayout('unwrap')}>
665
+ ${this.t('layout.unwrap')}
666
+ </button>
667
+ <button
668
+ type="button"
669
+ @click=${() => {
670
+ if (state.empty) this.#editLayout('delete');
671
+ else {
672
+ this.#askDelete = true;
673
+ this.requestUpdate();
674
+ void this.updateComplete.then(() =>
675
+ this.renderRoot.querySelector<HTMLElement>('[data-cancel]')?.focus(),
676
+ );
677
+ }
678
+ }}
679
+ >
680
+ ${this.t('layout.delete')}
681
+ </button>`
682
+ }
683
+ </div>
684
+ </div>
685
+ </details>`;
686
+ }
687
+
688
+ protected override render(): TemplateResult | typeof nothing {
689
+ const styles = this.#styles();
690
+ if (this.authoring === null) return nothing;
691
+ const { lang, dir } = this.ui;
692
+ const rule = this.#currentRule();
693
+ const element = rule === null ? this.#element() : null;
694
+ if (styles === null || (rule === null && element === null)) {
695
+ return html`<p class="note" lang=${lang} dir=${dir}>${this.t('style.form.none')}</p>`;
696
+ }
697
+ const style = rule?.style ?? element?.style ?? '';
698
+ const declared = new Map(cleanStyle(style).declarations);
699
+ const inFigure =
700
+ element?.kind === 'picture' && styles.state().targets.some((one) => one.kind === 'figure');
701
+ const kind =
702
+ rule === null
703
+ ? (element?.kind as EditorStyleTargetKind)
704
+ : rule.name !== undefined
705
+ ? 'named'
706
+ : rule.for === 'img'
707
+ ? 'picture'
708
+ : 'tag';
709
+ const sections = sectionsOf(kind, inFigure);
710
+ const own = this.#draft ?? style;
711
+ // A layout's form starts with its commands; then its style, closed.
712
+ const layout = element?.kind === 'layout';
713
+ return html`<div class="form" part="root" lang=${lang} dir=${dir} data-kind=${kind}>
714
+ ${layout ? this.#layout() : nothing}
715
+ ${sections.map(
716
+ (
717
+ [section, fields],
718
+ at,
719
+ ) => html`<details ?open=${at === 0 && !layout} part="section" data-section=${section}>
720
+ <summary>${this.t(`style.section.${section}`)}</summary>
721
+ <div class="fields">${fields.map((field) => this.#field(field, declared, style, section))}</div>
722
+ </details>`,
723
+ )}
724
+ ${element === null ? nothing : this.#named(element)}
725
+ <details ?open=${!layout} part="section" data-section="own">
726
+ <summary>${this.t('style.section.own')}</summary>
727
+ <div class="fields">
728
+ <div class="field wide" part="field">
729
+ <label for="own" part="label">${this.t('style.field.own')}</label>
730
+ <textarea
731
+ id="own"
732
+ rows="3"
733
+ spellcheck="false"
734
+ placeholder="background-color: #fff3cd; padding: 12px"
735
+ aria-describedby=${this.#refused.length > 0 ? 'own-refused' : nothing}
736
+ .value=${live(own)}
737
+ @change=${(event: Event) => this.#applyOwn((event.target as HTMLTextAreaElement).value)}
738
+ ></textarea>
739
+ <div role="alert">${this.#refusedList()}</div>
740
+ </div>
741
+ </div>
742
+ </details>
743
+ </div>`;
744
+ }
745
+ }
746
+
747
+ declare global {
748
+ interface HTMLElementTagNameMap {
749
+ 'astl-style-form': AstlStyleForm;
750
+ }
751
+ }