@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
@@ -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) {
@@ -924,17 +941,17 @@ export class AstlInspector extends AuthoringElement {
924
941
  }`;
925
942
  } else {
926
943
  // The heading says the type; the id is beside it in a chip, outside the heading.
927
- // A graded element's help for students: a form of its own, loaded on first use.
944
+ // A graded element's help for students heads the card, before its fields: a form of its own, loaded on first use.
928
945
  const manifest = authoring.descriptor(model.tag)?.manifest;
929
946
  if (manifest?.graded === true) void loadHelpForm().catch(() => {});
930
947
  body = html`${this.#head(model)}${this.#issues(model)}
931
- <div class="fields" data-tag=${model.tag}>${model.fields.map((f, i) => this.#field(f, i))}</div>
932
948
  ${
933
949
  manifest?.graded === true
934
950
  ? html`<astl-help-form .authoring=${authoring} .adapter=${adapter} .element=${this.#shown}
935
- ?inline=${manifest.display === 'inline'}></astl-help-form>`
951
+ ?inline=${manifest.display === 'inline'} exportparts=${HELP_PARTS}></astl-help-form>`
936
952
  : nothing
937
- }`;
953
+ }
954
+ <div class="fields" data-tag=${model.tag}>${model.fields.map((f, i) => this.#field(f, i))}</div>`;
938
955
  }
939
956
  return html`<section class="kit" part="root" lang=${lang} dir=${dir} aria-labelledby="title">${body}${this.#styleSection()}</section>`;
940
957
  }
@@ -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
 
package/src/index.ts CHANGED
@@ -28,8 +28,8 @@ export type { FieldChange } from './elements/picture-field.js';
28
28
  export { AstlPictureField, FIELD_CHANGE } from './elements/picture-field.js';
29
29
  export { AstlPreview } from './elements/preview.js';
30
30
  export type { AstlStyleForm } from './elements/style-form.js';
31
- export type { StyleOpen } from './elements/style-load.js';
32
- export { loadHelpForm, loadStyleElements, STYLE_OPEN } from './elements/style-load.js';
31
+ export type { HelpOpen, HelpPart, StyleOpen } from './elements/style-load.js';
32
+ export { HELP_OPEN, loadHelpForm, loadStyleElements, STYLE_OPEN } from './elements/style-load.js';
33
33
  export type { AstlStyleRules } from './elements/style-rules.js';
34
34
  export type { InsertMenu, ToolbarView } from './elements/toolbar.js';
35
35
  export { AstlToolbar, FEATURED_ITEMS, VIEW_PICKED } from './elements/toolbar.js';
@@ -245,12 +245,28 @@
245
245
  "surface.picture.place": "Position and size: {position}, {width}",
246
246
  "surface.picture.placeAuto": "Position and size: {position}",
247
247
  "style.target.more": "{target}…",
248
- "help.cost": "costs {percent}%",
249
248
  "help.when.correct": "Correct",
250
249
  "help.when.incorrect": "Wrong",
251
250
  "help.when.partial": "Partly correct",
252
251
  "help.when.omitted": "Skipped",
253
252
  "help.costLabel": "Cost of the hint",
254
253
  "help.whenLabel": "For the result",
255
- "surface.helpRemove": "{kind, select, hint {Remove the hint} explanation {Remove the explanation} other {Remove the feedback}}"
254
+ "surface.helpRemove": "{kind, select, hint {Remove the hint} explanation {Remove the explanation} other {Remove the feedback}}",
255
+ "surface.helpHints": "{n, plural, one {# hint} other {# hints}}",
256
+ "surface.helpOpen": "Open in the card",
257
+ "surface.helpPlaceholder": "{kind, select, hint {Write the hint...} explanation {Write the explanation...} other {Write the feedback...}}",
258
+ "surface.pageCheck": "Check this page",
259
+ "surface.pageCheckOn": "Page {page} has a Check of its own now.",
260
+ "surface.pageCheckOff": "Page {page} has no Check of its own now.",
261
+ "help.costFree": "Free",
262
+ "surface.hintPageCost": "Costs {percent}%: a page's hints are free now.",
263
+ "surface.hintMakeFree": "Make it free",
264
+ "surface.moveTo": "Move to question...",
265
+ "surface.moveLabel": "Move the {kind, select, hint {hint} other {explanation}} into a question",
266
+ "surface.moveTarget": "{name} ({id})",
267
+ "surface.moveTargetFull": "{name}: has an explanation",
268
+ "surface.moved": "Moved into {name}.",
269
+ "surface.moveNone": "No question on this page",
270
+ "surface.pagesCheckOn": "Every page has a Check of its own now.",
271
+ "surface.pagesCheckOff": "No page has a Check of its own now."
256
272
  }
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "help.title": "Help for students",
3
+ "help.summaryNone": "none",
4
+ "help.summaryExplanation": "explanation",
5
+ "help.summaryFeedback": "feedback",
3
6
  "help.hints": "Hints",
4
7
  "help.hintsNote": "Students ask for them one at a time, in this order. A cost is the share of the question's points a student loses for using that hint.",
5
8
  "help.hintN": "Hint {n}",
6
- "help.cost": "Cost (% of the points)",
7
- "help.costN": "Hint {n}: cost (% of the points)",
9
+ "help.cost": "Cost",
10
+ "help.costN": "Hint {n}: cost",
11
+ "help.costShare": "-{percent}% of this question's points",
12
+ "help.costPoints": "-{percent}%: -{points} of {max, plural, one {# point} other {# points}}",
8
13
  "help.addHint": "Add a hint",
9
- "help.up": "Up",
10
- "help.down": "Down",
11
14
  "help.upN": "Move hint {n} up",
12
15
  "help.downN": "Move hint {n} down",
13
16
  "help.removeN": "Remove hint {n}",
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "help.title": "Pomoc dla ucznia",
3
+ "help.summaryNone": "brak",
4
+ "help.summaryExplanation": "wyjaśnienie",
5
+ "help.summaryFeedback": "informacja zwrotna",
3
6
  "help.hints": "Podpowiedzi",
4
7
  "help.hintsNote": "Uczeń prosi o nie po kolei, w tej kolejności. Koszt to część punktów za to pytanie, którą uczeń traci, gdy użyje podpowiedzi.",
5
8
  "help.hintN": "Podpowiedź {n}",
6
- "help.cost": "Koszt (% punktów)",
7
- "help.costN": "Podpowiedź {n}: koszt (% punktów)",
9
+ "help.cost": "Koszt",
10
+ "help.costN": "Podpowiedź {n}: koszt",
11
+ "help.costShare": "-{percent}% punktów za to pytanie",
12
+ "help.costPoints": "-{percent}%: -{points} z {max, plural, one {# punktu} few {# punktów} many {# punktów} other {# punktu}}",
8
13
  "help.addHint": "Dodaj podpowiedź",
9
- "help.up": "W górę",
10
- "help.down": "W dół",
11
14
  "help.upN": "Przenieś podpowiedź {n} w górę",
12
15
  "help.downN": "Przenieś podpowiedź {n} w dół",
13
16
  "help.removeN": "Usuń podpowiedź {n}",
@@ -245,12 +245,28 @@
245
245
  "surface.picture.place": "Położenie i rozmiar: {position}, {width}",
246
246
  "surface.picture.placeAuto": "Położenie i rozmiar: {position}",
247
247
  "style.target.more": "{target}…",
248
- "help.cost": "koszt {percent}%",
249
248
  "help.when.correct": "Dobrze",
250
249
  "help.when.incorrect": "Źle",
251
250
  "help.when.partial": "Częściowo dobrze",
252
251
  "help.when.omitted": "Pominięte",
253
252
  "help.costLabel": "Koszt podpowiedzi",
254
253
  "help.whenLabel": "Dla wyniku",
255
- "surface.helpRemove": "{kind, select, hint {Usuń podpowiedź} explanation {Usuń wyjaśnienie} other {Usuń informację zwrotną}}"
254
+ "surface.helpRemove": "{kind, select, hint {Usuń podpowiedź} explanation {Usuń wyjaśnienie} other {Usuń informację zwrotną}}",
255
+ "surface.helpHints": "{n, plural, one {# podpowiedź} few {# podpowiedzi} many {# podpowiedzi} other {# podpowiedzi}}",
256
+ "surface.helpOpen": "Otwórz w karcie",
257
+ "surface.helpPlaceholder": "{kind, select, hint {Napisz podpowiedź...} explanation {Napisz wyjaśnienie...} other {Napisz informację zwrotną...}}",
258
+ "surface.pageCheck": "Sprawdź tę stronę",
259
+ "surface.pageCheckOn": "Strona {page} ma teraz własne sprawdzanie.",
260
+ "surface.pageCheckOff": "Strona {page} nie ma już własnego sprawdzania.",
261
+ "help.costFree": "Bez kosztu",
262
+ "surface.hintPageCost": "Kosztuje {percent}%: podpowiedzi strony są teraz bez kosztu.",
263
+ "surface.hintMakeFree": "Ustaw bez kosztu",
264
+ "surface.moveTo": "Przenieś do pytania...",
265
+ "surface.moveLabel": "Przenieś {kind, select, hint {podpowiedź} other {wyjaśnienie}} do pytania",
266
+ "surface.moveTarget": "{name} ({id})",
267
+ "surface.moveTargetFull": "{name}: ma już wyjaśnienie",
268
+ "surface.moved": "Przeniesiono do: {name}.",
269
+ "surface.moveNone": "Na tej stronie nie ma pytań",
270
+ "surface.pagesCheckOn": "Każda strona ma teraz własne sprawdzanie.",
271
+ "surface.pagesCheckOff": "Żadna strona nie ma już własnego sprawdzania."
256
272
  }
package/src/pages.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  /**
2
- * Page breaks, new pages and joins as edits of the source text, for text-based adapters (the textarea adapter's
3
- * `splitPage`, `addPage` and `mergePage`): the rules of `EditorAdapter`, on the element ranges of `scan.ts`, so
4
- * everything around an edit stays as written.
2
+ * Page breaks, new pages, joins and a page's own check as edits of the source text, for text-based adapters (the
3
+ * textarea adapter's `splitPage`, `addPage`, `mergePage` and `setPageCheck`) and for `Authoring.setPageCheck`: the rules
4
+ * of `EditorAdapter`, on the element ranges of `scan.ts`, so everything around an edit stays as written.
5
5
  */
6
- import { CLASS_ATTRIBUTE, CONTENT_ATTRIBUTES, DOC_TAG, PAGE_TAG, STYLE_TAG } from '@astl/core';
6
+ import {
7
+ CLASS_ATTRIBUTE,
8
+ CONTENT_ATTRIBUTES,
9
+ DOC_TAG,
10
+ PAGE_CHECK_ATTRIBUTE,
11
+ PAGE_TAG,
12
+ STYLE_TAG,
13
+ } from '@astl/core';
7
14
  import { attributeValue, type ElementRange, elementTree, tokenize, usedIds } from './scan.js';
8
15
 
9
16
  /** A source after a page edit, and the offset of the caret in it. */
@@ -156,3 +163,52 @@ export function mergePageSource(source: string, id: string): PageEdit | null {
156
163
  const left = source.slice(0, end.start) + source.slice(end.end, start.start);
157
164
  return { source: left + source.slice(start.end), caret: left.length };
158
165
  }
166
+
167
+ /**
168
+ * `source` with `check` set (`check: true`) or cleared on the page `page`, or on every page when `page` is left out
169
+ * (`EditorAdapter.setPageCheck`, `Authoring.setPageCheck`). In a document without pages, setting it makes the content
170
+ * one page with a fresh id (`page-<n>`) and `check`, after the named styles at its head; clearing it there changes
171
+ * nothing, and clearing it on a page keeps the page (a document that became one page stays one). Null when nothing
172
+ * changes: set already, cleared already, or no page with that id. The caret stays where it was, moved with the text.
173
+ */
174
+ export function setPageCheckSource(
175
+ source: string,
176
+ check: boolean,
177
+ caret: number,
178
+ page?: string,
179
+ ): PageEdit | null {
180
+ const doc = documentOf(source);
181
+ if (doc.pages.length === 0) {
182
+ if (!check || page !== undefined || doc.blocks.length === 0) return null;
183
+ const open = `<astl-page id="${freshPage(usedIds(source))}" ${PAGE_CHECK_ATTRIBUTE}>`;
184
+ const close = '</astl-page>';
185
+ const next = `${source.slice(0, doc.start)}${open}${source.slice(doc.start, doc.end)}${close}${source.slice(doc.end)}`;
186
+ return {
187
+ source: next,
188
+ caret: caret <= doc.start ? caret : caret + open.length + (caret > doc.end ? close.length : 0),
189
+ };
190
+ }
191
+ const edits: { readonly start: number; readonly end: number; readonly text: string }[] = [];
192
+ for (const target of doc.pages) {
193
+ if (page !== undefined && attributeValue(target.startTag, 'id') !== page) continue;
194
+ const written = target.startTag.attributes.find((a) => a.name === PAGE_CHECK_ATTRIBUTE);
195
+ if (check && written === undefined) {
196
+ // Last in the start tag, before its `>`.
197
+ const at =
198
+ source.charAt(target.startTag.end - 1) === '>' ? target.startTag.end - 1 : target.startTag.end;
199
+ edits.push({ start: at, end: at, text: ` ${PAGE_CHECK_ATTRIBUTE}` });
200
+ } else if (!check && written !== undefined) {
201
+ let start = written.start;
202
+ while (start > 0 && /\s/.test(source.charAt(start - 1))) start--;
203
+ edits.push({ start, end: written.end, text: '' });
204
+ }
205
+ }
206
+ if (edits.length === 0) return null;
207
+ let next = source;
208
+ let moved = caret;
209
+ for (const e of [...edits].sort((a, b) => b.start - a.start)) {
210
+ next = next.slice(0, e.start) + e.text + next.slice(e.end);
211
+ if (caret >= e.end) moved += e.text.length - (e.end - e.start);
212
+ }
213
+ return { source: next, caret: moved };
214
+ }
package/src/textarea.ts CHANGED
@@ -3,7 +3,13 @@
3
3
  * edits are string splices, so the author's formatting outside the edited element is never touched.
4
4
  */
5
5
  import { DOC_TAG, type EditorAdapter, type EditorSelection, PAGE_TAG } from '@astl/core';
6
- import { addPageSource, mergePageSource, type PageEdit, splitPageSource } from './pages.js';
6
+ import {
7
+ addPageSource,
8
+ mergePageSource,
9
+ type PageEdit,
10
+ setPageCheckSource,
11
+ splitPageSource,
12
+ } from './pages.js';
7
13
  import { type ScannedElement, scanElements, tokenize } from './scan.js';
8
14
 
9
15
  export interface TextareaAdapter extends EditorAdapter {
@@ -13,6 +19,8 @@ export interface TextareaAdapter extends EditorAdapter {
13
19
  addPage(after?: string): string;
14
20
  /** The page `id` joined to the page before it, in the source text (`EditorAdapter.mergePage`). */
15
21
  mergePage(id: string): boolean;
22
+ /** `check` set or cleared on a page or on every page, in the source text (`EditorAdapter.setPageCheck`). */
23
+ setPageCheck(check: boolean, page?: string): boolean;
16
24
  /** Removes the textarea listeners and every subscription. */
17
25
  destroy(): void;
18
26
  }
@@ -165,6 +173,11 @@ export function createTextareaAdapter(textarea: HTMLTextAreaElement): TextareaAd
165
173
  if (edit !== null) apply(edit);
166
174
  return edit !== null;
167
175
  },
176
+ setPageCheck(check, page) {
177
+ const edit = setPageCheckSource(textarea.value, check, textarea.selectionStart, page);
178
+ if (edit !== null) apply(edit);
179
+ return edit !== null;
180
+ },
168
181
  onChange(listener) {
169
182
  changes.add(listener);
170
183
  return () => changes.delete(listener);