@astl/editor 0.2.0 → 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.
@@ -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,6 +27,7 @@ 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';
30
+ import type { HelpPart } from './style-load.js';
25
31
  import { kitStyles } from './styles.js';
26
32
 
27
33
  const helpMessages: readonly MessageCatalog[] = [
@@ -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,
@@ -102,13 +127,44 @@ export class AstlHelpForm extends AuthoringElement {
102
127
  display: flex;
103
128
  flex-direction: column;
104
129
  gap: var(--astl-space-3);
105
- padding-block-start: var(--astl-space-3);
130
+ padding-block-start: var(--astl-space-2);
106
131
  border-block-start: var(--astl-outline-width) solid var(--astl-line);
107
132
  }
108
133
  h3 {
109
134
  margin: 0;
110
135
  font-size: 1rem;
111
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
+ }
112
168
  fieldset,
113
169
  .field {
114
170
  display: flex;
@@ -127,7 +183,7 @@ export class AstlHelpForm extends AuthoringElement {
127
183
  ol {
128
184
  display: flex;
129
185
  flex-direction: column;
130
- gap: var(--astl-space-3);
186
+ gap: var(--astl-space-2);
131
187
  margin: 0;
132
188
  padding: 0;
133
189
  list-style: none;
@@ -143,16 +199,37 @@ export class AstlHelpForm extends AuthoringElement {
143
199
  li[data-over] {
144
200
  border-color: var(--astl-accent);
145
201
  }
202
+ li[data-draft] {
203
+ border-style: dashed;
204
+ }
146
205
  .row {
147
206
  display: flex;
148
207
  flex-wrap: wrap;
149
208
  align-items: center;
150
- 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;
151
215
  }
152
216
  .handle {
153
217
  cursor: grab;
154
218
  user-select: none;
155
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
+ }
156
233
  .cost {
157
234
  display: flex;
158
235
  align-items: center;
@@ -188,6 +265,12 @@ export class AstlHelpForm extends AuthoringElement {
188
265
  #points: { readonly html: string; readonly value: number | null } | null = null;
189
266
  #counting: string | null = null;
190
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;
191
274
 
192
275
  constructor() {
193
276
  super();
@@ -195,20 +278,52 @@ export class AstlHelpForm extends AuthoringElement {
195
278
  this.inline = false;
196
279
  }
197
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
+
198
291
  protected override willUpdate(changed: PropertyValues<this>): void {
199
292
  super.willUpdate(changed);
200
293
  if (changed.has('authoring')) this.authoring?.addMessages(helpMessages);
201
- 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();
202
300
  }
203
301
 
204
302
  protected override updated(): void {
205
- if (pending === null || pending.element !== this.element) return;
206
- 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}"]`);
207
305
  if (field === null) return;
208
- pending = null;
306
+ this.#focus = null;
209
307
  field.focus();
210
308
  }
211
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
+
212
327
  #help(): { readonly html: string; readonly help: HelpValue } | null {
213
328
  const html = this.element === null ? null : (this.adapter?.getElement(this.element) ?? null);
214
329
  return html === null ? null : { html, help: readHelp(html) };
@@ -258,6 +373,16 @@ export class AstlHelpForm extends AuthoringElement {
258
373
  });
259
374
  }
260
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
+
261
386
  #write(change: (help: HelpValue) => HelpValue): void {
262
387
  const current = this.#help();
263
388
  if (current === null || this.element === null || this.adapter === null) return;
@@ -265,6 +390,12 @@ export class AstlHelpForm extends AuthoringElement {
265
390
  if (next !== current.html) this.adapter.replaceElement(this.element, next);
266
391
  }
267
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
+
268
399
  /** A text field's value: as typed while it is being edited, else what the source says. */
269
400
  #text(field: string, html: string): string {
270
401
  return this.#drafts.get(field) ?? helpText(html);
@@ -299,30 +430,63 @@ export class AstlHelpForm extends AuthoringElement {
299
430
  this.#drafts.clear();
300
431
  }
301
432
 
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
+
302
456
  #hint(help: HelpValue, index: number, points: number | null): TemplateResult {
303
- const hint = help.hints[index] as HelpValue['hints'][number];
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]);
304
461
  const n = index + 1;
305
462
  const field = `hint-${index}`;
306
463
  const last = index === help.hints.length - 1;
307
464
  const formatted = this.#formatted(field, hint.html);
308
465
  const over = (event: DragEvent, on: boolean) => {
309
- if (this.#dragged === null) return;
466
+ if (this.#dragged === null || draft) return;
310
467
  event.preventDefault();
311
468
  (event.currentTarget as HTMLElement).toggleAttribute('data-over', on);
312
469
  };
313
470
  return html`<li
471
+ ?data-draft=${draft}
314
472
  @dragover=${(e: DragEvent) => over(e, true)}
315
473
  @dragleave=${(e: DragEvent) => over(e, false)}
316
474
  @drop=${(e: DragEvent) => {
317
475
  over(e, false);
318
- if (this.#dragged !== null) this.#move(this.#dragged, index);
476
+ if (this.#dragged !== null && !draft) this.#move(this.#dragged, index);
319
477
  this.#dragged = null;
320
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
+ }}
321
485
  >
322
486
  <div class="row">
323
487
  <span
324
488
  class="handle"
325
- draggable="true"
489
+ draggable=${draft ? 'false' : 'true'}
326
490
  aria-hidden="true"
327
491
  title=${this.t('help.drag')}
328
492
  @dragstart=${(e: DragEvent) => {
@@ -335,6 +499,24 @@ export class AstlHelpForm extends AuthoringElement {
335
499
  >⠿</span
336
500
  >
337
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>
338
520
  </div>
339
521
  <textarea
340
522
  id=${field}
@@ -344,55 +526,52 @@ export class AstlHelpForm extends AuthoringElement {
344
526
  .value=${live(this.#text(field, hint.html))}
345
527
  @input=${(e: Event) => {
346
528
  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
- }));
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 }] }));
351
541
  }}
352
542
  @change=${() => this.#done(field)}
353
543
  ></textarea>
354
544
  ${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>
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>
390
569
  </li>`;
391
570
  }
392
571
 
393
572
  /**
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.
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.
396
575
  */
397
576
  #area(
398
577
  field: string,
@@ -403,7 +582,19 @@ export class AstlHelpForm extends AuthoringElement {
403
582
  ): TemplateResult {
404
583
  const formatted = this.#formatted(field, value ?? '');
405
584
  return html`<div class="field">
406
- <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>
407
598
  <textarea
408
599
  id=${field}
409
600
  data-help=${field}
@@ -417,43 +608,25 @@ export class AstlHelpForm extends AuthoringElement {
417
608
  @change=${() => this.#done(field)}
418
609
  ></textarea>
419
610
  ${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
611
  </div>`;
434
612
  }
435
613
 
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>
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">
443
618
  <fieldset aria-describedby="hints-note">
444
619
  <legend>${this.t('help.hints')}</legend>
445
620
  <p class="note" id="hints-note">${this.t('help.hintsNote')}</p>
446
621
  ${
447
- help.hints.length === 0
622
+ rows === 0
448
623
  ? nothing
449
- : html`<ol>${help.hints.map((_, i) => this.#hint(help, i, this.#pointsOf(current.html)))}</ol>`
624
+ : html`<ol>${Array.from({ length: rows }, (_, i) => this.#hint(help, i, points))}</ol>`
450
625
  }
451
626
  <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>
627
+ <button type="button" data-help="add-hint" part="help-add" @click=${() => this.#addHint(help)}>
628
+ ${this.t('help.addHint')}
629
+ </button>
457
630
  </div>
458
631
  </fieldset>
459
632
  ${this.#area(
@@ -483,6 +656,29 @@ export class AstlHelpForm extends AuthoringElement {
483
656
  ),
484
657
  )}
485
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}
486
682
  </section>`;
487
683
  }
488
684
  }
@@ -29,7 +29,7 @@ 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';
32
+ import { type HelpPart, loadHelpForm, loadStyleElements, STYLE_OPEN, type StyleOpen } from './style-load.js';
33
33
  import { inheritTokens, kitStyles } from './styles.js';
34
34
 
35
35
  /** A control to focus after the next render (text list rows come and go). */
@@ -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' &&
@@ -856,6 +859,20 @@ export class AstlInspector extends AuthoringElement {
856
859
  this.requestUpdate();
857
860
  }
858
861
 
862
+ /**
863
+ * Opens the help for students of the graded element at the cursor at `part`, as a tag on its frame asks
864
+ * (`astl-help-open`): the section opens and the part's first field takes the focus (a hint's, or "Add a hint" without
865
+ * one; the first feedback text the element has). Nothing without a graded element at the cursor.
866
+ */
867
+ async openHelp(part: HelpPart): Promise<void> {
868
+ const { adapter, authoring } = this;
869
+ const selection = adapter?.getSelection() ?? null;
870
+ if (adapter === null || authoring === null || selection === null) return;
871
+ if (authoring.descriptor(selection.tag)?.manifest.graded !== true) return;
872
+ const { openHelp } = await loadHelpForm();
873
+ openHelp(adapter, selection.id, part);
874
+ }
875
+
859
876
  /** A press on a target: a host may take it (`astl-style-open`); otherwise its form opens here, or closes. */
860
877
  #askStyle(kind: EditorStyleTargetKind): void {
861
878
  if (this.#styleOpen === kind) {
@@ -931,7 +948,7 @@ export class AstlInspector extends AuthoringElement {
931
948
  ${
932
949
  manifest?.graded === true
933
950
  ? html`<astl-help-form .authoring=${authoring} .adapter=${adapter} .element=${this.#shown}
934
- ?inline=${manifest.display === 'inline'}></astl-help-form>`
951
+ ?inline=${manifest.display === 'inline'} exportparts=${HELP_PARTS}></astl-help-form>`
935
952
  : nothing
936
953
  }
937
954
  <div class="fields" data-tag=${model.tag}>${model.fields.map((f, i) => this.#field(f, i))}</div>`;
@@ -12,6 +12,23 @@ export interface StyleOpen {
12
12
  readonly target: EditorStyleTargetKind | 'rules';
13
13
  }
14
14
 
15
+ /**
16
+ * The event of a request for a graded element's help for students (`detail`: the element's id and a part), from a tag
17
+ * on its frame on the surface; the cursor is in the element by then. It bubbles, is composed and can be cancelled: a
18
+ * host that shows its own inspector calls `preventDefault`, shows it and calls its `openHelp(part)`. Otherwise
19
+ * `<astl-editor>` opens it in its own inspector.
20
+ */
21
+ export const HELP_OPEN = 'astl-help-open';
22
+
23
+ /** A part of a graded element's help for students: its hints, its explanation, its feedback texts. */
24
+ export type HelpPart = 'hints' | 'explanation' | 'feedback';
25
+
26
+ /** What `astl-help-open` asks for. */
27
+ export interface HelpOpen {
28
+ readonly element: string;
29
+ readonly part: HelpPart;
30
+ }
31
+
15
32
  let registry: CustomElementRegistry | null = null;
16
33
  let loading: Promise<void> | null = null;
17
34