@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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # @astl/editor
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - f86ccf1: "Add option" in Select words works: it ends the exercise's last line with a new marked word, selected, so what the author types replaces it and stays a word to select (one undo step). It did nothing before, because the words are marks in the text and the button looked for a node.
8
+
9
+ The editor's authoring controls are compact, so an exercise looks as students see it: on the surface the page heads' buttons ("Add title", "Check this page"), "Add option" and "Add prompt", a frame's "In the text", a page box's select and cross, the tags and a picture's buttons; in the card (`<astl-inspector>`, its help form and picture fields) its buttons. They are small ghost buttons (no fill or line at rest, muted ink, a subtle fill on hover, the focus ring), 1.75rem high with 0.8125rem text and 0.875rem icons, sized by `--astl-editor-control-size`, `--astl-editor-control-font-size` and `--astl-editor-control-icon-size` on `<astl-editor>`; a target never goes under 24px (WCAG 2.5.8).
10
+
11
+ `@astl/astrolabe/editor` exports what a host of `<astl-editor>` needs for the help for students, as `@astl/editor` does: `HELP_OPEN` with the types `HelpOpen` and `HelpPart`, `loadHelpForm`, `EDITOR_NAMESPACE`, and the types `AstlInspector`, `AstlIssues` and `AstlHelpForm` for a host that places those elements itself. A test keeps the two entries in step.
12
+ - @astl/core@0.2.2
13
+ - @astl/engine@0.2.2
14
+ - @astl/player@0.2.2
15
+ - @astl/tokens@0.2.2
16
+
17
+ ## 0.2.1
18
+
19
+ ### Patch Changes
20
+
21
+ - 8ce6118: The editor shows an exercise closer to the students' view: a question's help for students is no longer drawn on the surface. Its frame's tab says what it has with tags ("2 hints", "Explanation", "Feedback"), and a tag puts the cursor in the question and opens that help in the card, focused (`astl-help-open`, cancelable, `detail: { element, part }`; `AstlInspector.openHelp(part)`). The source keeps the help as the question's children, so a copy or an undo keeps it.
22
+
23
+ The card's "Help for students" is a disclosure, closed for each element until opened, that says what the element has ("2 hints · explanation"). Its buttons to order and remove hints, and to remove the explanation or a feedback text, are icons named by their task. "Add a hint" (in the card or "Elements") adds a draft that reaches the source with its first text, and the menu's "Explanation" and "Feedback" open their field, so adding help no longer raises `HELP_EMPTY`. Parts: `help-toggle`, `help-summary`, `help-body`, `help-add`, `help-move`, `help-remove` (on `<astl-editor>` as `inspector-help-*`).
24
+
25
+ Help on the page stays a box, more compact, with a cross to remove it; an empty one has a line that says what to write there.
26
+ - @astl/core@0.2.1
27
+ - @astl/engine@0.2.1
28
+ - @astl/player@0.2.1
29
+ - @astl/tokens@0.2.1
30
+
3
31
  ## 0.2.0
4
32
 
5
33
  ### Minor Changes
@@ -1,18 +1,30 @@
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
- import { type EditorAdapter } from '@astl/core';
10
+ import { type EditorAdapter, type FeedbackVariant } from '@astl/core';
8
11
  import { type CSSResultGroup, nothing, type PropertyDeclarations, type PropertyValues, type TemplateResult } from 'lit';
9
12
  import type { Authoring } from '../authoring.js';
10
13
  import { AuthoringElement } from './base.js';
14
+ import type { HelpPart } from './style-load.js';
11
15
  /** What the insert menu adds: a hint, the explanation, or the next feedback text the element lacks. */
12
16
  export type HelpKind = 'hint' | 'explanation' | 'feedback';
