@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/CHANGELOG.md +37 -0
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +8 -1
- package/dist/adapter.js.map +1 -1
- package/dist/blocks.d.ts +5 -3
- package/dist/blocks.d.ts.map +1 -1
- package/dist/blocks.js +63 -5
- package/dist/blocks.js.map +1 -1
- package/dist/element.d.ts.map +1 -1
- package/dist/element.js +12 -2
- package/dist/element.js.map +1 -1
- package/dist/nodes.d.ts +13 -1
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +301 -44
- package/dist/nodes.js.map +1 -1
- package/dist/styles.d.ts.map +1 -1
- package/dist/styles.js +91 -12
- package/dist/styles.js.map +1 -1
- package/package.json +6 -6
- package/src/adapter.ts +15 -1
- package/src/blocks.ts +63 -3
- package/src/element.ts +14 -1
- package/src/nodes.ts +309 -43
- package/src/styles.ts +91 -12
package/src/nodes.ts
CHANGED
|
@@ -43,12 +43,14 @@ 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';
|
|
49
50
|
import {
|
|
50
51
|
type Authoring,
|
|
51
52
|
EDITOR_NAMESPACE,
|
|
53
|
+
type HelpPart,
|
|
52
54
|
type PictureFields,
|
|
53
55
|
type PictureRequest,
|
|
54
56
|
type Placement,
|
|
@@ -63,6 +65,7 @@ import { type Editor, Extension, type Extensions, Mark, Node } from '@tiptap/cor
|
|
|
63
65
|
import { closeHistory } from '@tiptap/pm/history';
|
|
64
66
|
import {
|
|
65
67
|
type DOMOutputSpec,
|
|
68
|
+
DOMSerializer,
|
|
66
69
|
Fragment,
|
|
67
70
|
type Mark as PMMark,
|
|
68
71
|
type Node as PMNode,
|
|
@@ -72,6 +75,7 @@ import {
|
|
|
72
75
|
type EditorState,
|
|
73
76
|
NodeSelection,
|
|
74
77
|
Plugin,
|
|
78
|
+
PluginKey,
|
|
75
79
|
Selection,
|
|
76
80
|
TextSelection,
|
|
77
81
|
type Transaction,
|
|
@@ -87,6 +91,7 @@ import {
|
|
|
87
91
|
type SurfaceChange,
|
|
88
92
|
} from './blocks.js';
|
|
89
93
|
import { currentLayoutExtension, deleteLayoutIn, isEmptyLayout, layoutAt } from './layouts.js';
|
|
94
|
+
import { inert } from './source.js';
|
|
90
95
|
import { setPlacedStyle } from './style.js';
|
|
91
96
|
|
|
92
97
|
type Attributes = Record<string, string>;
|
|
@@ -372,7 +377,7 @@ const updater =
|
|
|
372
377
|
function summary(manifest: ComponentManifest | null, attrs: Record<string, unknown>): string {
|
|
373
378
|
const main = manifest?.attributes.find((a) => a.type === 'string')?.name;
|
|
374
379
|
if (main !== undefined) return (attrs.attributes as Attributes)[main] ?? '';
|
|
375
|
-
// Its help for students is not what it holds: the frame
|
|
380
|
+
// Its help for students is not what it holds: the frame's tags say it, the card edits it.
|
|
376
381
|
const children = (attrs.children as readonly Kept[]).filter(
|
|
377
382
|
(c) => typeof c === 'string' || !HELP_TAGS.has(c.tag),
|
|
378
383
|
);
|
|
@@ -425,6 +430,7 @@ const SVG_ICONS = {
|
|
|
425
430
|
check: 'M4 10.5l4 4 8-9',
|
|
426
431
|
warning: 'M10 3l8 14H2zM10 8v4M10 14.5v.5',
|
|
427
432
|
add: 'M10 4v12M4 10h12',
|
|
433
|
+
remove: 'M5 5l10 10M15 5L5 15',
|
|
428
434
|
};
|
|
429
435
|
|
|
430
436
|
/** A 20 by 20 line icon, hidden from assistive technology. */
|
|
@@ -538,9 +544,7 @@ function atomNode(
|
|
|
538
544
|
if (picture !== null) dom.append(picture.dom);
|
|
539
545
|
const body = dom.appendChild(document.createElement('div'));
|
|
540
546
|
body.className = 'astl-atom-summary';
|
|
541
|
-
// Its help for students
|
|
542
|
-
const help = dom.appendChild(document.createElement('div'));
|
|
543
|
-
help.className = 'astl-help-kept';
|
|
547
|
+
// Its help for students is not drawn: the frame's tags say what it has, the card edits it.
|
|
544
548
|
// Inside the frame, not on it: an author's border must not hide what the frame says.
|
|
545
549
|
const paint = placedPainter(body, tag);
|
|
546
550
|
const showPlace = authoring === undefined ? null : componentPlacing(authoring, dom, editor, getPos);
|
|
@@ -551,8 +555,6 @@ function atomNode(
|
|
|
551
555
|
// The picture shows the zones and pins; a summary of them would only repeat them.
|
|
552
556
|
body.textContent = picture === null ? summary(manifest, attrs) : '';
|
|
553
557
|
body.hidden = picture !== null;
|
|
554
|
-
if (authoring !== undefined)
|
|
555
|
-
help.replaceChildren(...keptHelp(attrs.children as readonly Kept[], authoring));
|
|
556
558
|
};
|
|
557
559
|
render(node.attrs);
|
|
558
560
|
return {
|
|
@@ -579,43 +581,83 @@ const FLOW = `(${FLOW_NODES.join(' | ')})+`;
|
|
|
579
581
|
/**
|
|
580
582
|
* The group of the help for students (`<astl-hint>`, `<astl-explanation>`, `<astl-feedback>`): rich content in a page
|
|
581
583
|
* or in a block component, never in a column or in text (an inline component or an atom keeps its own as data). In
|
|
582
|
-
* the editor, a labelled box; its select sets
|
|
584
|
+
* the editor, a page's is a labelled box; its select sets the verdict a feedback text is for. A block component's is
|
|
585
|
+
* kept, not drawn (the frame's tags say it, the card edits it): it cannot be selected, so no key reaches it unseen.
|
|
583
586
|
*/
|
|
584
587
|
const HELP = 'help';
|
|
585
588
|
|
|
586
|
-
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
const
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
head.lang = authoring.locale;
|
|
611
|
-
head.textContent = helpLabel(c.tag, c.attributes, t);
|
|
612
|
-
box.appendChild(document.createElement('div')).textContent = textOf(c).trim();
|
|
613
|
-
return [box];
|
|
589
|
+
/** Costs a question's hint offers, as written in `cost` ('0': free, no attribute); a cost besides them stays. */
|
|
590
|
+
const COSTS: readonly string[] = ['0', '0.1', '0.25', '0.5'];
|
|
591
|
+
|
|
592
|
+
/** Whether help in `parent` is the page's: directly on a page, in a column, or in a document without pages. */
|
|
593
|
+
const pageLevel = (parent: PMNode, doc: PMNode): boolean =>
|
|
594
|
+
parent === doc || parent.type.name === PAGE || parent.type.name === LAYOUT_COLUMN;
|
|
595
|
+
|
|
596
|
+
/** The graded questions on the page (or in the document without pages) that holds `pos`: position, node, tag. */
|
|
597
|
+
function pageQuestions(
|
|
598
|
+
doc: PMNode,
|
|
599
|
+
pos: number,
|
|
600
|
+
graded: ReadonlyMap<string, string>,
|
|
601
|
+
): { readonly pos: number; readonly node: PMNode; readonly tag: string }[] {
|
|
602
|
+
const $pos = doc.resolve(pos);
|
|
603
|
+
let depth = 0;
|
|
604
|
+
for (let d = $pos.depth; d > 0; d--) if ($pos.node(d).type.name === PAGE) depth = d;
|
|
605
|
+
const page = $pos.node(depth);
|
|
606
|
+
const base = depth === 0 ? 0 : $pos.before(depth) + 1;
|
|
607
|
+
const found: { pos: number; node: PMNode; tag: string }[] = [];
|
|
608
|
+
page.descendants((node, offset) => {
|
|
609
|
+
const tag = graded.get(node.type.name);
|
|
610
|
+
if (tag === undefined) return true;
|
|
611
|
+
found.push({ pos: base + offset, node, tag });
|
|
612
|
+
return false;
|
|
614
613
|
});
|
|
614
|
+
return found;
|
|
615
615
|
}
|
|
616
616
|
|
|
617
|
-
/**
|
|
618
|
-
const
|
|
617
|
+
/** Whether a question has an explanation already: a child box of a block, kept data of an atom. */
|
|
618
|
+
const explained = (node: PMNode): boolean =>
|
|
619
|
+
node.isAtom
|
|
620
|
+
? ((node.attrs.children as readonly Kept[] | undefined) ?? []).some(
|
|
621
|
+
(c) => typeof c !== 'string' && c.tag === EXPLANATION_TAG,
|
|
622
|
+
)
|
|
623
|
+
: node.children.some((c) => c.type.name === nodeName(EXPLANATION_TAG));
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Moves the help box at `pos` (on the page) into the question at `target`, after its other help, in one undo step. A
|
|
627
|
+
* 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
|
|
628
|
+
* breaks, as an inline component's help holds them. A page or a column the box leaves empty keeps an empty paragraph.
|
|
629
|
+
*/
|
|
630
|
+
function moveHelpIn(editor: Editor, pos: number, target: number, change: SurfaceChange): boolean {
|
|
631
|
+
const { state } = editor;
|
|
632
|
+
const box = state.doc.nodeAt(pos);
|
|
633
|
+
const owner = state.doc.nodeAt(target);
|
|
634
|
+
const paragraph = state.schema.nodes.paragraph;
|
|
635
|
+
if (box === null || owner === null || paragraph === undefined) return false;
|
|
636
|
+
const tr = closeHistory(state.tr);
|
|
637
|
+
if (owner.isAtom) {
|
|
638
|
+
const dom = DOMSerializer.fromSchema(state.schema).serializeNode(box, { document: inert }) as Element;
|
|
639
|
+
const inline = inert.createElement(dom.localName);
|
|
640
|
+
for (const attribute of [...dom.attributes]) inline.setAttribute(attribute.name, attribute.value);
|
|
641
|
+
[...dom.children].forEach((block, i) => {
|
|
642
|
+
if (i > 0) inline.append(inert.createElement('br'));
|
|
643
|
+
if (block.localName === 'p') inline.append(...block.childNodes);
|
|
644
|
+
else inline.append(block.textContent ?? '');
|
|
645
|
+
});
|
|
646
|
+
tr.setNodeAttribute(target, 'children', [
|
|
647
|
+
...((owner.attrs.children as readonly Kept[] | undefined) ?? []),
|
|
648
|
+
...keep(inline),
|
|
649
|
+
]);
|
|
650
|
+
} else {
|
|
651
|
+
if (!owner.canReplace(owner.childCount, owner.childCount, Fragment.from(box))) return false;
|
|
652
|
+
tr.insert(target + owner.nodeSize - 1, box);
|
|
653
|
+
}
|
|
654
|
+
const from = tr.mapping.map(pos);
|
|
655
|
+
if (tr.doc.resolve(from).parent.childCount === 1)
|
|
656
|
+
tr.replaceWith(from, from + box.nodeSize, paragraph.create());
|
|
657
|
+
else tr.delete(from, from + box.nodeSize);
|
|
658
|
+
editor.view.dispatch(tr.setMeta(PAGE_CHANGE, change).scrollIntoView());
|
|
659
|
+
return true;
|
|
660
|
+
}
|
|
619
661
|
|
|
620
662
|
/** Takes the help box at `pos` away in one undo step; a page or a column it leaves empty keeps an empty paragraph. */
|
|
621
663
|
function removeHelp(editor: Editor, pos: number): void {
|
|
@@ -633,12 +675,23 @@ function removeHelp(editor: Editor, pos: number): void {
|
|
|
633
675
|
function helpNode(tag: string, authoring?: Authoring): Node {
|
|
634
676
|
const name = nodeName(tag);
|
|
635
677
|
const t = (key: string, params?: MessageParams) => authoring?.t(EDITOR_NAMESPACE, key, params) ?? key;
|
|
678
|
+
/** Node names of the graded component types, to their tags: the questions page help can move into. */
|
|
679
|
+
const graded = new Map(
|
|
680
|
+
(authoring?.engine.manifests ?? []).flatMap((m) => (m.graded ? [[nodeName(m.tag), m.tag] as const] : [])),
|
|
681
|
+
);
|
|
682
|
+
const nameOf = (question: { readonly node: PMNode; readonly tag: string }) => {
|
|
683
|
+
const descriptor = authoring?.descriptor(question.tag);
|
|
684
|
+
const label = descriptor ? authoring?.t(descriptor.manifest.messages, descriptor.label) : undefined;
|
|
685
|
+
const id = (question.node.attrs.attributes as Attributes).id ?? '';
|
|
686
|
+
return t('surface.moveTarget', { name: label && label !== descriptor?.label ? label : question.tag, id });
|
|
687
|
+
};
|
|
636
688
|
return Node.create({
|
|
637
689
|
name,
|
|
638
690
|
group: HELP,
|
|
639
691
|
content: FLOW,
|
|
640
692
|
defining: true,
|
|
641
693
|
isolating: true,
|
|
694
|
+
selectable: false,
|
|
642
695
|
addAttributes: () => sourceAttributes,
|
|
643
696
|
parseHTML: () => [{ tag }],
|
|
644
697
|
renderHTML: ({ node }) => [tag, { ...node.attrs.attributes }, 0],
|
|
@@ -682,6 +735,70 @@ function helpNode(tag: string, authoring?: Authoring): Node {
|
|
|
682
735
|
const select = tag === EXPLANATION_TAG ? null : head.appendChild(document.createElement('select'));
|
|
683
736
|
const values = tag === HINT_TAG ? COSTS : FEEDBACK_VARIANTS;
|
|
684
737
|
select?.setAttribute('aria-label', t(tag === HINT_TAG ? 'help.costLabel' : 'help.whenLabel'));
|
|
738
|
+
// A page's hint is free: no cost to pick. One with a cost (an older source) says so, with "Make it free".
|
|
739
|
+
const notice = tag === HINT_TAG ? head.appendChild(document.createElement('span')) : null;
|
|
740
|
+
const free = tag === HINT_TAG ? head.appendChild(document.createElement('button')) : null;
|
|
741
|
+
if (notice !== null && free !== null) {
|
|
742
|
+
notice.className = 'astl-help-notice';
|
|
743
|
+
free.type = 'button';
|
|
744
|
+
free.className = 'astl-help-free';
|
|
745
|
+
free.textContent = t('surface.hintMakeFree');
|
|
746
|
+
free.addEventListener('click', () => {
|
|
747
|
+
const pos = getPos();
|
|
748
|
+
const current = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
|
|
749
|
+
if (current === null || typeof pos !== 'number') return;
|
|
750
|
+
const { cost: _, ...rest } = current.attrs.attributes as Attributes;
|
|
751
|
+
editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', rest));
|
|
752
|
+
editor.view.focus();
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
// A hint or the explanation on the page can go into one of its questions.
|
|
756
|
+
const move = tag === FEEDBACK_TAG ? null : head.appendChild(document.createElement('select'));
|
|
757
|
+
move?.classList.add('astl-help-move');
|
|
758
|
+
const questions = () => {
|
|
759
|
+
const pos = getPos();
|
|
760
|
+
return typeof pos === 'number' ? pageQuestions(editor.state.doc, pos, graded) : [];
|
|
761
|
+
};
|
|
762
|
+
// Filled with the box, and again when it is about to be used: the page's questions change while the box stays.
|
|
763
|
+
const fill = () => {
|
|
764
|
+
if (move === null) return;
|
|
765
|
+
const first = Object.assign(document.createElement('option'), {
|
|
766
|
+
value: '',
|
|
767
|
+
textContent: t('surface.moveTo'),
|
|
768
|
+
});
|
|
769
|
+
const found = questions();
|
|
770
|
+
const none = Object.assign(document.createElement('option'), {
|
|
771
|
+
value: '',
|
|
772
|
+
disabled: true,
|
|
773
|
+
textContent: t('surface.moveNone'),
|
|
774
|
+
});
|
|
775
|
+
move.replaceChildren(
|
|
776
|
+
first,
|
|
777
|
+
...(found.length === 0 ? [none] : []),
|
|
778
|
+
...found.map((q) => {
|
|
779
|
+
const option = document.createElement('option');
|
|
780
|
+
option.value = String(q.pos);
|
|
781
|
+
// A question has one explanation at most: one that has it already is not a place for another.
|
|
782
|
+
const full = tag === EXPLANATION_TAG && explained(q.node);
|
|
783
|
+
option.textContent = full ? t('surface.moveTargetFull', { name: nameOf(q) }) : nameOf(q);
|
|
784
|
+
option.disabled = full;
|
|
785
|
+
return option;
|
|
786
|
+
}),
|
|
787
|
+
);
|
|
788
|
+
move.value = '';
|
|
789
|
+
};
|
|
790
|
+
move?.setAttribute('aria-label', t('surface.moveLabel', { kind: tag.replace(/^astl-/, '') }));
|
|
791
|
+
move?.addEventListener('focus', fill);
|
|
792
|
+
move?.addEventListener('pointerdown', fill);
|
|
793
|
+
move?.addEventListener('change', () => {
|
|
794
|
+
const pos = getPos();
|
|
795
|
+
const target = move.value === '' ? null : Number(move.value);
|
|
796
|
+
const owner = target === null ? null : editor.state.doc.nodeAt(target);
|
|
797
|
+
const question = owner === null ? undefined : graded.get(owner.type.name);
|
|
798
|
+
if (typeof pos !== 'number' || target === null || owner === null || question === undefined) return;
|
|
799
|
+
const change = { key: 'surface.moved', params: { name: nameOf({ node: owner, tag: question }) } };
|
|
800
|
+
if (moveHelpIn(editor, pos, target, change)) editor.view.focus();
|
|
801
|
+
});
|
|
685
802
|
select?.addEventListener('change', () => {
|
|
686
803
|
const pos = getPos();
|
|
687
804
|
const current = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
|
|
@@ -691,11 +808,14 @@ function helpNode(tag: string, authoring?: Authoring): Node {
|
|
|
691
808
|
else attributes[tag === HINT_TAG ? 'cost' : 'when'] = select.value;
|
|
692
809
|
editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', attributes));
|
|
693
810
|
});
|
|
694
|
-
// "Remove" at the end of the head, named by the kind of box: a box the menu added empty has no other way
|
|
811
|
+
// "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
|
|
812
|
+
// out.
|
|
695
813
|
const remove = head.appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
|
|
696
814
|
remove.className = 'astl-help-remove';
|
|
697
|
-
remove.
|
|
698
|
-
|
|
815
|
+
remove.append(lineIcon('remove', 'astl-icon'));
|
|
816
|
+
const removeName = t('surface.helpRemove', { kind: tag.replace(/^astl-/, '') });
|
|
817
|
+
remove.setAttribute('aria-label', removeName);
|
|
818
|
+
remove.title = removeName;
|
|
699
819
|
remove.addEventListener('click', () => {
|
|
700
820
|
const pos = getPos();
|
|
701
821
|
if (typeof pos !== 'number') return;
|
|
@@ -705,23 +825,61 @@ function helpNode(tag: string, authoring?: Authoring): Node {
|
|
|
705
825
|
remove.addEventListener('keydown', (event) => historyKey(editor.view, event));
|
|
706
826
|
const content = dom.appendChild(document.createElement('div'));
|
|
707
827
|
content.className = 'astl-help-content';
|
|
828
|
+
let shown = node.attrs;
|
|
708
829
|
const render = (attrs: Record<string, unknown>) => {
|
|
830
|
+
shown = attrs;
|
|
709
831
|
const attributes = attrs.attributes as Attributes;
|
|
832
|
+
// Where the box is: unknown while the view that creates it is still being built (then said again below).
|
|
833
|
+
let onPage = false;
|
|
834
|
+
try {
|
|
835
|
+
const pos = getPos();
|
|
836
|
+
const parent = typeof pos === 'number' ? editor.state.doc.resolve(pos).parent : null;
|
|
837
|
+
onPage = parent !== null && pageLevel(parent, editor.state.doc);
|
|
838
|
+
} catch {
|
|
839
|
+
onPage = false;
|
|
840
|
+
}
|
|
841
|
+
if (move !== null) {
|
|
842
|
+
move.hidden = !onPage;
|
|
843
|
+
if (onPage) fill();
|
|
844
|
+
}
|
|
845
|
+
const cost = attributes.cost ?? '0';
|
|
846
|
+
if (notice !== null && free !== null) {
|
|
847
|
+
const costly = onPage && Number(cost) !== 0;
|
|
848
|
+
notice.hidden = !costly;
|
|
849
|
+
free.hidden = !costly;
|
|
850
|
+
notice.textContent = costly
|
|
851
|
+
? t('surface.hintPageCost', { percent: Math.round(Number(cost) * 100) })
|
|
852
|
+
: '';
|
|
853
|
+
}
|
|
710
854
|
if (select === null) return;
|
|
855
|
+
select.hidden = tag === HINT_TAG && onPage;
|
|
711
856
|
const value =
|
|
712
857
|
(tag === HINT_TAG ? attributes.cost : attributes.when) ?? (tag === HINT_TAG ? '0' : '');
|
|
713
|
-
const options = values.includes(value)
|
|
858
|
+
const options = values.includes(value)
|
|
859
|
+
? values
|
|
860
|
+
: tag === HINT_TAG
|
|
861
|
+
? [...values, value].sort((a, b) => Number(a) - Number(b))
|
|
862
|
+
: [...values, value];
|
|
714
863
|
select.replaceChildren(
|
|
715
864
|
...options.map((v) => {
|
|
716
865
|
const option = document.createElement('option');
|
|
717
866
|
option.value = v;
|
|
718
|
-
option.textContent =
|
|
867
|
+
option.textContent =
|
|
868
|
+
tag !== HINT_TAG
|
|
869
|
+
? t(`help.when.${v}`)
|
|
870
|
+
: v === '0'
|
|
871
|
+
? t('help.costFree')
|
|
872
|
+
: Number.isFinite(Number(v))
|
|
873
|
+
? `-${Math.round(Number(v) * 100)}%`
|
|
874
|
+
: v;
|
|
719
875
|
return option;
|
|
720
876
|
}),
|
|
721
877
|
);
|
|
722
878
|
select.value = value;
|
|
723
879
|
};
|
|
724
880
|
render(node.attrs);
|
|
881
|
+
// Once the view that creates the box is built, its place is known.
|
|
882
|
+
queueMicrotask(() => render(shown));
|
|
725
883
|
return {
|
|
726
884
|
dom,
|
|
727
885
|
contentDOM: content,
|
|
@@ -742,6 +900,50 @@ export function helpNodes(authoring?: Authoring): Node[] {
|
|
|
742
900
|
return [HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map((tag) => helpNode(tag, authoring));
|
|
743
901
|
}
|
|
744
902
|
|
|
903
|
+
/**
|
|
904
|
+
* An empty help box's line says what to write there ("Write the hint..."): the box keeps one empty paragraph to type
|
|
905
|
+
* in, and a decoration marks it (`astl-help-empty`, the text in `data-placeholder`, which the surface's style shows).
|
|
906
|
+
*/
|
|
907
|
+
function helpPlaceholderExtension(authoring: Authoring): Extension {
|
|
908
|
+
const kinds = new Map(
|
|
909
|
+
[HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map(
|
|
910
|
+
(tag) => [nodeName(tag), tag.replace(/^astl-/, '')] as const,
|
|
911
|
+
),
|
|
912
|
+
);
|
|
913
|
+
const decorate = (doc: PMNode): DecorationSet => {
|
|
914
|
+
const decorations: Decoration[] = [];
|
|
915
|
+
doc.descendants((node, pos) => {
|
|
916
|
+
const kind = kinds.get(node.type.name);
|
|
917
|
+
if (kind === undefined) return !node.isTextblock;
|
|
918
|
+
const line = node.firstChild;
|
|
919
|
+
if (node.childCount === 1 && line !== null && line.isTextblock && line.content.size === 0) {
|
|
920
|
+
decorations.push(
|
|
921
|
+
Decoration.node(pos + 1, pos + 1 + line.nodeSize, {
|
|
922
|
+
class: 'astl-help-empty',
|
|
923
|
+
'data-placeholder': authoring.t(EDITOR_NAMESPACE, 'surface.helpPlaceholder', { kind }),
|
|
924
|
+
}),
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
return false;
|
|
928
|
+
});
|
|
929
|
+
return DecorationSet.create(doc, decorations);
|
|
930
|
+
};
|
|
931
|
+
const key = new PluginKey<DecorationSet>('astlHelpPlaceholder');
|
|
932
|
+
return Extension.create({
|
|
933
|
+
name: 'astlHelpPlaceholder',
|
|
934
|
+
addProseMirrorPlugins: () => [
|
|
935
|
+
new Plugin<DecorationSet>({
|
|
936
|
+
key,
|
|
937
|
+
state: {
|
|
938
|
+
init: (_, state) => decorate(state.doc),
|
|
939
|
+
apply: (tr, previous) => (tr.docChanged ? decorate(tr.doc) : previous),
|
|
940
|
+
},
|
|
941
|
+
props: { decorations: (state) => key.getState(state) },
|
|
942
|
+
}),
|
|
943
|
+
],
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
|
|
745
947
|
/**
|
|
746
948
|
* A block with rich children, or with rich content (`content: 'flow'`), framed and labelled in the editor, with its
|
|
747
949
|
* picture when it has picture fields.
|
|
@@ -787,6 +989,9 @@ function blockNode(manifest: ComponentManifest, pictures?: PictureView, authorin
|
|
|
787
989
|
({ node, getPos, editor }) => {
|
|
788
990
|
const dom = document.createElement('div');
|
|
789
991
|
dom.className = 'astl-block';
|
|
992
|
+
// A graded one's help for students is not drawn (its frame's tags say it, the card edits it); an ungraded one
|
|
993
|
+
// shows help it should not hold, which the engine warns of, so it can be taken away.
|
|
994
|
+
dom.toggleAttribute('data-astl-graded', manifest.graded);
|
|
790
995
|
const picture = pictures?.layers(manifest.tag).length ? pictures.create(manifest.tag) : null;
|
|
791
996
|
const contentDOM = document.createElement('div');
|
|
792
997
|
// Inside the frame, not on it: an author's border must not hide what the frame says.
|
|
@@ -1317,6 +1522,11 @@ export interface PictureHooks {
|
|
|
1317
1522
|
* does).
|
|
1318
1523
|
*/
|
|
1319
1524
|
style?(kind: 'picture' | 'figure' | 'component'): void;
|
|
1525
|
+
/**
|
|
1526
|
+
* Opens the help for students of the component at the cursor at a part, from a tag on its frame (`<astl-editor>`:
|
|
1527
|
+
* `astl-help-open`, else its inspector).
|
|
1528
|
+
*/
|
|
1529
|
+
help?(part: HelpPart): void;
|
|
1320
1530
|
}
|
|
1321
1531
|
|
|
1322
1532
|
/** A width from the corner handle on the node at `pos`, one undo step; a figure's picture follows. */
|
|
@@ -1604,6 +1814,8 @@ export function pageNode(authoring: Authoring): Node {
|
|
|
1604
1814
|
return button;
|
|
1605
1815
|
};
|
|
1606
1816
|
const addTitle = named('add-title', t('surface.pageAddTitle'));
|
|
1817
|
+
// "Check this page", pressed while the page has `check`: its own Check in practice, then its help.
|
|
1818
|
+
const check = named('check', t('surface.pageCheck'));
|
|
1607
1819
|
// Hidden until numbered: the first page has no join button, the only page no delete button.
|
|
1608
1820
|
const join = Object.assign(document.createElement('button'), {
|
|
1609
1821
|
type: 'button',
|
|
@@ -1673,6 +1885,17 @@ export function pageNode(authoring: Authoring): Node {
|
|
|
1673
1885
|
sync();
|
|
1674
1886
|
addTitle.focus();
|
|
1675
1887
|
});
|
|
1888
|
+
// The same step as `EditorAdapter.setPageCheck` for this page, found by its place (its id may be missing).
|
|
1889
|
+
check.addEventListener('click', () => {
|
|
1890
|
+
const pos = getPos();
|
|
1891
|
+
if (typeof pos !== 'number') return;
|
|
1892
|
+
const on = !Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE);
|
|
1893
|
+
const tr = editor.state.tr;
|
|
1894
|
+
if (!checkPageAt(tr, pos, on)) return;
|
|
1895
|
+
const page = editor.state.doc.resolve(pos).index(0) + 1;
|
|
1896
|
+
pageStep(tr, { key: on ? 'surface.pageCheckOn' : 'surface.pageCheckOff', params: { page } });
|
|
1897
|
+
editor.view.dispatch(tr);
|
|
1898
|
+
});
|
|
1676
1899
|
join.addEventListener('click', () => {
|
|
1677
1900
|
const pos = getPos();
|
|
1678
1901
|
if (typeof pos !== 'number' || editor.state.doc.resolve(pos).index(0) === 0) return;
|
|
@@ -1690,11 +1913,12 @@ export function pageNode(authoring: Authoring): Node {
|
|
|
1690
1913
|
// As after a join: the editor takes the focus, the cursor at the start of the page it went to.
|
|
1691
1914
|
editor.view.focus();
|
|
1692
1915
|
});
|
|
1693
|
-
head.append(number, addTitle, join, remove, field);
|
|
1916
|
+
head.append(number, addTitle, check, join, remove, field);
|
|
1694
1917
|
const contentDOM = document.createElement('div');
|
|
1695
1918
|
// The page's look is for its content, not for the controls in its head.
|
|
1696
1919
|
const paint = painter(contentDOM, 'astl-page');
|
|
1697
1920
|
paint(attributes);
|
|
1921
|
+
check.setAttribute('aria-pressed', String(Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE)));
|
|
1698
1922
|
dom.append(head, contentDOM);
|
|
1699
1923
|
input.value = attributes.label ?? '';
|
|
1700
1924
|
sync();
|
|
@@ -1709,6 +1933,7 @@ export function pageNode(authoring: Authoring): Node {
|
|
|
1709
1933
|
attributes = attrs.attributes as Attributes;
|
|
1710
1934
|
paint(attributes);
|
|
1711
1935
|
if (input.value !== (attributes.label ?? '')) input.value = attributes.label ?? '';
|
|
1936
|
+
check.setAttribute('aria-pressed', String(Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE)));
|
|
1712
1937
|
sync();
|
|
1713
1938
|
};
|
|
1714
1939
|
return {
|
|
@@ -1838,6 +2063,45 @@ export function addPageIn(tr: Transaction, after?: string): string {
|
|
|
1838
2063
|
return id;
|
|
1839
2064
|
}
|
|
1840
2065
|
|
|
2066
|
+
/** Sets or clears `check` on the page at `pos` in `tr`: whether it changed. */
|
|
2067
|
+
function checkPageAt(tr: Transaction, pos: number, check: boolean): boolean {
|
|
2068
|
+
const node = tr.doc.nodeAt(pos);
|
|
2069
|
+
if (node?.type.name !== PAGE) return false;
|
|
2070
|
+
const attributes = node.attrs.attributes as Attributes;
|
|
2071
|
+
if (Object.hasOwn(attributes, PAGE_CHECK_ATTRIBUTE) === check) return false;
|
|
2072
|
+
const { [PAGE_CHECK_ATTRIBUTE]: _, ...rest } = attributes;
|
|
2073
|
+
tr.setNodeAttribute(pos, 'attributes', check ? { ...rest, [PAGE_CHECK_ATTRIBUTE]: '' } : rest);
|
|
2074
|
+
return true;
|
|
2075
|
+
}
|
|
2076
|
+
|
|
2077
|
+
/**
|
|
2078
|
+
* Sets or clears `check` on the page `id`, or on every page without one, in `tr` (`EditorAdapter.setPageCheck`, and the
|
|
2079
|
+
* page head's "Check this page" through `checkPageAt`): a document without pages becomes one page with a fresh id and
|
|
2080
|
+
* `check` first. One undo step, said on the notice line; false, changing nothing, when it was so already or for an id
|
|
2081
|
+
* that is not a page's.
|
|
2082
|
+
*/
|
|
2083
|
+
export function setPageCheckIn(tr: Transaction, check: boolean, id?: string): boolean {
|
|
2084
|
+
const type = tr.doc.type.schema.nodes[PAGE];
|
|
2085
|
+
if (type === undefined) return false;
|
|
2086
|
+
if (tr.doc.firstChild?.type !== type) {
|
|
2087
|
+
if (!check || id !== undefined) return false;
|
|
2088
|
+
intoPages(tr, idsIn(tr.doc));
|
|
2089
|
+
}
|
|
2090
|
+
const changed: number[] = [];
|
|
2091
|
+
tr.doc.forEach((node, pos, index) => {
|
|
2092
|
+
if (id !== undefined && (node.attrs.attributes as Attributes | undefined)?.id !== id) return;
|
|
2093
|
+
if (checkPageAt(tr, pos, check)) changed.push(index + 1);
|
|
2094
|
+
});
|
|
2095
|
+
if (changed.length === 0) return false;
|
|
2096
|
+
pageStep(
|
|
2097
|
+
tr,
|
|
2098
|
+
id !== undefined || tr.doc.childCount === 1
|
|
2099
|
+
? { key: check ? 'surface.pageCheckOn' : 'surface.pageCheckOff', params: { page: changed[0] ?? 1 } }
|
|
2100
|
+
: { key: check ? 'surface.pagesCheckOn' : 'surface.pagesCheckOff', params: {} },
|
|
2101
|
+
);
|
|
2102
|
+
return true;
|
|
2103
|
+
}
|
|
2104
|
+
|
|
1841
2105
|
/**
|
|
1842
2106
|
* Joins the page `id` to the page before it in `tr` (`EditorAdapter.mergePage`), the cursor at the start of the joined
|
|
1843
2107
|
* content: false, changing nothing, for the first page or an id that is not a page's.
|
|
@@ -2796,6 +3060,8 @@ export function astrolabeExtensions(
|
|
|
2796
3060
|
(node) => types.get(node.type.name) ?? null,
|
|
2797
3061
|
nodeName,
|
|
2798
3062
|
hooks?.style === undefined ? undefined : () => hooks.style?.('component'),
|
|
3063
|
+
hooks?.help === undefined ? undefined : (part) => hooks.help?.(part),
|
|
2799
3064
|
),
|
|
3065
|
+
helpPlaceholderExtension(authoring),
|
|
2800
3066
|
];
|
|
2801
3067
|
}
|