@astl/editor 0.1.4 → 0.2.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 (45) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/authoring.d.ts +12 -0
  3. package/dist/authoring.d.ts.map +1 -1
  4. package/dist/authoring.js +6 -3
  5. package/dist/authoring.js.map +1 -1
  6. package/dist/elements/help-form.d.ts +22 -7
  7. package/dist/elements/help-form.d.ts.map +1 -1
  8. package/dist/elements/help-form.js +333 -110
  9. package/dist/elements/help-form.js.map +1 -1
  10. package/dist/elements/inspector.d.ts +7 -0
  11. package/dist/elements/inspector.d.ts.map +1 -1
  12. package/dist/elements/inspector.js +21 -4
  13. package/dist/elements/inspector.js.map +1 -1
  14. package/dist/elements/style-load.d.ts +14 -0
  15. package/dist/elements/style-load.d.ts.map +1 -1
  16. package/dist/elements/style-load.js +7 -0
  17. package/dist/elements/style-load.js.map +1 -1
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/messages/en.json +18 -2
  23. package/dist/messages/help/en.json +7 -4
  24. package/dist/messages/help/pl.json +7 -4
  25. package/dist/messages/pl.json +18 -2
  26. package/dist/pages.d.ts +8 -0
  27. package/dist/pages.d.ts.map +1 -1
  28. package/dist/pages.js +52 -4
  29. package/dist/pages.js.map +1 -1
  30. package/dist/textarea.d.ts +2 -0
  31. package/dist/textarea.d.ts.map +1 -1
  32. package/dist/textarea.js +7 -1
  33. package/dist/textarea.js.map +1 -1
  34. package/package.json +5 -5
  35. package/src/authoring.ts +25 -2
  36. package/src/elements/help-form.ts +366 -113
  37. package/src/elements/inspector.ts +22 -5
  38. package/src/elements/style-load.ts +17 -0
  39. package/src/index.ts +2 -2
  40. package/src/messages/en.json +18 -2
  41. package/src/messages/help/en.json +7 -4
  42. package/src/messages/help/pl.json +7 -4
  43. package/src/messages/pl.json +18 -2
  44. package/src/pages.ts +60 -4
  45. package/src/textarea.ts +14 -1
@@ -1,8 +1,11 @@
1
1
  /**
2
- * `<astl-help-form>`: the inspector's "Help for students" section of a graded element: its hints in order, each with
3
- * its cost (Up, Down, a drag, Remove, Add), its explanation and its four feedback texts. Every edit rewrites the
4
- * element's help (`writeHelp`) through `EditorAdapter.replaceElement`. A module of its own, outside the editor's first
5
- * load (`loadHelpForm`), with its messages; `addHelp` is the insert menu's way in.
2
+ * `<astl-help-form>`: the inspector's "Help for students" section of a graded element, a disclosure closed for each
3
+ * element until it is opened (its button, a tag on the element's frame, the insert menu): closed, it says what the
4
+ * element has ("2 hints · explanation"); open, its hints in order, each with its cost (Up, Down, a drag, Remove, Add),
5
+ * its explanation and its four feedback texts. Every edit rewrites the element's help (`writeHelp`) through
6
+ * `EditorAdapter.replaceElement`; a hint added here is a draft until it has text, so nothing empty reaches the source. A
7
+ * module of its own, outside the editor's first load (`loadHelpForm`), with its messages; `addHelp` is the insert
8
+ * menu's way in, `openHelp` the inspector's.
6
9
  */
7
10
  import { type EditorAdapter, FEEDBACK_VARIANTS, type FeedbackVariant, type MessageCatalog } from '@astl/core';