17
+ /** Where a form opens: a part, a new hint (a draft), or one feedback text. */
18
+ type HelpPlace = HelpPart | 'hint-new' | `feedback-${FeedbackVariant}`;
13
19
  /**
14
- * Adds help to the graded element at the cursor (and asks the form to focus it), or else to the page at the cursor.
15
- * Returns the HTML inserted on a page, or null when the element got it.
20
+ * Opens the help for students of `element` at `place` in the form that shows it (or will): the section opens, a new
21
+ * hint shows as a draft, and the place's first field takes the focus.
22
+ */
23
+ export declare function openHelp(adapter: EditorAdapter, element: string, place: HelpPlace): void;
24
+ /**
25
+ * Adds help to the graded element at the cursor: its form opens at a new hint (a draft until it has text), at its
26
+ * explanation or at the next feedback text it lacks, writing nothing yet; or else puts the help on the page at the
27
+ * cursor. Returns the HTML inserted on a page, or null when the element's form opened.
16
28
  */
17
29
  export declare function addHelp(authoring: Authoring, adapter: EditorAdapter, kind: HelpKind): string | null;
18
30
  export declare class AstlHelpForm extends AuthoringElement {
@@ -24,8 +36,11 @@ export declare class AstlHelpForm extends AuthoringElement {
24
36
  /** The element sits in a line of text: its help holds text, not paragraphs. */
25
37
  inline: boolean;
26
38
  constructor();
39
+ connectedCallback(): void;
40
+ disconnectedCallback(): void;
27
41
  protected willUpdate(changed: PropertyValues<this>): void;
28
42
  protected updated(): void;
29
43
  protected render(): TemplateResult | typeof nothing;
30
44
  }
45
+ export {};
31
46
  //# sourceMappingURL=help-form.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"help-form.d.ts","sourceRoot":"","sources":["../../src/elements/help-form.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,KAAK,aAAa,EAAgE,MAAM,YAAY,CAAC;AAC9G,OAAO,EACL,KAAK,cAAc,EAGnB,OAAO,EACP,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,cAAc,EACpB,MAAM,KAAK,CAAC;AAEb,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAMjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAQ7C,uGAAuG;AACvG,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,aAAa,GAAG,UAAU,CAAC;AAO3D;;;GAGG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CA4BnG;AAgBD,qBAAa,YAAa,SAAQ,gBAAgB;;IAChD,OAAgB,UAAU,EAAE,oBAAoB,CAI9C;IAEF,OAAgB,MAAM,EAAE,cAAc,CAoFpC;IAEF,mDAAmD;IAC3C,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,+EAA+E;IACvE,MAAM,EAAE,OAAO,CAAC;IAQxB,cAIC;IAED,UAAmB,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC,GAAG,IAAI,CAIjE;IAED,UAAmB,OAAO,IAAI,IAAI,CAMjC;IAkOD,UAAmB,MAAM,IAAI,cAAc,GAAG,OAAO,OAAO,CAmD3D;CACF"}
1
+ {"version":3,"file":"help-form.d.ts","sourceRoot":"","sources":["../../src/elements/help-form.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,KAAK,aAAa,EAAqB,KAAK,eAAe,EAAuB,MAAM,YAAY,CAAC;AAC9G,OAAO,EACL,KAAK,cAAc,EAGnB,OAAO,EACP,KAAK,oBAAoB,EACzB,KAAK,cAAc,EAGnB,KAAK,cAAc,EACpB,MAAM,KAAK,CAAC;AAEb,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAMjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAQhD,uGAAuG;AACvG,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,aAAa,GAAG,UAAU,CAAC;AAE3D,8EAA8E;AAC9E,KAAK,SAAS,GAAG,QAAQ,GAAG,UAAU,GAAG,YAAY,eAAe,EAAE,CAAC;AASvE;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAGxF;AAID;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CAmBnG;AA2BD,qBAAa,YAAa,SAAQ,gBAAgB;;IAChD,OAAgB,UAAU,EAAE,oBAAoB,CAI9C;IAEF,OAAgB,MAAM,EAAE,cAAc,CA4IpC;IAEF,mDAAmD;IAC3C,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,+EAA+E;IACvE,MAAM,EAAE,OAAO,CAAC;IAcxB,cAIC;IAEQ,iBAAiB,IAAI,IAAI,CAGjC;IAEQ,oBAAoB,IAAI,IAAI,CAGpC;IAED,UAAmB,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC,GAAG,IAAI,CASjE;IAED,UAAmB,OAAO,IAAI,IAAI,CAMjC;IAkWD,UAAmB,MAAM,IAAI,cAAc,GAAG,OAAO,OAAO,CAqB3D;CACF"}
@@ -1,11 +1,14 @@
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' };
@@ -13,17 +16,30 @@ import pl from '../messages/help/pl.json' with { type: 'json' };
13
16
  import { EDITOR_NAMESPACE } from '../messages.js';
14
17
  import { elementTree } from '../scan.js';
15
18
  import { AuthoringElement } from './base.js';
16
- import { kitStyles } from './styles.js';
19
+ import { compactButtons, kitStyles } from './styles.js';
17
20
  const helpMessages = [
18
21
  { namespace: EDITOR_NAMESPACE, locale: 'en', messages: en },
19
22
  { namespace: EDITOR_NAMESPACE, locale: 'pl', messages: pl },
20
23
  ];
21
- /** 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. */
22
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
+ }
23
38
  const graded = (authoring, tag) => authoring.descriptor(tag)?.manifest.graded === true;
24
39
  /**
25
- * Adds help to the graded element at the cursor (and asks the form to focus it), or else to the page at the cursor.
26
- * 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.
27
43
  */
28
44
  export function addHelp(authoring, adapter, kind) {
29
45
  const selection = adapter.getSelection();
@@ -36,23 +52,8 @@ export function addHelp(authoring, adapter, kind) {
36
52
  return block;
37
53
  }
38
54
  const help = readHelp(current);
39
- let next;
40
- let field;
41
- if (kind === 'hint') {
42
- next = { ...help, hints: [...help.hints, { html: '', cost: null }] };
43
- field = `hint-${help.hints.length}`;
44
- }
45
- else if (kind === 'explanation') {
46
- next = { ...help, explanation: help.explanation ?? '' };
47
- field = 'explanation';
48
- }
49
- else {
50
- const when = FEEDBACK_ORDER.find((w) => help.feedback[w] === undefined) ?? 'correct';
51
- next = { ...help, feedback: { ...help.feedback, [when]: help.feedback[when] ?? '' } };
52
- field = `feedback-${when}`;
53
- }
54
- pending = { element: selection.id, field };
55
- 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}`);
56
57
  return null;
57
58
  }
58
59
  /** The order the menu fills feedback texts in: right and wrong first. */
@@ -64,6 +65,14 @@ const costOf = (cost) => cost ?? '0';
64
65
  /** Rounds points for display, to at most 2 decimals. */
65
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,
@@ -72,6 +81,7 @@ export class AstlHelpForm extends AuthoringElement {
72
81
  };
73
82
  static styles = [
74
83
  kitStyles,
84
+ compactButtons,
75
85
  css `
76
86
  :host {
77
87
  display: block;
@@ -80,13 +90,47 @@ export class AstlHelpForm extends AuthoringElement {
80
90
  display: flex;
81
91
  flex-direction: column;
82
92
  gap: var(--astl-space-3);
83
- padding-block-start: var(--astl-space-3);
93
+ padding-block: var(--astl-space-2);
94
+ padding-inline: var(--astl-space-3);
84
95
  border-block-start: var(--astl-outline-width) solid var(--astl-line);
85
96
  }
86
97
  h3 {
87
98
  margin: 0;
88
99
  font-size: 1rem;
89
100
  }
101
+ .toggle {
102
+ inline-size: 100%;
103
+ flex-wrap: wrap;
104
+ gap: 0 var(--astl-space-2);
105
+ padding-inline: var(--astl-space-1);
106
+ border-color: transparent;
107
+ background: transparent;
108
+ color: var(--astl-ink);
109
+ font-size: 1rem;
110
+ font-weight: 700;
111
+ }
112
+ .toggle .glyph {
113
+ flex: none;
114
+ transition: rotate 0.15s;
115
+ }
116
+ .toggle[aria-expanded='true'] .glyph {
117
+ rotate: 90deg;
118
+ }
119
+ .toggle:dir(rtl) .glyph {
120
+ scale: -1 1;
121
+ }
122
+ .toggle:dir(rtl)[aria-expanded='true'] .glyph {
123
+ rotate: -90deg;
124
+ }
125
+ .summary {
126
+ color: var(--astl-ink-muted);
127
+ font-weight: 400;
128
+ }
129
+ .body {
130
+ display: flex;
131
+ flex-direction: column;
132
+ gap: var(--astl-space-3);
133
+ }
90
134
  fieldset,
91
135
  .field {
92
136
  display: flex;
@@ -105,7 +149,7 @@ export class AstlHelpForm extends AuthoringElement {
105
149
  ol {
106
150
  display: flex;
107
151
  flex-direction: column;
108
- gap: var(--astl-space-3);
152
+ gap: var(--astl-space-2);
109
153
  margin: 0;
110
154
  padding: 0;
111
155
  list-style: none;
@@ -121,16 +165,37 @@ export class AstlHelpForm extends AuthoringElement {
121
165
  li[data-over] {
122
166
  border-color: var(--astl-accent);
123
167
  }
168
+ li[data-draft] {
169
+ border-style: dashed;
170
+ }
124
171
  .row {
125
172
  display: flex;
126
173
  flex-wrap: wrap;
127
174
  align-items: center;
128
- gap: var(--astl-space-2);
175
+ gap: var(--astl-space-1) var(--astl-space-2);
176
+ }
177
+ .tools {
178
+ display: flex;
179
+ gap: var(--astl-space-1);
180
+ margin-inline-start: auto;
129
181
  }
130
182
  .handle {
131
183
  cursor: grab;
132
184
  user-select: none;
133
185
  }
186
+ .icon {
187
+ justify-content: center;
188
+ padding: 0;
189
+ }
190
+ .glyph {
191
+ inline-size: 1.25rem;
192
+ block-size: 1.25rem;
193
+ fill: none;
194
+ stroke: currentColor;
195
+ stroke-width: 1.8;
196
+ stroke-linecap: round;
197
+ stroke-linejoin: round;
198
+ }
134
199
  .cost {
135
200
  display: flex;
136
201
  align-items: center;
@@ -161,27 +226,67 @@ export class AstlHelpForm extends AuthoringElement {
161
226
  #points = null;
162
227
  #counting = null;
163
228
  #dragged = null;
229
+ /** Whether the section is open: closed for each element until it is opened. */
230
+ #open = false;
231
+ /** A hint added and not yet written, with the cost picked for it: it goes into the source with its first text. */
232
+ #draft = null;
233
+ /** The field to focus once the form shows it. */
234
+ #focus = null;
164
235
  constructor() {
165
236
  super();
166
237
  this.element = null;
167
238
  this.inline = false;
168
239
  }
240
+ connectedCallback() {
241
+ super.connectedCallback();
242
+ forms.add(this);
243
+ }
244
+ disconnectedCallback() {
245
+ super.disconnectedCallback();
246
+ forms.delete(this);
247
+ }
169
248
  willUpdate(changed) {
170
249
  super.willUpdate(changed);
171
250
  if (changed.has('authoring'))
172
251
  this.authoring?.addMessages(helpMessages);
173
- if (changed.has('element'))
252
+ if (changed.has('element')) {
174
253
  this.#drafts.clear();
254
+ this.#open = false;
255
+ this.#draft = null;
256
+ }
257
+ this.#take();
175
258
  }
176
259
  updated() {
177
- if (pending === null || pending.element !== this.element)
260
+ if (this.#focus === null)
178
261
  return;
179
- const field = this.renderRoot.querySelector(`[data-help="${pending.field}"]`);
262
+ const field = this.renderRoot.querySelector(`[data-help="${this.#focus}"]`);
180
263
  if (field === null)
181
264
  return;
182
- pending = null;
265
+ this.#focus = null;
183
266
  field.focus();
184
267
  }
268
+ /** Takes a request to open this form: the section opens at its place, a new hint as a draft. */
269
+ #take() {
270
+ if (pending === null || pending.adapter !== this.adapter || pending.element !== this.element)
271
+ return;
272
+ const { place } = pending;
273
+ const current = this.#help();
274
+ if (current === null)
275
+ return;
276
+ pending = null;
277
+ const { help } = current;
278
+ this.#open = true;
279
+ if (place === 'hint-new')
280
+ this.#draft ??= { cost: null };
281
+ if (place === 'hints')
282
+ this.#focus = help.hints.length > 0 ? 'hint-0' : 'add-hint';
283
+ else if (place === 'hint-new')
284
+ this.#focus = `hint-${help.hints.length}`;
285
+ else if (place === 'feedback')
286
+ this.#focus = `feedback-${FEEDBACK_VARIANTS.find((w) => help.feedback[w] !== undefined) ?? 'correct'}`;
287
+ else
288
+ this.#focus = place;
289
+ }
185
290
  #help() {
186
291
  const html = this.element === null ? null : (this.adapter?.getElement(this.element) ?? null);
187
292
  return html === null ? null : { html, help: readHelp(html) };
@@ -228,6 +333,15 @@ export class AstlHelpForm extends AuthoringElement {
228
333
  };
229
334
  });
230
335
  }
336
+ /** What the element has, as the closed section says it: "2 hints · explanation · feedback", or "none". */
337
+ #summary(help) {
338
+ const parts = [
339
+ ...(help.hints.length > 0 ? [this.t('surface.helpHints', { n: help.hints.length })] : []),
340
+ ...(help.explanation !== null ? [this.t('help.summaryExplanation')] : []),
341
+ ...(Object.keys(help.feedback).length > 0 ? [this.t('help.summaryFeedback')] : []),
342
+ ];
343
+ return parts.length === 0 ? this.t('help.summaryNone') : parts.join(' · ');
344
+ }
231
345
  #write(change) {
232
346
  const current = this.#help();
233
347
  if (current === null || this.element === null || this.adapter === null)
@@ -236,6 +350,11 @@ export class AstlHelpForm extends AuthoringElement {
236
350
  if (next !== current.html)
237
351
  this.adapter.replaceElement(this.element, next);
238
352
  }
353
+ /** Focuses a field once the form shows it again. */
354
+ #refocus(field) {
355
+ this.#focus = field;
356
+ this.requestUpdate();
357
+ }
239
358
  /** A text field's value: as typed while it is being edited, else what the source says. */
240
359
  #text(field, html) {
241
360
  return this.#drafts.get(field) ?? helpText(html);
@@ -267,32 +386,63 @@ export class AstlHelpForm extends AuthoringElement {
267
386
  });
268
387
  this.#drafts.clear();
269
388
  }
389
+ /** "Add a hint": a draft row (one at a time), focused. */
390
+ #addHint(help) {
391
+ this.#draft ??= { cost: null };
392
+ this.#refocus(`hint-${help.hints.length}`);
393
+ }
394
+ /** The draft goes without a trace: left empty, or removed. */
395
+ #dropDraft(field) {
396
+ this.#draft = null;
397
+ this.#drafts.delete(field);
398
+ this.requestUpdate();
399
+ }
400
+ /** A small button with an icon, named and with a tooltip by its text. */
401
+ #iconButton(name, label, off, run) {
402
+ return html `<button type="button" class=${name === 'remove' ? 'icon delete' : 'icon'}
403
+ part=${name === 'remove' ? 'help-remove' : 'help-move'} aria-label=${label} title=${label}
404
+ aria-disabled=${off ? 'true' : nothing}
405
+ @click=${() => {
406
+ if (!off)
407
+ run();
408
+ }}>${icon(name)}</button>`;
409
+ }
270
410
  #hint(help, index, points) {
