@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.
- package/CHANGELOG.md +19 -0
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +19 -12
- 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 +6 -4
- package/dist/blocks.js.map +1 -1
- package/dist/element.js +1 -1
- package/dist/element.js.map +1 -1
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +255 -6
- package/dist/nodes.js.map +1 -1
- package/dist/styles.d.ts.map +1 -1
- package/dist/styles.js +100 -2
- package/dist/styles.js.map +1 -1
- package/package.json +6 -6
- package/src/adapter.ts +27 -11
- package/src/blocks.ts +7 -4
- package/src/element.ts +1 -1
- package/src/nodes.ts +255 -5
- package/src/styles.ts +100 -2
package/dist/nodes.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
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, PAGE_CHECK_ATTRIBUTE, TAG_ATTRIBUTES, } from '@astl/core';
|
|
22
|
+
import { CAPTION_TAG, CHECK_FOR_ATTRIBUTE, CHECK_LABEL_ATTRIBUTE, CHECK_TAG, CLASS_ATTRIBUTE, COLUMN_TAG, COLUMNS_TAG, CONTENT_ATTRIBUTES, checkFor, checkNodes, checkParts, childTagConflict, directionOf, EXPLANATION_TAG, FEEDBACK_TAG, FEEDBACK_VARIANTS, FIGURE_TAG, HELP_TAGS, HINT_TAG, INERT_STYLE, inertStyles, isCheckable, isSourceId, MAX_COLUMNS, MIN_COLUMNS, PAGE_CHECK_ATTRIBUTE, planChecks, 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';
|
|
@@ -28,7 +28,7 @@ import { DOMSerializer, Fragment, Slice, } from '@tiptap/pm/model';
|
|
|
28
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
|
-
import { blocksExtension, historyKey, LAYOUT, LAYOUT_COLUMN, placeLabel, } from './blocks.js';
|
|
31
|
+
import { blocksExtension, CHECK_NODE, historyKey, LAYOUT, LAYOUT_COLUMN, placeLabel, } from './blocks.js';
|
|
32
32
|
import { currentLayoutExtension, deleteLayoutIn, isEmptyLayout, layoutAt } from './layouts.js';
|
|
33
33
|
import { inert } from './source.js';
|
|
34
34
|
import { setPlacedStyle } from './style.js';
|
|
@@ -795,6 +795,252 @@ function helpPlaceholderExtension(authoring) {
|
|
|
795
795
|
],
|
|
796
796
|
});
|
|
797
797
|
}
|
|
798
|
+
/** The group of `<astl-check>`: directly on a page or in a column, never in text or in a component. */
|
|
799
|
+
const CHECK = 'check';
|
|
800
|
+
/** The decoration spec key a Check element's view reads how many questions it checks from (`checksExtension`). */
|
|
801
|
+
const CHECK_COUNT = 'astlCheckCount';
|
|
802
|
+
/** The class of the questions the selected Check element checks. */
|
|
803
|
+
const CHECK_TARGET = 'astl-check-target';
|
|
804
|
+
/** A Check element's count among its view's decorations, or null before the plugin has given it. */
|
|
805
|
+
const countIn = (decorations) => {
|
|
806
|
+
for (const decoration of decorations) {
|
|
807
|
+
const count = decoration.spec[CHECK_COUNT];
|
|
808
|
+
if (typeof count === 'number')
|
|
809
|
+
return count;
|
|
810
|
+
}
|
|
811
|
+
return null;
|
|
812
|
+
};
|
|
813
|
+
/**
|
|
814
|
+
* `<astl-check>`, a Check for several questions: a block atom kept as data, drawn as the compact button it puts in the
|
|
815
|
+
* player (`astl-check-button`: its `label`, else "Check") with a tag saying how many questions it checks
|
|
816
|
+
* (`astl-check-tag`: "3 questions"; without any, "No questions" and `data-empty` on `astl-check`). Its card ticks them.
|
|
817
|
+
*/
|
|
818
|
+
function checkNode(authoring) {
|
|
819
|
+
const t = (key, params) => authoring.t(EDITOR_NAMESPACE, key, params);
|
|
820
|
+
const ui = { lang: authoring.locale, dir: directionOf(authoring.locale) };
|
|
821
|
+
return Node.create({
|
|
822
|
+
name: CHECK_NODE,
|
|
823
|
+
group: CHECK,
|
|
824
|
+
atom: true,
|
|
825
|
+
selectable: true,
|
|
826
|
+
addAttributes: () => sourceAttributes,
|
|
827
|
+
parseHTML: () => [{ tag: CHECK_TAG }],
|
|
828
|
+
renderHTML: ({ node }) => [CHECK_TAG, { ...node.attrs.attributes }],
|
|
829
|
+
addNodeView: () => ({ node, decorations }) => {
|
|
830
|
+
const dom = document.createElement('div');
|
|
831
|
+
dom.className = 'astl-check';
|
|
832
|
+
const button = dom.appendChild(document.createElement('span'));
|
|
833
|
+
button.className = 'astl-check-button';
|
|
834
|
+
button.append(lineIcon('check', 'astl-icon'));
|
|
835
|
+
const label = button.appendChild(document.createElement('span'));
|
|
836
|
+
const tag = dom.appendChild(Object.assign(document.createElement('span'), ui));
|
|
837
|
+
tag.className = 'astl-check-tag';
|
|
838
|
+
const render = (attrs, count) => {
|
|
839
|
+
const own = (attrs.attributes[CHECK_LABEL_ATTRIBUTE] ?? '').trim();
|
|
840
|
+
label.textContent = own === '' ? t('check.button') : own;
|
|
841
|
+
// The default words are the editor's, the author's the exercise's.
|
|
842
|
+
if (own === '')
|
|
843
|
+
Object.assign(label, ui);
|
|
844
|
+
else {
|
|
845
|
+
label.removeAttribute('lang');
|
|
846
|
+
label.removeAttribute('dir');
|
|
847
|
+
}
|
|
848
|
+
tag.textContent =
|
|
849
|
+
count === null ? '' : count === 0 ? t('check.none') : t('check.count', { n: count });
|
|
850
|
+
dom.toggleAttribute('data-empty', count === 0);
|
|
851
|
+
};
|
|
852
|
+
render(node.attrs, countIn(decorations));
|
|
853
|
+
return {
|
|
854
|
+
dom,
|
|
855
|
+
update: (next, nextDecorations) => {
|
|
856
|
+
if (next.type.name !== CHECK_NODE)
|
|
857
|
+
return false;
|
|
858
|
+
render(next.attrs, countIn(nextDecorations));
|
|
859
|
+
return true;
|
|
860
|
+
},
|
|
861
|
+
ignoreMutation: () => true,
|
|
862
|
+
};
|
|
863
|
+
},
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Each Check element's questions, by the rule the engine and the player use (`planChecks`) over the editor's nodes: its
|
|
868
|
+
* view says how many (a node decoration's spec), and while it is selected its questions are marked (`astl-check-target`
|
|
869
|
+
* on each), inline ones and blocks alike. Where two or more questions of a run of text have none, the automatic Check
|
|
870
|
+
* the player puts there shows after the block of the last of them (`astl-check astl-check-auto`: "automatic · 3
|
|
871
|
+
* questions"), with "Make it a button" (`astl-check-make`), which writes a Check element for them in its place.
|
|
872
|
+
*/
|
|
873
|
+
function checksExtension(authoring, manifests) {
|
|
874
|
+
const t = (key, params) => authoring.t(EDITOR_NAMESPACE, key, params);
|
|
875
|
+
const ui = { lang: authoring.locale, dir: directionOf(authoring.locale) };
|
|
876
|
+
const types = new Map(manifests.map((m) => [nodeName(m.tag), m]));
|
|
877
|
+
const idOf = (node) => node.attrs.attributes?.id || null;
|
|
878
|
+
const childrenOf = (node) => {
|
|
879
|
+
const out = [];
|
|
880
|
+
node.forEach((child) => {
|
|
881
|
+
out.push(child);
|
|
882
|
+
});
|
|
883
|
+
return out;
|
|
884
|
+
};
|
|
885
|
+
/** The positions of the Check elements directly in `node` and in its layouts' columns, as `checkNodes` meets them. */
|
|
886
|
+
const checkPositions = (node, start) => {
|
|
887
|
+
const out = [];
|
|
888
|
+
node.forEach((child, offset) => {
|
|
889
|
+
const pos = start + offset;
|
|
890
|
+
if (child.type.name === CHECK_NODE)
|
|
891
|
+
out.push(pos);
|
|
892
|
+
else if (child.type.name === LAYOUT) {
|
|
893
|
+
child.forEach((column, at) => {
|
|
894
|
+
if (column.type.name === LAYOUT_COLUMN)
|
|
895
|
+
out.push(...checkPositions(column, pos + 1 + at + 1));
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
return out;
|
|
900
|
+
};
|
|
901
|
+
/** A Check element for `run` at `pos` (after the block of its last question), selected, as one undo step. */
|
|
902
|
+
const make = (view, pos, run) => {
|
|
903
|
+
const type = view.state.schema.nodes[CHECK_NODE];
|
|
904
|
+
if (type === undefined)
|
|
905
|
+
return;
|
|
906
|
+
const used = idsIn(view.state.doc);
|
|
907
|
+
let n = 1;
|
|
908
|
+
while (used.has(`check-${n}`))
|
|
909
|
+
n++;
|
|
910
|
+
const node = type.create({ attributes: { id: `check-${n}`, [CHECK_FOR_ATTRIBUTE]: run.join(' ') } });
|
|
911
|
+
const tr = closeHistory(view.state.tr).insert(pos, node);
|
|
912
|
+
view.dispatch(tr.setSelection(NodeSelection.create(tr.doc, pos)).scrollIntoView());
|
|
913
|
+
view.focus();
|
|
914
|
+
};
|
|
915
|
+
/** The automatic Check of `run`: the button it is, its tag, and "Make it a button". */
|
|
916
|
+
const automaticDom = (view, run, getPos) => {
|
|
917
|
+
const dom = Object.assign(document.createElement('div'), ui);
|
|
918
|
+
dom.className = 'astl-check astl-check-auto';
|
|
919
|
+
dom.contentEditable = 'false';
|
|
920
|
+
const button = dom.appendChild(document.createElement('span'));
|
|
921
|
+
button.className = 'astl-check-button';
|
|
922
|
+
button.append(lineIcon('check', 'astl-icon'));
|
|
923
|
+
button.appendChild(document.createElement('span')).textContent = t('check.button');
|
|
924
|
+
const tag = dom.appendChild(document.createElement('span'));
|
|
925
|
+
tag.className = 'astl-check-tag';
|
|
926
|
+
tag.textContent = t('check.auto', { n: run.length });
|
|
927
|
+
const press = dom.appendChild(document.createElement('button'));
|
|
928
|
+
Object.assign(press, { type: 'button', tabIndex: -1, title: t('check.makeHelp') });
|
|
929
|
+
press.className = 'astl-check-make';
|
|
930
|
+
press.textContent = t('check.make');
|
|
931
|
+
press.addEventListener('click', (event) => {
|
|
932
|
+
event.preventDefault();
|
|
933
|
+
const pos = getPos();
|
|
934
|
+
if (pos !== undefined)
|
|
935
|
+
make(view, pos, run);
|
|
936
|
+
});
|
|
937
|
+
return dom;
|
|
938
|
+
};
|
|
939
|
+
const decorate = (state) => {
|
|
940
|
+
const { doc, selection } = state;
|
|
941
|
+
const selected = selection instanceof NodeSelection && selection.node.type.name === CHECK_NODE ? selection.from : null;
|
|
942
|
+
const decorations = [];
|
|
943
|
+
const containers = [];
|
|
944
|
+
if (doc.firstChild?.type.name === PAGE) {
|
|
945
|
+
doc.forEach((page, offset) => {
|
|
946
|
+
containers.push({ node: page, start: offset + 1 });
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
else
|
|
950
|
+
containers.push({ node: doc, start: 0 });
|
|
951
|
+
for (const container of containers) {
|
|
952
|
+
// The page's graded questions by id, where they are.
|
|
953
|
+
const questions = new Map();
|
|
954
|
+
container.node.descendants((node, offset) => {
|
|
955
|
+
const manifest = types.get(node.type.name);
|
|
956
|
+
if (manifest === undefined)
|
|
957
|
+
return true;
|
|
958
|
+
const id = idOf(node);
|
|
959
|
+
if (isCheckable(manifest) && id !== null)
|
|
960
|
+
questions.set(id, { pos: container.start + offset, node });
|
|
961
|
+
return false;
|
|
962
|
+
});
|
|
963
|
+
const tree = {
|
|
964
|
+
tag: (node) => {
|
|
965
|
+
const name = node.type.name;
|
|
966
|
+
if (node.isText)
|
|
967
|
+
return null;
|
|
968
|
+
if (name === CHECK_NODE)
|
|
969
|
+
return CHECK_TAG;
|
|
970
|
+
if (name === LAYOUT)
|
|
971
|
+
return COLUMNS_TAG;
|
|
972
|
+
if (name === LAYOUT_COLUMN)
|
|
973
|
+
return COLUMN_TAG;
|
|
974
|
+
return name === 'heading' ? `h${node.attrs.level}` : name;
|
|
975
|
+
},
|
|
976
|
+
children: childrenOf,
|
|
977
|
+
component: (node) => {
|
|
978
|
+
const manifest = types.get(node.type.name);
|
|
979
|
+
if (manifest === undefined)
|
|
980
|
+
return null;
|
|
981
|
+
return { id: idOf(node), graded: isCheckable(manifest), block: manifest.display === 'block' };
|
|
982
|
+
},
|
|
983
|
+
checkFor: (node) => {
|
|
984
|
+
const attributes = node.attrs.attributes;
|
|
985
|
+
return Object.hasOwn(attributes, CHECK_FOR_ATTRIBUTE)
|
|
986
|
+
? checkFor(attributes[CHECK_FOR_ATTRIBUTE]).filter((id) => questions.has(id))
|
|
987
|
+
: null;
|
|
988
|
+
},
|
|
989
|
+
};
|
|
990
|
+
const content = childrenOf(container.node);
|
|
991
|
+
const { targets, uncovered } = planChecks(checkParts(content, tree));
|
|
992
|
+
const positions = checkPositions(container.node, container.start);
|
|
993
|
+
checkNodes(content, tree).forEach((check, k) => {
|
|
994
|
+
const pos = positions[k];
|
|
995
|
+
if (pos === undefined)
|
|
996
|
+
return;
|
|
997
|
+
const own = targets[k] ?? [];
|
|
998
|
+
decorations.push(Decoration.node(pos, pos + check.nodeSize, {}, { [CHECK_COUNT]: own.length }));
|
|
999
|
+
if (pos !== selected)
|
|
1000
|
+
return;
|
|
1001
|
+
for (const id of own) {
|
|
1002
|
+
const question = questions.get(id);
|
|
1003
|
+
if (question !== undefined)
|
|
1004
|
+
decorations.push(Decoration.node(question.pos, question.pos + question.node.nodeSize, { class: CHECK_TARGET }));
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
// After the block, directly in the page or its column, that holds the last question of the run.
|
|
1008
|
+
for (const run of uncovered) {
|
|
1009
|
+
const last = questions.get(run.at(-1) ?? '');
|
|
1010
|
+
if (run.length < 2 || last === undefined)
|
|
1011
|
+
continue;
|
|
1012
|
+
const $last = doc.resolve(last.pos);
|
|
1013
|
+
let depth = $last.depth;
|
|
1014
|
+
while (depth > 0 && ![PAGE, LAYOUT_COLUMN].includes($last.node(depth).type.name))
|
|
1015
|
+
depth--;
|
|
1016
|
+
const at = $last.after(depth + 1);
|
|
1017
|
+
decorations.push(
|
|
1018
|
+
// Before the tab and the notes of a block framed right there (sides -2 and -1): it ends the run above.
|
|
1019
|
+
Decoration.widget(at, (view, getPos) => automaticDom(view, run, getPos), {
|
|
1020
|
+
key: `auto\n${run.join(' ')}`,
|
|
1021
|
+
side: -3,
|
|
1022
|
+
stopEvent: () => true,
|
|
1023
|
+
ignoreSelection: true,
|
|
1024
|
+
}));
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
return DecorationSet.create(doc, decorations);
|
|
1028
|
+
};
|
|
1029
|
+
const key = new PluginKey('astlChecks');
|
|
1030
|
+
return Extension.create({
|
|
1031
|
+
name: 'astlChecks',
|
|
1032
|
+
addProseMirrorPlugins: () => [
|
|
1033
|
+
new Plugin({
|
|
1034
|
+
key,
|
|
1035
|
+
state: {
|
|
1036
|
+
init: (_, state) => decorate(state),
|
|
1037
|
+
apply: (tr, previous, _, state) => (tr.docChanged || tr.selectionSet ? decorate(state) : previous),
|
|
1038
|
+
},
|
|
1039
|
+
props: { decorations: (state) => key.getState(state) },
|
|
1040
|
+
}),
|
|
1041
|
+
],
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
798
1044
|
/**
|
|
799
1045
|
* A block with rich children, or with rich content (`content: 'flow'`), framed and labelled in the editor, with its
|
|
800
1046
|
* picture when it has picture fields.
|
|
@@ -1579,8 +1825,8 @@ function figureNode(authoring, hooks) {
|
|
|
1579
1825
|
}
|
|
1580
1826
|
/** The node name of `<astl-page>`. */
|
|
1581
1827
|
const PAGE = 'astlPage';
|
|
1582
|
-
/** What a page or a document without pages holds: blocks and
|
|
1583
|
-
const PAGE_CONTENT = `(block | ${LAYOUT} | ${HELP})+`;
|
|
1828
|
+
/** What a page or a document without pages holds: blocks, layouts, help for students and Check elements. */
|
|
1829
|
+
const PAGE_CONTENT = `(block | ${LAYOUT} | ${HELP} | ${CHECK})+`;
|
|
1584
1830
|
/**
|
|
1585
1831
|
* A layout: `<astl-columns>` with its `<astl-column>`s, under their real tags (nothing in them asks for a node view).
|
|
1586
1832
|
* Outside the `block` group, so one holds no other, nor does a list or a rich block. Isolating: Backspace at the start
|
|
@@ -1616,7 +1862,7 @@ const layoutNodes = [
|
|
|
1616
1862
|
}),
|
|
1617
1863
|
Node.create({
|
|
1618
1864
|
name: LAYOUT_COLUMN,
|
|
1619
|
-
content:
|
|
1865
|
+
content: `(block | ${CHECK})+`,
|
|
1620
1866
|
isolating: true,
|
|
1621
1867
|
selectable: false,
|
|
1622
1868
|
addAttributes: () => sourceAttributes,
|
|
@@ -2505,7 +2751,8 @@ function idsIn(doc) {
|
|
|
2505
2751
|
* text.
|
|
2506
2752
|
*/
|
|
2507
2753
|
function pastedIdsExtension(authoring, manifests, hooks) {
|
|
2508
|
-
|
|
2754
|
+
// Check elements share the namespace: a pasted one whose id is taken gets `check-<n>`.
|
|
2755
|
+
const tags = new Map([...manifests.map((m) => [nodeName(m.tag), m.tag]), [CHECK_NODE, CHECK_TAG]]);
|
|
2509
2756
|
const prefixOf = (name) => {
|
|
2510
2757
|
const tag = tags.get(name);
|
|
2511
2758
|
if (tag === undefined)
|
|
@@ -2903,6 +3150,8 @@ export function astrolabeExtensions(authoring, manifests = authoring.engine.mani
|
|
|
2903
3150
|
currentFigureExtension,
|
|
2904
3151
|
...componentNodes(manifests, label, zonesView(authoring), authoring),
|
|
2905
3152
|
...helpNodes(authoring),
|
|
3153
|
+
checkNode(authoring),
|
|
3154
|
+
checksExtension(authoring, manifests),
|
|
2906
3155
|
referencesExtension(authoring, manifests),
|
|
2907
3156
|
childrenExtension(manifests),
|
|
2908
3157
|
richBlocksExtension(authoring, manifests),
|