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