@astl/editor 0.2.0 → 0.2.2

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 (39) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/elements/help-form.d.ts +22 -7
  3. package/dist/elements/help-form.d.ts.map +1 -1
  4. package/dist/elements/help-form.js +276 -95
  5. package/dist/elements/help-form.js.map +1 -1
  6. package/dist/elements/inspector.d.ts +7 -0
  7. package/dist/elements/inspector.d.ts.map +1 -1
  8. package/dist/elements/inspector.js +20 -2
  9. package/dist/elements/inspector.js.map +1 -1
  10. package/dist/elements/picture-field.d.ts.map +1 -1
  11. package/dist/elements/picture-field.js +2 -1
  12. package/dist/elements/picture-field.js.map +1 -1
  13. package/dist/elements/style-load.d.ts +14 -0
  14. package/dist/elements/style-load.d.ts.map +1 -1
  15. package/dist/elements/style-load.js +7 -0
  16. package/dist/elements/style-load.js.map +1 -1
  17. package/dist/elements/styles.d.ts +8 -0
  18. package/dist/elements/styles.d.ts.map +1 -1
  19. package/dist/elements/styles.js +30 -0
  20. package/dist/elements/styles.js.map +1 -1
  21. package/dist/index.d.ts +2 -2
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/messages/en.json +4 -1
  26. package/dist/messages/help/en.json +3 -2
  27. package/dist/messages/help/pl.json +3 -2
  28. package/dist/messages/pl.json +4 -1
  29. package/package.json +5 -5
  30. package/src/elements/help-form.ts +304 -104
  31. package/src/elements/inspector.ts +21 -3
  32. package/src/elements/picture-field.ts +2 -1
  33. package/src/elements/style-load.ts +17 -0
  34. package/src/elements/styles.ts +31 -0
  35. package/src/index.ts +2 -2
  36. package/src/messages/en.json +4 -1
  37. package/src/messages/help/en.json +3 -2
  38. package/src/messages/help/pl.json +3 -2
  39. package/src/messages/pl.json +4 -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';
@@ -22,7 +27,8 @@ import pl from '../messages/help/pl.json' with { type: 'json' };
22
27
  import { EDITOR_NAMESPACE } from '../messages.js';
23
28
  import { elementTree } from '../scan.js';
24
29
  import { AuthoringElement } from './base.js';
25
- import { kitStyles } from './styles.js';
30
+ import type { HelpPart } from './style-load.js';
31
+ import { compactButtons, kitStyles } from './styles.js';
26
32
 
27
33
  const helpMessages: readonly MessageCatalog[] = [
28
34
  { namespace: EDITOR_NAMESPACE, locale: 'en', messages: en },
@@ -32,14 +38,31 @@ const helpMessages: readonly MessageCatalog[] = [
32
38
  /** What the insert menu adds: a hint, the explanation, or the next feedback text the element lacks. */
33
39
  export type HelpKind = 'hint' | 'explanation' | 'feedback';
34
40
 
35
- /** The field to focus once the form shows it (after an insert), by element id. */
36
- 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
+ }
37
59
 
38
60
  const graded = (authoring: Authoring, tag: string) => authoring.descriptor(tag)?.manifest.graded === true;
39
61
 
40
62
  /**
41
- * Adds help to the graded element at the cursor (and asks the form to focus it), or else to the page at the cursor.
42
- * 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.
43
66
  */
44
67
  export function addHelp(authoring: Authoring, adapter: EditorAdapter, kind: HelpKind): string | null {
45
68
  const selection = adapter.getSelection();
@@ -53,21 +76,12 @@ export function addHelp(authoring: Authoring, adapter: EditorAdapter, kind: Help
53
76
  return block;
54
77
  }
55
78
  const help = readHelp(current);
56
- let next: HelpValue;
57
- let field: string;
58
- if (kind === 'hint') {
59
- next = { ...help, hints: [...help.hints, { html: '', cost: null }] };
60
- field = `hint-${help.hints.length}`;
61
- } else if (kind === 'explanation') {
62
- next = { ...help, explanation: help.explanation ?? '' };
63
- field = 'explanation';
64
- } else {
65
- const when = FEEDBACK_ORDER.find((w) => help.feedback[w] === undefined) ?? 'correct';
66
- next = { ...help, feedback: { ...help.feedback, [when]: help.feedback[when] ?? '' } };
67
- field = `feedback-${when}`;
68
- }
69
- pending = { element: selection.id, field };
70
- 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
+ );
71
85
  return null;
72
86
  }