8
11
  import {
@@ -12,6 +15,8 @@ import {
12
15
  nothing,
13
16
  type PropertyDeclarations,
14
17
  type PropertyValues,
18
+ type SVGTemplateResult,
19
+ svg,
15
20
  type TemplateResult,
16
21
  } from 'lit';
17
22
  import { live } from 'lit/directives/live.js';
@@ -20,7 +25,9 @@ import { type HelpValue, helpFormatted, helpText, readHelp, textHelp, writeHelp
20
25
  import en from '../messages/help/en.json' with { type: 'json' };
21
26
  import pl from '../messages/help/pl.json' with { type: 'json' };
22
27
  import { EDITOR_NAMESPACE } from '../messages.js';
28
+ import { elementTree } from '../scan.js';
23
29
  import { AuthoringElement } from './base.js';
30
+ import type { HelpPart } from './style-load.js';
24
31
  import { kitStyles } from './styles.js';
25
32
 
26
33
  const helpMessages: readonly MessageCatalog[] = [
@@ -31,14 +38,31 @@ const helpMessages: readonly MessageCatalog[] = [
31
38
  /** What the insert menu adds: a hint, the explanation, or the next feedback text the element lacks. */
32
39
  export type HelpKind = 'hint' | 'explanation' | 'feedback';
33
40
 
34
- /** The field to focus once the form shows it (after an insert), by element id. */
35
- let pending: { readonly element: string; readonly field: string } | null = null;
41
+ /** Where a form opens: a part, a new hint (a draft), or one feedback text. */
42
+ type HelpPlace = HelpPart | 'hint-new' | `feedback-${FeedbackVariant}`;
43
+
44
+ /** The place the form of `element` on `adapter` opens at: taken by the form that shows it, now or once it does. */
45
+ let pending: { readonly adapter: EditorAdapter; readonly element: string; readonly place: HelpPlace } | null =
46
+ null;
47
+
48
+ /** The forms on the page, so a request reaches the one that shows its element. */
49
+ const forms = new Set<AstlHelpForm>();
50
+
51
+ /**
52
+ * Opens the help for students of `element` at `place` in the form that shows it (or will): the section opens, a new
53
+ * hint shows as a draft, and the place's first field takes the focus.
54
+ */
55
+ export function openHelp(adapter: EditorAdapter, element: string, place: HelpPlace): void {
56
+ pending = { adapter, element, place };
57
+ for (const form of forms) if (form.adapter === adapter && form.element === element) form.requestUpdate();
58
+ }
36
59
 
37
60
  const graded = (authoring: Authoring, tag: string) => authoring.descriptor(tag)?.manifest.graded === true;
38
61
 
39
62
  /**
40
- * Adds help to the graded element at the cursor (and asks the form to focus it), or else to the page at the cursor.
41
- * Returns the HTML inserted on a page, or null when the element got it.
63
+ * Adds help to the graded element at the cursor: its form opens at a new hint (a draft until it has text), at its
64
+ * explanation or at the next feedback text it lacks, writing nothing yet; or else puts the help on the page at the
65
+ * cursor. Returns the HTML inserted on a page, or null when the element's form opened.
42
66
  */
43
67
  export function addHelp(authoring: Authoring, adapter: EditorAdapter, kind: HelpKind): string | null {
44
68
  const selection = adapter.getSelection();
@@ -52,36 +76,40 @@ export function addHelp(authoring: Authoring, adapter: EditorAdapter, kind: Help
52
76
  return block;
53
77
  }
54
78
  const help = readHelp(current);
55
- let next: HelpValue;
56
- let field: string;
57
- if (kind === 'hint') {
58
- next = { ...help, hints: [...help.hints, { html: '', cost: null }] };
59
- field = `hint-${help.hints.length}`;
60
- } else if (kind === 'explanation') {
61
- next = { ...help, explanation: help.explanation ?? '' };
62
- field = 'explanation';
63
- } else {
64
- const when = FEEDBACK_ORDER.find((w) => help.feedback[w] === undefined) ?? 'correct';
65
- next = { ...help, feedback: { ...help.feedback, [when]: help.feedback[when] ?? '' } };
66
- field = `feedback-${when}`;
67
- }
68
- pending = { element: selection.id, field };
69
- adapter.replaceElement(selection.id, writeHelp(current, next));
79
+ const when = FEEDBACK_ORDER.find((w) => help.feedback[w] === undefined) ?? 'correct';
80
+ openHelp(
81
+ adapter,
82
+ selection.id,
83
+ kind === 'hint' ? 'hint-new' : kind === 'explanation' ? 'explanation' : `feedback-${when}`,
84
+ );
70
85
  return null;
71
86
  }
72
87
 
73
88
  /** The order the menu fills feedback texts in: right and wrong first. */
74
89
  const FEEDBACK_ORDER: readonly FeedbackVariant[] = ['correct', 'incorrect', 'omitted', 'partial'];
75
90
 
76
- /** A cost in percent of the points (0 to 100) from a `cost` attribute, or '' when there is none or it is not one. */
77
- const percentOf = (cost: string | null): string => {
78
- if (cost === null) return '0';
79
- const n = Number(cost);
80
- return Number.isFinite(n) && n >= 0 && n <= 1 ? String(Math.round(n * 100)) : '';
81
- };
91
+ /** The costs the card offers ('0': free, no attribute); a cost the source has besides stays as an option of its own. */
92
+ const COSTS: readonly string[] = ['0', '0.1', '0.25', '0.5'];
93
+
94
+ /** A hint's cost as one of the select's values: '0' without one, else as written. */
95
+ const costOf = (cost: string | null): string => cost ?? '0';
96
+
97
+ /** Rounds points for display, to at most 2 decimals. */
98
+ const round = (n: number): number => Math.round(n * 100) / 100;
82
99
 
83
100
  const inputValue = (event: Event) => (event.target as HTMLInputElement | HTMLTextAreaElement).value;
84
101
 
102
+ /** The icons of the section: shapes only, stroked by the `.glyph` rule. */
103
+ const ICONS = {
104
+ chevron: svg`<path d="M8 5l5 5-5 5"/>`,
105
+ up: svg`<path d="M10 16V4M5 9l5-5 5 5"/>`,
106
+ down: svg`<path d="M10 4v12M5 11l5 5 5-5"/>`,
107
+ remove: svg`<path d="M5 5l10 10M15 5L5 15"/>`,
108
+ } satisfies Readonly<Record<string, SVGTemplateResult>>;
109
+
110
+ const icon = (name: keyof typeof ICONS): TemplateResult =>
111
+ html`<svg class="glyph" viewBox="0 0 20 20" aria-hidden="true" focusable="false">${ICONS[name]}</svg>`;
112
+
85
113
  export class AstlHelpForm extends AuthoringElement {
86
114
  static override properties: PropertyDeclarations = {
87
115
  ...AuthoringElement.properties,
@@ -99,13 +127,44 @@ export class AstlHelpForm extends AuthoringElement {
99
127
  display: flex;
100
128
  flex-direction: column;
101
129
  gap: var(--astl-space-3);
102
- padding-block-start: var(--astl-space-3);
130
+ padding-block-start: var(--astl-space-2);
103
131
  border-block-start: var(--astl-outline-width) solid var(--astl-line);
104
132
  }
105
133
  h3 {
106
134
  margin: 0;
107
135
  font-size: 1rem;
108
136
  }
137
+ .toggle {
138
+ inline-size: 100%;
139
+ flex-wrap: wrap;
140
+ gap: 0 var(--astl-space-2);
141
+ padding-inline: var(--astl-space-1);
142
+ border-color: transparent;
143
+ background: transparent;
144
+ font-weight: 700;
145
+ }
146
+ .toggle .glyph {
147
+ flex: none;
148
+ transition: rotate 0.15s;
149
+ }
150
+ .toggle[aria-expanded='true'] .glyph {
151
+ rotate: 90deg;
152
+ }
153
+ .toggle:dir(rtl) .glyph {
154
+ scale: -1 1;
155
+ }
156
+ .toggle:dir(rtl)[aria-expanded='true'] .glyph {
157
+ rotate: -90deg;
158
+ }
159
+ .summary {
160
+ color: var(--astl-ink-muted);
161
+ font-weight: 400;
162
+ }
163
+ .body {
164
+ display: flex;
165
+ flex-direction: column;
166
+ gap: var(--astl-space-3);
167
+ }
109
168
  fieldset,
110
169
  .field {
111
170
  display: flex;
@@ -124,7 +183,7 @@ export class AstlHelpForm extends AuthoringElement {
124
183
  ol {
125
184
  display: flex;
126
185
  flex-direction: column;
127
- gap: var(--astl-space-3);
186
+ gap: var(--astl-space-2);
128
187
  margin: 0;
129
188
  padding: 0;
130
189
  list-style: none;
@@ -140,24 +199,50 @@ export class AstlHelpForm extends AuthoringElement {
140
199
  li[data-over] {
141
200
  border-color: var(--astl-accent);
142
201
  }
202
+ li[data-draft] {
203
+ border-style: dashed;
204
+ }
143
205
  .row {
144
206
  display: flex;
145
207
  flex-wrap: wrap;
146
208
  align-items: center;
147
- gap: var(--astl-space-2);
209
+ gap: var(--astl-space-1) var(--astl-space-2);
210
+ }
211
+ .tools {
212
+ display: flex;
213
+ gap: var(--astl-space-1);
214
+ margin-inline-start: auto;
148
215
  }
149
216
  .handle {
150
217
  cursor: grab;
151
218
  user-select: none;
152
219
  }
220
+ .icon {
221
+ justify-content: center;
222
+ padding: 0;
223
+ }
224
+ .glyph {
225
+ inline-size: 1.25rem;
226
+ block-size: 1.25rem;
227
+ fill: none;
228
+ stroke: currentColor;
229
+ stroke-width: 1.8;
230
+ stroke-linecap: round;
231
+ stroke-linejoin: round;
232
+ }
153
233
  .cost {
154
234
  display: flex;
155
235
  align-items: center;
156
236
  gap: var(--astl-space-1);
237
+ min-inline-size: 0;
238
+ max-inline-size: 100%;
157
239
  font-weight: 400;
158
240
  }
159
- .cost input {
160
- inline-size: 6em;
241
+ .cost select {
242
+ inline-size: auto;
243
+ min-inline-size: 0;
244
+ max-inline-size: 100%;
245
+ text-overflow: ellipsis;
161
246
  }
162
247
  textarea {
163
248
  field-sizing: content;
@@ -176,7 +261,16 @@ export class AstlHelpForm extends AuthoringElement {
176
261
  declare inline: boolean;
177
262
  /** What the author is typing in a field, by field: the source has it cleaned, the field keeps it as typed. */
178
263
  #drafts = new Map<string, string>();
264
+ /** The question's points as last worked out, for its source then; and the source being worked out now. */
265
+ #points: { readonly html: string; readonly value: number | null } | null = null;
266
+ #counting: string | null = null;
179
267
  #dragged: number | null = null;
268
+ /** Whether the section is open: closed for each element until it is opened. */
269
+ #open = false;
270
+ /** A hint added and not yet written, with the cost picked for it: it goes into the source with its first text. */
271
+ #draft: { readonly cost: string | null } | null = null;
272
+ /** The field to focus once the form shows it. */
273
+ #focus: string | null = null;
180
274
 
181
275
  constructor() {
182
276
  super();
@@ -184,25 +278,111 @@ export class AstlHelpForm extends AuthoringElement {
184
278
  this.inline = false;
185
279
  }
186
280
 
281
+ override connectedCallback(): void {
282
+ super.connectedCallback();
283
+ forms.add(this);
284
+ }
285
+
286
+ override disconnectedCallback(): void {
287
+ super.disconnectedCallback();
288
+ forms.delete(this);
289
+ }
290
+
187
291
  protected override willUpdate(changed: PropertyValues<this>): void {
188
292
  super.willUpdate(changed);
189
293
  if (changed.has('authoring')) this.authoring?.addMessages(helpMessages);
190
- if (changed.has('element')) this.#drafts.clear();
294
+ if (changed.has('element')) {
295
+ this.#drafts.clear();
296
+ this.#open = false;
297
+ this.#draft = null;
298
+ }
299
+ this.#take();
191
300
  }
192
301
 
193
302
  protected override updated(): void {
194
- if (pending === null || pending.element !== this.element) return;
195
- const field = this.renderRoot.querySelector<HTMLElement>(`[data-help="${pending.field}"]`);
303
+ if (this.#focus === null) return;
304
+ const field = this.renderRoot.querySelector<HTMLElement>(`[data-help="${this.#focus}"]`);
196
305
  if (field === null) return;
197
- pending = null;
306
+ this.#focus = null;
198
307
  field.focus();
199
308
  }
200
309
 
310
+ /** Takes a request to open this form: the section opens at its place, a new hint as a draft. */
311
+ #take(): void {
312
+ if (pending === null || pending.adapter !== this.adapter || pending.element !== this.element) return;
313
+ const { place } = pending;
314
+ const current = this.#help();
315
+ if (current === null) return;
316
+ pending = null;
317
+ const { help } = current;
318
+ this.#open = true;
319
+ if (place === 'hint-new') this.#draft ??= { cost: null };
320
+ if (place === 'hints') this.#focus = help.hints.length > 0 ? 'hint-0' : 'add-hint';
321
+ else if (place === 'hint-new') this.#focus = `hint-${help.hints.length}`;
322
+ else if (place === 'feedback')
323
+ this.#focus = `feedback-${FEEDBACK_VARIANTS.find((w) => help.feedback[w] !== undefined) ?? 'correct'}`;
324
+ else this.#focus = place;
325
+ }
326
+
201
327
  #help(): { readonly html: string; readonly help: HelpValue } | null {
202
328
  const html = this.element === null ? null : (this.adapter?.getElement(this.element) ?? null);
203
329
  return html === null ? null : { html, help: readHelp(html) };
204
330
  }
205
331
 
332
+ /**
333
+ * The question's points, from a compilation of the element alone (its points, scoring and parts as the engine counts
334
+ * them): null until known, or when it does not compile on its own; then the costs say only their share.
335
+ */
336
+ #pointsOf(html: string): number | null {
337
+ if (this.#points?.html === html) return this.#points.value;
338
+ const authoring = this.authoring;
339
+ if (this.#counting !== html && authoring !== null) {
340
+ this.#counting = html;
341
+ const inline = authoring.descriptor(elementTree(html)[0]?.name ?? '')?.manifest.display === 'inline';
342
+ void authoring.engine
343
+ .compile(`<astl-doc lang="en">${inline ? `<p>${html}</p>` : html}</astl-doc>`)
344
+ .then(
345
+ (result) => (result.ok ? result.metadata.maxScore : null),
346
+ () => null,
347
+ )
348
+ .then((value) => {
349
+ if (this.#counting !== html) return;
350
+ this.#points = { html, value };
351
+ this.requestUpdate();
352
+ });
353
+ }
354
+ return null;
355
+ }
356
+
357
+ /** The cost select's options: free, a share of the points (in points where known), and the source's own cost. */
358
+ #costs(cost: string | null, points: number | null): { readonly value: string; readonly label: string }[] {
359
+ const own = costOf(cost);
360
+ const values = COSTS.includes(own) ? COSTS : [...COSTS, own].sort((a, b) => Number(a) - Number(b));
361
+ return values.map((value) => {
362
+ const share = Number(value);
363
+ if (value === '0') return { value, label: this.t('help.costFree') };
364
+ if (!Number.isFinite(share)) return { value, label: value };
365
+ const percent = round(share * 100);
366
+ return {
367
+ value,
368
+ label:
369
+ points === null || points <= 0
370
+ ? this.t('help.costShare', { percent })
371
+ : this.t('help.costPoints', { percent, points: round(share * points), max: points }),
372
+ };
373
+ });
374
+ }
375
+
376
+ /** What the element has, as the closed section says it: "2 hints · explanation · feedback", or "none". */
377
+ #summary(help: HelpValue): string {
378
+ const parts = [
379
+ ...(help.hints.length > 0 ? [this.t('surface.helpHints', { n: help.hints.length })] : []),
380
+ ...(help.explanation !== null ? [this.t('help.summaryExplanation')] : []),
381
+ ...(Object.keys(help.feedback).length > 0 ? [this.t('help.summaryFeedback')] : []),
382
+ ];
383
+ return parts.length === 0 ? this.t('help.summaryNone') : parts.join(' · ');
384
+ }
385
+
206
386
  #write(change: (help: HelpValue) => HelpValue): void {
207
387
  const current = this.#help();
208
388
  if (current === null || this.element === null || this.adapter === null) return;
@@ -210,6 +390,12 @@ export class AstlHelpForm extends AuthoringElement {
210
390
  if (next !== current.html) this.adapter.replaceElement(this.element, next);
211
391
  }
212
392
 
393
+ /** Focuses a field once the form shows it again. */
394
+ #refocus(field: string): void {
395
+ this.#focus = field;
396
+ this.requestUpdate();
397
+ }
398
+
213
399
  /** A text field's value: as typed while it is being edited, else what the source says. */
214
400
  #text(field: string, html: string): string {
215
401
  return this.#drafts.get(field) ?? helpText(html);
@@ -244,30 +430,63 @@ export class AstlHelpForm extends AuthoringElement {
244
430
  this.#drafts.clear();
245
431
  }
246
432
 
247
- #hint(help: HelpValue, index: number): TemplateResult {
248
- const hint = help.hints[index] as HelpValue['hints'][number];
433
+ /** "Add a hint": a draft row (one at a time), focused. */
434
+ #addHint(help: HelpValue): void {
435
+ this.#draft ??= { cost: null };
436
+ this.#refocus(`hint-${help.hints.length}`);
437
+ }
438
+
439
+ /** The draft goes without a trace: left empty, or removed. */
440
+ #dropDraft(field: string): void {
441
+ this.#draft = null;
442
+ this.#drafts.delete(field);
443
+ this.requestUpdate();
444
+ }
445
+
446
+ /** A small button with an icon, named and with a tooltip by its text. */
447
+ #iconButton(name: 'up' | 'down' | 'remove', label: string, off: boolean, run: () => void): TemplateResult {
448
+ return html`<button type="button" class=${name === 'remove' ? 'icon delete' : 'icon'}
449
+ part=${name === 'remove' ? 'help-remove' : 'help-move'} aria-label=${label} title=${label}
450
+ aria-disabled=${off ? 'true' : nothing}
451
+ @click=${() => {
452
+ if (!off) run();
453
+ }}>${icon(name)}</button>`;
454
+ }
455
+
456
+ #hint(help: HelpValue, index: number, points: number | null): TemplateResult {
457
+ const draft = index === help.hints.length;
458
+ const hint: HelpValue['hints'][number] = draft
459
+ ? { html: '', cost: this.#draft?.cost ?? null }
460
+ : (help.hints[index] as HelpValue['hints'][number]);
249
461
  const n = index + 1;
250
462
  const field = `hint-${index}`;
251
463
  const last = index === help.hints.length - 1;
252
464
  const formatted = this.#formatted(field, hint.html);
253
465
  const over = (event: DragEvent, on: boolean) => {
254
- if (this.#dragged === null) return;
466
+ if (this.#dragged === null || draft) return;
255
467
  event.preventDefault();
256
468
  (event.currentTarget as HTMLElement).toggleAttribute('data-over', on);
257
469
  };
258
470
  return html`<li
471
+ ?data-draft=${draft}
259
472
  @dragover=${(e: DragEvent) => over(e, true)}
260
473
  @dragleave=${(e: DragEvent) => over(e, false)}
261
474
  @drop=${(e: DragEvent) => {
262
475
  over(e, false);
263
- if (this.#dragged !== null) this.#move(this.#dragged, index);
476
+ if (this.#dragged !== null && !draft) this.#move(this.#dragged, index);
264
477
  this.#dragged = null;
265
478
  }}
479
+ @focusout=${(e: FocusEvent) => {
480
+ // A draft left empty goes: the focus is outside its row, and it has no text yet.
481
+ const row = e.currentTarget as HTMLElement;
482
+ if (draft && this.#draft !== null && !row.contains(e.relatedTarget as Node | null))
483
+ this.#dropDraft(field);
484
+ }}
266
485
  >
267
486
  <div class="row">
268
487
  <span
269
488
  class="handle"
270
- draggable="true"
489
+ draggable=${draft ? 'false' : 'true'}
271
490
  aria-hidden="true"
272
491
  title=${this.t('help.drag')}
273
492
  @dragstart=${(e: DragEvent) => {
@@ -280,6 +499,24 @@ export class AstlHelpForm extends AuthoringElement {
280
499
  >⠿</span
281
500
  >
282
501
  <label for=${field}>${this.t('help.hintN', { n })}</label>
502
+ <span class="tools">
503
+ ${
504
+ draft
505
+ ? nothing
506
+ : html`${this.#iconButton('up', this.t('help.upN', { n }), index === 0, () => this.#move(index, index - 1))}
507
+ ${this.#iconButton('down', this.t('help.downN', { n }), last, () => this.#move(index, index + 1))}`
508
+ }
509
+ ${this.#iconButton('remove', this.t('help.removeN', { n }), false, () => {
510
+ if (draft) {
511
+ this.#dropDraft(field);
512
+ this.#refocus('add-hint');
513
+ return;
514
+ }
515
+ this.#drafts.clear();
516
+ this.#write((h) => ({ ...h, hints: h.hints.filter((_, i) => i !== index) }));
517
+ this.#refocus(help.hints.length > 1 ? `hint-${Math.max(0, index - 1)}` : 'add-hint');
518
+ })}
519
+ </span>
283
520
  </div>
284
521
  <textarea
285
522
  id=${field}
@@ -289,57 +526,52 @@ export class AstlHelpForm extends AuthoringElement {
289
526
  .value=${live(this.#text(field, hint.html))}
290
527
  @input=${(e: Event) => {
291
528
  const text = this.#typed(field, e);
292
- this.#write((h) => ({
293
- ...h,
294
- hints: h.hints.map((x, i) => (i === index ? { ...x, html: text } : x)),
295
- }));
529
+ if (!draft) {
530
+ this.#write((h) => ({
531
+ ...h,
532
+ hints: h.hints.map((x, i) => (i === index ? { ...x, html: text } : x)),
533
+ }));
534
+ return;
535
+ }
536
+ // The draft's first text writes it, with the cost picked for it.
537
+ if (inputValue(e).trim() === '') return;
538
+ const cost = this.#draft?.cost ?? null;
539
+ this.#draft = null;
540
+ this.#write((h) => ({ ...h, hints: [...h.hints, { html: text, cost }] }));
296
541
  }}
297
542
  @change=${() => this.#done(field)}
298
543
  ></textarea>
299
544
  ${formatted ?? nothing}
300
- <div class="row">
301
- <label class="cost">
302
- ${this.t('help.cost')}
303
- <input
304
- type="number"
305
- aria-label=${this.t('help.costN', { n })}
306
- min="0"
307
- max="100"
308
- step="5"
309
- inputmode="numeric"
310
- data-help=${`cost-${index}`}
311
- .value=${live(percentOf(hint.cost))}
312
- @change=${(e: Event) => {
313
- const percent = Number(inputValue(e));
314
- if (!Number.isFinite(percent) || percent < 0 || percent > 100) return;
315
- const cost = percent === 0 ? null : String(Math.round(percent) / 100);
316
- this.#write((h) => ({
317
- ...h,
318
- hints: h.hints.map((x, i) => (i === index ? { ...x, cost } : x)),
319
- }));
320
- }}
321
- />
322
- </label>
323
- <button type="button" aria-label=${this.t('help.upN', { n })} aria-disabled=${String(index === 0)}
324
- @click=${() => this.#move(index, index - 1)}>${this.t('help.up')}</button>
325
- <button type="button" aria-label=${this.t('help.downN', { n })} aria-disabled=${String(last)}
326
- @click=${() => this.#move(index, index + 1)}>${this.t('help.down')}</button>
327
- <button type="button" class="delete" aria-label=${this.t('help.removeN', { n })}
328
- @click=${() => {
329
- this.#drafts.clear();
330
- this.#write((h) => ({ ...h, hints: h.hints.filter((_, i) => i !== index) }));
331
- pending = {
332
- element: this.element ?? '',
333
- field: help.hints.length > 1 ? `hint-${Math.max(0, index - 1)}` : 'add-hint',
334
- };
335
- }}>${this.t('inspector.remove')}</button>
336
- </div>
545
+ <label class="cost">
546
+ ${this.t('help.cost')}
547
+ <select
548
+ aria-label=${this.t('help.costN', { n })}
549
+ data-help=${`cost-${index}`}
550
+ @change=${(e: Event) => {
551
+ const value = inputValue(e);
552
+ const cost = value === '0' ? null : value;
553
+ if (draft) {
554
+ this.#draft = { cost };
555
+ return;
556
+ }
557
+ this.#write((h) => ({
558
+ ...h,
559
+ hints: h.hints.map((x, i) => (i === index ? { ...x, cost } : x)),
560
+ }));
561
+ }}
562
+ >
563
+ ${this.#costs(hint.cost, points).map(
564
+ ({ value, label }) =>
565
+ html`<option value=${value} ?selected=${value === costOf(hint.cost)}>${label}</option>`,
566
+ )}
567
+ </select>
568
+ </label>
337
569
  </li>`;
338
570
  }
339
571
 
340
572
  /**
341
- * A text field of the help: the explanation or a feedback text. While the element has it, "Remove" takes it away:
342
- * one the menu added stays empty, so clearing the field would not.
573
+ * A text field of the help: the explanation or a feedback text. An empty field is none; while the element has it,
574
+ * "Remove" beside its label takes it away.
343
575
  */
344
576
  #area(
345
577
  field: string,
@@ -350,7 +582,19 @@ export class AstlHelpForm extends AuthoringElement {
350
582
  ): TemplateResult {
351
583
  const formatted = this.#formatted(field, value ?? '');
352
584
  return html`<div class="field">
353
- <label for=${field}>${label}</label>
585
+ <div class="row">
586
+ <label for=${field}>${label}</label>
587
+ ${
588
+ value === null || value === undefined
589
+ ? nothing
590
+ : html`<span class="tools">${this.#iconButton('remove', remove, false, () => {
591
+ this.#drafts.delete(field);
592
+ // The field stays, empty: the focus goes back to it.
593
+ set(null);
594
+ this.#refocus(field);
595
+ })}</span>`
596
+ }
597
+ </div>
354
598
  <textarea
355
599
  id=${field}
356
600
  data-help=${field}
@@ -364,39 +608,25 @@ export class AstlHelpForm extends AuthoringElement {
364
608
  @change=${() => this.#done(field)}
365
609
  ></textarea>
366
610
  ${formatted ?? nothing}
367
- ${
368
- value === null || value === undefined
369
- ? nothing
370
- : html`<div class="row">
371
- <button type="button" class="delete" aria-label=${remove}
372
- @click=${() => {
373
- this.#drafts.delete(field);
374
- // The field stays, empty: the focus goes back to it.
375
- pending = { element: this.element ?? '', field };
376
- set(null);
377
- }}>${this.t('inspector.remove')}</button>
378
- </div>`
379
- }
380
611
  </div>`;
381
612
  }
382
613
 
383
- protected override render(): TemplateResult | typeof nothing {
384
- const current = this.#help();
385
- if (current === null || this.authoring === null) return nothing;
386
- const { help } = current;
387
- const { lang, dir } = this.ui;
388
- return html`<section class="kit" part="help" lang=${lang} dir=${dir} aria-labelledby="help-title">
389
- <h3 id="help-title">${this.t('help.title')}</h3>
614
+ #body(source: string, help: HelpValue): TemplateResult {
615
+ const rows = help.hints.length + (this.#draft === null ? 0 : 1);
616
+ const points = this.#pointsOf(source);
617
+ return html`<div class="body" id="help-body" part="help-body">
390
618
  <fieldset aria-describedby="hints-note">
391
619
  <legend>${this.t('help.hints')}</legend>
392
620
  <p class="note" id="hints-note">${this.t('help.hintsNote')}</p>
393
- ${help.hints.length === 0 ? nothing : html`<ol>${help.hints.map((_, i) => this.#hint(help, i))}</ol>`}
621
+ ${
622
+ rows === 0
623
+ ? nothing
624
+ : html`<ol>${Array.from({ length: rows }, (_, i) => this.#hint(help, i, points))}</ol>`
625
+ }
394
626
  <div class="row">
395
- <button type="button" data-help="add-hint"
396
- @click=${() => {
397
- pending = { element: this.element ?? '', field: `hint-${help.hints.length}` };
398
- this.#write((h) => ({ ...h, hints: [...h.hints, { html: '', cost: null }] }));
399
- }}>${this.t('help.addHint')}</button>
627
+ <button type="button" data-help="add-hint" part="help-add" @click=${() => this.#addHint(help)}>
628
+ ${this.t('help.addHint')}
629
+ </button>
400
630
  </div>
401
631
  </fieldset>
402
632
  ${this.#area(
@@ -426,6 +656,29 @@ export class AstlHelpForm extends AuthoringElement {
426
656
  ),
427
657
  )}
428
658
  </fieldset>
659
+ </div>`;
660
+ }
661
+
662
+ protected override render(): TemplateResult | typeof nothing {
663
+ const current = this.#help();
664
+ if (current === null || this.authoring === null) return nothing;
665
+ const { help } = current;
666
+ const { lang, dir } = this.ui;
667
+ const open = this.#open;
668
+ return html`<section class="kit" part="help" lang=${lang} dir=${dir} aria-labelledby="help-title">
669
+ <h3>
670
+ <button type="button" class="toggle" part="help-toggle" aria-expanded=${String(open)}
671
+ aria-controls=${open ? 'help-body' : nothing}
672
+ @click=${() => {
673
+ this.#open = !open;
674
+ if (open) this.#draft = null;
675
+ this.requestUpdate();
676
+ }}>
677
+ ${icon('chevron')}<span id="help-title">${this.t('help.title')}</span>
678
+ ${open ? nothing : html`<span class="summary" part="help-summary">${this.#summary(help)}</span>`}
679
+ </button>
680
+ </h3>
681
+ ${open ? this.#body(current.html, help) : nothing}
429
682
  </section>`;
430
683
  }
431
684
  }