@astl/editor-tiptap 0.1.3 → 0.2.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.
package/dist/styles.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * The built-in surface's stylesheet. Only `surface.ts` imports it, so it loads with TipTap, never in the first load of
3
3
  * `<astl-editor>` (whose own shadow styles are in `element.ts`).
4
4
  */
5
+ import { nativeSelectCss } from '@astl/tokens';
5
6
  import { css, unsafeCSS } from 'lit';
6
7
  /** Class of the element the built-in surface mounts TipTap in, a light-DOM child of `<astl-editor>`. */
7
8
  export const SURFACE_CLASS = 'astl-surface';
@@ -176,13 +177,23 @@ const baseSurfaceStyles = css `
176
177
  .astl-page-delete,
177
178
  .astl-page-add-title,
178
179
  .astl-page-remove-title,
179
- .astl-help-remove
180
+ .astl-page-check,
181
+ .astl-help-remove,
182
+ .astl-help-free
180
183
  ):focus-visible {
181
184
  outline: var(--astl-focus-width) solid var(--astl-focus);
182
185
  outline-offset: var(--astl-focus-offset);
183
186
  }
184
187
  .astl-surface
185
- :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title, .astl-help-remove) {
188
+ :is(
189
+ .astl-page-join,
190
+ .astl-page-delete,
191
+ .astl-page-add-title,
192
+ .astl-page-remove-title,
193
+ .astl-page-check,
194
+ .astl-help-remove,
195
+ .astl-help-free
196
+ ) {
186
197
  min-block-size: var(--astl-target-min);
187
198
  min-inline-size: var(--astl-target-min);
188
199
  padding-inline: var(--astl-space-3);
@@ -194,9 +205,18 @@ const baseSurfaceStyles = css `
194
205
  font-size: var(--astl-size-small);
195
206
  cursor: pointer;
196
207
  }
197
- .astl-surface :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title)[hidden] {
208
+ .astl-surface
209
+ :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title, .astl-help-free)[hidden] {
198
210
  display: none;
199
211
  }
212
+ /* "Check this page" pressed: filled, with a tick beside its words, so the state shows without colour alone. */
213
+ .astl-surface .astl-page-check[aria-pressed='true'] {
214
+ border-color: var(--astl-accent);
215
+ background: var(--astl-accent-soft);
216
+ }
217
+ .astl-surface .astl-page-check[aria-pressed='true']::before {
218
+ content: '✓ ' / '';
219
+ }
200
220
  /* Beside the title the button keeps its words whole (it broke "Remove" at 320 px): the title gives way instead. */