73
87
 
@@ -85,6 +99,17 @@ const round = (n: number): number => Math.round(n * 100) / 100;
85
99
 
86
100
  const inputValue = (event: Event) => (event.target as HTMLInputElement | HTMLTextAreaElement).value;
87
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
+
88
113
  export class AstlHelpForm extends AuthoringElement {
89
114
  static override properties: PropertyDeclarations = {
90
115
  ...AuthoringElement.properties,
@@ -94,6 +119,7 @@ export class AstlHelpForm extends AuthoringElement {
94
119
 
95
120
  static override styles: CSSResultGroup = [
96
121
  kitStyles,
122
+ compactButtons,
97
123
  css`
98
124
  :host {
99
125
  display: block;
@@ -102,13 +128,47 @@ export class AstlHelpForm extends AuthoringElement {
102
128
  display: flex;
103
129
  flex-direction: column;
104
130
  gap: var(--astl-space-3);
105
- padding-block-start: var(--astl-space-3);
131
+ padding-block: var(--astl-space-2);
132
+ padding-inline: var(--astl-space-3);
106
133
  border-block-start: var(--astl-outline-width) solid var(--astl-line);
107
134
  }
108
135
  h3 {
109
136
  margin: 0;
110
137
  font-size: 1rem;
111
138
  }
139
+ .toggle {
140
+ inline-size: 100%;
141
+ flex-wrap: wrap;
142
+ gap: 0 var(--astl-space-2);
143
+ padding-inline: var(--astl-space-1);
144
+ border-color: transparent;
145
+ background: transparent;
146
+ color: var(--astl-ink);
147
+ font-size: 1rem;
148
+ font-weight: 700;
149
+ }
150
+ .toggle .glyph {
151
+ flex: none;
152
+ transition: rotate 0.15s;
153
+ }
154
+ .toggle[aria-expanded='true'] .glyph {
155
+ rotate: 90deg;
156
+ }
157
+ .toggle:dir(rtl) .glyph {
158
+ scale: -1 1;
159
+ }
160
+ .toggle:dir(rtl)[aria-expanded='true'] .glyph {
161
+ rotate: -90deg;
162
+ }
163
+ .summary {
164
+ color: var(--astl-ink-muted);
165
+ font-weight: 400;
166
+ }
167
+ .body {
168
+ display: flex;
169
+ flex-direction: column;
170
+ gap: var(--astl-space-3);
171
+ }
112
172
  fieldset,
113
173
  .field {
114
174
  display: flex;
@@ -127,7 +187,7 @@ export class AstlHelpForm extends AuthoringElement {
127
187
  ol {
128
188
  display: flex;
129
189
  flex-direction: column;
130
- gap: var(--astl-space-3);
190
+ gap: var(--astl-space-2);
131
191
  margin: 0;
132
192
  padding: 0;
133
193
  list-style: none;
@@ -143,16 +203,37 @@ export class AstlHelpForm extends AuthoringElement {
143
203
  li[data-over] {
144
204
  border-color: var(--astl-accent);
145
205
  }
206
+ li[data-draft] {
207
+ border-style: dashed;
208
+ }
146
209
  .row {
147
210
  display: flex;
148
211
  flex-wrap: wrap;
149
212
  align-items: center;
150
- gap: var(--astl-space-2);
213
+ gap: var(--astl-space-1) var(--astl-space-2);
214
+ }
215
+ .tools {
216
+ display: flex;
217
+ gap: var(--astl-space-1);
218
+ margin-inline-start: auto;
151
219
  }
152
220
  .handle {
153
221
  cursor: grab;
154
222
  user-select: none;
155
223
  }
224
+ .icon {
225
+ justify-content: center;
226
+ padding: 0;
227
+ }
228
+ .glyph {
229
+ inline-size: 1.25rem;
230
+ block-size: 1.25rem;
231
+ fill: none;
232
+ stroke: currentColor;
233
+ stroke-width: 1.8;
234
+ stroke-linecap: round;
235
+ stroke-linejoin: round;
236
+ }
156
237
  .cost {
157
238
  display: flex;
158
239
  align-items: center;
@@ -188,6 +269,12 @@ export class AstlHelpForm extends AuthoringElement {
188
269
  #points: { readonly html: string; readonly value: number | null } | null = null;
189
270
  #counting: string | null = null;
190
271
  #dragged: number | null = null;
272
+ /** Whether the section is open: closed for each element until it is opened. */
273
+ #open = false;
274
+ /** A hint added and not yet written, with the cost picked for it: it goes into the source with its first text. */
275
+ #draft: { readonly cost: string | null } | null = null;
276
+ /** The field to focus once the form shows it. */
277
+ #focus: string | null = null;
191
278
 
192
279
  constructor() {
193
280
  super();
@@ -195,20 +282,52 @@ export class AstlHelpForm extends AuthoringElement {
195
282
  this.inline = false;
196
283
  }
197
284
 
285
+ override connectedCallback(): void {
286
+ super.connectedCallback();
287
+ forms.add(this);
288
+ }
289
+
290
+ override disconnectedCallback(): void {
291
+ super.disconnectedCallback();
292
+ forms.delete(this);
293
+ }
294
+
198
295
  protected override willUpdate(changed: PropertyValues<this>): void {
199
296
  super.willUpdate(changed);
200
297
  if (changed.has('authoring')) this.authoring?.addMessages(helpMessages);
201
- if (changed.has('element')) this.#drafts.clear();
298
+ if (changed.has('element')) {
299
+ this.#drafts.clear();
300
+ this.#open = false;
301
+ this.#draft = null;
302
+ }
303
+ this.#take();
202
304
  }
203
305
 
204
306
  protected override updated(): void {
205
- if (pending === null || pending.element !== this.element) return;
206
- const field = this.renderRoot.querySelector<HTMLElement>(`[data-help="${pending.field}"]`);
307
+ if (this.#focus === null) return;
308
+ const field = this.renderRoot.querySelector<HTMLElement>(`[data-help="${this.#focus}"]`);
207
309
  if (field === null) return;
208
- pending = null;
310
+ this.#focus = null;
209
311
  field.focus();
210
312
  }
211
313
 
314
+ /** Takes a request to open this form: the section opens at its place, a new hint as a draft. */
315
+ #take(): void {
316
+ if (pending === null || pending.adapter !== this.adapter || pending.element !== this.element) return;
317
+ const { place } = pending;
318
+ const current = this.#help();
319
+ if (current === null) return;
320
+ pending = null;
321
+ const { help } = current;
322
+ this.#open = true;
323
+ if (place === 'hint-new') this.#draft ??= { cost: null };
324
+ if (place === 'hints') this.#focus = help.hints.length > 0 ? 'hint-0' : 'add-hint';
325
+ else if (place === 'hint-new') this.#focus = `hint-${help.hints.length}`;
326
+ else if (place === 'feedback')
327
+ this.#focus = `feedback-${FEEDBACK_VARIANTS.find((w) => help.feedback[w] !== undefined) ?? 'correct'}`;
328
+ else this.#focus = place;
329
+ }
330
+
212
331
  #help(): { readonly html: string; readonly help: HelpValue } | null {
213
332
  const html = this.element === null ? null : (this.adapter?.getElement(this.element) ?? null);
214
333
  return html === null ? null : { html, help: readHelp(html) };
@@ -258,6 +377,16 @@ export class AstlHelpForm extends AuthoringElement {
258
377
  });
259
378
  }
260
379
 
380
+ /** What the element has, as the closed section says it: "2 hints · explanation · feedback", or "none". */
381
+ #summary(help: HelpValue): string {
382
+ const parts = [
383
+ ...(help.hints.length > 0 ? [this.t('surface.helpHints', { n: help.hints.length })] : []),
384
+ ...(help.explanation !== null ? [this.t('help.summaryExplanation')] : []),
385
+ ...(Object.keys(help.feedback).length > 0 ? [this.t('help.summaryFeedback')] : []),
386
+ ];
387
+ return parts.length === 0 ? this.t('help.summaryNone') : parts.join(' · ');
388
+ }
389
+
261
390
  #write(change: (help: HelpValue) => HelpValue): void {
262
391
  const current = this.#help();
263
392
  if (current === null || this.element === null || this.adapter === null) return;
@@ -265,6 +394,12 @@ export class AstlHelpForm extends AuthoringElement {
265
394
  if (next !== current.html) this.adapter.replaceElement(this.element, next);
266
395
  }
267
396
 
397
+ /** Focuses a field once the form shows it again. */
398
+ #refocus(field: string): void {
399
+ this.#focus = field;
400
+ this.requestUpdate();
401
+ }
402
+
268
403
  /** A text field's value: as typed while it is being edited, else what the source says. */
269
404
  #text(field: string, html: string): string {
270
405
  return this.#drafts.get(field) ?? helpText(html);
@@ -299,30 +434,63 @@ export class AstlHelpForm extends AuthoringElement {
299
434
  this.#drafts.clear();
300
435
  }
301
436
 
437
+ /** "Add a hint": a draft row (one at a time), focused. */
438
+ #addHint(help: HelpValue): void {
439
+ this.#draft ??= { cost: null };
440
+ this.#refocus(`hint-${help.hints.length}`);
441
+ }
442
+
443
+ /** The draft goes without a trace: left empty, or removed. */
444
+ #dropDraft(field: string): void {
445
+ this.#draft = null;
446
+ this.#drafts.delete(field);
447
+ this.requestUpdate();
448
+ }
449
+
450
+ /** A small button with an icon, named and with a tooltip by its text. */
451
+ #iconButton(name: 'up' | 'down' | 'remove', label: string, off: boolean, run: () => void): TemplateResult {
452
+ return html`<button type="button" class=${name === 'remove' ? 'icon delete' : 'icon'}
453
+ part=${name === 'remove' ? 'help-remove' : 'help-move'} aria-label=${label} title=${label}
454
+ aria-disabled=${off ? 'true' : nothing}
455
+ @click=${() => {
456
+ if (!off) run();
457
+ }}>${icon(name)}</button>`;
458
+ }
459
+
302
460
  #hint(help: HelpValue, index: number, points: number | null): TemplateResult {
303
- const hint = help.hints[index] as HelpValue['hints'][number];
461
+ const draft = index === help.hints.length;
462
+ const hint: HelpValue['hints'][number] = draft
463
+ ? { html: '', cost: this.#draft?.cost ?? null }
464
+ : (help.hints[index] as HelpValue['hints'][number]);
304
465
  const n = index + 1;
305
466
  const field = `hint-${index}`;
306
467
  const last = index === help.hints.length - 1;
307
468
  const formatted = this.#formatted(field, hint.html);
308
469
  const over = (event: DragEvent, on: boolean) => {
309
- if (this.#dragged === null) return;
470
+ if (this.#dragged === null || draft) return;
310
471
  event.preventDefault();
311
472
  (event.currentTarget as HTMLElement).toggleAttribute('data-over', on);
312
473
  };
313
474
  return html`<li
475
+ ?data-draft=${draft}
314
476
  @dragover=${(e: DragEvent) => over(e, true)}
315
477
  @dragleave=${(e: DragEvent) => over(e, false)}
316
478
  @drop=${(e: DragEvent) => {
317
479
  over(e, false);
318
- if (this.#dragged !== null) this.#move(this.#dragged, index);
480
+ if (this.#dragged !== null && !draft) this.#move(this.#dragged, index);
319
481
  this.#dragged = null;
320
482
  }}
483
+ @focusout=${(e: FocusEvent) => {
484
+ // A draft left empty goes: the focus is outside its row, and it has no text yet.
485
+ const row = e.currentTarget as HTMLElement;
486
+ if (draft && this.#draft !== null && !row.contains(e.relatedTarget as Node | null))
487
+ this.#dropDraft(field);
488
+ }}
321
489
  >
322
490
  <div class="row">
323
491
  <span
324
492
  class="handle"
325
- draggable="true"
493
+ draggable=${draft ? 'false' : 'true'}
326
494
  aria-hidden="true"
327
495
  title=${this.t('help.drag')}
328
496
  @dragstart=${(e: DragEvent) => {
@@ -335,6 +503,24 @@ export class AstlHelpForm extends AuthoringElement {
335
503
  >⠿</span
336
504
  >
337
505
  <label for=${field}>${this.t('help.hintN', { n })}</label>
506
+ <span class="tools">
507
+ ${
508
+ draft
509
+ ? nothing
510
+ : html`${this.#iconButton('up', this.t('help.upN', { n }), index === 0, () => this.#move(index, index - 1))}
511
+ ${this.#iconButton('down', this.t('help.downN', { n }), last, () => this.#move(index, index + 1))}`
512
+ }
513
+ ${this.#iconButton('remove', this.t('help.removeN', { n }), false, () => {
514
+ if (draft) {
515
+ this.#dropDraft(field);
516
+ this.#refocus('add-hint');
517
+ return;
518
+ }
519
+ this.#drafts.clear();
520
+ this.#write((h) => ({ ...h, hints: h.hints.filter((_, i) => i !== index) }));
521
+ this.#refocus(help.hints.length > 1 ? `hint-${Math.max(0, index - 1)}` : 'add-hint');
522
+ })}
523
+ </span>
338
524
  </div>
339
525
  <textarea
340
526
  id=${field}
@@ -344,55 +530,52 @@ export class AstlHelpForm extends AuthoringElement {
344
530
  .value=${live(this.#text(field, hint.html))}
345
531
  @input=${(e: Event) => {
346
532
  const text = this.#typed(field, e);
347
- this.#write((h) => ({
348
- ...h,
349
- hints: h.hints.map((x, i) => (i === index ? { ...x, html: text } : x)),
350
- }));
533
+ if (!draft) {
534
+ this.#write((h) => ({
535
+ ...h,
536
+ hints: h.hints.map((x, i) => (i === index ? { ...x, html: text } : x)),
537
+ }));
538
+ return;
539
+ }
540
+ // The draft's first text writes it, with the cost picked for it.
541
+ if (inputValue(e).trim() === '') return;
542
+ const cost = this.#draft?.cost ?? null;
543
+ this.#draft = null;
544
+ this.#write((h) => ({ ...h, hints: [...h.hints, { html: text, cost }] }));
351
545
  }}
352
546
  @change=${() => this.#done(field)}
353
547
  ></textarea>
354
548
  ${formatted ?? nothing}
355
- <div class="row">
356
- <label class="cost">
357
- ${this.t('help.cost')}
358
- <select
359
- aria-label=${this.t('help.costN', { n })}
360
- data-help=${`cost-${index}`}
361
- @change=${(e: Event) => {
362
- const value = inputValue(e);
363
- const cost = value === '0' ? null : value;
364
- this.#write((h) => ({
365
- ...h,
366
- hints: h.hints.map((x, i) => (i === index ? { ...x, cost } : x)),
367
- }));
368
- }}
369
- >
370
- ${this.#costs(hint.cost, points).map(
371
- ({ value, label }) =>
372
- html`<option value=${value} ?selected=${value === costOf(hint.cost)}>${label}</option>`,
373
- )}
374
- </select>
375
- </label>
376
- <button type="button" aria-label=${this.t('help.upN', { n })} aria-disabled=${String(index === 0)}
377
- @click=${() => this.#move(index, index - 1)}>${this.t('help.up')}</button>
378
- <button type="button" aria-label=${this.t('help.downN', { n })} aria-disabled=${String(last)}
379
- @click=${() => this.#move(index, index + 1)}>${this.t('help.down')}</button>
380
- <button type="button" class="delete" aria-label=${this.t('help.removeN', { n })}
381
- @click=${() => {
382
- this.#drafts.clear();
383
- this.#write((h) => ({ ...h, hints: h.hints.filter((_, i) => i !== index) }));
384
- pending = {
385
- element: this.element ?? '',
386
- field: help.hints.length > 1 ? `hint-${Math.max(0, index - 1)}` : 'add-hint',
387
- };
388
- }}>${this.t('inspector.remove')}</button>
389
- </div>
549
+ <label class="cost">
550
+ ${this.t('help.cost')}
551
+ <select
552
+ aria-label=${this.t('help.costN', { n })}
553
+ data-help=${`cost-${index}`}
554
+ @change=${(e: Event) => {
555
+ const value = inputValue(e);
556
+ const cost = value === '0' ? null : value;
557
+ if (draft) {
558
+ this.#draft = { cost };
559
+ return;
560
+ }
561
+ this.#write((h) => ({
562
+ ...h,
563
+ hints: h.hints.map((x, i) => (i === index ? { ...x, cost } : x)),
564
+ }));
565
+ }}
566
+ >
567
+ ${this.#costs(hint.cost, points).map(
568
+ ({ value, label }) =>
569
+ html`<option value=${value} ?selected=${value === costOf(hint.cost)}>${label}</option>`,
570
+ )}
571
+ </select>
572
+ </label>
390
573
  </li>`;
391
574
  }
392
575
 
393
576
  /**
394
- * A text field of the help: the explanation or a feedback text. While the element has it, "Remove" takes it away:
395
- * one the menu added stays empty, so clearing the field would not.
577
+ * A text field of the help: the explanation or a feedback text. An empty field is none; while the element has it,
578
+ * "Remove" beside its label takes it away.
396
579
  */
397
580
  #area(
398
581
  field: string,
@@ -403,7 +586,19 @@ export class AstlHelpForm extends AuthoringElement {
403
586
  ): TemplateResult {
404
587
  const formatted = this.#formatted(field, value ?? '');
405
588
  return html`<div class="field">