271
- const hint = help.hints[index];
411
+ const draft = index === help.hints.length;
412
+ const hint = draft
413
+ ? { html: '', cost: this.#draft?.cost ?? null }
414
+ : help.hints[index];
272
415
  const n = index + 1;
273
416
  const field = `hint-${index}`;
274
417
  const last = index === help.hints.length - 1;
275
418
  const formatted = this.#formatted(field, hint.html);
276
419
  const over = (event, on) => {
277
- if (this.#dragged === null)
420
+ if (this.#dragged === null || draft)
278
421
  return;
279
422
  event.preventDefault();
280
423
  event.currentTarget.toggleAttribute('data-over', on);
281
424
  };
282
425
  return html `<li
426
+ ?data-draft=${draft}
283
427
  @dragover=${(e) => over(e, true)}
284
428
  @dragleave=${(e) => over(e, false)}
285
429
  @drop=${(e) => {
286
430
  over(e, false);
287
- if (this.#dragged !== null)
431
+ if (this.#dragged !== null && !draft)
288
432
  this.#move(this.#dragged, index);
289
433
  this.#dragged = null;
290
434
  }}
435
+ @focusout=${(e) => {
436
+ // A draft left empty goes: the focus is outside its row, and it has no text yet.
437
+ const row = e.currentTarget;
438
+ if (draft && this.#draft !== null && !row.contains(e.relatedTarget))
439
+ this.#dropDraft(field);
440
+ }}
291
441
  >
292
442
  <div class="row">
293
443
  <span
294
444
  class="handle"
295
- draggable="true"
445
+ draggable=${draft ? 'false' : 'true'}
296
446
  aria-hidden="true"
297
447
  title=${this.t('help.drag')}
298
448
  @dragstart=${(e) => {
@@ -305,6 +455,22 @@ export class AstlHelpForm extends AuthoringElement {
305
455
  >⠿</span
306
456
  >
307
457
  <label for=${field}>${this.t('help.hintN', { n })}</label>
458
+ <span class="tools">
459
+ ${draft
460
+ ? nothing
461
+ : html `${this.#iconButton('up', this.t('help.upN', { n }), index === 0, () => this.#move(index, index - 1))}
462
+ ${this.#iconButton('down', this.t('help.downN', { n }), last, () => this.#move(index, index + 1))}`}
463
+ ${this.#iconButton('remove', this.t('help.removeN', { n }), false, () => {
464
+ if (draft) {
465
+ this.#dropDraft(field);
466
+ this.#refocus('add-hint');
467
+ return;
468
+ }
469
+ this.#drafts.clear();
470
+ this.#write((h) => ({ ...h, hints: h.hints.filter((_, i) => i !== index) }));
471
+ this.#refocus(help.hints.length > 1 ? `hint-${Math.max(0, index - 1)}` : 'add-hint');
472
+ })}
473
+ </span>
308
474
  </div>
309
475
  <textarea
310
476
  id=${field}
@@ -314,56 +480,64 @@ export class AstlHelpForm extends AuthoringElement {
314
480
  .value=${live(this.#text(field, hint.html))}
315
481
  @input=${(e) => {
316
482
  const text = this.#typed(field, e);
317
- this.#write((h) => ({
318
- ...h,
319
- hints: h.hints.map((x, i) => (i === index ? { ...x, html: text } : x)),
320
- }));
483
+ if (!draft) {
484
+ this.#write((h) => ({
485
+ ...h,
486
+ hints: h.hints.map((x, i) => (i === index ? { ...x, html: text } : x)),
487
+ }));
488
+ return;
489
+ }
490
+ // The draft's first text writes it, with the cost picked for it.
491
+ if (inputValue(e).trim() === '')
492
+ return;
493
+ const cost = this.#draft?.cost ?? null;
494
+ this.#draft = null;
495
+ this.#write((h) => ({ ...h, hints: [...h.hints, { html: text, cost }] }));
321
496
  }}
