@astl/core 0.0.0 → 0.1.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 (139) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/LICENSE +202 -0
  3. package/README.md +38 -0
  4. package/dist/answers.d.ts +29 -0
  5. package/dist/answers.d.ts.map +1 -0
  6. package/dist/answers.js +40 -0
  7. package/dist/answers.js.map +1 -0
  8. package/dist/ast.d.ts +25 -0
  9. package/dist/ast.d.ts.map +1 -0
  10. package/dist/ast.js +27 -0
  11. package/dist/ast.js.map +1 -0
  12. package/dist/authoring.d.ts +451 -0
  13. package/dist/authoring.d.ts.map +1 -0
  14. package/dist/authoring.js +2 -0
  15. package/dist/authoring.js.map +1 -0
  16. package/dist/checker.d.ts +78 -0
  17. package/dist/checker.d.ts.map +1 -0
  18. package/dist/checker.js +10 -0
  19. package/dist/checker.js.map +1 -0
  20. package/dist/compiled.d.ts +94 -0
  21. package/dist/compiled.d.ts.map +1 -0
  22. package/dist/compiled.js +16 -0
  23. package/dist/compiled.js.map +1 -0
  24. package/dist/content.d.ts +72 -0
  25. package/dist/content.d.ts.map +1 -0
  26. package/dist/content.js +128 -0
  27. package/dist/content.js.map +1 -0
  28. package/dist/declarations.d.ts +52 -0
  29. package/dist/declarations.d.ts.map +1 -0
  30. package/dist/declarations.js +358 -0
  31. package/dist/declarations.js.map +1 -0
  32. package/dist/definition.d.ts +106 -0
  33. package/dist/definition.d.ts.map +1 -0
  34. package/dist/definition.js +2 -0
  35. package/dist/definition.js.map +1 -0
  36. package/dist/engine.d.ts +95 -0
  37. package/dist/engine.d.ts.map +1 -0
  38. package/dist/engine.js +2 -0
  39. package/dist/engine.js.map +1 -0
  40. package/dist/format.d.ts +29 -0
  41. package/dist/format.d.ts.map +1 -0
  42. package/dist/format.js +55 -0
  43. package/dist/format.js.map +1 -0
  44. package/dist/help.d.ts +38 -0
  45. package/dist/help.d.ts.map +1 -0
  46. package/dist/help.js +22 -0
  47. package/dist/help.js.map +1 -0
  48. package/dist/i18n/check.d.ts +11 -0
  49. package/dist/i18n/check.d.ts.map +1 -0
  50. package/dist/i18n/check.js +64 -0
  51. package/dist/i18n/check.js.map +1 -0
  52. package/dist/i18n/format.d.ts +40 -0
  53. package/dist/i18n/format.d.ts.map +1 -0
  54. package/dist/i18n/format.js +280 -0
  55. package/dist/i18n/format.js.map +1 -0
  56. package/dist/i18n/i18n.d.ts +14 -0
  57. package/dist/i18n/i18n.d.ts.map +1 -0
  58. package/dist/i18n/i18n.js +82 -0
  59. package/dist/i18n/i18n.js.map +1 -0
  60. package/dist/i18n/types.d.ts +31 -0
  61. package/dist/i18n/types.d.ts.map +1 -0
  62. package/dist/i18n/types.js +7 -0
  63. package/dist/i18n/types.js.map +1 -0
  64. package/dist/index.d.ts +45 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.js +21 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/issues.d.ts +98 -0
  69. package/dist/issues.d.ts.map +1 -0
  70. package/dist/issues.js +74 -0
  71. package/dist/issues.js.map +1 -0
  72. package/dist/json.d.ts +7 -0
  73. package/dist/json.d.ts.map +1 -0
  74. package/dist/json.js +2 -0
  75. package/dist/json.js.map +1 -0
  76. package/dist/manifest.d.ts +135 -0
  77. package/dist/manifest.d.ts.map +1 -0
  78. package/dist/manifest.js +63 -0
  79. package/dist/manifest.js.map +1 -0
  80. package/dist/player.d.ts +129 -0
  81. package/dist/player.d.ts.map +1 -0
  82. package/dist/player.js +18 -0
  83. package/dist/player.js.map +1 -0
  84. package/dist/policy.d.ts +53 -0
  85. package/dist/policy.d.ts.map +1 -0
  86. package/dist/policy.js +55 -0
  87. package/dist/policy.js.map +1 -0
  88. package/dist/protocol.d.ts +54 -0
  89. package/dist/protocol.d.ts.map +1 -0
  90. package/dist/protocol.js +51 -0
  91. package/dist/protocol.js.map +1 -0
  92. package/dist/result.d.ts +44 -0
  93. package/dist/result.d.ts.map +1 -0
  94. package/dist/result.js +5 -0
  95. package/dist/result.js.map +1 -0
  96. package/dist/scoring.d.ts +27 -0
  97. package/dist/scoring.d.ts.map +1 -0
  98. package/dist/scoring.js +6 -0
  99. package/dist/scoring.js.map +1 -0
  100. package/dist/slots.d.ts +47 -0
  101. package/dist/slots.d.ts.map +1 -0
  102. package/dist/slots.js +9 -0
  103. package/dist/slots.js.map +1 -0
  104. package/dist/snapshot.d.ts +23 -0
  105. package/dist/snapshot.d.ts.map +1 -0
  106. package/dist/snapshot.js +22 -0
  107. package/dist/snapshot.js.map +1 -0
  108. package/dist/style.d.ts +51 -0
  109. package/dist/style.d.ts.map +1 -0
  110. package/dist/style.js +36 -0
  111. package/dist/style.js.map +1 -0
  112. package/package.json +52 -4
  113. package/src/answers.ts +52 -0
  114. package/src/ast.ts +49 -0
  115. package/src/authoring.ts +475 -0
  116. package/src/checker.ts +91 -0
  117. package/src/compiled.ts +115 -0
  118. package/src/content.ts +156 -0
  119. package/src/declarations.ts +427 -0
  120. package/src/definition.ts +104 -0
  121. package/src/engine.ts +98 -0
  122. package/src/format.ts +67 -0
  123. package/src/help.ts +41 -0
  124. package/src/i18n/check.ts +67 -0
  125. package/src/i18n/format.ts +318 -0
  126. package/src/i18n/i18n.ts +92 -0
  127. package/src/i18n/types.ts +35 -0
  128. package/src/index.ts +201 -0
  129. package/src/issues.ts +107 -0
  130. package/src/json.ts +6 -0
  131. package/src/manifest.ts +193 -0
  132. package/src/player.ts +145 -0
  133. package/src/policy.ts +117 -0
  134. package/src/protocol.ts +65 -0
  135. package/src/result.ts +53 -0
  136. package/src/scoring.ts +31 -0
  137. package/src/slots.ts +57 -0
  138. package/src/snapshot.ts +40 -0
  139. package/src/style.ts +89 -0
