@astl/editor-tiptap 0.1.4 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/nodes.d.ts CHANGED
@@ -20,7 +20,7 @@
20
20
  * views instead, so no `<astl-*>` custom element is ever created inside the editor (the preview defines them).
21
21
  */
22
22
  import { type ComponentManifest, type EditorDescriptor } from '@astl/core';
23
- import { type Authoring, type PictureFields, type PictureRequest } from '@astl/editor';
23
+ import { type Authoring, type HelpPart, type PictureFields, type PictureRequest } from '@astl/editor';
24
24
  import { type Extensions, Mark, Node } from '@tiptap/core';
25
25
  import { type Transaction } from '@tiptap/pm/state';
26
26
  type Attributes = Record<string, string>;
@@ -98,6 +98,11 @@ export interface PictureHooks {
98
98
  * does).
99
99
  */
100
100
  style?(kind: 'picture' | 'figure' | 'component'): void;
101
+ /**
102
+ * Opens the help for students of the component at the cursor at a part, from a tag on its frame (`<astl-editor>`:
103
+ * `astl-help-open`, else its inspector).
104
+ */
105
+ help?(part: HelpPart): void;
101
106
  }
102
107
  /**
103
108
  * `<astl-page>` with a head: the page's number of all (`surface.pageNumber`), "Add title" (`surface.pageAddTitle`: a
@@ -125,6 +130,13 @@ export declare const PAGE_CHANGE = "astlPageChange";
125
130
  export declare function splitPageIn(tr: Transaction): string | null;
126
131
  /** An empty page after the page `after`, else after the last page, in `tr` (`EditorAdapter.addPage`): its id. */
127
132
  export declare function addPageIn(tr: Transaction, after?: string): string;
133
+ /**
134
+ * Sets or clears `check` on the page `id`, or on every page without one, in `tr` (`EditorAdapter.setPageCheck`, and the
135
+ * page head's "Check this page" through `checkPageAt`): a document without pages becomes one page with a fresh id and
136
+ * `check` first. One undo step, said on the notice line; false, changing nothing, when it was so already or for an id
137
+ * that is not a page's.
138
+ */
139
+ export declare function setPageCheckIn(tr: Transaction, check: boolean, id?: string): boolean;
128
140
  /**
129
141
  * Joins the page `id` to the page before it in `tr` (`EditorAdapter.mergePage`), the cursor at the start of the joined
130
142
  * content: false, changing nothing, for the first page or an id that is not a page's.
@@ -1 +1 @@
1
- {"version":3,"file":"nodes.d.ts","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EAOL,KAAK,iBAAiB,EAGtB,KAAK,gBAAgB,EAetB,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,aAAa,EAClB,KAAK,cAAc,EAQpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAA0B,KAAK,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AASnF,OAAO,EAML,KAAK,WAAW,EACjB,MAAM,kBAAkB,CAAC;AAc1B,KAAK,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAOzC,qGAAqG;AACrG,eAAO,MAAM,QAAQ,QAAS,MAAM,KAAG,MACyB,CAAC;AAQjE;;;;GAIG;AACH,eAAO,MAAM,YAAY,OAAQ,OAAO,KAAG,UAQxC,CAAC;AAqDJ;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,iBAAiB,GAAG,gBAAgB,CAAC;IACpD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,oHAAoH;AACpH,UAAU,MAAM;IACd,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;CACjC;AAED,sHAAsH;AACtH,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,YAAY,EAAE,CAAC;IAC7C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG;QACnB,QAAQ,CAAC,GAAG,EAAE,WAAW,C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AAE,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAajE;AAaD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAoBpF;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAKhE;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAGjE;AAwOD,OAAO,QAAQ,cAAc,CAAC;IAC5B,UAAU,QAAQ,CAAC,UAAU;QAC3B,QAAQ,EAAE;YACR,4FAA4F;YAC5F,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,UAAU,CAAC;SACvC,CAAC;KACH;CACF;AAipBD,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,SAAS,EACpB,SAAS,GAAE,SAAS,iBAAiB,EAA+B,EACpE,OAAO,GAAE;IAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAA;CAAO,GACjD,UAAU,CA4CZ"}
package/dist/nodes.js CHANGED
@@ -19,17 +19,18 @@
19
19
  * `renderHTML` writes the real `<astl-*>` tags (that is what `getSource()` serialises). The editing surface uses node
20
20
  * views instead, so no `<astl-*>` custom element is ever created inside the editor (the preview defines them).
21
21
  */