406
- <label for=${field}>${label}</label>
589
+ <div class="row">
590
+ <label for=${field}>${label}</label>
591
+ ${
592
+ value === null || value === undefined
593
+ ? nothing
594
+ : html`<span class="tools">${this.#iconButton('remove', remove, false, () => {
595
+ this.#drafts.delete(field);
596
+ // The field stays, empty: the focus goes back to it.
597
+ set(null);
598
+ this.#refocus(field);
599
+ })}</span>`
600
+ }
601
+ </div>
407
602
  <textarea
408
603
  id=${field}
409
604
  data-help=${field}
@@ -417,43 +612,25 @@ export class AstlHelpForm extends AuthoringElement {
417
612
  @change=${() => this.#done(field)}
418
613
  ></textarea>
419
614
  ${formatted ?? nothing}
420
- ${
421
- value === null || value === undefined
422
- ? nothing
423
- : html`<div class="row">
424
- <button type="button" class="delete" aria-label=${remove}
425
- @click=${() => {
426
- this.#drafts.delete(field);
427
- // The field stays, empty: the focus goes back to it.
428
- pending = { element: this.element ?? '', field };
429
- set(null);
430
- }}>${this.t('inspector.remove')}</button>
431
- </div>`
432
- }
433
615
  </div>`;
434
616
  }
435
617
 
436
- protected override render(): TemplateResult | typeof nothing {
437
- const current = this.#help();
438
- if (current === null || this.authoring === null) return nothing;
439
- const { help } = current;
440
- const { lang, dir } = this.ui;
441
- return html`<section class="kit" part="help" lang=${lang} dir=${dir} aria-labelledby="help-title">
442
- <h3 id="help-title">${this.t('help.title')}</h3>
618
+ #body(source: string, help: HelpValue): TemplateResult {
619
+ const rows = help.hints.length + (this.#draft === null ? 0 : 1);
620
+ const points = this.#pointsOf(source);
621
+ return html`<div class="body" id="help-body" part="help-body">
443
622
  <fieldset aria-describedby="hints-note">
444
623
  <legend>${this.t('help.hints')}</legend>
445
624
  <p class="note" id="hints-note">${this.t('help.hintsNote')}</p>
446
625
  ${
447
- help.hints.length === 0
626
+ rows === 0
448
627
  ? nothing
449
- : html`<ol>${help.hints.map((_, i) => this.#hint(help, i, this.#pointsOf(current.html)))}</ol>`
628
+ : html`<ol>${Array.from({ length: rows }, (_, i) => this.#hint(help, i, points))}</ol>`
450
629
  }
451
630
  <div class="row">
452
- <button type="button" data-help="add-hint"
453
- @click=${() => {
454
- pending = { element: this.element ?? '', field: `hint-${help.hints.length}` };
455
- this.#write((h) => ({ ...h, hints: [...h.hints, { html: '', cost: null }] }));
456
- }}>${this.t('help.addHint')}</button>
631
+ <button type="button" data-help="add-hint" part="help-add" @click=${() => this.#addHint(help)}>
632
+ ${this.t('help.addHint')}
633
+ </button>
457
634
  </div>
458
635
  </fieldset>
459
636
  ${this.#area(
@@ -483,6 +660,29 @@ export class AstlHelpForm extends AuthoringElement {
483
660
  ),
484
661
  )}
485
662
  </fieldset>
663
+ </div>`;
664
+ }
665
+
666
+ protected override render(): TemplateResult | typeof nothing {
667
+ const current = this.#help();
668
+ if (current === null || this.authoring === null) return nothing;
669
+ const { help } = current;
670
+ const { lang, dir } = this.ui;
671
+ const open = this.#open;
672
+ return html`<section class="kit" part="help" lang=${lang} dir=${dir} aria-labelledby="help-title">
673
+ <h3>
674
+ <button type="button" class="toggle" part="help-toggle" aria-expanded=${String(open)}
675
+ aria-controls=${open ? 'help-body' : nothing}
676
+ @click=${() => {
677
+ this.#open = !open;
678
+ if (open) this.#draft = null;
679
+ this.requestUpdate();
680
+ }}>
681
+ ${icon('chevron')}<span id="help-title">${this.t('help.title')}</span>
682
+ ${open ? nothing : html`<span class="summary" part="help-summary">${this.#summary(help)}</span>`}
683
+ </button>
684
+ </h3>
685
+ ${open ? this.#body(current.html, help) : nothing}
486
686
  </section>`;
487
687
  }
488
688
  }
@@ -29,8 +29,8 @@ import { AuthoringElement } from './base.js';
29
29
  import { GLYPHS, TYPE_GLYPHS } from './insert-menu.js';
30
30
  import { SEVERITY_ICONS, sortIssues } from './issue-list.js';
31
31
  import type { FieldChange } from './picture-field.js';
32
- import { loadHelpForm, loadStyleElements, STYLE_OPEN, type StyleOpen } from './style-load.js';
33
- import { inheritTokens, kitStyles } from './styles.js';
32
+ import { type HelpPart, loadHelpForm, loadStyleElements, STYLE_OPEN, type StyleOpen } from './style-load.js';
33
+ import { compactButtons, inheritTokens, kitStyles } from './styles.js';
34
34
 
35
35
  /** A control to focus after the next render (text list rows come and go). */
36
36
  interface PendingFocus {
@@ -45,6 +45,9 @@ const ACTION_ICONS = {
45
45
  delete: svg`<path d="M4 6h12M8 6V4h4v2M6 6l.8 10h6.4L14 6M9 9v4M11 9v4"/>`,
46
46
  } satisfies Readonly<Record<string, SVGTemplateResult>>;
47
47
 
48
+ /** The parts of the help for students (`<astl-help-form>`), forwarded so hosts can restyle its disclosure and buttons. */
49
+ const HELP_PARTS = 'help, help-toggle, help-summary, help-body, help-add, help-move, help-remove';
50
+
48
51
  /** Fields that fit beside another in the two-column form: a select, a number or a short text (no upload). */
49
52
  const fitsBeside = (field: InspectorFieldModel): boolean =>
50
53
  field.kind === 'attribute' &&
@@ -68,6 +71,7 @@ const checkedOf = (event: Event) => (event.target as HTMLInputElement).checked;
68
71
  export class AstlInspector extends AuthoringElement {
69
72
  static override styles: CSSResultGroup = [
70
73
  kitStyles,
74
+ compactButtons,
71
75
  // The picture fields redeclare the tokens on their :host: they take this element's (theme, host overrides).
72
76
  inheritTokens('astl-picture-field, astl-style-form, astl-help-form'),
73
77
  css`
@@ -856,6 +860,20 @@ export class AstlInspector extends AuthoringElement {
856
860
  this.requestUpdate();
857
861
  }
858
862
 
863
+ /**
864
+ * Opens the help for students of the graded element at the cursor at `part`, as a tag on its frame asks
865
+ * (`astl-help-open`): the section opens and the part's first field takes the focus (a hint's, or "Add a hint" without
866
+ * one; the first feedback text the element has). Nothing without a graded element at the cursor.
867
+ */
868
+ async openHelp(part: HelpPart): Promise<void> {
869
+ const { adapter, authoring } = this;
870
+ const selection = adapter?.getSelection() ?? null;
871
+ if (adapter === null || authoring === null || selection === null) return;
872
+ if (authoring.descriptor(selection.tag)?.manifest.graded !== true) return;
873
+ const { openHelp } = await loadHelpForm();
874
+ openHelp(adapter, selection.id, part);
875
+ }
876
+
859
877
  /** A press on a target: a host may take it (`astl-style-open`); otherwise its form opens here, or closes. */
860
878
  #askStyle(kind: EditorStyleTargetKind): void {
861
879
  if (this.#styleOpen === kind) {
@@ -931,7 +949,7 @@ export class AstlInspector extends AuthoringElement {
931
949
  ${
932
950
  manifest?.graded === true
933
951
  ? html`<astl-help-form .authoring=${authoring} .adapter=${adapter} .element=${this.#shown}
934
- ?inline=${manifest.display === 'inline'}></astl-help-form>`
952
+ ?inline=${manifest.display === 'inline'} exportparts=${HELP_PARTS}></astl-help-form>`
935
953
  : nothing
936
954
  }
937
955
  <div class="fields" data-tag=${model.tag}>${model.fields.map((f, i) => this.#field(f, i))}</div>`;