@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,846 @@
1
+ import { css, html, nothing, svg, } from 'lit';
2
+ import { ifDefined } from 'lit/directives/if-defined.js';
3
+ import { live } from 'lit/directives/live.js';
4
+ import { refusalOf } from '../authoring.js';
5
+ import { AuthoringElement } from './base.js';
6
+ import { GLYPHS, TYPE_GLYPHS } from './insert-menu.js';
7
+ import { SEVERITY_ICONS, sortIssues } from './issue-list.js';
8
+ import { loadHelpForm, loadStyleElements, STYLE_OPEN } from './style-load.js';
9
+ import { inheritTokens, kitStyles } from './styles.js';
10
+ /** The icons of the head's actions: shapes only, stroked by the `.glyph` rule. */
11
+ const ACTION_ICONS = {
12
+ moveUp: svg `<path d="M10 16V4M5 9l5-5 5 5"/>`,
13
+ moveDown: svg `<path d="M10 4v12M5 11l5 5 5-5"/>`,
14
+ delete: svg `<path d="M4 6h12M8 6V4h4v2M6 6l.8 10h6.4L14 6M9 9v4M11 9v4"/>`,
15
+ };
16
+ /** Fields that fit beside another in the two-column form: a select, a number or a short text (no upload). */
17
+ const fitsBeside = (field) => field.kind === 'attribute' &&
18
+ field.control !== 'checkbox' &&
19
+ !(field.control === 'text' && field.accept !== null);
20
+ const inputValue = (event) => event.target.value;
21
+ const checkedOf = (event) => event.target.checked;
22
+ /**
23
+ * `<astl-inspector>`: a form for the component at the cursor (answers, alternatives, correct options, whether each
24
+ * statement is true, the category of each card, scoring, points, variant, look), or, with the cursor outside every
25
+ * component, the document's own settings (its look). Every edit goes through `authoring.update` and the adapter
26
+ * (`replaceElement`, or `setSource` for the document), so the editor stays the source of truth. A block component's
27
+ * head has "Move up", "Move down" and "Delete" (`EditorAdapter.moveBlock`, `removeBlock`, at `getCursor`), each left
28
+ * out when the adapter cannot do it; at its page's start or end, a move is unavailable (`aria-disabled`). The head
29
+ * shows the type's icon and name and the element's id in a chip; under it, the element's own issues, as banners. The
30
+ * custom property `--astl-inspector-columns` (default 1) sets the form's columns: with 2, selects, numbers and short
31
+ * texts sit side by side and every other field spans the row.
32
+ */
33
+ export class AstlInspector extends AuthoringElement {
34
+ static styles = [
35
+ kitStyles,
36
+ // The picture fields redeclare the tokens on their :host: they take this element's (theme, host overrides).
37
+ inheritTokens('astl-picture-field, astl-style-form, astl-help-form'),
38
+ css `
39
+ .style {
40
+ display: flex;
41
+ flex-direction: column;
42
+ gap: var(--astl-space-2);
43
+ padding-block-start: var(--astl-space-3);
44
+ border-block-start: var(--astl-outline-width) solid var(--astl-line);
45
+ }
46
+ .style-title {
47
+ margin: 0;
48
+ font-size: 1rem;
49
+ }
50
+ .style-targets {
51
+ display: flex;
52
+ flex-wrap: wrap;
53
+ gap: var(--astl-space-1);
54
+ }
55
+ .fields {
56
+ display: grid;
57
+ grid-template-columns: repeat(var(--astl-inspector-columns, 1), minmax(0, 1fr));
58
+ gap: var(--astl-space-3);
59
+ }
60
+ .fields > * {
61
+ grid-column: 1 / -1;
62
+ }
63
+ .fields > .short {
64
+ grid-column: auto;
65
+ }
66
+ .field,
67
+ fieldset {
68
+ display: flex;
69
+ flex-direction: column;
70
+ gap: var(--astl-space-1);
71
+ min-inline-size: 0;
72
+ margin: 0;
73
+ padding: 0;
74
+ border: 0;
75
+ }
76
+ label:not(.check),
77
+ legend {
78
+ padding: 0;
79
+ font-weight: 600;
80
+ }
81
+ fieldset > ul,
82
+ fieldset > .check:first-of-type {
83
+ margin-block-start: var(--astl-space-1);
84
+ }
85
+ .check {
86
+ box-sizing: border-box;
87
+ display: flex;
88
+ align-items: center;
89
+ gap: var(--astl-space-2);
90
+ min-block-size: var(--astl-target-min);
91
+ cursor: pointer;
92
+ }
93
+ `,
94
+ // One child of a child-choice field with a select: its text above the select.
95
+ css `
96
+ .pick {
97
+ display: flex;
98
+ flex-direction: column;
99
+ gap: var(--astl-space-1);
100
+ margin-block-start: var(--astl-space-1);
101
+ }
102
+ .pick label {
103
+ font-weight: 400;
104
+ }
105
+ .item {
106
+ display: flex;
107
+ gap: var(--astl-space-2);
108
+ align-items: center;
109
+ }
110
+ .item :is(input, textarea) {
111
+ flex: 1 1 auto;
112
+ }
113
+ .item:has(textarea) {
114
+ align-items: flex-start;
115
+ }
116
+ .item button {
117
+ flex: none;
118
+ }
119
+ fieldset > button {
120
+ align-self: flex-start;
121
+ }
122
+ `,
123
+ // One child of a child-choice field with radios: its text, then the values side by side.
124
+ css `
125
+ .choice > legend {
126
+ font-weight: 400;
127
+ }
128
+ .choice > .values {
129
+ display: flex;
130
+ flex-wrap: wrap;
131
+ column-gap: var(--astl-space-4);
132
+ }
133
+ .help {
134
+ margin: 0;
135
+ color: var(--astl-ink-muted);
136
+ font-size: var(--astl-size-small);
137
+ }
138
+ `,
139
+ // One row of a child-table field: its text, then its controls side by side while they fit, and Remove.
140
+ css `
141
+ .row {
142
+ margin-block-start: var(--astl-space-2);
143
+ padding: var(--astl-space-2) var(--astl-space-3) var(--astl-space-3);
144
+ border: var(--astl-outline-width) solid var(--astl-line);
145
+ border-radius: var(--astl-radius-m);
146
+ }
147
+ .row > legend {
148
+ padding-inline: var(--astl-space-1);
149
+ font-weight: 400;
150
+ }
151
+ .cells {
152
+ display: flex;
153
+ flex-wrap: wrap;
154
+ align-items: flex-end;
155
+ gap: var(--astl-space-2);
156
+ }
157
+ .cell {
158
+ flex: 1 1 5rem;
159
+ display: flex;
160
+ flex-direction: column;
161
+ gap: var(--astl-space-1);
162
+ min-inline-size: 0;
163
+ font-weight: 400;
164
+ }
165
+ .cell > input,
166
+ .cell > select {
167
+ inline-size: 100%;
168
+ }
169
+ `,
170
+ // The head: the type's icon and name, the id in a chip, then the block's actions (icons), which wrap under it when
171
+ // narrow. Under it the element's issues, each a banner in its severity's colour with an icon and words.
172
+ css `
173
+ .head,
174
+ .actions {
175
+ display: flex;
176
+ flex-wrap: wrap;
177
+ align-items: center;
178
+ gap: var(--astl-space-1) var(--astl-space-2);
179
+ }
180
+ .title {
181
+ flex: 0 1 auto;
182
+ min-inline-size: 0;
183
+ }
184
+ .actions {
185
+ margin-inline-start: auto;
186
+ }
187
+ .glyph {
188
+ fill: none;
189
+ stroke: currentColor;
190
+ stroke-width: 1.8;
191
+ stroke-linecap: round;
192
+ stroke-linejoin: round;
193
+ }
194
+ .glyph circle {
195
+ fill: currentColor;
196
+ stroke: none;
197
+ }
198
+ .id {
199
+ padding-inline: var(--astl-space-2);
200
+ border-radius: var(--astl-radius-s);
201
+ background: var(--astl-accent-soft);
202
+ color: var(--astl-ink);
203
+ font-family: var(--astl-font-mono);
204
+ font-size: var(--astl-size-small);
205
+ }
206
+ .delete {
207
+ color: var(--astl-danger);
208
+ }
209
+ .issue {
210
+ display: flex;
211
+ align-items: flex-start;
212
+ gap: var(--astl-space-2);
213
+ padding-block: var(--astl-space-2);
214
+ padding-inline: var(--astl-space-3);
215
+ border-radius: var(--astl-radius-s);
216
+ background: var(--astl-danger-soft);
217
+ color: var(--astl-ink);
218
+ }
219
+ .issue .icon {
220
+ color: var(--astl-danger);
221
+ }
222
+ .issue[data-severity='warning'] {
223
+ background: var(--astl-warning-soft);
224
+ }
225
+ .issue[data-severity='warning'] .icon {
226
+ color: var(--astl-warning);
227
+ }
228
+ @media (forced-colors: active) {
229
+ .issue {
230
+ border: var(--astl-outline-width) solid CanvasText;
231
+ }
232
+ }
233
+ `,
234
+ // A text-list row in a text area keeps its line breaks and grows with them.
235
+ css `
236
+ textarea {
237
+ field-sizing: content;
238
+ min-block-size: 5lh;
239
+ resize: vertical;
240
+ }
241
+ .upload {
242
+ display: flex;
243
+ flex-direction: column;
244
+ align-items: flex-start;
245
+ }
246
+ .upload p {
247
+ margin-block: var(--astl-space-1) 0;
248
+ }
249
+ .error {
250
+ margin: 0;
251
+ padding: var(--astl-space-2);
252
+ border-inline-start: var(--astl-space-1) solid var(--astl-danger);
253
+ background: var(--astl-danger-soft);
254
+ }
255
+ `,
256
+ ];
257
+ /** Source id of the element shown: edits go to it. */
258
+ #shown = null;
259
+ /** The document's settings are shown: edits go to the start tag of `<astl-doc>`. */
260
+ #document = false;
261
+ #model = null;
262
+ #focus = null;
263
+ /**
264
+ * The last upload of a `url` field: its element and attribute, and what it says (a message key, or with `text` a
265
+ * host's own words, `UploadRefused`).
266
+ */
267
+ #upload = null;
268
+ /** Aborts the upload under way (its `signal`): another pick replaces it. */
269
+ #abort = null;
270
+ /** The inspector model on screen (a component's, or the document's), or null. */
271
+ get model() {
272
+ return this.#model;
273
+ }
274
+ /**
275
+ * Applies a change to what is shown (the element, or the document), or to the element `element` (a picture field's
276
+ * late upload, whatever the cursor is in by then).
277
+ */
278
+ #apply(change, element) {
279
+ const { authoring, adapter } = this;
280
+ if (authoring === null || adapter === null)
281
+ return;
282
+ if (element === undefined && this.#document) {
283
+ const source = adapter.getSource();
284
+ const next = authoring.update(source, change);
285
+ if (next !== source)
286
+ adapter.setSource(next);
287
+ return;
288
+ }
289
+ const id = element === undefined ? this.#shown : element;
290
+ if (id === null)
291
+ return;
292
+ const current = adapter.getElement(id);
293
+ if (current === null)
294
+ return;
295
+ const next = authoring.update(current, change);
296
+ if (next !== current)
297
+ adapter.replaceElement(id, next);
298
+ }
299
+ updated() {
300
+ const focus = this.#focus;
301
+ if (focus === null)
302
+ return;
303
+ this.#focus = null;
304
+ const item = focus.item === 'add' ? '[data-add]' : `[data-item="${focus.item}"]`;
305
+ this.renderRoot.querySelector(`[data-field="${focus.field}"]${item}`)?.focus();
306
+ }
307
+ /** `short` marks a field that may sit beside another (`--astl-inspector-columns`). */
308
+ #fieldClass(field) {
309
+ return fitsBeside(field) ? 'field short' : 'field';
310
+ }
311
+ #help(field, i) {
312
+ return field.help === null ? nothing : html `<p class="help" id="help-${i}">${field.help}</p>`;
313
+ }
314
+ #describedBy(field, i) {
315
+ return field.help === null ? undefined : `help-${i}`;
316
+ }
317
+ #text(field, i) {
318
+ const commit = (event) => {
319
+ const value = inputValue(event);
320
+ this.#apply({
321
+ kind: 'attribute',
322
+ name: field.name,
323
+ value: value === '' && !field.required ? null : value,
324
+ });
325
+ };
326
+ return html `<div class=${this.#fieldClass(field)} part="field">
327
+ <label for="field-${i}" part="label">${field.label}</label>
328
+ <input
329
+ id="field-${i}"
330
+ type="text"
331
+ dir="auto"
332
+ autocomplete="off"
333
+ data-name=${field.name}
334
+ .value=${live(field.value)}
335
+ ?required=${field.required}
336
+ aria-describedby=${ifDefined(this.#describedBy(field, i))}
337
+ @input=${commit}
338
+ />
339
+ ${this.#help(field, i)}
340
+ ${field.accept === null || this.authoring?.uploadAsset == null ? nothing : this.#uploadControl(field, field.accept)}
341
+ </div>`;
342
+ }
343
+ /** A button that picks a file for a `url` field and uploads it, and what the last upload says. */
344
+ #uploadControl(field, accept) {
345
+ const said = this.#upload;
346
+ const status = said?.element === this.#shown && said.name === field.name ? said : null;
347
+ const words = status === null ? '' : (status.text ?? this.t(status.key));
348
+ const pick = (event) => {
349
+ const input = event.target;
350
+ const file = input.files?.[0];
351
+ input.value = '';
352
+ if (file !== undefined)
353
+ void this.#uploadFile(field, file);
354
+ };
355
+ return html `<div class="upload">
356
+ <button
357
+ type="button"
358
+ aria-label=${this.t('inspector.uploadItem', { label: field.label })}
359
+ @click=${(event) => event.currentTarget.nextElementSibling.click()}
360
+ >
361
+ ${this.t('inspector.upload')}
362
+ </button>
363
+ <input type="file" accept=${accept} hidden data-name=${field.name} @change=${pick} />
364
+ <div role="status">${status !== null && !status.error ? html `<p class="help">${words}</p>` : nothing}</div>
365
+ <div role="alert">${status?.error ? html `<p class="error">${words}</p>` : nothing}</div>
366
+ </div>`;
367
+ }
368
+ /**
369
+ * Uploads a file with the host's `uploadAsset` and writes its address, when the URL policy allows it (asked as the
370
+ * engine asks: the element's tag and attribute), into the element shown when it started.
371
+ */
372
+ async #uploadFile(field, file) {
373
+ const authoring = this.authoring;
374
+ const upload = authoring?.uploadAsset ?? null;
375
+ if (authoring === null || upload === null)
376
+ return;
377
+ const element = this.#shown;
378
+ const tag = this.#model?.tag ?? '';
379
+ this.#abort?.abort();
380
+ const abort = new AbortController();
381
+ this.#abort = abort;
382
+ const say = (key, error = false, text) => {
383
+ this.#upload = { element, name: field.name, key, error, text };
384
+ this.requestUpdate();
385
+ };
386
+ say('inspector.uploading');
387
+ try {
388
+ const asset = await upload(file, { signal: abort.signal });
389
+ if (abort.signal.aborted)
390
+ return;
391
+ const src = typeof asset?.src === 'string' ? asset.src.trim() : '';
392
+ if (src === '' || !authoring.urlPolicy(src, { tag, attribute: field.name })) {
393
+ say('inspector.uploadBlocked', true);
394
+ return;
395
+ }
396
+ this.#apply({ kind: 'attribute', name: field.name, value: src }, element);
397
+ say('inspector.uploaded');
398
+ }
399
+ catch (error) {
400
+ if (!abort.signal.aborted)
401
+ say('inspector.uploadFailed', true, refusalOf(error));
402
+ }
403
+ }
404
+ #number(field, i) {
405
+ // Numbers commit on change (blur, Enter, arrows): half-typed values like "0." never reach the source.
406
+ const commit = (event) => {
407
+ const value = inputValue(event);
408
+ this.#apply({ kind: 'attribute', name: field.name, value: value === '' ? null : value });
409
+ };
410
+ return html `<div class=${this.#fieldClass(field)} part="field">
411
+ <label for="field-${i}" part="label">${field.label}</label>
412
+ <input
413
+ id="field-${i}"
414
+ type="number"
415
+ data-name=${field.name}
416
+ min=${ifDefined(field.min ?? undefined)}
417
+ max=${ifDefined(field.max ?? undefined)}
418
+ step=${field.step}
419
+ .value=${live(field.value)}
420
+ ?required=${field.required}
421
+ aria-describedby=${ifDefined(this.#describedBy(field, i))}
422
+ @change=${commit}
423
+ />
424
+ ${this.#help(field, i)}
425
+ </div>`;
426
+ }
427
+ #checkbox(field, i) {
428
+ return html `<div class="field" part="field">
429
+ <label class="check" part="label">
430
+ <input
431
+ type="checkbox"
432
+ data-name=${field.name}
433
+ .checked=${live(field.checked)}
434
+ aria-describedby=${ifDefined(this.#describedBy(field, i))}
435
+ @change=${(event) => this.#apply({ kind: 'attribute', name: field.name, value: checkedOf(event) })}
436
+ />
437
+ <span>${field.label}</span>
438
+ </label>
439
+ ${this.#help(field, i)}
440
+ </div>`;
441
+ }
442
+ #select(field, i) {
443
+ const commit = (event) => {
444
+ const value = inputValue(event);
445
+ this.#apply({ kind: 'attribute', name: field.name, value: value === '' ? null : value });
446
+ };
447
+ return html `<div class=${this.#fieldClass(field)} part="field">
448
+ <label for="field-${i}" part="label">${field.label}</label>
449
+ <select
450
+ id="field-${i}"
451
+ data-name=${field.name}
452
+ aria-describedby=${ifDefined(this.#describedBy(field, i))}
453
+ @change=${commit}
454
+ >
455
+ ${field.options.map((option) => html `<option value=${option.value} .selected=${live(option.value === field.value)}>${option.label}</option>`)}
456
+ </select>
457
+ ${this.#help(field, i)}
458
+ </div>`;
459
+ }
460
+ #textList(field, i) {
461
+ const values = field.values;
462
+ const set = (next) => this.#apply({ kind: 'text-list', tag: field.tag, values: next });
463
+ const add = () => {
464
+ this.#focus = { field: i, item: values.length };
465
+ set([...values, '']);
466
+ };
467
+ const remove = (j) => {
468
+ const next = values.filter((_, k) => k !== j);
469
+ this.#focus = { field: i, item: next.length === 0 ? 'add' : Math.min(j, next.length - 1) };
470
+ set(next);
471
+ };
472
+ const full = field.max !== null && values.length >= field.max;
473
+ return html `<fieldset data-tag=${field.tag} part="field">
474
+ <legend part="label">${field.label}</legend>
475
+ ${this.#help(field, i)}
476
+ ${values.length === 0
477
+ ? nothing
478
+ : html `<ul role="list">
479
+ ${values.map((value, j) => {
480
+ const name = this.t('inspector.item', { label: field.label, n: j + 1 });
481
+ const described = ifDefined(this.#describedBy(field, i));
482
+ const write = (event) => set(values.map((v, k) => (k === j ? inputValue(event) : v)));
483
+ // A multi-line value (a transcript) in a text area that keeps its line breaks.
484
+ return html `<li class="item">
485
+ ${field.multiline
486
+ ? html `<textarea dir="auto" data-field=${i} data-item=${j} aria-label=${name}
487
+ aria-describedby=${described} .value=${live(value)} @input=${write}></textarea>`
488
+ : html `<input type="text" dir="auto" autocomplete="off" data-field=${i} data-item=${j}
489
+ aria-label=${name} aria-describedby=${described} .value=${live(value)} @input=${write} />`}
490
+ <button
491
+ type="button"
492
+ aria-label=${this.t('inspector.removeItem', { label: field.label, n: j + 1 })}
493
+ @click=${() => remove(j)}
494
+ >
495
+ ${this.t('inspector.remove')}
496
+ </button>
497
+ </li>`;
498
+ })}
499
+ </ul>`}
500
+ <button type="button" data-field=${i} data-add ?disabled=${full} @click=${add}>${this.t('inspector.add')}</button>
501
+ </fieldset>`;
502
+ }
503
+ #childFlags(field, i) {
504
+ return html `<fieldset data-tag=${field.tag} part="field" aria-describedby=${ifDefined(this.#describedBy(field, i))}>
505
+ <legend part="label">${field.label}</legend>
506
+ ${this.#help(field, i)}
507
+ ${field.children.map((child) => html `<label class="check">
508
+ <input
509
+ type="checkbox"
510
+ data-index=${child.index}
511
+ .checked=${live(child.checked)}
512
+ @change=${(event) => this.#apply({
513
+ kind: 'child-flag',
514
+ tag: field.tag,
515
+ attribute: field.attribute,
516
+ index: child.index,
517
+ value: checkedOf(event),
518
+ })}
519
+ />
520
+ <span dir="auto">${child.label}</span>
521
+ </label>`)}
522
+ </fieldset>`;
523
+ }
524
+ /** A text input per child, labelled by the child's text (e.g. the word each clue hides); empty removes the value. */
525
+ #childTexts(field, i) {
526
+ return html `<fieldset data-tag=${field.tag} part="field" aria-describedby=${ifDefined(this.#describedBy(field, i))}>
527
+ <legend part="label">${field.label}</legend>
528
+ ${this.#help(field, i)}
529
+ ${field.children.map((child) => html `<div class="pick">
530
+ <label for="field-${i}-${child.index}" dir="auto">${child.label}</label>
531
+ <input
532
+ id="field-${i}-${child.index}"
533
+ type="text"
534
+ dir="auto"
535
+ autocomplete="off"
536
+ data-index=${child.index}
537
+ .value=${live(child.value)}
538
+ @input=${(event) => this.#apply({
539
+ kind: 'child-attributes',
540
+ tag: field.tag,
541
+ index: child.index,
542
+ attributes: { [field.attribute]: inputValue(event) || null },
543
+ })}
544
+ />
545
+ </div>`)}
546
+ </fieldset>`;
547
+ }
548
+ /**
549
+ * One control per child, named by the child's text: a radio group of the attribute's values (e.g. True / False for
550
+ * each statement), or a select of the ids of other children (e.g. the group of each card).
551
+ */
552
+ #childChoices(field, i) {
553
+ const choose = (index, value) => this.#apply({ kind: 'child-choice', tag: field.tag, attribute: field.attribute, index, value });
554
+ return html `<fieldset data-tag=${field.tag} part="field" aria-describedby=${ifDefined(this.#describedBy(field, i))}>
555
+ <legend part="label">${field.label}</legend>
556
+ ${this.#help(field, i)}
557
+ ${field.children.map((child) => field.control === 'radios'
558
+ ? html `<fieldset class="choice" data-index=${child.index}>
559
+ <legend dir="auto">${child.label}</legend>
560
+ <div class="values">
561
+ ${child.options.map((option) => html `<label class="check">
562
+ <input
563
+ type="radio"
564
+ name="choice-${i}-${child.index}"
565
+ value=${option.value}
566
+ .checked=${live(child.value === option.value)}
567
+ @change=${() => choose(child.index, option.value)}
568
+ />
569
+ <span>${option.label}</span>
570
+ </label>`)}
571
+ </div>
572
+ </fieldset>`
573
+ : html `<div class="pick">
574
+ <label for="field-${i}-${child.index}" dir="auto">${child.label}</label>
575
+ <select
576
+ id="field-${i}-${child.index}"
577
+ data-index=${child.index}
578
+ @change=${(event) => choose(child.index, inputValue(event) || null)}
579
+ >
580
+ ${child.options.map((option) => html `<option value=${option.value} .selected=${live(option.value === child.value)}>${option.label}</option>`)}
581
+ </select>
582
+ </div>`)}
583
+ </fieldset>`;
584
+ }
585
+ /**
586
+ * A row per child, named by its text, with a labelled control per column (text and numbers commit like the attribute
587
+ * fields), a button that removes the row, and one that adds a row at the end.
588
+ */
589
+ #childTable(field, i) {
590
+ const { tag, rows, columns } = field;
591
+ const change = (index, attribute, value, required) => this.#apply({
592
+ kind: 'child-attributes',
593
+ tag,
594
+ index,
595
+ attributes: { [attribute]: value || (required ? '' : null) },
596
+ });
597
+ const add = () => {
598
+ this.#focus = { field: i, item: rows.length };
599
+ this.#apply({ kind: 'child-add', tag, attributes: field.added });
600
+ };
601
+ const remove = (index) => {
602
+ this.#focus = { field: i, item: rows.length > 1 ? Math.min(index, rows.length - 2) : 'add' };
603
+ this.#apply({ kind: 'child-remove', tag, index });
604
+ };
605
+ const control = (column, row, k) => {
606
+ const value = row.values[k] ?? '';
607
+ const commit = (event) => change(row.index, column.attribute, inputValue(event), column.required);
608
+ const first = k === 0 ? { field: i, item: row.index } : null;
609
+ if (column.control === 'select') {
610
+ const known = column.options.some((o) => o.value === value);
611
+ const options = known
612
+ ? column.options
613
+ : [...column.options, { value, label: value || this.t('inspector.unset') }];
614
+ return html `<select data-field=${ifDefined(first?.field)} data-item=${ifDefined(first?.item)} data-name=${column.attribute} @change=${commit}>
615
+ ${options.map((o) => html `<option value=${o.value} .selected=${live(o.value === value)}>${o.label}</option>`)}
616
+ </select>`;
617
+ }
618
+ return html `<input
619
+ type=${column.control}
620
+ dir=${column.control === 'text' ? 'auto' : nothing}
621
+ autocomplete="off"
622
+ data-field=${ifDefined(first?.field)}
623
+ data-item=${ifDefined(first?.item)}
624
+ data-name=${column.attribute}
625
+ min=${ifDefined(column.min ?? undefined)}
626
+ max=${ifDefined(column.max ?? undefined)}
627
+ step=${ifDefined(column.control === 'number' ? column.step : undefined)}
628
+ .value=${live(value)}
629
+ @input=${column.control === 'text' ? commit : nothing}
630
+ @change=${column.control === 'number' ? commit : nothing}
631
+ />`;
632
+ };
633
+ return html `<fieldset data-tag=${tag} part="field" aria-describedby=${ifDefined(this.#describedBy(field, i))}>
634
+ <legend part="label">${field.label}</legend>
635
+ ${this.#help(field, i)}
636
+ ${rows.map((row) => html `<fieldset class="row" data-index=${row.index}>
637
+ <legend dir="auto">${row.label}</legend>
638
+ <div class="cells">
639
+ ${columns.map((column, k) => html `<label class="cell"><span>${column.label}</span>${control(column, row, k)}</label>`)}
640
+ <button
641
+ type="button"
642
+ aria-label=${this.t('inspector.removeItem', { label: field.label, n: row.index + 1 })}
643
+ ?disabled=${rows.length <= field.min}
644
+ @click=${() => remove(row.index)}
645
+ >
646
+ ${this.t('inspector.remove')}
647
+ </button>
648
+ </div>
649
+ </fieldset>`)}
650
+ <button type="button" data-field=${i} data-add ?disabled=${field.max !== null && rows.length >= field.max} @click=${add}>
651
+ ${this.t('inspector.add')}
652
+ </button>
653
+ </fieldset>`;
654
+ }
655
+ #field(field, i) {
656
+ switch (field.kind) {
657
+ case 'picture-regions':
658
+ case 'picture-points':
659
+ return html `<astl-picture-field
660
+ .field=${field}
661
+ .authoring=${this.authoring}
662
+ .element=${this.#shown}
663
+ @astl-field-change=${(event) => this.#apply(event.detail.change, event.detail.element)}
664
+ ></astl-picture-field>`;
665
+ case 'text-list':
666
+ return this.#textList(field, i);
667
+ case 'child-flag':
668
+ return this.#childFlags(field, i);
669
+ case 'child-text':
670
+ return this.#childTexts(field, i);
671
+ case 'child-choice':
672
+ return this.#childChoices(field, i);
673
+ case 'child-table':
674
+ return this.#childTable(field, i);
675
+ case 'attribute':
676
+ switch (field.control) {
677
+ case 'text':
678
+ return this.#text(field, i);
679
+ case 'number':
680
+ return this.#number(field, i);
681
+ case 'checkbox':
682
+ return this.#checkbox(field, i);
683
+ case 'select':
684
+ return this.#select(field, i);
685
+ }
686
+ }
687
+ }
688
+ /**
689
+ * The icon buttons of a block component's head, named and with a tooltip by their text: each needs the cursor's block
690
+ * (`getCursor`) and its own capability. A move off the page's start or end is unavailable but keeps the focus; after a
691
+ * delete the focus goes to the editor.
692
+ */
693
+ #actions(tag) {
694
+ const { authoring, adapter } = this;
695
+ const place = adapter?.getCursor?.() ?? null;
696
+ if (authoring === null || adapter === null || place === null)
697
+ return nothing;
698
+ if (authoring.descriptor(tag)?.manifest.display !== 'block')
699
+ return nothing;
700
+ const page = authoring.outline(adapter.getSource()).pages.find((p) => p.id === place.page);
701
+ const button = (name, off, run) => html `<button type="button" class=${name} data-action=${name} aria-label=${this.t(`inspector.${name}`)}
702
+ title=${this.t(`inspector.${name}`)} aria-disabled=${off ? 'true' : nothing}
703
+ @click=${() => {
704
+ if (!off)
705
+ run();
706
+ }}><svg class="icon glyph" viewBox="0 0 20 20" aria-hidden="true" focusable="false">${ACTION_ICONS[name]}</svg></button>`;
707
+ const move = (direction) => () => adapter.moveBlock?.(place, direction);
708
+ return html `<div class="actions" part="actions">
709
+ ${adapter.moveBlock === undefined ? nothing : button('moveUp', place.index === 0, move('up'))}
710
+ ${adapter.moveBlock === undefined ? nothing : button('moveDown', place.index >= (page?.blocks.length ?? 0) - 1, move('down'))}
711
+ ${adapter.removeBlock === undefined
712
+ ? nothing
713
+ : button('delete', false, () => {
714
+ adapter.removeBlock?.(place);
715
+ adapter.focus?.();
716
+ })}
717
+ </div>`;
718
+ }
719
+ /** The element's own issues, the sentences its frame shows (`authoring.describe` without the page), errors first. */
720
+ #issues(model) {
721
+ const { authoring, adapter } = this;
722
+ if (authoring === null || adapter === null || model.id === null)
723
+ return nothing;
724
+ const source = adapter.getSource();
725
+ const issues = sortIssues(authoring.validate(source).issues.filter((i) => i.at.element === model.id));
726
+ if (issues.length === 0)
727
+ return nothing;
728
+ return html `<ul class="issues" role="list">
729
+ ${issues.map((issue) => html `<li class="issue" part="issue" data-code=${issue.code} data-severity=${issue.severity}>
730
+ <svg class="icon" viewBox="0 0 20 20" aria-hidden="true" focusable="false">${SEVERITY_ICONS[issue.severity]}</svg>
731
+ <span class="astl-vh">${this.t(`severity.${issue.severity}`)}: </span>
732
+ <span>${authoring.describe(issue, source, { page: false })}</span>
733
+ </li>`)}
734
+ </ul>`;
735
+ }
736
+ /** The head: the type's icon and name, the id in a chip when it has one, then the block's actions. */
737
+ #head(model) {
738
+ const display = this.authoring?.descriptor(model.tag)?.manifest.display;
739
+ const glyph = TYPE_GLYPHS.get(model.icon ?? '') ?? GLYPHS[display === 'inline' ? 'inline' : 'block'];
740
+ return html `<div class="head" part="head header">
741
+ <svg class="icon glyph" viewBox="0 0 20 20" aria-hidden="true" focusable="false">${glyph}</svg>
742
+ <h2 class="title" id="title" part="title" data-id=${model.id ?? nothing}>${model.label}</h2>
743
+ ${model.id === null ? nothing : html `<code class="id" part="id">${model.id}</code>`}
744
+ ${this.#actions(model.tag)}
745
+ </div>`;
746
+ }
747
+ /** The kinds at the cursor: when they change, so does the style section. */
748
+ #styleKinds = '';
749
+ /** The target whose form is open here, while it is at the cursor. */
750
+ #styleOpen = null;
751
+ cursorChanged() {
752
+ const targets = this.adapter?.style?.state().targets ?? [];
753
+ const kinds = targets.map((target) => target.kind).join(' ');
754
+ if (kinds === this.#styleKinds)
755
+ return;
756
+ this.#styleKinds = kinds;
757
+ // A picture has nothing else to set here, and its position and size are why it was selected.
758
+ const first = targets[0]?.kind;
759
+ this.#styleOpen = first === 'picture' ? first : null;
760
+ if (this.#styleOpen !== null)
761
+ void this.#showStyle(this.#styleOpen);
762
+ this.requestUpdate();
763
+ }
764
+ /** Shows a form here, the module loaded first. */
765
+ async #showStyle(kind) {
766
+ await loadStyleElements();
767
+ this.#styleOpen = kind;
768
+ this.requestUpdate();
769
+ }
770
+ /** A press on a target: a host may take it (`astl-style-open`); otherwise its form opens here, or closes. */
771
+ #askStyle(kind) {
772
+ if (this.#styleOpen === kind) {
773
+ this.#styleOpen = null;
774
+ this.requestUpdate();
775
+ return;
776
+ }
777
+ const detail = { target: kind };
778
+ const event = new CustomEvent(STYLE_OPEN, { detail, bubbles: true, composed: true, cancelable: true });
779
+ if (this.dispatchEvent(event))
780
+ void this.#showStyle(kind);
781
+ }
782
+ /** The style of what the cursor is in: a button per target, and the open one's form. */
783
+ #styleSection() {
784
+ const targets = this.adapter?.style?.state().targets ?? [];
785
+ if (targets.length === 0)
786
+ return nothing;
787
+ const open = targets.some((target) => target.kind === this.#styleOpen) ? this.#styleOpen : null;
788
+ return html `<div class="style" part="style">
789
+ <h3 class="style-title">${this.t('inspector.style')}</h3>
790
+ <div class="style-targets">
791
+ ${targets.map((target) => html `<button
792
+ type="button"
793
+ data-style-target=${target.kind}
794
+ aria-expanded=${String(open === target.kind)}
795
+ @click=${() => this.#askStyle(target.kind)}
796
+ >
797
+ ${this.t(`style.target.${target.kind}`)}
798
+ </button>`)}
799
+ </div>
800
+ ${open === null
801
+ ? nothing
802
+ : html `<astl-style-form .authoring=${this.authoring} .adapter=${this.adapter} .target=${open}></astl-style-form>`}
803
+ </div>`;
804
+ }
805
+ render() {
806
+ const { authoring, adapter } = this;
807
+ if (authoring === null)
808
+ return nothing;
809
+ const selection = adapter?.getSelection() ?? null;
810
+ const source = selection === null || adapter === null ? null : adapter.getElement(selection.id);
811
+ // With the document: a field may list what another element holds (the words of a drop's bank).
812
+ const model = source === null ? null : authoring.inspect(source, adapter?.getSource());
813
+ // Outside every component: the document's own settings, under the hint.
814
+ const document = selection === null && adapter !== null ? authoring.inspectDocument(adapter.getSource()) : null;
815
+ this.#shown = model === null ? null : (selection?.id ?? null);
816
+ this.#document = model === null && document !== null;
817
+ this.#model = model ?? document;
818
+ const { lang, dir } = this.ui;
819
+ let body;
820
+ if (model === null) {
821
+ const note = source !== null && selection !== null
822
+ ? this.t('inspector.unsupported', { tag: selection.tag })
823
+ : this.t('inspector.empty');
824
+ body = html `<h2 class="title" id="title" part="title">${this.t('inspector.title')}</h2>
825
+ <p class="note">${note}</p>
826
+ ${document === null
827
+ ? nothing
828
+ : html `<div class="fields" data-tag=${document.tag}>${document.fields.map((f, i) => this.#field(f, i))}</div>`}`;
829
+ }
830
+ else {
831
+ // The heading says the type; the id is beside it in a chip, outside the heading.
832
+ // A graded element's help for students: a form of its own, loaded on first use.
833
+ const manifest = authoring.descriptor(model.tag)?.manifest;
834
+ if (manifest?.graded === true)
835
+ void loadHelpForm().catch(() => { });
836
+ body = html `${this.#head(model)}${this.#issues(model)}
837
+ <div class="fields" data-tag=${model.tag}>${model.fields.map((f, i) => this.#field(f, i))}</div>
838
+ ${manifest?.graded === true
839
+ ? html `<astl-help-form .authoring=${authoring} .adapter=${adapter} .element=${this.#shown}
840
+ ?inline=${manifest.display === 'inline'}></astl-help-form>`
841
+ : nothing}`;
842
+ }
843
+ return html `<section class="kit" part="root" lang=${lang} dir=${dir} aria-labelledby="title">${body}${this.#styleSection()}</section>`;
844
+ }
845
+ }
846
+ //# sourceMappingURL=inspector.js.map