22
- import { CAPTION_TAG, CLASS_ATTRIBUTE, COLUMN_TAG, COLUMNS_TAG, CONTENT_ATTRIBUTES, childTagConflict, directionOf, EXPLANATION_TAG, FEEDBACK_TAG, FEEDBACK_VARIANTS, FIGURE_TAG, HELP_TAGS, HINT_TAG, INERT_STYLE, inertStyles, isSourceId, MAX_COLUMNS, MIN_COLUMNS, TAG_ATTRIBUTES, } from '@astl/core';
22
+ import { CAPTION_TAG, CLASS_ATTRIBUTE, COLUMN_TAG, COLUMNS_TAG, CONTENT_ATTRIBUTES, childTagConflict, directionOf, EXPLANATION_TAG, FEEDBACK_TAG, FEEDBACK_VARIANTS, FIGURE_TAG, HELP_TAGS, HINT_TAG, INERT_STYLE, inertStyles, isSourceId, MAX_COLUMNS, MIN_COLUMNS, PAGE_CHECK_ATTRIBUTE, TAG_ATTRIBUTES, } from '@astl/core';
23
23
  import { cleanClass, cleanStyle, isStyleName } from '@astl/core/declarations';
24
24
  import { EDITOR_NAMESPACE, pictureHtml, pictureSize, positionOf, splitPlacement, widthChanges, widthPercent, } from '@astl/editor';
25
25
  import { Extension, Mark, Node } from '@tiptap/core';
26
26
  import { closeHistory } from '@tiptap/pm/history';
27
- import { Fragment, Slice, } from '@tiptap/pm/model';
28
- import { NodeSelection, Plugin, Selection, TextSelection, } from '@tiptap/pm/state';
27
+ import { DOMSerializer, Fragment, Slice, } from '@tiptap/pm/model';
28
+ import { NodeSelection, Plugin, PluginKey, Selection, TextSelection, } from '@tiptap/pm/state';
29
29
  import { Decoration, DecorationSet } from '@tiptap/pm/view';
30
30
  import { StarterKit } from '@tiptap/starter-kit';
31
31
  import { blocksExtension, historyKey, LAYOUT, LAYOUT_COLUMN, placeLabel, } from './blocks.js';
32
32
  import { currentLayoutExtension, deleteLayoutIn, isEmptyLayout, layoutAt } from './layouts.js';
33
+ import { inert } from './source.js';
33
34
  import { setPlacedStyle } from './style.js';
34
35
  /** `astl-choice` becomes `astlChoice`: ProseMirror content expressions only take word characters. */
35
36
  export const nodeName = (tag) => tag.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