322
497
  @change=${() => this.#done(field)}
323
498
  ></textarea>
324
499
  ${formatted ?? nothing}
325
- <div class="row">
326
- <label class="cost">
327
- ${this.t('help.cost')}
328
- <select
329
- aria-label=${this.t('help.costN', { n })}
330
- data-help=${`cost-${index}`}
331
- @change=${(e) => {
500
+ <label class="cost">
501
+ ${this.t('help.cost')}
502
+ <select
503
+ aria-label=${this.t('help.costN', { n })}
504
+ data-help=${`cost-${index}`}
505
+ @change=${(e) => {
332
506
  const value = inputValue(e);
333
507
  const cost = value === '0' ? null : value;
508
+ if (draft) {
509
+ this.#draft = { cost };
510
+ return;
511
+ }
334
512
  this.#write((h) => ({
335
513
  ...h,
336
514
  hints: h.hints.map((x, i) => (i === index ? { ...x, cost } : x)),
337
515
  }));
338
516
  }}
339
- >
340
- ${this.#costs(hint.cost, points).map(({ value, label }) => html `<option value=${value} ?selected=${value === costOf(hint.cost)}>${label}</option>`)}
341
- </select>
342
- </label>
343
- <button type="button" aria-label=${this.t('help.upN', { n })} aria-disabled=${String(index === 0)}
344
- @click=${() => this.#move(index, index - 1)}>${this.t('help.up')}</button>
345
- <button type="button" aria-label=${this.t('help.downN', { n })} aria-disabled=${String(last)}
346
- @click=${() => this.#move(index, index + 1)}>${this.t('help.down')}</button>
347
- <button type="button" class="delete" aria-label=${this.t('help.removeN', { n })}
348
- @click=${() => {
349
- this.#drafts.clear();
350
- this.#write((h) => ({ ...h, hints: h.hints.filter((_, i) => i !== index) }));
351
- pending = {
352
- element: this.element ?? '',
353
- field: help.hints.length > 1 ? `hint-${Math.max(0, index - 1)}` : 'add-hint',
354
- };
355
- }}>${this.t('inspector.remove')}</button>
356
- </div>
517
+ >
518
+ ${this.#costs(hint.cost, points).map(({ value, label }) => html `<option value=${value} ?selected=${value === costOf(hint.cost)}>${label}</option>`)}
519
+ </select>
520
+ </label>
357
521
  </li>`;
358
522
  }
359
523
  /**
360
- * A text field of the help: the explanation or a feedback text. While the element has it, "Remove" takes it away:
361
- * one the menu added stays empty, so clearing the field would not.
524
+ * A text field of the help: the explanation or a feedback text. An empty field is none; while the element has it,
525
+ * "Remove" beside its label takes it away.
362
526
  */
363
527
  #area(field, label, remove, value, set) {
364
528
  const formatted = this.#formatted(field, value ?? '');
365
529
  return html `<div class="field">
366
- <label for=${field}>${label}</label>
530
+ <div class="row">
531
+ <label for=${field}>${label}</label>
532
+ ${value === null || value === undefined
533
+ ? nothing
534
+ : html `<span class="tools">${this.#iconButton('remove', remove, false, () => {
535
+ this.#drafts.delete(field);
536
+ // The field stays, empty: the focus goes back to it.
537
+ set(null);
538
+ this.#refocus(field);
539
+ })}</span>`}
540
+ </div>
367
541
  <textarea
368
542
  id=${field}
369
543
  data-help=${field}
@@ -377,39 +551,22 @@ export class AstlHelpForm extends AuthoringElement {
377
551
  @change=${() => this.#done(field)}
378
552
  ></textarea>
379
553
  ${formatted ?? nothing}
380
- ${value === null || value === undefined
381
- ? nothing
382
- : html `<div class="row">
383
- <button type="button" class="delete" aria-label=${remove}
384
- @click=${() => {
385
- this.#drafts.delete(field);
386
- // The field stays, empty: the focus goes back to it.
387
- pending = { element: this.element ?? '', field };
388
- set(null);
389
- }}>${this.t('inspector.remove')}</button>
390
- </div>`}
391
554
  </div>`;
392
555
  }
393
- render() {
394
- const current = this.#help();
395
- if (current === null || this.authoring === null)
396
- return nothing;
397
- const { help } = current;
398
- const { lang, dir } = this.ui;
399
- return html `<section class="kit" part="help" lang=${lang} dir=${dir} aria-labelledby="help-title">
400
- <h3 id="help-title">${this.t('help.title')}</h3>
556
+ #body(source, help) {
557
+ const rows = help.hints.length + (this.#draft === null ? 0 : 1);
558
+ const points = this.#pointsOf(source);
559
+ return html `<div class="body" id="help-body" part="help-body">
401
560
  <fieldset aria-describedby="hints-note">
402
561
  <legend>${this.t('help.hints')}</legend>
403
562
  <p class="note" id="hints-note">${this.t('help.hintsNote')}</p>
404
- ${help.hints.length === 0
563
+ ${rows === 0
405
564
  ? nothing
406
- : html `<ol>${help.hints.map((_, i) => this.#hint(help, i, this.#pointsOf(current.html)))}</ol>`}
565
+ : html `<ol>${Array.from({ length: rows }, (_, i) => this.#hint(help, i, points))}</ol>`}
407
566
  <div class="row">
408
- <button type="button" data-help="add-hint"
409
- @click=${() => {
410
- pending = { element: this.element ?? '', field: `hint-${help.hints.length}` };
411
- this.#write((h) => ({ ...h, hints: [...h.hints, { html: '', cost: null }] }));
412
- }}>${this.t('help.addHint')}</button>
567
+ <button type="button" data-help="add-hint" part="help-add" @click=${() => this.#addHint(help)}>
568
+ ${this.t('help.addHint')}
569
+ </button>
413
570
  </div>
414
571
  </fieldset>
415
572
  ${this.#area('explanation', this.t('insert.explanation'), this.t('help.removeExplanation'), help.explanation, (text) => this.#write((h) => ({ ...h, explanation: text })))}
@@ -426,6 +583,30 @@ export class AstlHelpForm extends AuthoringElement {
426
583
  return { ...h, feedback };
427
584
  })))}
428
585
  </fieldset>
586
+ </div>`;
587
+ }
588
+ render() {
589
+ const current = this.#help();
590
+ if (current === null || this.authoring === null)
591
+ return nothing;
592
+ const { help } = current;
593
+ const { lang, dir } = this.ui;
594
+ const open = this.#open;
595
+ return html `<section class="kit" part="help" lang=${lang} dir=${dir} aria-labelledby="help-title">
596
+ <h3>
597
+ <button type="button" class="toggle" part="help-toggle" aria-expanded=${String(open)}
598
+ aria-controls=${open ? 'help-body' : nothing}
599
+ @click=${() => {
600
+ this.#open = !open;
601
+ if (open)
602
+ this.#draft = null;
603
+ this.requestUpdate();
604
+ }}>
605
+ ${icon('chevron')}<span id="help-title">${this.t('help.title')}</span>
606
+ ${open ? nothing : html `<span class="summary" part="help-summary">${this.#summary(help)}</span>`}
607
+ </button>
608
+ </h3>
609
+ ${open ? this.#body(current.html, help) : nothing}
429
610
  </section>`;
430
611
  }
431
612
  }