201
221
  .astl-surface .astl-page-remove-title {
202
222
  flex: none;
@@ -544,11 +564,23 @@ const baseSurfaceStyles = css `
544
564
  outline: var(--astl-focus-width) solid var(--astl-focus);
545
565
  outline-offset: var(--astl-focus-offset);
546
566
  }
567
+ ${unsafeCSS(nativeSelectCss('.astl-surface .astl-help-head select'))}
547
568
  /* "Remove" closes the head, a page button's look. */
548
569
  .astl-surface .astl-help-remove {
549
570
  margin-inline-start: auto;
550
571
  font-weight: 400;
551
572
  }
573
+ /* A page's hint with a cost (an older source): what it costs, and "Make it free". */
574
+ .astl-surface .astl-help-notice {
575
+ flex: 1 1 12rem;
576
+ font-weight: 400;
577
+ }
578
+ .astl-surface .astl-help-free {
579
+ font-weight: 400;
580
+ }
581
+ .astl-surface :is(.astl-help-notice, .astl-help-head select)[hidden] {
582
+ display: none;
583
+ }
552
584
  .astl-surface .astl-help-content > :first-child {
553
585
  margin-block-start: var(--astl-space-1);
554
586
  }
@@ -893,11 +925,12 @@ const zoneStyles = css `
893
925
  `;
894
926
  /** The palette slot of a zone's number: `c1` to `c8`. */
895
927
  const zonePalette = unsafeCSS(Array.from({ length: 8 }, (_, i) => `.astl-surface .astl-zones .c${i + 1}{--line:var(--astl-palette-${i + 1});--soft:var(--astl-palette-${i + 1}-soft)}`).join(''));
896
- // Authors' styles and layouts, as the player draws them: floats stay in their page, no style makes the surface scroll
897
- // sideways, and columns wrap by themselves. A column's edge is the surface's own: the student sees none.
928
+ // Authors' styles and layouts, as the player draws them: floats stay in their page (its content, not its head, which
929
+ // stays a row of its own), no style makes the surface scroll sideways, and columns wrap by themselves. A column's edge
930
+ // is the surface's own: the student sees none.
898
931
  const layoutStyles = css `
899
932
  .astl-surface .ProseMirror,
900
- .astl-surface .astl-page > div {
933
+ .astl-surface .astl-page > div:not(.astl-page-head) {
901
934
  display: flow-root;
902
935
  }
903
936
  .astl-surface .ProseMirror [style] {
@@ -1 +1 @@
1
- {"version":3,"file":"styles.js","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,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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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.1.3",
3
+ "version": "0.2.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.1.3",
57
- "@astl/editor": "0.1.3",
58
- "@astl/engine": "0.1.3",
59
- "@astl/player": "0.1.3",
60
- "@astl/tokens": "0.1.3",
56
+ "@astl/core": "0.2.0",
57
+ "@astl/editor": "0.2.0",
58
+ "@astl/engine": "0.2.0",
59
+ "@astl/player": "0.2.0",
60
+ "@astl/tokens": "0.2.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
@@ -17,7 +17,15 @@ import { closeHistory } from '@tiptap/pm/history';
17
17
  import { DOMSerializer, type Node as PMNode, DOMParser as SchemaParser } from '@tiptap/pm/model';
18
18
  import { NodeSelection, type Selection, TextSelection, type Transaction } from '@tiptap/pm/state';
19
19
  import { cursorIn, isBlockOf, LAYOUT, LAYOUT_COLUMN, moveBlockIn, removeBlockIn } from './blocks.js';
20
- import { addPageIn, attributesOf, mergePageIn, nodeName, removePageIn, splitPageIn } from './nodes.js';
20
+ import {
21
+ addPageIn,
22
+ attributesOf,
23
+ mergePageIn,
24
+ nodeName,
25
+ removePageIn,
26
+ setPageCheckIn,
27
+ splitPageIn,
28
+ } from './nodes.js';
21
29
  import { inert, sourceOf, toHtml } from './source.js';
22
30
  import { createStyles } from './style.js';
23
31
 
@@ -353,6 +361,12 @@ export function createTiptapAdapter(editor: Editor, manifests: readonly Componen
353
361
  if (removed) editor.view.dispatch(tr);
354
362
  return removed;
355
363
  },
364
+ setPageCheck(check, page) {
365
+ const tr = editor.state.tr;
366
+ const changed = setPageCheckIn(tr, check, page);
367
+ if (changed) editor.view.dispatch(tr);
368
+ return changed;
369
+ },
356
370
  format(format, href) {
357
371
  if (!allowed().includes(format)) return false;
358
372
  const active = formatState().active.includes(format);
package/src/nodes.ts CHANGED
@@ -43,6 +43,7 @@ import {
43
43
  MAX_COLUMNS,
44
44
  type MessageParams,
45
45
  MIN_COLUMNS,
46
+ PAGE_CHECK_ATTRIBUTE,
46
47
  TAG_ATTRIBUTES,
47
48
  } from '@astl/core';
48
49
  import { cleanClass, cleanStyle, isStyleName } from '@astl/core/declarations';
@@ -63,6 +64,7 @@ import { type Editor, Extension, type Extensions, Mark, Node } from '@tiptap/cor
63
64
  import { closeHistory } from '@tiptap/pm/history';
64
65
  import {
65
66
  type DOMOutputSpec,
67
+ DOMSerializer,
66
68
  Fragment,
67
69
  type Mark as PMMark,
68
70
  type Node as PMNode,
@@ -87,6 +89,7 @@ import {
87
89
  type SurfaceChange,
88
90
  } from './blocks.js';
89
91
  import { currentLayoutExtension, deleteLayoutIn, isEmptyLayout, layoutAt } from './layouts.js';
92
+ import { inert } from './source.js';
90
93
  import { setPlacedStyle } from './style.js';
91
94
 
92
95
  type Attributes = Record<string, string>;
@@ -614,8 +617,78 @@ function keptHelp(children: readonly Kept[], authoring: Authoring): HTMLElement[
614
617
  });
615
618
  }
616
619
 
617
- /** Costs the select offers, as written in `cost` ('0': free, no attribute). */
618
- const COSTS: readonly string[] = ['0', '0.1', '0.25', '0.5', '1'];
620
+ /** Costs a question's hint offers, as written in `cost` ('0': free, no attribute); a cost besides them stays. */
621
+ const COSTS: readonly string[] = ['0', '0.1', '0.25', '0.5'];
622
+
623
+ /** Whether help in `parent` is the page's: directly on a page, in a column, or in a document without pages. */
624
+ const pageLevel = (parent: PMNode, doc: PMNode): boolean =>
625
+ parent === doc || parent.type.name === PAGE || parent.type.name === LAYOUT_COLUMN;
626
+
627
+ /** The graded questions on the page (or in the document without pages) that holds `pos`: position, node, tag. */
628
+ function pageQuestions(
629
+ doc: PMNode,
630
+ pos: number,
631
+ graded: ReadonlyMap<string, string>,
632
+ ): { readonly pos: number; readonly node: PMNode; readonly tag: string }[] {
633
+ const $pos = doc.resolve(pos);
634
+ let depth = 0;
635
+ for (let d = $pos.depth; d > 0; d--) if ($pos.node(d).type.name === PAGE) depth = d;
636
+ const page = $pos.node(depth);
637
+ const base = depth === 0 ? 0 : $pos.before(depth) + 1;
638
+ const found: { pos: number; node: PMNode; tag: string }[] = [];
639
+ page.descendants((node, offset) => {
640
+ const tag = graded.get(node.type.name);
641
+ if (tag === undefined) return true;
642
+ found.push({ pos: base + offset, node, tag });
643
+ return false;
644
+ });
645
+ return found;
646
+ }
647
+
648
+ /** Whether a question has an explanation already: a child box of a block, kept data of an atom. */
649
+ const explained = (node: PMNode): boolean =>
650
+ node.isAtom
651
+ ? ((node.attrs.children as readonly Kept[] | undefined) ?? []).some(
652
+ (c) => typeof c !== 'string' && c.tag === EXPLANATION_TAG,
653
+ )
654
+ : node.children.some((c) => c.type.name === nodeName(EXPLANATION_TAG));
655
+
656
+ /**
657
+ * Moves the help box at `pos` (on the page) into the question at `target`, after its other help, in one undo step. A
658
+ * block takes the box as it is; an atom (a field in a line of text) keeps it as data, its paragraphs joined by line
659
+ * breaks, as an inline component's help holds them. A page or a column the box leaves empty keeps an empty paragraph.
660
+ */
661
+ function moveHelpIn(editor: Editor, pos: number, target: number, change: SurfaceChange): boolean {
662
+ const { state } = editor;
663
+ const box = state.doc.nodeAt(pos);
664
+ const owner = state.doc.nodeAt(target);
665
+ const paragraph = state.schema.nodes.paragraph;
666
+ if (box === null || owner === null || paragraph === undefined) return false;
667
+ const tr = closeHistory(state.tr);
668
+ if (owner.isAtom) {
669
+ const dom = DOMSerializer.fromSchema(state.schema).serializeNode(box, { document: inert }) as Element;
670
+ const inline = inert.createElement(dom.localName);
671
+ for (const attribute of [...dom.attributes]) inline.setAttribute(attribute.name, attribute.value);
672
+ [...dom.children].forEach((block, i) => {
673
+ if (i > 0) inline.append(inert.createElement('br'));
674
+ if (block.localName === 'p') inline.append(...block.childNodes);
675
+ else inline.append(block.textContent ?? '');
676
+ });
677
+ tr.setNodeAttribute(target, 'children', [
678
+ ...((owner.attrs.children as readonly Kept[] | undefined) ?? []),
679
+ ...keep(inline),
680
+ ]);
681
+ } else {
682
+ if (!owner.canReplace(owner.childCount, owner.childCount, Fragment.from(box))) return false;
683
+ tr.insert(target + owner.nodeSize - 1, box);
684
+ }
685
+ const from = tr.mapping.map(pos);
686
+ if (tr.doc.resolve(from).parent.childCount === 1)
687
+ tr.replaceWith(from, from + box.nodeSize, paragraph.create());
688
+ else tr.delete(from, from + box.nodeSize);
689
+ editor.view.dispatch(tr.setMeta(PAGE_CHANGE, change).scrollIntoView());
690
+ return true;
691
+ }
619
692
 
620
693
  /** Takes the help box at `pos` away in one undo step; a page or a column it leaves empty keeps an empty paragraph. */
621
694
  function removeHelp(editor: Editor, pos: number): void {
@@ -633,6 +706,16 @@ function removeHelp(editor: Editor, pos: number): void {
633
706
  function helpNode(tag: string, authoring?: Authoring): Node {
634
707
  const name = nodeName(tag);
635
708
  const t = (key: string, params?: MessageParams) => authoring?.t(EDITOR_NAMESPACE, key, params) ?? key;
709
+ /** Node names of the graded component types, to their tags: the questions page help can move into. */
710
+ const graded = new Map(
711
+ (authoring?.engine.manifests ?? []).flatMap((m) => (m.graded ? [[nodeName(m.tag), m.tag] as const] : [])),
712
+ );
713
+ const nameOf = (question: { readonly node: PMNode; readonly tag: string }) => {
714
+ const descriptor = authoring?.descriptor(question.tag);
715
+ const label = descriptor ? authoring?.t(descriptor.manifest.messages, descriptor.label) : undefined;
716
+ const id = (question.node.attrs.attributes as Attributes).id ?? '';
717
+ return t('surface.moveTarget', { name: label && label !== descriptor?.label ? label : question.tag, id });
718
+ };
636
719
  return Node.create({
637
720
  name,
638
721
  group: HELP,
@@ -682,6 +765,70 @@ function helpNode(tag: string, authoring?: Authoring): Node {
682
765
  const select = tag === EXPLANATION_TAG ? null : head.appendChild(document.createElement('select'));
683
766
  const values = tag === HINT_TAG ? COSTS : FEEDBACK_VARIANTS;
684
767
  select?.setAttribute('aria-label', t(tag === HINT_TAG ? 'help.costLabel' : 'help.whenLabel'));
768
+ // A page's hint is free: no cost to pick. One with a cost (an older source) says so, with "Make it free".
769
+ const notice = tag === HINT_TAG ? head.appendChild(document.createElement('span')) : null;
770
+ const free = tag === HINT_TAG ? head.appendChild(document.createElement('button')) : null;
771
+ if (notice !== null && free !== null) {
772
+ notice.className = 'astl-help-notice';
773
+ free.type = 'button';
774
+ free.className = 'astl-help-free';
775
+ free.textContent = t('surface.hintMakeFree');
776
+ free.addEventListener('click', () => {
777
+ const pos = getPos();
778
+ const current = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
779
+ if (current === null || typeof pos !== 'number') return;
780
+ const { cost: _, ...rest } = current.attrs.attributes as Attributes;
781
+ editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', rest));
782
+ editor.view.focus();
783
+ });
784
+ }
785
+ // A hint or the explanation on the page can go into one of its questions.
786
+ const move = tag === FEEDBACK_TAG ? null : head.appendChild(document.createElement('select'));
787
+ move?.classList.add('astl-help-move');
788
+ const questions = () => {
789
+ const pos = getPos();
790
+ return typeof pos === 'number' ? pageQuestions(editor.state.doc, pos, graded) : [];
791
+ };
792
+ // Filled with the box, and again when it is about to be used: the page's questions change while the box stays.
793
+ const fill = () => {
794
+ if (move === null) return;
795
+ const first = Object.assign(document.createElement('option'), {
796
+ value: '',
797
+ textContent: t('surface.moveTo'),
798
+ });
799
+ const found = questions();
800
+ const none = Object.assign(document.createElement('option'), {
801
+ value: '',
802
+ disabled: true,
803
+ textContent: t('surface.moveNone'),
804
+ });
805
+ move.replaceChildren(
806
+ first,
807
+ ...(found.length === 0 ? [none] : []),
808
+ ...found.map((q) => {
809
+ const option = document.createElement('option');
810
+ option.value = String(q.pos);
811
+ // A question has one explanation at most: one that has it already is not a place for another.
812
+ const full = tag === EXPLANATION_TAG && explained(q.node);
813
+ option.textContent = full ? t('surface.moveTargetFull', { name: nameOf(q) }) : nameOf(q);
814
+ option.disabled = full;
815
+ return option;
816
+ }),
817
+ );
818
+ move.value = '';
819
+ };
820
+ move?.setAttribute('aria-label', t('surface.moveLabel', { kind: tag.replace(/^astl-/, '') }));
821
+ move?.addEventListener('focus', fill);
822
+ move?.addEventListener('pointerdown', fill);
823
+ move?.addEventListener('change', () => {
824
+ const pos = getPos();
825
+ const target = move.value === '' ? null : Number(move.value);
826
+ const owner = target === null ? null : editor.state.doc.nodeAt(target);
827
+ const question = owner === null ? undefined : graded.get(owner.type.name);
828
+ if (typeof pos !== 'number' || target === null || owner === null || question === undefined) return;
829
+ const change = { key: 'surface.moved', params: { name: nameOf({ node: owner, tag: question }) } };
830
+ if (moveHelpIn(editor, pos, target, change)) editor.view.focus();
831
+ });
685
832
  select?.addEventListener('change', () => {
686
833
  const pos = getPos();
687
834
  const current = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
@@ -705,23 +852,61 @@ function helpNode(tag: string, authoring?: Authoring): Node {
705
852
  remove.addEventListener('keydown', (event) => historyKey(editor.view, event));
706
853
  const content = dom.appendChild(document.createElement('div'));
707
854
  content.className = 'astl-help-content';
855
+ let shown = node.attrs;
708
856
  const render = (attrs: Record<string, unknown>) => {
857
+ shown = attrs;
709
858
  const attributes = attrs.attributes as Attributes;
859
+ // Where the box is: unknown while the view that creates it is still being built (then said again below).
860
+ let onPage = false;
861
+ try {
862
+ const pos = getPos();
863
+ const parent = typeof pos === 'number' ? editor.state.doc.resolve(pos).parent : null;
864
+ onPage = parent !== null && pageLevel(parent, editor.state.doc);
865
+ } catch {
866
+ onPage = false;
867
+ }
868
+ if (move !== null) {
869
+ move.hidden = !onPage;
870
+ if (onPage) fill();
871
+ }
872
+ const cost = attributes.cost ?? '0';
873
+ if (notice !== null && free !== null) {
874
+ const costly = onPage && Number(cost) !== 0;
875
+ notice.hidden = !costly;
876
+ free.hidden = !costly;
877
+ notice.textContent = costly
878
+ ? t('surface.hintPageCost', { percent: Math.round(Number(cost) * 100) })
879
+ : '';
880
+ }
710
881
  if (select === null) return;
882
+ select.hidden = tag === HINT_TAG && onPage;
711
883
  const value =
712
884
  (tag === HINT_TAG ? attributes.cost : attributes.when) ?? (tag === HINT_TAG ? '0' : '');
713
- const options = values.includes(value) ? values : [...values, value];
885
+ const options = values.includes(value)
886
+ ? values
887
+ : tag === HINT_TAG
888
+ ? [...values, value].sort((a, b) => Number(a) - Number(b))
889
+ : [...values, value];
714
890
  select.replaceChildren(
715
891
  ...options.map((v) => {
716
892
  const option = document.createElement('option');
717
893
  option.value = v;
718
- option.textContent = tag === HINT_TAG ? `${Math.round(Number(v) * 100)}%` : t(`help.when.${v}`);
894
+ option.textContent =
895
+ tag !== HINT_TAG
896
+ ? t(`help.when.${v}`)
897
+ : v === '0'
898
+ ? t('help.costFree')
899
+ : Number.isFinite(Number(v))
900
+ ? `-${Math.round(Number(v) * 100)}%`
901
+ : v;
719
902
  return option;
720
903
  }),
721
904
  );
722
905
  select.value = value;
723
906
  };
724
907
  render(node.attrs);
908
+ // Once the view that creates the box is built, its place is known.
909
+ queueMicrotask(() => render(shown));
725
910
  return {
726
911
  dom,
727
912
  contentDOM: content,
@@ -1604,6 +1789,8 @@ export function pageNode(authoring: Authoring): Node {
1604
1789
  return button;
1605
1790
  };
1606
1791
  const addTitle = named('add-title', t('surface.pageAddTitle'));
1792
+ // "Check this page", pressed while the page has `check`: its own Check in practice, then its help.
1793
+ const check = named('check', t('surface.pageCheck'));
1607
1794
  // Hidden until numbered: the first page has no join button, the only page no delete button.
1608
1795
  const join = Object.assign(document.createElement('button'), {
1609
1796
  type: 'button',
@@ -1673,6 +1860,17 @@ export function pageNode(authoring: Authoring): Node {
1673
1860
  sync();
1674
1861
  addTitle.focus();
1675
1862
  });
1863
+ // The same step as `EditorAdapter.setPageCheck` for this page, found by its place (its id may be missing).
1864
+ check.addEventListener('click', () => {
1865
+ const pos = getPos();
1866
+ if (typeof pos !== 'number') return;
1867
+ const on = !Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE);
1868
+ const tr = editor.state.tr;
1869
+ if (!checkPageAt(tr, pos, on)) return;
1870
+ const page = editor.state.doc.resolve(pos).index(0) + 1;
1871
+ pageStep(tr, { key: on ? 'surface.pageCheckOn' : 'surface.pageCheckOff', params: { page } });
1872
+ editor.view.dispatch(tr);
1873
+ });
1676
1874
  join.addEventListener('click', () => {
1677
1875
  const pos = getPos();
1678
1876
  if (typeof pos !== 'number' || editor.state.doc.resolve(pos).index(0) === 0) return;
@@ -1690,11 +1888,12 @@ export function pageNode(authoring: Authoring): Node {
1690
1888
  // As after a join: the editor takes the focus, the cursor at the start of the page it went to.
1691
1889
  editor.view.focus();
1692
1890
  });
1693
- head.append(number, addTitle, join, remove, field);
1891
+ head.append(number, addTitle, check, join, remove, field);
1694
1892
  const contentDOM = document.createElement('div');
1695
1893
  // The page's look is for its content, not for the controls in its head.
1696
1894
  const paint = painter(contentDOM, 'astl-page');
1697
1895
  paint(attributes);
1896
+ check.setAttribute('aria-pressed', String(Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE)));
1698
1897
  dom.append(head, contentDOM);
1699
1898
  input.value = attributes.label ?? '';
1700
1899
  sync();
@@ -1709,6 +1908,7 @@ export function pageNode(authoring: Authoring): Node {
1709
1908
  attributes = attrs.attributes as Attributes;
1710
1909
  paint(attributes);
1711
1910
  if (input.value !== (attributes.label ?? '')) input.value = attributes.label ?? '';
1911
+ check.setAttribute('aria-pressed', String(Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE)));
1712
1912
  sync();
1713
1913
  };
1714
1914
  return {
@@ -1838,6 +2038,45 @@ export function addPageIn(tr: Transaction, after?: string): string {
1838
2038
  return id;
1839
2039
  }
1840
2040
 
2041
+ /** Sets or clears `check` on the page at `pos` in `tr`: whether it changed. */
2042
+ function checkPageAt(tr: Transaction, pos: number, check: boolean): boolean {
2043
+ const node = tr.doc.nodeAt(pos);
2044
+ if (node?.type.name !== PAGE) return false;
2045
+ const attributes = node.attrs.attributes as Attributes;
2046
+ if (Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE) === check) return false;
2047
+ const { [PAGE_CHECK_ATTRIBUTE]: _, ...rest } = attributes;
2048
+ tr.setNodeAttribute(pos, 'attributes', check ? { ...rest, [PAGE_CHECK_ATTRIBUTE]: '' } : rest);
2049
+ return true;
2050
+ }
2051
+
2052
+ /**
2053
+ * Sets or clears `check` on the page `id`, or on every page without one, in `tr` (`EditorAdapter.setPageCheck`, and the
2054
+ * page head's "Check this page" through `checkPageAt`): a document without pages becomes one page with a fresh id and
2055
+ * `check` first. One undo step, said on the notice line; false, changing nothing, when it was so already or for an id
2056
+ * that is not a page's.
2057
+ */
2058
+ export function setPageCheckIn(tr: Transaction, check: boolean, id?: string): boolean {
2059
+ const type = tr.doc.type.schema.nodes[PAGE];
2060
+ if (type === undefined) return false;
2061
+ if (tr.doc.firstChild?.type !== type) {
2062
+ if (!check || id !== undefined) return false;
2063
+ intoPages(tr, idsIn(tr.doc));
2064
+ }
2065
+ const changed: number[] = [];
2066
+ tr.doc.forEach((node, pos, index) => {
2067
+ if (id !== undefined && (node.attrs.attributes as Attributes | undefined)?.id !== id) return;
2068
+ if (checkPageAt(tr, pos, check)) changed.push(index + 1);
2069
+ });
2070
+ if (changed.length === 0) return false;
2071
+ pageStep(
2072
+ tr,
2073
+ id !== undefined || tr.doc.childCount === 1
2074
+ ? { key: check ? 'surface.pageCheckOn' : 'surface.pageCheckOff', params: { page: changed[0] ?? 1 } }
2075
+ : { key: check ? 'surface.pagesCheckOn' : 'surface.pagesCheckOff', params: {} },
2076
+ );
2077
+ return true;
2078
+ }
2079
+
1841
2080
  /**
1842
2081
  * Joins the page `id` to the page before it in `tr` (`EditorAdapter.mergePage`), the cursor at the start of the joined
1843
2082
  * content: false, changing nothing, for the first page or an id that is not a page's.
package/src/styles.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * The built-in surface's stylesheet. Only `surface.ts` imports it, so it loads with TipTap, never in the first load of
3
3
  * `<astl-editor>` (whose own shadow styles are in `element.ts`).
4
4
  */
5
+ import { nativeSelectCss } from '@astl/tokens';
5
6
  import { type CSSResult, css, unsafeCSS } from 'lit';
6
7
 
7
8
  /** Class of the element the built-in surface mounts TipTap in, a light-DOM child of `<astl-editor>`. */
@@ -178,13 +179,23 @@ const baseSurfaceStyles: CSSResult = css`
178
179
  .astl-page-delete,
179
180
  .astl-page-add-title,
180
181
  .astl-page-remove-title,
181
- .astl-help-remove
182
+ .astl-page-check,
183
+ .astl-help-remove,
184
+ .astl-help-free
182
185
  ):focus-visible {
183
186
  outline: var(--astl-focus-width) solid var(--astl-focus);
184
187
  outline-offset: var(--astl-focus-offset);
185
188
  }
186
189
  .astl-surface
187
- :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title, .astl-help-remove) {
190
+ :is(
191
+ .astl-page-join,
192
+ .astl-page-delete,
193
+ .astl-page-add-title,
194
+ .astl-page-remove-title,
195
+ .astl-page-check,
196
+ .astl-help-remove,
197
+ .astl-help-free
198
+ ) {
188
199
  min-block-size: var(--astl-target-min);
189
200
  min-inline-size: var(--astl-target-min);
190
201
  padding-inline: var(--astl-space-3);
@@ -196,9 +207,18 @@ const baseSurfaceStyles: CSSResult = css`
196
207
  font-size: var(--astl-size-small);
197
208
  cursor: pointer;
198
209
  }
199
- .astl-surface :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title)[hidden] {
210
+ .astl-surface
211
+ :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title, .astl-help-free)[hidden] {
200
212
  display: none;
201
213
  }
214
+ /* "Check this page" pressed: filled, with a tick beside its words, so the state shows without colour alone. */
215
+ .astl-surface .astl-page-check[aria-pressed='true'] {
216
+ border-color: var(--astl-accent);
217
+ background: var(--astl-accent-soft);
218
+ }
219
+ .astl-surface .astl-page-check[aria-pressed='true']::before {
220
+ content: '✓ ' / '';
221
+ }
202
222
  /* Beside the title the button keeps its words whole (it broke "Remove" at 320 px): the title gives way instead. */
203
223
  .astl-surface .astl-page-remove-title {
204
224
  flex: none;
@@ -546,11 +566,23 @@ const baseSurfaceStyles: CSSResult = css`
546
566
  outline: var(--astl-focus-width) solid var(--astl-focus);
547
567
  outline-offset: var(--astl-focus-offset);
548
568
  }
569
+ ${unsafeCSS(nativeSelectCss('.astl-surface .astl-help-head select'))}
549
570
  /* "Remove" closes the head, a page button's look. */
550
571
  .astl-surface .astl-help-remove {
551
572
  margin-inline-start: auto;
552
573
  font-weight: 400;
553
574
  }
575
+ /* A page's hint with a cost (an older source): what it costs, and "Make it free". */
576
+ .astl-surface .astl-help-notice {
577
+ flex: 1 1 12rem;
578
+ font-weight: 400;
579
+ }
580
+ .astl-surface .astl-help-free {
581
+ font-weight: 400;
582
+ }
583
+ .astl-surface :is(.astl-help-notice, .astl-help-head select)[hidden] {
584
+ display: none;
585
+ }
554
586
  .astl-surface .astl-help-content > :first-child {
555
587
  margin-block-start: var(--astl-space-1);
556
588
  }
@@ -904,11 +936,12 @@ const zonePalette = unsafeCSS(
904
936
  ).join(''),
905
937
  );
906
938
 
907
- // Authors' styles and layouts, as the player draws them: floats stay in their page, no style makes the surface scroll
908
- // sideways, and columns wrap by themselves. A column's edge is the surface's own: the student sees none.
939
+ // Authors' styles and layouts, as the player draws them: floats stay in their page (its content, not its head, which
940
+ // stays a row of its own), no style makes the surface scroll sideways, and columns wrap by themselves. A column's edge
941
+ // is the surface's own: the student sees none.
909
942
  const layoutStyles = css`
910
943
  .astl-surface .ProseMirror,
911
- .astl-surface .astl-page > div {
944
+ .astl-surface .astl-page > div:not(.astl-page-head) {
912
945
  display: flow-root;
913
946
  }
914
947
  .astl-surface .ProseMirror [style] {