@@ -0,0 +1,475 @@
1
+ /**
2
+ * Authoring contracts, independent of any WYSIWYG editor. These contracts and the authoring kit (`@astl/editor`)
3
+ * need no editor engine; `@astl/editor-tiptap` brings TipTap as the default, and any other editor (ProseMirror,
4
+ * CKEditor, Lexical, a plain textarea) plugs in by implementing EditorAdapter. The kit builds inspectors, insert
5
+ * menus, issue lists and previews on top of this contract.
6
+ */
7
+ import type { MessageCatalog } from './i18n/types.js';
8
+ import type { ComponentManifest } from './manifest.js';
9
+
10
+ /**
11
+ * How authors create and edit one component type. Plain data, so it can also feed agent tools and docs.
12
+ * Message keys are looked up in the manifest's `messages` namespace.
13
+ */
14
+ export interface EditorDescriptor {
15
+ readonly manifest: ComponentManifest;
16
+ /** Catalogs of the manifest's namespace (labels, help texts, template texts). */
17
+ readonly messages?: readonly MessageCatalog[];
18
+ /** Message key of the menu label, e.g. `editor.label`. */
19
+ readonly label: string;
20
+ /** Message key of a one-line description for insert menus. */
21
+ readonly description: string;
22
+ /** Icon name in the host's icon set (Phosphor names recommended), e.g. `text-t`. */
23
+ readonly icon: string;
24
+ /** Prefix for generated ids: `gap` gives `gap-1`, `gap-2`, ... */
25
+ readonly idPrefix: string;
26
+ /**
27
+ * Source HTML that insert menus add. Placeholders: `{{id}}` = a fresh unique id, `{{t:key}}` = a message from the
28
+ * manifest's namespace in the author's UI locale (escaped), `{{selection}}` = the text the author selected (escaped,
29
+ * `''` without one), which the snippet then replaces. Local child ids (option ids) stay as written.
30
+ */
31
+ readonly template: string;
32
+ /**
33
+ * Named starters: whole exercises with sample content, listed apart from the default `template` (which stays what
34
+ * the type's own entry inserts).
35
+ */
36
+ readonly templates?: readonly EditorTemplate[];
37
+ /** Inspector fields. Absent = derived from the manifest by the authoring kit. */
38
+ readonly fields?: readonly InspectorField[];
39
+ /**
40
+ * The element this type works with, such as the word bank of a gap: an insert writes into an empty `attribute` the
41
+ * id of the first `tag` element of the inserted template (a starter that brings one), else of the new element's
42
+ * page (pages play one at a time), and adds that tag's template at the end of the page when it has none.
43
+ */
44
+ readonly companion?: { readonly tag: string; readonly attribute: string };
45
+ /** A command on the author's selection, for a type that holds rich content (`content: 'flow'`). */
46
+ readonly wrap?: EditorWrap;
47
+ }
48
+
49
+ /**
50
+ * Wraps the selected words in an inline child (`Mark word`): the editor puts each selected word in a new `child`
51
+ * element (a fresh local id, `attributes` as given) and the blocks around them in an element of this type when they
52
+ * are in none (with a fresh id, joined to one right before or after); on words already in the child it removes the
53
+ * child, keeping the text. Editors offer it as a button labelled `label` (`EditorAdapter.wrap`).
54
+ */
55
+ export interface EditorWrap {
56
+ /** Message key of the button label. */
57
+ readonly label: string;
58
+ /** The inline child tag, one of the manifest's children. */
59
+ readonly child: string;
60
+ /** Attributes of a new child besides its id, e.g. `{ correct: '' }` for a boolean. */
61
+ readonly attributes?: Readonly<Record<string, string>>;
62
+ }
63
+
64
+ /** A named starter of a type, inserted like its default template. */
65
+ export interface EditorTemplate {
66
+ /** Kebab-case, unique among the descriptor's templates, e.g. `two-gaps`. */
67
+ readonly id: string;
68
+ /** Message key of the menu label. */
69
+ readonly label: string;
70
+ /** Message key of a one-line description. */
71
+ readonly description?: string;
72
+ /** Icon name; default: the descriptor's. */
73
+ readonly icon?: string;
74
+ /**
75
+ * Source HTML with the placeholders of `EditorDescriptor.template` (one `{{id}}`, texts as `{{t:key}}`). Other
76
+ * components in it (the gaps of a word bank) are written without an id: each insert gives them fresh ones.
77
+ */
78
+ readonly template: string;
79
+ }
80
+
81
+ /**
82
+ * One inspector control:
83
+ * - `attribute`: an attribute of the element (control picked from the AttributeSpec type);
84
+ * - `text-list`: repeated private text children (e.g. accepted alternatives `<astl-alt>`);
85
+ * - `child-flag`: a boolean attribute on each child of a tag, listed by the child's text (e.g. which options are
86
+ * correct); its label and help messages get `count` (children) and `checked` (children with the flag);
87
+ * - `child-text`: a free-text attribute on each child of a tag, listed by the child's text (e.g. the word each clue
88
+ * hides); an empty value removes the attribute;
89
+ * - `child-choice`: one value of an attribute on each child of a tag, listed by the child's text: one of the
90
+ * attribute's enum values (e.g. whether each statement is true or false), or with `options`, the id of a child of
91
+ * another tag (e.g. the category of each card).
92
+ * - `child-table`: the children of a tag as rows, each listed by its text, with a control for each of `columns` (by
93
+ * the attribute's type: text, number or a select of its enum values) and a button that removes it; a button adds a
94
+ * row, a child with a fresh local id and a first value for each required attribute (e.g. a crossword's words).
95
+ * - `picture-regions`: zones on the element's picture, one child of `tag` each, drawn with a pointer or typed in
96
+ * number fields. A zone has `id` (local), `shape` (`rect`, `circle` or `polygon`), `x` and `y` (rect: its top-left
97
+ * corner; circle: its centre), `width` and `height` (rect), `r` (circle, a share of the width), `points`
98
+ * (polygon: `x,y x,y ...`) and an optional `label`. Every coordinate is a percentage of the picture, which never
99
+ * mirrors: x of its width from the left edge, y of its height from the top.
100
+ * - `picture-points`: pins on the picture, one child of `tag` each: `id`, `x` and `y` (percent) and `label`.
101
+ * Both show the picture of the element's `url` attribute `src`, which they set by upload or address.
102
+ * Rich content (prompts, option texts) is edited in the WYSIWYG itself. The children of a tag are found anywhere in
103
+ * the element, so the inline children of a type with rich content (`content: 'flow'`) are listed too.
104
+ */
105
+ export type InspectorField =
106
+ | {
107
+ readonly kind: 'attribute';
108
+ /** Attribute name, e.g. `answer`. */
109
+ readonly name: string;
110
+ /** Message key of the label. */
111
+ readonly label: string;
112
+ /** Message key of help text, optional. */
113
+ readonly help?: string;
114
+ /**
115
+ * A `url` attribute: the files an author may upload for it with the host's `uploadAsset` (a file input's
116
+ * `accept`, e.g. `video/mp4,video/webm`). Absent, or without an upload, the address is typed.
117
+ */
118
+ readonly accept?: string;
119
+ /**
120
+ * Choices from another element of the document, in a select: the ids of the `tag` children of the element whose
121
+ * id is in this element's attribute `from`, listed by their text (e.g. the words of the bank a drop takes its
122
+ * words from). Without the document or that element, the attribute's own control.
123
+ */
124
+ readonly options?: { readonly from: string; readonly tag: string };
125
+ /**
126
+ * Shown only while the element shows one of these variants (its valid `variant`, else the manifest's default),
127
+ * for an attribute that only one presentation reads (a dropdown's `native`). Absent: shown in every variant.
128
+ */
129
+ readonly variants?: readonly string[];
130
+ }
131
+ | {
132
+ readonly kind: 'text-list';
133
+ /** Child tag, e.g. `astl-alt`. */
134
+ readonly tag: string;
135
+ readonly label: string;
136
+ readonly help?: string;
137
+ /** Each child in a text area that keeps its line breaks (a transcript), not on one line. */
138
+ readonly multiline?: boolean;
139
+ }
140
+ | {
141
+ readonly kind: 'child-flag' | 'child-text';
142
+ /** Child tag, e.g. `astl-option`. */
143
+ readonly tag: string;
144
+ /** Attribute on each child: a boolean for `child-flag` (e.g. `correct`), a string for `child-text`. */
145
+ readonly attribute: string;
146
+ readonly label: string;
147
+ readonly help?: string;
148
+ }
149
+ | {
150
+ readonly kind: 'child-choice';
151
+ /** Child tag, e.g. `astl-statement` or `astl-card`. */
152
+ readonly tag: string;
153
+ /** Attribute on each child, e.g. `answer` or `category`. */
154
+ readonly attribute: string;
155
+ /**
156
+ * Where the choices come from. Absent: the attribute's enum values (`editor.value.<attribute>.<value>`, e.g.
157
+ * true / false of each statement). A child tag: the ids of the element's children with that tag, listed by their
158
+ * text (e.g. `astl-category` for the category of each card).
159
+ */
160
+ readonly options?: string;
161
+ readonly label: string;
162
+ readonly help?: string;
163
+ }
164
+ | {
165
+ readonly kind: 'child-table';
166
+ /** Child tag, e.g. `astl-crossword-word`. */
167
+ readonly tag: string;
168
+ /** The child's attributes edited in each row, in order, each with the message key of its label. */
169
+ readonly columns: readonly { readonly attribute: string; readonly label: string }[];
170
+ readonly label: string;
171
+ readonly help?: string;
172
+ }
173
+ | {
174
+ readonly kind: 'picture-regions' | 'picture-points';
175
+ /** Child tag of a zone or a pin, e.g. `astl-region`. */
176
+ readonly tag: string;
177
+ /** The element's `url` attribute that holds the picture, e.g. `src`. */
178
+ readonly src: string;
179
+ readonly label: string;
180
+ readonly help?: string;
181
+ };
182
+
183
+ /** The component the cursor is in, by source id. */
184
+ export interface EditorSelection {
185
+ readonly id: string;
186
+ readonly tag: string;
187
+ }
188
+
189
+ /**
190
+ * A text format a toolbar toggles (`EditorAdapter.format`), each one of content format 1: the marks `bold`
191
+ * (`<strong>` or `<b>`), `italic` (`<em>` or `<i>`), `underline` (`<u>`) and `link` (`<a href>`); the text styles
192
+ * `paragraph` (`<p>`), `heading` (`<h3>`, below the page title the player shows as `<h2>`) and `subheading` (`<h4>`);
193
+ * the lists `bulleted` (`<ul>`) and `numbered` (`<ol>`).
194
+ */
195
+ export type EditorFormat =
196
+ | 'bold'
197
+ | 'italic'
198
+ | 'underline'
199
+ | 'link'
200
+ | 'paragraph'
201
+ | 'heading'
202
+ | 'subheading'
203
+ | 'bulleted'
204
+ | 'numbered';
205
+
206
+ /** The formats at the cursor (`EditorAdapter.formatState`), for a toolbar's pressed and disabled buttons. */
207
+ export interface EditorFormatState {
208
+ /**
209
+ * The formats in effect: each mark on all of the selected text (at a cursor: the text before it), the text style of
210
+ * the block at the cursor (an `<h2>` counts as `heading`) and the list it is in.
211
+ */
212
+ readonly active: readonly EditorFormat[];
213
+ /**
214
+ * The formats that can be toggled here: none while a component is selected, marks only in text a component holds
215
+ * (an option, a word), all of them in a page's own text.
216
+ */
217
+ readonly enabled: readonly EditorFormat[];
218
+ /** The address of the link at the cursor, or null. */
219
+ readonly href: string | null;
220
+ /** Whether `undo` would change the document. */
221
+ readonly undo: boolean;
222
+ /** Whether `redo` would change the document. */
223
+ readonly redo: boolean;
224
+ }
225
+
226
+ /**
227
+ * A block by its place. A page's content is a list of blocks: each block component (`display: 'block'`) directly on
228
+ * the page is one block, and the content between two of them (paragraphs, headings, lists, quotes, pictures, with the
229
+ * inline components in their text) is one text block. The authoring kit's outline lists the same blocks
230
+ * (`OutlinePage.blocks`). The content format has no blocks: they are a way to show and move a page's content. A
231
+ * layout is not a block either: the blocks in its columns are on the page's list, in reading order, and a column's
232
+ * content divides as a page's does.
233
+ */
234
+ export interface EditorBlockPlace {
235
+ /** The page's source id, or null for the content of a document without pages. */
236
+ readonly page: string | null;
237
+ /** The block's index on its page, from 0. */
238
+ readonly index: number;
239
+ }
240
+
241
+ /** How `EditorAdapter.insert` places a snippet. */
242
+ export interface EditorInsertOptions {
243
+ /**
244
+ * The snippet replaces the selected text (`EditorAdapter.selectedText`), e.g. a gap whose answer is that text.
245
+ * Default false: it goes after the cursor and the selection stays.
246
+ */
247
+ readonly replaceSelection?: boolean;
248
+ }
249
+
250
+ /**
251
+ * What takes a style besides the selected text (`EditorStyles`): a picture in text or in a figure, a figure, the text
252
+ * block with the cursor (a paragraph, a heading, a caption, an option), a component, a column or a layout.
253
+ */
254
+ export type EditorStyleTargetKind = 'picture' | 'figure' | 'block' | 'component' | 'column' | 'layout';
255
+
256
+ /** An element at the cursor that takes a style (`EditorStyleState.targets`). */
257
+ export interface EditorStyleTarget {
258
+ readonly kind: EditorStyleTargetKind;
259
+ /** Its tag in the source: `p`, `img`, `astl-choice`. */
260
+ readonly tag: string;
261
+ /** Its own `style` in the allow-list's normal form (`name: value; name: value`), `''` without one. */
262
+ readonly style: string;
263
+ /** The named style on it (the first, for a source written by hand with more), or null. */
264
+ readonly className: string | null;
265
+ }
266
+
267
+ /** The state at the cursor (`EditorStyles.state`), for a toolbar and a form. */
268
+ export interface EditorStyleState {
269
+ /** Whether styles can go on text here: not with a component or a picture selected. */
270
+ readonly textEnabled: boolean;
271
+ /** Text is selected; without it, only a cursor. */
272
+ readonly hasSelection: boolean;
273
+ /** The declarations all of the selected text has, or what typing would get at the cursor. */
274
+ readonly text: Readonly<Record<string, string>>;
275
+ /** The properties that differ across the selected text. */
276
+ readonly mixed: readonly string[];
277
+ /** The named style on all of the selected text, or at the cursor; null: none, or not the same throughout. */
278
+ readonly textClass: string | null;
279
+ /** What the cursor is in, from the inside out. */
280
+ readonly targets: readonly EditorStyleTarget[];
281
+ }
282
+
283
+ /** Declarations to set; null removes one. Only what the style allow-list keeps is ever written. */
284
+ export type EditorStyleChanges = Readonly<Record<string, string | null>>;
285
+
286
+ /** A rule of the document (`<astl-style>`): a named style (`name`, `label`), or the style of a tag (`for`). */
287
+ export interface EditorStyleRule {
288
+ readonly name?: string;
289
+ readonly label?: string;
290
+ readonly for?: string;
291
+ readonly style: string;
292
+ }
293
+
294
+ /**
295
+ * The styling commands of an editor (`EditorAdapter.style`). Each is one undo step, keeps the cursor and leaves the
296
+ * focus where it is, so a form beside the text can call them on every change.
297
+ */
298
+ export interface EditorStyles {
299
+ state(): EditorStyleState;
300
+ /**
301
+ * Sets declarations on the selected text; with only a cursor, on what is typed next. Text ends up in one span, whose
302
+ * language and classes stay; a span left with nothing goes. Returns whether anything changed.
303
+ */
304
+ setText(changes: EditorStyleChanges): boolean;
305
+ /** Sets declarations on the target of this kind at the cursor; `block`: on each text block of the selection. */
306
+ setTarget(kind: EditorStyleTargetKind, changes: EditorStyleChanges): boolean;
307
+ /** Replaces the target's own style with what the allow-list keeps of `style` (the "Own CSS" field). */
308
+ replaceTarget(kind: EditorStyleTargetKind, style: string): boolean;
309
+ /**
310
+ * Sets the named style on the selected text (`text`) or a target, in the place of any other; null clears it. With
311
+ * `rule` (a ready style: a named rule of the same `name`), a document without a rule of that name gets it in the same
312
+ * undo step; a rule of the document's own by that name stays as it is. Returns false, changing nothing, for a `rule`
313
+ * of another name or with `for`, a new rule past the limit, or a class that changes nothing (no target, or there).
314
+ */
315
+ setClass(where: 'text' | EditorStyleTargetKind, name: string | null, rule?: EditorStyleRule): boolean;
316
+ /**
317
+ * Clears formatting: the marks and styles of the selected text (a language and a link stay), and the style and named
318
+ * style of the blocks it covers whole; with only a cursor, of its block.
319
+ */
320
+ clear(): boolean;
321
+ /** The document's rules, in order. */
322
+ rules(): readonly EditorStyleRule[];
323
+ /**
324
+ * Adds a rule, or changes the one with its `name` or `for`, in place. A tag's rule without declarations goes. Returns
325
+ * false, changing nothing, for a name the format refuses, or a new rule past the limit.
326
+ */
327
+ setRule(rule: EditorStyleRule): boolean;
328
+ /** Deletes a rule; a named style's class goes from every element too. Returns false for an unknown rule. */
329
+ removeRule(rule: Pick<EditorStyleRule, 'name' | 'for'>): boolean;
330
+ /**
331
+ * Makes the target's own style a named style: a new rule labelled `label`, the target's `style` gone and its class
332
+ * the new name, which it returns. Null, changing nothing, past the limit or without a target of this kind.
333
+ */
334
+ saveRule(kind: EditorStyleTargetKind, label: string): string | null;
335
+ /** Inserts a layout (source HTML, one `<astl-columns>`) as `insert` does, the cursor in its first empty block. */
336
+ insertLayout(html: string): boolean;
337
+ /** The layout the cursor is in, or null. Absent: the editor cannot change a layout once inserted. */
338
+ layout?(): EditorLayoutState | null;
339
+ /**
340
+ * Changes the layout the cursor is in, in one undo step: `unwrap` removes the columns and keeps their content in
341
+ * place, one column after another; `delete` removes it with its content (a host asks first when it holds any); a
342
+ * shape gives it that many columns, the content kept: a column that goes gives its content to the last one left.
343
+ * Returns false, changing nothing, without a layout at the cursor or for the shape it has.
344
+ */
345
+ editLayout?(edit: EditorLayoutEdit): boolean;
346
+ }
347
+
348
+ /** The shapes a layout can be changed to: two or three equal columns, or a wide and a narrow one. */
349
+ export type EditorLayoutShape = 'two' | 'three' | 'wide-narrow' | 'narrow-wide';
350
+
351
+ /** What `EditorStyles.editLayout` does to the layout at the cursor. */
352
+ export type EditorLayoutEdit = 'unwrap' | 'delete' | EditorLayoutShape;
353
+
354
+ /** The layout at the cursor (`EditorStyles.layout`). */
355
+ export interface EditorLayoutState {
356
+ /** Its shape, or null for one of none (four columns, other widths). */
357
+ readonly shape: EditorLayoutShape | null;
358
+ readonly columns: number;
359
+ /** Whether its columns hold nothing but empty paragraphs: it can go without asking. */
360
+ readonly empty: boolean;
361
+ }
362
+
363
+ /**
364
+ * What a WYSIWYG integration must provide so the authoring kit can drive it. Everything is in source HTML
365
+ * (the `<astl-doc>` format), so an adapter is free to use any internal model.
366
+ */
367
+ export interface EditorAdapter {
368
+ /** The whole document as source HTML. */
369
+ getSource(): string;
370
+ /** Replaces the whole document. */
371
+ setSource(source: string): void;
372
+ /**
373
+ * Inserts a source snippet (an instantiated template) at the cursor, never into a component: after the component the
374
+ * cursor is in. With `replaceSelection`, an inline snippet takes the place of the selected text. A rich-text editor
375
+ * keeps the text whole: a snippet of blocks (a block component, a paragraph) goes after the element the cursor is in
376
+ * directly on its page or column (a paragraph, a heading, a list), or in the place of that element when it is an
377
+ * empty paragraph; a layout goes after the layout the cursor is in, as layouts do not nest. A source editor (the
378
+ * textarea adapter) puts any snippet at the caret, as the author would type it.
379
+ */
380
+ insert(html: string, options?: EditorInsertOptions): void;
381
+ /** Source HTML of one component element, or null when the id is not in the document. */
382
+ getElement(id: string): string | null;
383
+ /** Replaces one component element in place (inspector edits); the rest of the document is untouched. */
384
+ replaceElement(id: string, html: string): void;
385
+ /** The component at the cursor, or null. */
386
+ getSelection(): EditorSelection | null;
387
+ /** Moves the cursor to an element by source id (a component or a page) and focuses the editor. */
388
+ select(id: string): void;
389
+ /** Subscribe to document changes. Returns an unsubscribe function. */
390
+ onChange(listener: () => void): () => void;
391
+ /** Subscribe to selection changes. Returns an unsubscribe function. */
392
+ onSelectionChange(listener: (selection: EditorSelection | null) => void): () => void;
393
+ /**
394
+ * Runs the `wrap` command of the type `tag` (`EditorDescriptor.wrap`) on the selection and focuses the editor;
395
+ * returns whether the document changed. Absent: the editor offers no such command.
396
+ */
397
+ wrap?(tag: string): boolean;
398
+ /**
399
+ * A page break at the cursor (at the start of a selection, which stays): a new page from there on, whose id it
400
+ * returns, or null when the cursor is in no page's content. In a paragraph or a heading directly on a page, the page
401
+ * breaks at the cursor (at its start: before it; at its end: after it); anywhere else (a component, a rich block, a
402
+ * list, a column of a layout) after the block directly on the page, which stays whole. A side left without content gets one empty
403
+ * paragraph. The new page has a fresh id `page-<n>` (pages and components share one namespace), no `label`, and the
404
+ * `lang` and `dir` of the page it comes from; the cursor goes to the start of its first line. A document without
405
+ * pages becomes pages in the same step, the first with a fresh id too. One undo step. Absent: the editor cannot
406
+ * split pages.
407
+ */
408
+ splitPage?(): string | null;
409
+ /**
410
+ * Adds an empty page (one empty paragraph) after the page `after`, or after the last page when `after` is not the
411
+ * id of one, and returns its id: fresh (`page-<n>`), without a `label`, `lang` or `dir`. The cursor goes into it. A
412
+ * document without pages becomes its first page in the same step. One undo step.
413
+ */
414
+ addPage?(after?: string): string;
415
+ /**
416
+ * Joins the page `id` to the page before it: its content follows that page's content, and that page keeps its id,
417
+ * `label`, `lang` and `dir`. The cursor goes to the start of the joined content. Returns false, changing nothing, for
418
+ * the first page or an id that is not a page's. One undo step.
419
+ */
420
+ mergePage?(id: string): boolean;
421
+ /**
422
+ * Deletes the page `id` and everything on it. The cursor goes to the start of the page before it, or of the next
423
+ * page when it was the first. Returns false, changing nothing, for the only page or an id that is not a page's. One
424
+ * undo step.
425
+ */
426
+ removePage?(id: string): boolean;
427
+ /**
428
+ * Toggles a format on the selection and focuses the editor; returns whether the document changed. A mark goes on
429
+ * all of the selected text, or off when all of it has it; `link` with an `href` links the selected text (or changes
430
+ * the link at the cursor) and without one removes the link at the cursor; a text style sets the blocks of the
431
+ * selection to it (`heading` or `subheading` again sets `paragraph`); a list wraps them in that list, turns the
432
+ * other list into it, or lifts them out of it. One undo step. Absent (with `formatState`): no formatting commands.
433
+ */
434
+ format?(format: EditorFormat, href?: string): boolean;
435
+ /** The formats at the cursor and whether undo or redo can run. Present with `format`. */
436
+ formatState?(): EditorFormatState;
437
+ /** Undoes the last step and focuses the editor; returns whether the document changed. Absent: no history. */
438
+ undo?(): boolean;
439
+ /** Redoes the last undone step and focuses the editor; returns whether the document changed. */
440
+ redo?(): boolean;
441
+ /**
442
+ * Moves a block one place up or down within its page, the cursor with it; returns false, changing nothing, at the
443
+ * page's start or end or for a place that holds no block. One undo step. Absent: blocks do not move. A block in a
444
+ * column stays in its column; a block directly on the page moves past a whole layout, which itself never moves.
445
+ */
446
+ moveBlock?(block: EditorBlockPlace, direction: 'up' | 'down'): boolean;
447
+ /**
448
+ * Deletes a block: its component, or all of its text; a page or a column left without content keeps one empty
449
+ * paragraph.
450
+ * Returns false, changing nothing, for a place that holds no block. One undo step.
451
+ */
452
+ removeBlock?(block: EditorBlockPlace): boolean;
453
+ /**
454
+ * The block holding the cursor (in a document without pages: `page` null), or null while the editor has no cursor
455
+ * in content. A host follows it to show the page being edited.
456
+ */
457
+ getCursor?(): EditorBlockPlace | null;
458
+ /**
459
+ * Subscribe to every move of the cursor and every change of the document or its history: what `getCursor` and
460
+ * `formatState` return may have changed. Returns an unsubscribe function. Present with either.
461
+ */
462
+ onCursorChange?(listener: () => void): () => void;
463
+ /**
464
+ * The plain text of the selection when it lies within one text block (trimmed), else `''`. A template's
465
+ * `{{selection}}` takes it, and `insert` with `replaceSelection` puts the snippet in its place. Absent: `''`.
466
+ */
467
+ selectedText?(): string;
468
+ /**
469
+ * Focuses the editor, its cursor where it was (after a toolbar press from the keyboard). An editor that has the focus
470
+ * is left as it is, so a click there a moment before keeps its cursor.
471
+ */
472
+ focus?(): void;
473
+ /** Styles at the cursor, named styles and layouts. Absent: an editor of source text (the textarea), no controls. */
474
+ readonly style?: EditorStyles;
475
+ }
package/src/checker.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Checker: how a player gets verdicts without ever holding the answer key (unless the host opts into local grading).
3
+ * The HTTP binding of these bodies is the grading protocol (see protocol.ts and
4
+ * apps/docs/src/content/docs/reference/protocol.md).
5
+ */
6
+ import type { Answers } from './answers.js';
7
+ import type { Feedback } from './help.js';
8
+ import type { ProblemDetails } from './protocol.js';
9
+ import type { GradeResult, Solution } from './result.js';
10
+
11
+ export interface CheckRequest {
12
+ readonly answers: Answers;
13
+ /** View page id: grade only that page. Omit to grade everything. */
14
+ readonly page?: string;
15
+ /**
16
+ * View element id: grade that interaction only, under a policy with `checkScope: 'interaction'`. It counts against
17
+ * `maxChecks` like any check. Never together with `page`.
18
+ */
19
+ readonly element?: string;
20
+ }
21
+
22
+ /**
23
+ * Help that comes with a result when the policy shows it (the engine's `helpShown`), for what was graded: absent when there is
24
+ * none, so responses without help stay as they were.
25
+ */
26
+ export interface ResultHelp {
27
+ /** By view element id, and by view page id for a page's own feedback (picked by the page's score). */
28
+ readonly feedback?: Readonly<Record<string, Feedback>>;
29
+ /** HTML by view element id or view page id. */
30
+ readonly explanations?: Readonly<Record<string, string>>;
31
+ }
32
+
33
+ export interface CheckResponse extends ResultHelp {
34
+ readonly result: GradeResult;
35
+ /** Checks left in this attempt; null = unlimited. */
36
+ readonly checksLeft: number | null;
37
+ }
38
+
39
+ export interface SubmitRequest {
40
+ readonly answers: Answers;
41
+ }
42
+
43
+ export interface SubmitResponse extends ResultHelp {
44
+ /** null when the policy reveals nothing yet. */
45
+ readonly result: GradeResult | null;
46
+ /** Only when the policy reveals answers. */
47
+ readonly solution: Solution | null;
48
+ /** null when there is no pass mark or nothing is revealed. */
49
+ readonly passed: boolean | null;
50
+ /** ISO 8601, set by whoever grades. */
51
+ readonly submittedAt: string;
52
+ }
53
+
54
+ export interface HintRequest {
55
+ /** View element id, or a view page id for the page's own hints. */
56
+ readonly element: string;
57
+ }
58
+
59
+ /** The next hint of an interaction or page. Whoever grades records it: a hint's cost counts when it is used. */
60
+ export interface HintResponse {
61
+ /** HTML of content format 1. */
62
+ readonly html: string;
63
+ /** 0-based position of this hint. */
64
+ readonly index: number;
65
+ /** Hints still to come after this one. */
66
+ readonly left: number;
67
+ /** The fraction 0..1 of the points this hint takes off (0: free). */
68
+ readonly cost: number;
69
+ }
70
+
71
+ export interface Checker {
72
+ /** Practice only. Rejects with CheckerError (`check_not_allowed`) under other policies. */
73
+ check(request: CheckRequest): Promise<CheckResponse>;
74
+ submit(request: SubmitRequest): Promise<SubmitResponse>;
75
+ /**
76
+ * The next hint, practice only; checkers without hints leave it out. Rejects with CheckerError `hints_not_allowed`
77
+ * when the policy offers none, `hints_exhausted` when all are used, `not_found` for an unknown element.
78
+ */
79
+ hint?(request: HintRequest): Promise<HintResponse>;
80
+ }
81
+
82
+ /** Thrown by checkers for protocol-level refusals, carrying the RFC 9457 problem document. */
83
+ export class CheckerError extends Error {
84
+ readonly problem: ProblemDetails;
85
+
86
+ constructor(problem: ProblemDetails) {
87
+ super(problem.detail ?? problem.title);
88
+ this.name = 'CheckerError';
89
+ this.problem = problem;
90
+ }
91
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Compile outputs. A source compiles to three artefacts:
3
+ * - view: public HTML (no answers), cacheable on a CDN under its SHA-256;
4
+ * - key: private answer key, only for whoever grades;
5
+ * - metadata: public facts for catalogues and hosts.
6
+ */
7
+ import type { DocKind } from './content.js';
8
+ import type { FeedbackVariant } from './help.js';
9
+ import type { JsonObject } from './json.js';
10
+ import type { ScoringGroup } from './scoring.js';
11
+
12
+ export const KEY_FORMAT_VERSION = 1;
13
+
14
+ export interface KeySlot {
15
+ readonly kind: string;
16
+ readonly key: JsonObject;
17
+ /**
18
+ * Opaque id of the component the slot belongs to, which is also the id of its scoring group, unless the component
19
+ * is graded in parts (`ComponentDefinition.groups`): then its groups list the slot. Open slots (an ungraded kind)
20
+ * belong to no group: no `groups` entry has this id or lists them.
21
+ */
22
+ readonly group: string;
23
+ /** View page id. */
24
+ readonly page: string;
25
+ }
26
+
27
+ /** A hint as the key holds it: its content (HTML of format 1) and the fraction 0..1 of the points it costs. */
28
+ export interface KeyHint {
29
+ readonly html: string;
30
+ readonly cost: number;
31
+ }
32
+
33
+ /** The help of one interaction or page (see help.ts): key-side, as it may give the answer away. */
34
+ export interface KeyHelp {
35
+ readonly hints: readonly KeyHint[];
36
+ /** HTML; null when there is none. */
37
+ readonly explanation: string | null;
38
+ /** HTML by variant, only the variants written. */
39
+ readonly feedback: Readonly<Partial<Record<FeedbackVariant, string>>>;
40
+ }
41
+
42
+ export interface AnswerKey {
43
+ readonly format: typeof KEY_FORMAT_VERSION;
44
+ /** Content language of the document. Slots inherit it unless their key says otherwise. */
45
+ readonly lang: string;
46
+ /** View hash this key belongs to. Hosts must store and serve them together. */
47
+ readonly viewHash: string;
48
+ readonly slots: Readonly<Record<string, KeySlot>>;
49
+ readonly groups: readonly ScoringGroup[];
50
+ /** View page ids in order. */
51
+ readonly pages: readonly string[];
52
+ /** Help by view element id and view page id; absent when the source has none. */
53
+ readonly help?: Readonly<Record<string, KeyHelp>>;
54
+ }
55
+
56
+ export interface PageMetadata {
57
+ /** View page id. */
58
+ readonly id: string;
59
+ readonly label: string | null;
60
+ /** Answer slots on the page, open ones included. */
61
+ readonly slots: number;
62
+ readonly maxScore: number;
63
+ }
64
+
65
+ export interface Metadata {
66
+ readonly format: number;
67
+ readonly lang: string;
68
+ readonly dir: 'ltr' | 'rtl';
69
+ /**
70
+ * The document's `kind` when it is not `exercise`, the default: as in the view, the metadata of every exercise (and
71
+ * every conformance golden) stays as it was. Absent means `exercise`.
72
+ */
73
+ readonly kind?: DocKind;
74
+ readonly viewHash: string;
75
+ readonly pages: readonly PageMetadata[];
76
+ /** Element counts by tag, e.g. `{ "astl-gap": 5 }`. */
77
+ readonly elements: Readonly<Record<string, number>>;
78
+ /** Answer slots, open ones included: what a player's `astl-ready` counts. */
79
+ readonly slots: number;
80
+ /**
81
+ * Ids of the open slots (stored for a teacher, never graded), in reading order, so a server or a teacher's view can
82
+ * pick these answers out of a submission. They add nothing to `maxScore`.
83
+ */
84
+ readonly open: readonly string[];
85
+ readonly groups: number;
86
+ readonly maxScore: number;
87
+ /** Words of running text, for reading-time estimates. */
88
+ readonly words: number;
89
+ /** Numbers contributed by component `metadata` hooks, keyed `tag.name`. */
90
+ readonly extra: Readonly<Record<string, number>>;
91
+ }
92
+
93
+ export interface Compilation {
94
+ readonly view: string;
95
+ /** Lowercase hex SHA-256 of the UTF-8 view. */
96
+ readonly viewHash: string;
97
+ readonly key: AnswerKey;
98
+ readonly metadata: Metadata;
99
+ }
100
+
101
+ /** Narrow check for keys loaded from storage. Full validation happens in the engine. */
102
+ export function isAnswerKey(value: unknown): value is AnswerKey {
103
+ if (typeof value !== 'object' || value === null) return false;
104
+ const v = value as Partial<AnswerKey>;
105
+ return (
106
+ v.format === KEY_FORMAT_VERSION &&
107
+ typeof v.lang === 'string' &&
108
+ typeof v.viewHash === 'string' &&
109
+ typeof v.slots === 'object' &&
110
+ v.slots !== null &&
111
+ Array.isArray(v.groups) &&
112
+ Array.isArray(v.pages) &&
113
+ (v.help === undefined || (typeof v.help === 'object' && v.help !== null))
114
+ );
115
+ }