@astl/editor-tiptap 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAOA,wGAAwG;AACxG,eAAO,MAAM,aAAa,iBAAiB,CAAC;AA0lC5C,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAKnD"}
1
+ {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAOA,wGAAwG;AACxG,eAAO,MAAM,aAAa,iBAAiB,CAAC;AA4rC5C,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAKnD"}
package/dist/styles.js CHANGED
@@ -201,7 +201,8 @@ const baseSurfaceStyles = css `
201
201
  .astl-page-remove-title,
202
202
  .astl-page-check,
203
203
  .astl-help-remove,
204
- .astl-help-free
204
+ .astl-help-free,
205
+ .astl-check-make
205
206
  ) {
206
207
  box-sizing: border-box;
207
208
  display: inline-flex;
@@ -240,7 +241,8 @@ const baseSurfaceStyles = css `
240
241
  .astl-page-remove-title,
241
242
  .astl-page-check,
242
243
  .astl-help-remove,
243
- .astl-help-free
244
+ .astl-help-free,
245
+ .astl-check-make
244
246
  ):hover {
245
247
  background: var(--astl-accent-soft);
246
248
  color: var(--astl-ink);
@@ -321,6 +323,102 @@ const baseSurfaceStyles = css `
321
323
  color: var(--astl-danger);
322
324
  }
323
325
 
326
+ /* A Check element (astl-check): the compact button it puts in the player, the accent's outline, then a tag saying how
327
+ many questions it checks; without any, the tag says so, dashed in the warning colour: never by colour alone. While
328
+ it is selected, the questions it checks have a dashed accent outline (astl-check-target), apart from the solid
329
+ focus ring. */
330
+ .astl-surface .astl-check {
331
+ box-sizing: border-box;
332
+ display: flex;
333
+ flex-wrap: wrap;
334
+ align-items: center;
335
+ gap: var(--astl-space-2);
336
+ inline-size: fit-content;
337
+ max-inline-size: 100%;
338
+ margin-block: var(--astl-space-2);
339
+ border-radius: var(--astl-radius-s);
340
+ font-family: var(--astl-font-body);
341
+ cursor: pointer;
342
+ }
343
+ .astl-surface .astl-check-button {
344
+ box-sizing: border-box;
345
+ display: inline-flex;
346
+ align-items: center;
347
+ gap: 0.25rem;
348
+ min-block-size: max(24px, var(--astl-editor-control-size, 1.75rem));
349
+ max-inline-size: 100%;
350
+ padding-inline: var(--astl-space-3);
351
+ border: var(--astl-outline-width) solid var(--astl-accent);
352
+ border-radius: var(--astl-radius-s);
353
+ background: var(--astl-surface);
354
+ color: var(--astl-ink);
355
+ font-size: var(--astl-editor-control-font-size, 0.8125rem);
356
+ font-weight: 600;
357
+ line-height: 1.2;
358
+ overflow-wrap: anywhere;
359
+ }
360
+ .astl-surface .astl-check-button .astl-icon {
361
+ flex: none;
362
+ inline-size: var(--astl-editor-control-icon-size, 0.875rem);
363
+ block-size: var(--astl-editor-control-icon-size, 0.875rem);
364
+ fill: none;
365
+ stroke: var(--astl-accent);
366
+ stroke-width: 2.2;
367
+ stroke-linecap: round;
368
+ stroke-linejoin: round;
369
+ }
370
+ .astl-surface .astl-check-tag {
371
+ padding-inline: 0.5em;
372
+ border: 1px solid transparent;
373
+ border-radius: var(--astl-radius-pill);
374
+ background: var(--astl-accent-soft);
375
+ color: var(--astl-ink);
376
+ font-size: var(--astl-size-small);
377
+ font-weight: 600;
378
+ line-height: 1.4;
379
+ }
380
+ .astl-surface .astl-check-tag:empty {
381
+ display: none;
382
+ }
383
+ .astl-surface .astl-check[data-empty] .astl-check-tag {
384
+ border-style: dashed;
385
+ border-color: var(--astl-warning);
386
+ background: var(--astl-warning-soft);
387
+ }
388
+ .astl-surface .astl-check-target {
389
+ outline: var(--astl-editor-focus-width, 2px) dashed var(--astl-accent);
390
+ outline-offset: 2px;
391
+ }
392
+ /* A chip sits in running text: its outline hugs it, never over the words beside it. */
393
+ .astl-surface .astl-chip.astl-check-target {
394
+ outline-offset: 0;
395
+ }
396
+ /* An automatic Check is the player's, not the exercise's: in a dashed box, its button dashed and muted, with "Make it
397
+ a button" after its tag. */
398
+ .astl-surface .astl-check-auto {
399
+ padding-block: var(--astl-space-1);
400
+ padding-inline: var(--astl-space-2);
401
+ border: 1px dashed var(--astl-outline);
402
+ cursor: default;
403
+ }
404
+ .astl-surface .astl-check-auto .astl-check-button {
405
+ border-style: dashed;
406
+ border-color: var(--astl-outline);
407
+ color: var(--astl-ink-muted);
408
+ }
409
+ .astl-surface .astl-check-make:focus-visible {
410
+ outline: var(--astl-editor-focus-width, 2px) solid var(--astl-focus);
411
+ outline-offset: var(--astl-editor-focus-offset, 1px);
412
+ }
413
+ @media (forced-colors: active) {
414
+ .astl-surface .astl-check-target {
415
+ outline-color: Highlight;
416
+ }
417
+ .astl-surface .astl-check-button .astl-icon {
418
+ stroke: CanvasText;
419
+ }
420
+ }
421
+
324
422
  /* The page is one flowing text; a block component on it is framed, with a label tab at the frame's top start (its
325
423
  type's icon, its label, a summary). The tab sits on the frame's top border, covering it. The frame with the cursor
326
424
  is the accent, its tab filled; one with an error or a warning is dashed in that colour and says its issues under
@@ -1 +1 @@
1
- {"version":3,"file":"styles.js","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAkB,GAAG,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAErD,wGAAwG;AACxG,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,iBAAiB,GAAc,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,kBAAkB,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,kBAAkB,GAAG,CAAC,GAAG,IAAI,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;AAC9G,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astl/editor-tiptap",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "<astl-editor>: the authoring kit around a TipTap surface, loaded only when no other editor is plugged in through an adapter.",
5
5
  "keywords": [
6
6
  "astrolabe",
@@ -53,11 +53,11 @@
53
53
  "access": "public"
54
54
  },
55
55
  "dependencies": {
56
- "@astl/core": "0.2.3",
57
- "@astl/editor": "0.2.3",
58
- "@astl/engine": "0.2.3",
59
- "@astl/player": "0.2.3",
60
- "@astl/tokens": "0.2.3",
56
+ "@astl/core": "0.3.0",
57
+ "@astl/editor": "0.3.0",
58
+ "@astl/engine": "0.3.0",
59
+ "@astl/player": "0.3.0",
60
+ "@astl/tokens": "0.3.0",
61
61
  "@tiptap/core": "3.32.0",
62
62
  "@tiptap/pm": "3.32.0",
63
63
  "@tiptap/starter-kit": "3.32.0",
package/src/adapter.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  * authoring kit never sees a ProseMirror node, and this file is the whole integration.
4
4
  */
5
5
  import {
6
+ CHECK_TAG,
6
7
  type ComponentManifest,
7
8
  type EditorAdapter,
8
9
  type EditorFormat,
@@ -16,7 +17,15 @@ import type { ChainedCommands, Editor } from '@tiptap/core';
16
17
  import { closeHistory } from '@tiptap/pm/history';
17
18
  import { DOMSerializer, Fragment, type Node as PMNode, DOMParser as SchemaParser } from '@tiptap/pm/model';
18
19
  import { NodeSelection, type Selection, TextSelection, type Transaction } from '@tiptap/pm/state';
19
- import { cursorIn, isBlockOf, LAYOUT, LAYOUT_COLUMN, moveBlockIn, removeBlockIn } from './blocks.js';
20
+ import {
21
+ CHECK_NODE,
22
+ cursorIn,
23
+ isBlockOf,
24
+ LAYOUT,
25
+ LAYOUT_COLUMN,
26
+ moveBlockIn,
27
+ removeBlockIn,
28
+ } from './blocks.js';
20
29
  import {
21
30
  addPageIn,
22
31
  attributesOf,
@@ -116,7 +125,8 @@ function patch(tr: Transaction, pos: number, from: PMNode, to: PMNode): void {
116
125
  * its schema, as given to `astrolabeExtensions`.
117
126
  */
118
127
  export function createTiptapAdapter(editor: Editor, manifests: readonly ComponentManifest[]): EditorAdapter {
119
- const tags = new Map(manifests.map((m) => [nodeName(m.tag), m.tag]));
128
+ // The elements the kit selects, reads and replaces by id: the components and the Check elements.
129
+ const tags = new Map([...manifests.map((m) => [nodeName(m.tag), m.tag] as const), [CHECK_NODE, CHECK_TAG]]);
120
130
  const types = new Map(manifests.map((m) => [nodeName(m.tag), m]));
121
131
  const childTags = new Map(
122
132
  manifests.flatMap((m) => (m.children ?? []).map((child) => [nodeName(child.tag), child.tag] as const)),
@@ -124,7 +134,10 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
124
134
  const isBlock = isBlockOf((node) => types.get(node.type.name) ?? null);
125
135
  const serializer = DOMSerializer.fromSchema(editor.schema);
126
136
  const parser = SchemaParser.fromSchema(editor.schema);
127
- const isComponent = (node: PMNode): boolean => tags.has(node.type.name);
137
+ /** A component: what takes formats and styles as one. */
138
+ const isComponent = (node: PMNode): boolean => types.has(node.type.name);
139
+ /** A component or a Check element: what the kit selects, reads and replaces by id, never inserts into. */
140
+ const isElement = (node: PMNode): boolean => tags.has(node.type.name);
128
141
  const toSelection = (node: PMNode): EditorSelection | null => {
129
142
  const id = node.attrs.attributes.id;
130
143
  return typeof id === 'string' && id !== '' ? { id, tag: tags.get(node.type.name) ?? '' } : null;
@@ -135,7 +148,7 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
135
148
  let found: { node: PMNode; pos: number } | null = null;
136
149
  editor.state.doc.descendants((node, pos) => {
137
150
  if (found !== null) return false;
138
- if ((isComponent(node) || node.type.name === 'astlPage') && node.attrs.attributes.id === id) {
151
+ if ((isElement(node) || node.type.name === 'astlPage') && node.attrs.attributes.id === id) {
139
152
  found = { node, pos };
140
153
  }
141
154
  return found === null;
@@ -143,10 +156,13 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
143
156
  return found;
144
157
  };
145
158
 
146
- /** The component the cursor is on: a selected atom, a block around the cursor, or an atom next to it. */
159
+ /**
160
+ * The component the cursor is on: a selected atom (a Check element too), a block around the cursor, or an atom next
161
+ * to it.
162
+ */
147
163
  const selection = (): EditorSelection | null => {
148
164
  const current = editor.state.selection;
149
- if (current instanceof NodeSelection && isComponent(current.node)) return toSelection(current.node);
165
+ if (current instanceof NodeSelection && isElement(current.node)) return toSelection(current.node);
150
166
  const { $from } = current;
151
167
  for (let depth = $from.depth; depth > 0; depth--) {
152
168
  if (isComponent($from.node(depth))) return toSelection($from.node(depth));
@@ -183,7 +199,7 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
183
199
  /** Where formats apply: nowhere on a selected component, marks only in a component's children. */
184
200
  const allowed = (): readonly EditorFormat[] => {
185
201
  const current = editor.state.selection;
186
- if (current instanceof NodeSelection && isComponent(current.node)) return [];
202
+ if (current instanceof NodeSelection && isElement(current.node)) return [];
187
203
  for (let depth = current.$from.depth; depth > 0; depth--) {
188
204
  const node = current.$from.node(depth);
189
205
  if (isComponent(node)) return types.get(node.type.name)?.content === 'flow' ? FORMATS : MARKS;
@@ -195,7 +211,7 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
195
211
  const { selection: current } = editor.state;
196
212
  const active: EditorFormat[] = [];
197
213
  const parent = current.$from.parent;
198
- const component = current instanceof NodeSelection && isComponent(current.node);
214
+ const component = current instanceof NodeSelection && isElement(current.node);
199
215
  for (const mark of ['bold', 'italic', 'underline', 'link'] as const)
200
216
  if (editor.isActive(mark)) active.push(mark);
201
217
  if (!component && parent.type.name === 'paragraph') active.push('paragraph');
@@ -267,7 +283,7 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
267
283
  const layout = first?.type.name === LAYOUT;
268
284
  // Never replace or nest into the component at the cursor: insert after it. A selected component in text takes
269
285
  // inline snippets after it; a block goes after its block, below.
270
- const selected = current instanceof NodeSelection && isComponent(current.node) ? current.node : null;
286
+ const selected = current instanceof NodeSelection && isElement(current.node) ? current.node : null;
271
287
  let at: number | { from: number; to: number } | null =
272
288
  selected !== null && (inline || !selected.isInline) ? current.to : null;
273
289
  for (let depth = current.$from.depth; at === null && depth > 0; depth--) {
@@ -315,13 +331,13 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
315
331
  },
316
332
  getElement(id) {
317
333
  const hit = find(id);
318
- return hit !== null && isComponent(hit.node)
334
+ return hit !== null && isElement(hit.node)
319
335
  ? toHtml(serializer.serializeNode(hit.node, { document: inert }))
320
336
  : null;
321
337
  },
322
338
  replaceElement(id, html) {
323
339
  const hit = find(id);
324
- if (hit === null || !isComponent(hit.node)) return;
340
+ if (hit === null || !isElement(hit.node)) return;
325
341
  const box = inert.createElement('div');
326
342
  box.innerHTML = inertStyles(html);
327
343
  let next: PMNode | null = null;
package/src/blocks.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Blocks (`EditorBlockPlace`): a page's content as the adapter moves and deletes it. Each block component directly on a
3
- * page or in a column of a layout is one block; the nodes between two of them are one text block; they are numbered in
4
- * reading order, through the columns. The content format has no blocks, so nothing here changes the schema: the page is
3
+ * page or in a column of a layout is one block, and so is each Check element; the nodes between two of them are one text
4
+ * block; they are numbered in reading order, through the columns. The content format has no blocks, so nothing here changes the schema: the page is
5
5
  * one flowing text, and decorations frame each block component, with a label tab and its issues; the adapter (and the
6
6
  * inspector through it) moves and deletes blocks, each as one undo step.
7
7
  */
@@ -50,6 +50,9 @@ const PAGE = 'astlPage';
50
50
  export const LAYOUT = 'astlLayout';
51
51
  export const LAYOUT_COLUMN = 'astlLayoutColumn';
52
52
 
53
+ /** The node of `<astl-check>`, a Check for several questions: a block of its own. */
54
+ export const CHECK_NODE = 'astlCheck';
55
+
53
56
  /** The frames validate this long after the last change, as the issues list does, so typing is not interrupted. */
54
57
  const CHECK_DELAY_MS = 300;
55
58
 
@@ -81,11 +84,11 @@ export type IsBlock = (node: PMNode) => boolean;
81
84
  /** The component type of an editor node, or null for content and children. */
82
85
  export type TypeOf = (node: PMNode) => ComponentManifest | null;
83
86
 
84
- /** Block components are blocks of their own. */
87
+ /** Block components are blocks of their own, and so are Check elements. */
85
88
  export const isBlockOf =
86
89
  (typeOf: TypeOf): IsBlock =>
87
90
  (node) =>
88
- typeOf(node)?.display === 'block';
91
+ node.type.name === CHECK_NODE || typeOf(node)?.display === 'block';
89
92
 
90
93
  /**
91
94
  * What a block or a page transaction did, as the status line says it: a key of the kit's namespace and its parameters,
package/src/element.ts CHANGED
@@ -457,7 +457,7 @@ const KIT_ICONS: readonly string[] = [
457
457
 
458
458
  /** The inspector's parts, forwarded so hosts can restyle its header, banners and fields (`::part(inspector-issue)`). */
459
459
  const INSPECTOR_PARTS =
460
- 'header: inspector-header, id: inspector-id, actions: inspector-actions, issue: inspector-issue, field: inspector-field, label: inspector-label, style: inspector-style, help: inspector-help, help-toggle: inspector-help-toggle, help-summary: inspector-help-summary, help-body: inspector-help-body, help-add: inspector-help-add, help-move: inspector-help-move, help-remove: inspector-help-remove';
460
+ 'header: inspector-header, id: inspector-id, actions: inspector-actions, issue: inspector-issue, field: inspector-field, label: inspector-label, style: inspector-style, help: inspector-help, help-toggle: inspector-help-toggle, help-summary: inspector-help-summary, help-body: inspector-help-body, help-add: inspector-help-add, help-move: inspector-help-move, help-remove: inspector-help-remove, check-targets: inspector-check-targets, checked-by: inspector-checked-by';
461
461
 
462
462
  /** The built-in surface module, once imported. */
463
463
  let surfaces: typeof import('./surface.js') | null = null;
package/src/nodes.ts CHANGED
@@ -21,12 +21,19 @@
21
21
  */
22
22
  import {
23
23
  CAPTION_TAG,
24
+ CHECK_FOR_ATTRIBUTE,
25
+ CHECK_LABEL_ATTRIBUTE,
26
+ CHECK_TAG,
27
+ type CheckTree,
24
28
  type ChildSpec,
25
29
  CLASS_ATTRIBUTE,
26
30
  COLUMN_TAG,
27
31
  COLUMNS_TAG,
28
32
  CONTENT_ATTRIBUTES,
29
33
  type ComponentManifest,
34
+ checkFor,
35
+ checkNodes,
36
+ checkParts,
30
37
  childTagConflict,
31
38
  directionOf,
32
39
  type EditorDescriptor,
@@ -39,11 +46,13 @@ import {
39
46
  HINT_TAG,
40
47
  INERT_STYLE,
41
48
  inertStyles,
49
+ isCheckable,
42
50
  isSourceId,
43
51
  MAX_COLUMNS,
44
52
  type MessageParams,
45
53
  MIN_COLUMNS,
46
54
  PAGE_CHECK_ATTRIBUTE,
55
+ planChecks,
47
56
  TAG_ATTRIBUTES,
48
57
  } from '@astl/core';
49
58
  import { cleanClass, cleanStyle, isStyleName } from '@astl/core/declarations';
@@ -81,10 +90,11 @@ import {
81
90
  TextSelection,
82
91
  type Transaction,
83
92
  } from '@tiptap/pm/state';
84
- import { Decoration, DecorationSet } from '@tiptap/pm/view';
93
+ import { Decoration, DecorationSet, type EditorView } from '@tiptap/pm/view';
85
94
  import { StarterKit } from '@tiptap/starter-kit';
86
95
  import {
87
96
  blocksExtension,
97
+ CHECK_NODE,
88
98
  historyKey,
89
99
  LAYOUT,
90
100
  LAYOUT_COLUMN,
@@ -945,6 +955,243 @@ function helpPlaceholderExtension(authoring: Authoring): Extension {
945
955
  });
946
956
  }
947
957
 
958
+ /** The group of `<astl-check>`: directly on a page or in a column, never in text or in a component. */
959
+ const CHECK = 'check';
960
+
961
+ /** The decoration spec key a Check element's view reads how many questions it checks from (`checksExtension`). */
962
+ const CHECK_COUNT = 'astlCheckCount';
963
+
964
+ /** The class of the questions the selected Check element checks. */
965
+ const CHECK_TARGET = 'astl-check-target';
966
+
967
+ /** A Check element's count among its view's decorations, or null before the plugin has given it. */
968
+ const countIn = (decorations: readonly { spec: Record<string, unknown> }[]): number | null => {
969
+ for (const decoration of decorations) {
970
+ const count = decoration.spec[CHECK_COUNT];
971
+ if (typeof count === 'number') return count;
972
+ }
973
+ return null;
974
+ };
975
+
976
+ /**
977
+ * `<astl-check>`, a Check for several questions: a block atom kept as data, drawn as the compact button it puts in the
978
+ * player (`astl-check-button`: its `label`, else "Check") with a tag saying how many questions it checks
979
+ * (`astl-check-tag`: "3 questions"; without any, "No questions" and `data-empty` on `astl-check`). Its card ticks them.
980
+ */
981
+ function checkNode(authoring: Authoring): Node {
982
+ const t = (key: string, params?: MessageParams) => authoring.t(EDITOR_NAMESPACE, key, params);
983
+ const ui = { lang: authoring.locale, dir: directionOf(authoring.locale) };
984
+ return Node.create({
985
+ name: CHECK_NODE,
986
+ group: CHECK,
987
+ atom: true,
988
+ selectable: true,
989
+ addAttributes: () => sourceAttributes,
990
+ parseHTML: () => [{ tag: CHECK_TAG }],
991
+ renderHTML: ({ node }) => [CHECK_TAG, { ...node.attrs.attributes }],
992
+ addNodeView:
993
+ () =>
994
+ ({ node, decorations }) => {
995
+ const dom = document.createElement('div');
996
+ dom.className = 'astl-check';
997
+ const button = dom.appendChild(document.createElement('span'));
998
+ button.className = 'astl-check-button';
999
+ button.append(lineIcon('check', 'astl-icon'));
1000
+ const label = button.appendChild(document.createElement('span'));
1001
+ const tag = dom.appendChild(Object.assign(document.createElement('span'), ui));
1002
+ tag.className = 'astl-check-tag';
1003
+ const render = (attrs: Record<string, unknown>, count: number | null) => {
1004
+ const own = ((attrs.attributes as Attributes)[CHECK_LABEL_ATTRIBUTE] ?? '').trim();
1005
+ label.textContent = own === '' ? t('check.button') : own;
1006
+ // The default words are the editor's, the author's the exercise's.
1007
+ if (own === '') Object.assign(label, ui);
1008
+ else {
1009
+ label.removeAttribute('lang');
1010
+ label.removeAttribute('dir');
1011
+ }
1012
+ tag.textContent =
1013
+ count === null ? '' : count === 0 ? t('check.none') : t('check.count', { n: count });
1014
+ dom.toggleAttribute('data-empty', count === 0);
1015
+ };
1016
+ render(node.attrs, countIn(decorations));
1017
+ return {
1018
+ dom,
1019
+ update: (next: NodeLike, nextDecorations: readonly { spec: Record<string, unknown> }[]) => {
1020
+ if (next.type.name !== CHECK_NODE) return false;
1021
+ render(next.attrs, countIn(nextDecorations));
1022
+ return true;
1023
+ },
1024
+ ignoreMutation: () => true,
1025
+ };
1026
+ },
1027
+ });
1028
+ }
1029
+
1030
+ /**
1031
+ * Each Check element's questions, by the rule the engine and the player use (`planChecks`) over the editor's nodes: its
1032
+ * view says how many (a node decoration's spec), and while it is selected its questions are marked (`astl-check-target`
1033
+ * on each), inline ones and blocks alike. Where two or more questions of a run of text have none, the automatic Check
1034
+ * the player puts there shows after the block of the last of them (`astl-check astl-check-auto`: "automatic · 3
1035
+ * questions"), with "Make it a button" (`astl-check-make`), which writes a Check element for them in its place.
1036
+ */
1037
+ function checksExtension(authoring: Authoring, manifests: readonly ComponentManifest[]): Extension {
1038
+ const t = (key: string, params?: MessageParams) => authoring.t(EDITOR_NAMESPACE, key, params);
1039
+ const ui = { lang: authoring.locale, dir: directionOf(authoring.locale) };
1040
+ const types = new Map(manifests.map((m) => [nodeName(m.tag), m]));
1041
+ const idOf = (node: PMNode): string | null => (node.attrs.attributes as Attributes | undefined)?.id || null;
1042
+ const childrenOf = (node: PMNode): PMNode[] => {
1043
+ const out: PMNode[] = [];
1044
+ node.forEach((child) => {
1045
+ out.push(child);
1046
+ });
1047
+ return out;
1048
+ };
1049
+ /** The positions of the Check elements directly in `node` and in its layouts' columns, as `checkNodes` meets them. */
1050
+ const checkPositions = (node: PMNode, start: number): number[] => {
1051
+ const out: number[] = [];
1052
+ node.forEach((child, offset) => {
1053
+ const pos = start + offset;
1054
+ if (child.type.name === CHECK_NODE) out.push(pos);
1055
+ else if (child.type.name === LAYOUT) {
1056
+ child.forEach((column, at) => {
1057
+ if (column.type.name === LAYOUT_COLUMN) out.push(...checkPositions(column, pos + 1 + at + 1));
1058
+ });
1059
+ }
1060
+ });
1061
+ return out;
1062
+ };
1063
+ /** A Check element for `run` at `pos` (after the block of its last question), selected, as one undo step. */
1064
+ const make = (view: EditorView, pos: number, run: readonly string[]): void => {
1065
+ const type = view.state.schema.nodes[CHECK_NODE];
1066
+ if (type === undefined) return;
1067
+ const used = idsIn(view.state.doc);
1068
+ let n = 1;
1069
+ while (used.has(`check-${n}`)) n++;
1070
+ const node = type.create({ attributes: { id: `check-${n}`, [CHECK_FOR_ATTRIBUTE]: run.join(' ') } });
1071
+ const tr = closeHistory(view.state.tr).insert(pos, node);
1072
+ view.dispatch(tr.setSelection(NodeSelection.create(tr.doc, pos)).scrollIntoView());
1073
+ view.focus();
1074
+ };
1075
+ /** The automatic Check of `run`: the button it is, its tag, and "Make it a button". */
1076
+ const automaticDom = (view: EditorView, run: readonly string[], getPos: () => number | undefined) => {
1077
+ const dom = Object.assign(document.createElement('div'), ui);
1078
+ dom.className = 'astl-check astl-check-auto';
1079
+ dom.contentEditable = 'false';
1080
+ const button = dom.appendChild(document.createElement('span'));
1081
+ button.className = 'astl-check-button';
1082
+ button.append(lineIcon('check', 'astl-icon'));
1083
+ button.appendChild(document.createElement('span')).textContent = t('check.button');
1084
+ const tag = dom.appendChild(document.createElement('span'));
1085
+ tag.className = 'astl-check-tag';
1086
+ tag.textContent = t('check.auto', { n: run.length });
1087
+ const press = dom.appendChild(document.createElement('button'));
1088
+ Object.assign(press, { type: 'button', tabIndex: -1, title: t('check.makeHelp') });
1089
+ press.className = 'astl-check-make';
1090
+ press.textContent = t('check.make');
1091
+ press.addEventListener('click', (event) => {
1092
+ event.preventDefault();
1093
+ const pos = getPos();
1094
+ if (pos !== undefined) make(view, pos, run);
1095
+ });
1096
+ return dom;
1097
+ };
1098
+ const decorate = (state: EditorState): DecorationSet => {
1099
+ const { doc, selection } = state;
1100
+ const selected =
1101
+ selection instanceof NodeSelection && selection.node.type.name === CHECK_NODE ? selection.from : null;
1102
+ const decorations: Decoration[] = [];
1103
+ const containers: { node: PMNode; start: number }[] = [];
1104
+ if (doc.firstChild?.type.name === PAGE) {
1105
+ doc.forEach((page, offset) => {
1106
+ containers.push({ node: page, start: offset + 1 });
1107
+ });
1108
+ } else containers.push({ node: doc, start: 0 });
1109
+ for (const container of containers) {
1110
+ // The page's graded questions by id, where they are.
1111
+ const questions = new Map<string, { readonly pos: number; readonly node: PMNode }>();
1112
+ container.node.descendants((node, offset) => {
1113
+ const manifest = types.get(node.type.name);
1114
+ if (manifest === undefined) return true;
1115
+ const id = idOf(node);
1116
+ if (isCheckable(manifest) && id !== null) questions.set(id, { pos: container.start + offset, node });
1117
+ return false;
1118
+ });
1119
+ const tree: CheckTree<PMNode> = {
1120
+ tag: (node) => {
1121
+ const name = node.type.name;
1122
+ if (node.isText) return null;
1123
+ if (name === CHECK_NODE) return CHECK_TAG;
1124
+ if (name === LAYOUT) return COLUMNS_TAG;
1125
+ if (name === LAYOUT_COLUMN) return COLUMN_TAG;
1126
+ return name === 'heading' ? `h${node.attrs.level as number}` : name;
1127
+ },
1128
+ children: childrenOf,
1129
+ component: (node) => {
1130
+ const manifest = types.get(node.type.name);
1131
+ if (manifest === undefined) return null;
1132
+ return { id: idOf(node), graded: isCheckable(manifest), block: manifest.display === 'block' };
1133
+ },
1134
+ checkFor: (node) => {
1135
+ const attributes = node.attrs.attributes as Attributes;
1136
+ return Object.hasOwn(attributes, CHECK_FOR_ATTRIBUTE)
1137
+ ? checkFor(attributes[CHECK_FOR_ATTRIBUTE]).filter((id) => questions.has(id))
1138
+ : null;
1139
+ },
1140
+ };
1141
+ const content = childrenOf(container.node);
1142
+ const { targets, uncovered } = planChecks(checkParts(content, tree));
1143
+ const positions = checkPositions(container.node, container.start);
1144
+ checkNodes(content, tree).forEach((check, k) => {
1145
+ const pos = positions[k];
1146
+ if (pos === undefined) return;
1147
+ const own = targets[k] ?? [];
1148
+ decorations.push(Decoration.node(pos, pos + check.nodeSize, {}, { [CHECK_COUNT]: own.length }));
1149
+ if (pos !== selected) return;
1150
+ for (const id of own) {
1151
+ const question = questions.get(id);
1152
+ if (question !== undefined)
1153
+ decorations.push(
1154
+ Decoration.node(question.pos, question.pos + question.node.nodeSize, { class: CHECK_TARGET }),
1155
+ );
1156
+ }
1157
+ });
1158
+ // After the block, directly in the page or its column, that holds the last question of the run.
1159
+ for (const run of uncovered) {
1160
+ const last = questions.get(run.at(-1) ?? '');
1161
+ if (run.length < 2 || last === undefined) continue;
1162
+ const $last = doc.resolve(last.pos);
1163
+ let depth = $last.depth;
1164
+ while (depth > 0 && ![PAGE, LAYOUT_COLUMN].includes($last.node(depth).type.name)) depth--;
1165
+ const at = $last.after(depth + 1);
1166
+ decorations.push(
1167
+ // Before the tab and the notes of a block framed right there (sides -2 and -1): it ends the run above.
1168
+ Decoration.widget(at, (view, getPos) => automaticDom(view, run, getPos), {
1169
+ key: `auto\n${run.join(' ')}`,
1170
+ side: -3,
1171
+ stopEvent: () => true,
1172
+ ignoreSelection: true,
1173
+ }),
1174
+ );
1175
+ }
1176
+ }
1177
+ return DecorationSet.create(doc, decorations);
1178
+ };
1179
+ const key = new PluginKey<DecorationSet>('astlChecks');
1180
+ return Extension.create({
1181
+ name: 'astlChecks',
1182
+ addProseMirrorPlugins: () => [
1183
+ new Plugin<DecorationSet>({
1184
+ key,
1185
+ state: {
1186
+ init: (_, state) => decorate(state),
1187
+ apply: (tr, previous, _, state) => (tr.docChanged || tr.selectionSet ? decorate(state) : previous),
1188
+ },
1189
+ props: { decorations: (state) => key.getState(state) },
1190
+ }),
1191
+ ],
1192
+ });
1193
+ }
1194
+
948
1195
  /**
949
1196
  * A block with rich children, or with rich content (`content: 'flow'`), framed and labelled in the editor, with its
950
1197
  * picture when it has picture fields.
@@ -1811,8 +2058,8 @@ function figureNode(authoring: Authoring, hooks: PictureHooks | undefined): Node
1811
2058
  /** The node name of `<astl-page>`. */
1812
2059
  const PAGE = 'astlPage';
1813
2060
 
1814
- /** What a page or a document without pages holds: blocks and layouts. */
1815
- const PAGE_CONTENT = `(block | ${LAYOUT} | ${HELP})+`;
2061
+ /** What a page or a document without pages holds: blocks, layouts, help for students and Check elements. */
2062
+ const PAGE_CONTENT = `(block | ${LAYOUT} | ${HELP} | ${CHECK})+`;
1816
2063
 
1817
2064
  /**
1818
2065
  * A layout: `<astl-columns>` with its `<astl-column>`s, under their real tags (nothing in them asks for a node view).
@@ -1848,7 +2095,7 @@ const layoutNodes: readonly Node[] = [
1848
2095
  }),
1849
2096
  Node.create({
1850
2097
  name: LAYOUT_COLUMN,
1851
- content: 'block+',
2098
+ content: `(block | ${CHECK})+`,
1852
2099
  isolating: true,
1853
2100
  selectable: false,
1854
2101
  addAttributes: () => sourceAttributes,
@@ -2762,7 +3009,8 @@ function pastedIdsExtension(
2762
3009
  manifests: readonly ComponentManifest[],
2763
3010
  hooks: PictureHooks | undefined,
2764
3011
  ): Extension {
2765
- const tags = new Map(manifests.map((m) => [nodeName(m.tag), m.tag]));
3012
+ // Check elements share the namespace: a pasted one whose id is taken gets `check-<n>`.
3013
+ const tags = new Map([...manifests.map((m) => [nodeName(m.tag), m.tag] as const), [CHECK_NODE, CHECK_TAG]]);
2766
3014
  const prefixOf = (name: string): string => {
2767
3015
  const tag = tags.get(name);
2768
3016
  if (tag === undefined) return 'page';
@@ -3171,6 +3419,8 @@ export function astrolabeExtensions(
3171
3419
  currentFigureExtension,
3172
3420
  ...componentNodes(manifests, label, zonesView(authoring), authoring),
3173
3421
  ...helpNodes(authoring),
3422
+ checkNode(authoring),
3423
+ checksExtension(authoring, manifests),
3174
3424
  referencesExtension(authoring, manifests),
3175
3425
  childrenExtension(manifests),
3176
3426
  richBlocksExtension(authoring, manifests),