@@ -265,7 +266,7 @@ function summary(manifest, attrs) {
265
266
  const main = manifest?.attributes.find((a) => a.type === 'string')?.name;
266
267
  if (main !== undefined)
267
268
  return attrs.attributes[main] ?? '';
268
- // Its help for students is not what it holds: the frame shows it apart (`keptHelp`).
269
+ // Its help for students is not what it holds: the frame's tags say it, the card edits it.
269
270
  const children = attrs.children.filter((c) => typeof c === 'string' || !HELP_TAGS.has(c.tag));
270
271
  const elements = children.filter((c) => typeof c !== 'string');
271
272
  return elements.length > 0
@@ -302,6 +303,7 @@ const SVG_ICONS = {
302
303
  check: 'M4 10.5l4 4 8-9',
303
304
  warning: 'M10 3l8 14H2zM10 8v4M10 14.5v.5',
304
305
  add: 'M10 4v12M4 10h12',
306
+ remove: 'M5 5l10 10M15 5L5 15',
305
307
  };
306
308
  /** A 20 by 20 line icon, hidden from assistive technology. */
307
309
  function lineIcon(name, className) {
@@ -405,9 +407,7 @@ function atomNode(tag, manifest, label, place, pictures, authoring) {
405
407
  dom.append(picture.dom);
406
408
  const body = dom.appendChild(document.createElement('div'));
407
409
  body.className = 'astl-atom-summary';
408
- // Its help for students, read only (the inspector edits it), under what it holds.
409
- const help = dom.appendChild(document.createElement('div'));
410
- help.className = 'astl-help-kept';
410
+ // Its help for students is not drawn: the frame's tags say what it has, the card edits it.
411
411
  // Inside the frame, not on it: an author's border must not hide what the frame says.
412
412
  const paint = placedPainter(body, tag);
413
413
  const showPlace = authoring === undefined ? null : componentPlacing(authoring, dom, editor, getPos);
@@ -418,8 +418,6 @@ function atomNode(tag, manifest, label, place, pictures, authoring) {
418
418
  // The picture shows the zones and pins; a summary of them would only repeat them.
419
419
  body.textContent = picture === null ? summary(manifest, attrs) : '';
420
420
  body.hidden = picture !== null;
421
- if (authoring !== undefined)
422
- help.replaceChildren(...keptHelp(attrs.children, authoring));
423
421
  };
424
422
  render(node.attrs);
425
423
  return {
@@ -444,38 +442,81 @@ const FLOW = `(${FLOW_NODES.join(' | ')})+`;
444
442
  /**
445
443
  * The group of the help for students (`<astl-hint>`, `<astl-explanation>`, `<astl-feedback>`): rich content in a page
446
444
  * or in a block component, never in a column or in text (an inline component or an atom keeps its own as data). In
447
- * the editor, a labelled box; its select sets a hint's cost or the verdict a feedback text is for.
445
+ * the editor, a page's is a labelled box; its select sets the verdict a feedback text is for. A block component's is
446
+ * kept, not drawn (the frame's tags say it, the card edits it): it cannot be selected, so no key reaches it unseen.
448
447
  */
449
448
  const HELP = 'help';
450
- /** The label of a help element in the UI language: what it is, with its cost or its verdict. */
451
- function helpLabel(tag, attributes, t) {
452
- if (tag === HINT_TAG) {
453
- const percent = Math.round(Number(attributes.cost ?? 0) * 100);
454
- return percent > 0 ? `${t('insert.hint')}, ${t('help.cost', { percent })}` : t('insert.hint');
455
- }
456
- if (tag === EXPLANATION_TAG)
457
- return t('insert.explanation');
458
- return `${t('insert.feedback')}: ${t(`help.when.${attributes.when ?? ''}`)}`;
459
- }
460
- /** An atom's help kept as data, as read-only labelled boxes. */
461
- function keptHelp(children, authoring) {
462
- const t = (key, params) => authoring.t(EDITOR_NAMESPACE, key, params);
463
- return children.flatMap((c) => {
464
- if (typeof c === 'string' || !HELP_TAGS.has(c.tag))
465
- return [];
466
- const box = document.createElement('div');
467
- box.className = 'astl-help-box';
468
- box.dataset.help = c.tag;
469
- const head = box.appendChild(document.createElement('div'));
470
- head.className = 'astl-help-head';
471
- head.lang = authoring.locale;
472
- head.textContent = helpLabel(c.tag, c.attributes, t);
473
- box.appendChild(document.createElement('div')).textContent = textOf(c).trim();
474
- return [box];
449
+ /** Costs a question's hint offers, as written in `cost` ('0': free, no attribute); a cost besides them stays. */
450
+ const COSTS = ['0', '0.1', '0.25', '0.5'];
451
+ /** Whether help in `parent` is the page's: directly on a page, in a column, or in a document without pages. */
452
+ const pageLevel = (parent, doc) => parent === doc || parent.type.name === PAGE || parent.type.name === LAYOUT_COLUMN;
453
+ /** The graded questions on the page (or in the document without pages) that holds `pos`: position, node, tag. */
454
+ function pageQuestions(doc, pos, graded) {
455
+ const $pos = doc.resolve(pos);
456
+ let depth = 0;
457
+ for (let d = $pos.depth; d > 0; d--)
458
+ if ($pos.node(d).type.name === PAGE)
459
+ depth = d;
460
+ const page = $pos.node(depth);
461
+ const base = depth === 0 ? 0 : $pos.before(depth) + 1;
462
+ const found = [];
463
+ page.descendants((node, offset) => {
464
+ const tag = graded.get(node.type.name);
465
+ if (tag === undefined)
466
+ return true;
467
+ found.push({ pos: base + offset, node, tag });
468
+ return false;
475
469
  });
470
+ return found;
471
+ }
472
+ /** Whether a question has an explanation already: a child box of a block, kept data of an atom. */
473
+ const explained = (node) => node.isAtom
474
+ ? (node.attrs.children ?? []).some((c) => typeof c !== 'string' && c.tag === EXPLANATION_TAG)
475
+ : node.children.some((c) => c.type.name === nodeName(EXPLANATION_TAG));
476
+ /**
477
+ * Moves the help box at `pos` (on the page) into the question at `target`, after its other help, in one undo step. A
478
+ * 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
479
+ * breaks, as an inline component's help holds them. A page or a column the box leaves empty keeps an empty paragraph.
480
+ */
481
+ function moveHelpIn(editor, pos, target, change) {
482
+ const { state } = editor;
483
+ const box = state.doc.nodeAt(pos);
484
+ const owner = state.doc.nodeAt(target);
485
+ const paragraph = state.schema.nodes.paragraph;
486
+ if (box === null || owner === null || paragraph === undefined)
487
+ return false;
488
+ const tr = closeHistory(state.tr);
489
+ if (owner.isAtom) {
490
+ const dom = DOMSerializer.fromSchema(state.schema).serializeNode(box, { document: inert });
491
+ const inline = inert.createElement(dom.localName);
492
+ for (const attribute of [...dom.attributes])
493
+ inline.setAttribute(attribute.name, attribute.value);
494
+ [...dom.children].forEach((block, i) => {
495
+ if (i > 0)
496
+ inline.append(inert.createElement('br'));
497
+ if (block.localName === 'p')
498
+ inline.append(...block.childNodes);
499
+ else
500
+ inline.append(block.textContent ?? '');
501
+ });
502
+ tr.setNodeAttribute(target, 'children', [
503
+ ...(owner.attrs.children ?? []),
504
+ ...keep(inline),
505
+ ]);
506
+ }
507
+ else {
508
+ if (!owner.canReplace(owner.childCount, owner.childCount, Fragment.from(box)))
509
+ return false;
510
+ tr.insert(target + owner.nodeSize - 1, box);
511
+ }
512
+ const from = tr.mapping.map(pos);
513
+ if (tr.doc.resolve(from).parent.childCount === 1)
514
+ tr.replaceWith(from, from + box.nodeSize, paragraph.create());
515
+ else
516
+ tr.delete(from, from + box.nodeSize);
517
+ editor.view.dispatch(tr.setMeta(PAGE_CHANGE, change).scrollIntoView());
518
+ return true;
476
519
  }
477
- /** Costs the select offers, as written in `cost` ('0': free, no attribute). */
478
- const COSTS = ['0', '0.1', '0.25', '0.5', '1'];
479
520
  /** Takes the help box at `pos` away in one undo step; a page or a column it leaves empty keeps an empty paragraph. */
480
521
  function removeHelp(editor, pos) {
481
522
  const { state } = editor;
@@ -493,12 +534,21 @@ function removeHelp(editor, pos) {
493
534
  function helpNode(tag, authoring) {
494
535
  const name = nodeName(tag);
495
536
  const t = (key, params) => authoring?.t(EDITOR_NAMESPACE, key, params) ?? key;
537
+ /** Node names of the graded component types, to their tags: the questions page help can move into. */
538
+ const graded = new Map((authoring?.engine.manifests ?? []).flatMap((m) => (m.graded ? [[nodeName(m.tag), m.tag]] : [])));
539
+ const nameOf = (question) => {
540
+ const descriptor = authoring?.descriptor(question.tag);
541
+ const label = descriptor ? authoring?.t(descriptor.manifest.messages, descriptor.label) : undefined;
542
+ const id = question.node.attrs.attributes.id ?? '';
543
+ return t('surface.moveTarget', { name: label && label !== descriptor?.label ? label : question.tag, id });
544
+ };
496
545
  return Node.create({
497
546
  name,
498
547
  group: HELP,
499
548
  content: FLOW,
500
549
  defining: true,
501
550
  isolating: true,
551
+ selectable: false,
502
552
  addAttributes: () => sourceAttributes,
503
553
  parseHTML: () => [{ tag }],
504
554
  renderHTML: ({ node }) => [tag, { ...node.attrs.attributes }, 0],
@@ -539,6 +589,70 @@ function helpNode(tag, authoring) {
539
589
  const select = tag === EXPLANATION_TAG ? null : head.appendChild(document.createElement('select'));
540
590
  const values = tag === HINT_TAG ? COSTS : FEEDBACK_VARIANTS;
541
591
  select?.setAttribute('aria-label', t(tag === HINT_TAG ? 'help.costLabel' : 'help.whenLabel'));
592
+ // A page's hint is free: no cost to pick. One with a cost (an older source) says so, with "Make it free".
593
+ const notice = tag === HINT_TAG ? head.appendChild(document.createElement('span')) : null;
594
+ const free = tag === HINT_TAG ? head.appendChild(document.createElement('button')) : null;
595
+ if (notice !== null && free !== null) {
596
+ notice.className = 'astl-help-notice';
597
+ free.type = 'button';
598
+ free.className = 'astl-help-free';
599
+ free.textContent = t('surface.hintMakeFree');
600
+ free.addEventListener('click', () => {
601
+ const pos = getPos();
602
+ const current = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
603
+ if (current === null || typeof pos !== 'number')
604
+ return;
605
+ const { cost: _, ...rest } = current.attrs.attributes;
606
+ editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', rest));
607
+ editor.view.focus();
608
+ });
609
+ }
610
+ // A hint or the explanation on the page can go into one of its questions.
611
+ const move = tag === FEEDBACK_TAG ? null : head.appendChild(document.createElement('select'));
612
+ move?.classList.add('astl-help-move');
613
+ const questions = () => {
614
+ const pos = getPos();
615
+ return typeof pos === 'number' ? pageQuestions(editor.state.doc, pos, graded) : [];
616
+ };
617
+ // Filled with the box, and again when it is about to be used: the page's questions change while the box stays.
618
+ const fill = () => {
619
+ if (move === null)
620
+ return;
621
+ const first = Object.assign(document.createElement('option'), {
622
+ value: '',
623
+ textContent: t('surface.moveTo'),
624
+ });
625
+ const found = questions();
626
+ const none = Object.assign(document.createElement('option'), {
627
+ value: '',
628
+ disabled: true,
629
+ textContent: t('surface.moveNone'),
630
+ });
631
+ move.replaceChildren(first, ...(found.length === 0 ? [none] : []), ...found.map((q) => {
632
+ const option = document.createElement('option');
633
+ option.value = String(q.pos);
634
+ // A question has one explanation at most: one that has it already is not a place for another.
635
+ const full = tag === EXPLANATION_TAG && explained(q.node);
636
+ option.textContent = full ? t('surface.moveTargetFull', { name: nameOf(q) }) : nameOf(q);
637
+ option.disabled = full;
638
+ return option;
639
+ }));
640
+ move.value = '';
641
+ };
642
+ move?.setAttribute('aria-label', t('surface.moveLabel', { kind: tag.replace(/^astl-/, '') }));
643
+ move?.addEventListener('focus', fill);
644
+ move?.addEventListener('pointerdown', fill);
645
+ move?.addEventListener('change', () => {
646
+ const pos = getPos();
647
+ const target = move.value === '' ? null : Number(move.value);
648
+ const owner = target === null ? null : editor.state.doc.nodeAt(target);
649
+ const question = owner === null ? undefined : graded.get(owner.type.name);
650
+ if (typeof pos !== 'number' || target === null || owner === null || question === undefined)
651
+ return;
652
+ const change = { key: 'surface.moved', params: { name: nameOf({ node: owner, tag: question }) } };
653
+ if (moveHelpIn(editor, pos, target, change))
654
+ editor.view.focus();
655
+ });
542
656
  select?.addEventListener('change', () => {
543
657
  const pos = getPos();
544
658
  const current = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
@@ -551,11 +665,14 @@ function helpNode(tag, authoring) {
551
665
  attributes[tag === HINT_TAG ? 'cost' : 'when'] = select.value;
552
666
  editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', attributes));
553
667
  });
554
- // "Remove" at the end of the head, named by the kind of box: a box the menu added empty has no other way out.
668
+ // "Remove" at the end of the head, an icon named by the kind of box: a box the menu added empty has no other way
669
+ // out.
555
670
  const remove = head.appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
556
671
  remove.className = 'astl-help-remove';
557
- remove.textContent = t('inspector.remove');
558
- remove.setAttribute('aria-label', t('surface.helpRemove', { kind: tag.replace(/^astl-/, '') }));
672
+ remove.append(lineIcon('remove', 'astl-icon'));
673
+ const removeName = t('surface.helpRemove', { kind: tag.replace(/^astl-/, '') });
674
+ remove.setAttribute('aria-label', removeName);
675
+ remove.title = removeName;
559
676
  remove.addEventListener('click', () => {
560
677
  const pos = getPos();
561
678
  if (typeof pos !== 'number')
@@ -566,21 +683,61 @@ function helpNode(tag, authoring) {
566
683
  remove.addEventListener('keydown', (event) => historyKey(editor.view, event));
567
684
  const content = dom.appendChild(document.createElement('div'));
568
685
  content.className = 'astl-help-content';
686
+ let shown = node.attrs;
569
687
  const render = (attrs) => {
688
+ shown = attrs;
570
689
  const attributes = attrs.attributes;
690
+ // Where the box is: unknown while the view that creates it is still being built (then said again below).
691
+ let onPage = false;
692
+ try {
693
+ const pos = getPos();
694
+ const parent = typeof pos === 'number' ? editor.state.doc.resolve(pos).parent : null;
695
+ onPage = parent !== null && pageLevel(parent, editor.state.doc);
696
+ }
697
+ catch {
698
+ onPage = false;
699
+ }
700
+ if (move !== null) {
701
+ move.hidden = !onPage;
702
+ if (onPage)
703
+ fill();
704
+ }
705
+ const cost = attributes.cost ?? '0';
706
+ if (notice !== null && free !== null) {
707
+ const costly = onPage && Number(cost) !== 0;
708
+ notice.hidden = !costly;
709
+ free.hidden = !costly;
710
+ notice.textContent = costly
711
+ ? t('surface.hintPageCost', { percent: Math.round(Number(cost) * 100) })
712
+ : '';
713
+ }
571
714
  if (select === null)
572
715
  return;
716
+ select.hidden = tag === HINT_TAG && onPage;
573
717
  const value = (tag === HINT_TAG ? attributes.cost : attributes.when) ?? (tag === HINT_TAG ? '0' : '');
574
- const options = values.includes(value) ? values : [...values, value];
718
+ const options = values.includes(value)
719
+ ? values
720
+ : tag === HINT_TAG
721
+ ? [...values, value].sort((a, b) => Number(a) - Number(b))
722
+ : [...values, value];
575
723
  select.replaceChildren(...options.map((v) => {
576
724
  const option = document.createElement('option');
577
725
  option.value = v;
578
- option.textContent = tag === HINT_TAG ? `${Math.round(Number(v) * 100)}%` : t(`help.when.${v}`);
726
+ option.textContent =
727
+ tag !== HINT_TAG
728
+ ? t(`help.when.${v}`)
729
+ : v === '0'
730
+ ? t('help.costFree')
731
+ : Number.isFinite(Number(v))
732
+ ? `-${Math.round(Number(v) * 100)}%`
733
+ : v;
579
734
  return option;
580
735
  }));
581
736
  select.value = value;
582
737
  };
583
738
  render(node.attrs);
739
+ // Once the view that creates the box is built, its place is known.
740
+ queueMicrotask(() => render(shown));
584
741
  return {
585
742
  dom,
586
743
  contentDOM: content,
@@ -600,6 +757,44 @@ function helpNode(tag, authoring) {
600
757
  export function helpNodes(authoring) {
601
758
  return [HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map((tag) => helpNode(tag, authoring));
602
759
  }
760
+ /**
761
+ * An empty help box's line says what to write there ("Write the hint..."): the box keeps one empty paragraph to type
762
+ * in, and a decoration marks it (`astl-help-empty`, the text in `data-placeholder`, which the surface's style shows).
763
+ */
764
+ function helpPlaceholderExtension(authoring) {
765
+ const kinds = new Map([HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map((tag) => [nodeName(tag), tag.replace(/^astl-/, '')]));
766
+ const decorate = (doc) => {
767
+ const decorations = [];
768
+ doc.descendants((node, pos) => {
769
+ const kind = kinds.get(node.type.name);
770
+ if (kind === undefined)
771
+ return !node.isTextblock;
772
+ const line = node.firstChild;
773
+ if (node.childCount === 1 && line !== null && line.isTextblock && line.content.size === 0) {
774
+ decorations.push(Decoration.node(pos + 1, pos + 1 + line.nodeSize, {
775
+ class: 'astl-help-empty',
776
+ 'data-placeholder': authoring.t(EDITOR_NAMESPACE, 'surface.helpPlaceholder', { kind }),
777
+ }));
778
+ }
779
+ return false;
780
+ });
781
+ return DecorationSet.create(doc, decorations);
782
+ };
783
+ const key = new PluginKey('astlHelpPlaceholder');
784
+ return Extension.create({
785
+ name: 'astlHelpPlaceholder',
786
+ addProseMirrorPlugins: () => [
787
+ new Plugin({
788
+ key,
789
+ state: {
790
+ init: (_, state) => decorate(state.doc),
791
+ apply: (tr, previous) => (tr.docChanged ? decorate(tr.doc) : previous),
792
+ },
793
+ props: { decorations: (state) => key.getState(state) },
794
+ }),
795
+ ],
796
+ });
797
+ }
603
798
  /**
604
799
  * A block with rich children, or with rich content (`content: 'flow'`), framed and labelled in the editor, with its
605
800
  * picture when it has picture fields.
@@ -641,6 +836,9 @@ function blockNode(manifest, pictures, authoring) {
641
836
  addNodeView: () => ({ node, getPos, editor }) => {
642
837
  const dom = document.createElement('div');
643
838
  dom.className = 'astl-block';
839
+ // A graded one's help for students is not drawn (its frame's tags say it, the card edits it); an ungraded one
840
+ // shows help it should not hold, which the engine warns of, so it can be taken away.
841
+ dom.toggleAttribute('data-astl-graded', manifest.graded);
644
842
  const picture = pictures?.layers(manifest.tag).length ? pictures.create(manifest.tag) : null;
645
843
  const contentDOM = document.createElement('div');
646
844
  // Inside the frame, not on it: an author's border must not hide what the frame says.
@@ -1389,6 +1587,8 @@ export function pageNode(authoring) {
1389
1587
  return button;
1390
1588
  };
1391
1589
  const addTitle = named('add-title', t('surface.pageAddTitle'));
1590
+ // "Check this page", pressed while the page has `check`: its own Check in practice, then its help.
1591
+ const check = named('check', t('surface.pageCheck'));
1392
1592
  // Hidden until numbered: the first page has no join button, the only page no delete button.
1393
1593
  const join = Object.assign(document.createElement('button'), {
1394
1594
  type: 'button',
@@ -1457,6 +1657,19 @@ export function pageNode(authoring) {
1457
1657
  sync();
1458
1658
  addTitle.focus();
1459
1659
  });
1660
+ // The same step as `EditorAdapter.setPageCheck` for this page, found by its place (its id may be missing).
1661
+ check.addEventListener('click', () => {
1662
+ const pos = getPos();
1663
+ if (typeof pos !== 'number')
1664
+ return;
1665
+ const on = !Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE);
1666
+ const tr = editor.state.tr;
1667
+ if (!checkPageAt(tr, pos, on))
1668
+ return;
1669
+ const page = editor.state.doc.resolve(pos).index(0) + 1;
1670
+ pageStep(tr, { key: on ? 'surface.pageCheckOn' : 'surface.pageCheckOff', params: { page } });
1671
+ editor.view.dispatch(tr);
1672
+ });
1460
1673
  join.addEventListener('click', () => {
1461
1674
  const pos = getPos();
1462
1675
  if (typeof pos !== 'number' || editor.state.doc.resolve(pos).index(0) === 0)
@@ -1476,11 +1689,12 @@ export function pageNode(authoring) {
1476
1689
  // As after a join: the editor takes the focus, the cursor at the start of the page it went to.
1477
1690
  editor.view.focus();
1478
1691
  });
1479
- head.append(number, addTitle, join, remove, field);
1692
+ head.append(number, addTitle, check, join, remove, field);
1480
1693
  const contentDOM = document.createElement('div');
1481
1694
  // The page's look is for its content, not for the controls in its head.
1482
1695
  const paint = painter(contentDOM, 'astl-page');
1483
1696
  paint(attributes);
1697
+ check.setAttribute('aria-pressed', String(Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE)));
1484
1698
  dom.append(head, contentDOM);
1485
1699
  input.value = attributes.label ?? '';
1486
1700
  sync();
@@ -1497,6 +1711,7 @@ export function pageNode(authoring) {
1497
1711
  paint(attributes);
1498
1712
  if (input.value !== (attributes.label ?? ''))
1499
1713
  input.value = attributes.label ?? '';
1714
+ check.setAttribute('aria-pressed', String(Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE)));
1500
1715
  sync();
1501
1716
  };
1502
1717
  return {
@@ -1625,6 +1840,47 @@ export function addPageIn(tr, after) {
1625
1840
  pageStep(tr, { key: 'surface.pageAdded', params: { page: tr.doc.resolve(at).index(0) + 1 } });
1626
1841
  return id;
1627
1842
  }
1843
+ /** Sets or clears `check` on the page at `pos` in `tr`: whether it changed. */
1844
+ function checkPageAt(tr, pos, check) {
1845
+ const node = tr.doc.nodeAt(pos);
1846
+ if (node?.type.name !== PAGE)
1847
+ return false;
1848
+ const attributes = node.attrs.attributes;
1849
+ if (Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE) === check)
1850
+ return false;
1851
+ const { [PAGE_CHECK_ATTRIBUTE]: _, ...rest } = attributes;
1852
+ tr.setNodeAttribute(pos, 'attributes', check ? { ...rest, [PAGE_CHECK_ATTRIBUTE]: '' } : rest);
1853
+ return true;
1854
+ }
1855
+ /**
1856
+ * Sets or clears `check` on the page `id`, or on every page without one, in `tr` (`EditorAdapter.setPageCheck`, and the
1857
+ * page head's "Check this page" through `checkPageAt`): a document without pages becomes one page with a fresh id and
1858
+ * `check` first. One undo step, said on the notice line; false, changing nothing, when it was so already or for an id
1859
+ * that is not a page's.
1860
+ */
1861
+ export function setPageCheckIn(tr, check, id) {
1862
+ const type = tr.doc.type.schema.nodes[PAGE];
1863
+ if (type === undefined)
1864
+ return false;
1865
+ if (tr.doc.firstChild?.type !== type) {
1866
+ if (!check || id !== undefined)
1867
+ return false;
1868
+ intoPages(tr, idsIn(tr.doc));
1869
+ }
1870
+ const changed = [];
1871
+ tr.doc.forEach((node, pos, index) => {
1872
+ if (id !== undefined && node.attrs.attributes?.id !== id)
1873
+ return;
1874
+ if (checkPageAt(tr, pos, check))
1875
+ changed.push(index + 1);
1876
+ });
1877
+ if (changed.length === 0)
1878
+ return false;
1879
+ pageStep(tr, id !== undefined || tr.doc.childCount === 1
1880
+ ? { key: check ? 'surface.pageCheckOn' : 'surface.pageCheckOff', params: { page: changed[0] ?? 1 } }
1881
+ : { key: check ? 'surface.pagesCheckOn' : 'surface.pagesCheckOff', params: {} });
1882
+ return true;
1883
+ }
1628
1884
  /**
1629
1885
  * Joins the page `id` to the page before it in `tr` (`EditorAdapter.mergePage`), the cursor at the start of the joined
1630
1886
  * content: false, changing nothing, for the first page or an id that is not a page's.
@@ -2538,7 +2794,8 @@ export function astrolabeExtensions(authoring, manifests = authoring.engine.mani
2538
2794
  childrenExtension(manifests),
2539
2795
  richBlocksExtension(authoring, manifests),
2540
2796
  pastedIdsExtension(authoring, manifests, hooks),
2541
- blocksExtension(authoring, (node) => types.get(node.type.name) ?? null, nodeName, hooks?.style === undefined ? undefined : () => hooks.style?.('component')),
2797
+ blocksExtension(authoring, (node) => types.get(node.type.name) ?? null, nodeName, hooks?.style === undefined ? undefined : () => hooks.style?.('component'), hooks?.help === undefined ? undefined : (part) => hooks.help?.(part)),
2798
+ helpPlaceholderExtension(authoring),
2542
2799
  ];
2543
2800
  }
2544
2801
  //# sourceMappingURL=nodes.js.map