@astl/editor-tiptap 0.2.0 → 0.2.2
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 +32 -0
- 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 +6 -1
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +170 -40
- package/dist/nodes.js.map +1 -1
- package/dist/styles.d.ts.map +1 -1
- package/dist/styles.js +142 -35
- package/dist/styles.js.map +1 -1
- package/package.json +6 -6
- package/src/blocks.ts +63 -3
- package/src/element.ts +14 -1
- package/src/nodes.ts +189 -42
- package/src/styles.ts +142 -35
package/dist/nodes.js
CHANGED
|
@@ -25,7 +25,7 @@ import { EDITOR_NAMESPACE, pictureHtml, pictureSize, positionOf, splitPlacement,
|
|
|
25
25
|
import { Extension, Mark, Node } from '@tiptap/core';
|
|
26
26
|
import { closeHistory } from '@tiptap/pm/history';
|
|
27
27
|
import { DOMSerializer, Fragment, Slice, } from '@tiptap/pm/model';
|
|
28
|
-
import { NodeSelection, Plugin, Selection, TextSelection, } from '@tiptap/pm/state';
|
|
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';
|
|
@@ -266,7 +266,7 @@ function summary(manifest, attrs) {
|
|
|
266
266
|
const main = manifest?.attributes.find((a) => a.type === 'string')?.name;
|
|
267
267
|
if (main !== undefined)
|
|
268
268
|
return attrs.attributes[main] ?? '';
|
|
269
|
-
// Its help for students is not what it holds: the frame
|
|
269
|
+
// Its help for students is not what it holds: the frame's tags say it, the card edits it.
|
|
270
270
|
const children = attrs.children.filter((c) => typeof c === 'string' || !HELP_TAGS.has(c.tag));
|
|
271
271
|
const elements = children.filter((c) => typeof c !== 'string');
|
|
272
272
|
return elements.length > 0
|
|
@@ -303,6 +303,7 @@ const SVG_ICONS = {
|
|
|
303
303
|
check: 'M4 10.5l4 4 8-9',
|
|
304
304
|
warning: 'M10 3l8 14H2zM10 8v4M10 14.5v.5',
|
|
305
305
|
add: 'M10 4v12M4 10h12',
|
|
306
|
+
remove: 'M5 5l10 10M15 5L5 15',
|
|
306
307
|
};
|
|
307
308
|
/** A 20 by 20 line icon, hidden from assistive technology. */
|
|
308
309
|
function lineIcon(name, className) {
|
|
@@ -406,9 +407,7 @@ function atomNode(tag, manifest, label, place, pictures, authoring) {
|
|
|
406
407
|
dom.append(picture.dom);
|
|
407
408
|
const body = dom.appendChild(document.createElement('div'));
|
|
408
409
|
body.className = 'astl-atom-summary';
|
|
409
|
-
// Its help for students
|
|
410
|
-
const help = dom.appendChild(document.createElement('div'));
|
|
411
|
-
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.
|
|
412
411
|
// Inside the frame, not on it: an author's border must not hide what the frame says.
|
|
413
412
|
const paint = placedPainter(body, tag);
|
|
414
413
|
const showPlace = authoring === undefined ? null : componentPlacing(authoring, dom, editor, getPos);
|
|
@@ -419,8 +418,6 @@ function atomNode(tag, manifest, label, place, pictures, authoring) {
|
|
|
419
418
|
// The picture shows the zones and pins; a summary of them would only repeat them.
|
|
420
419
|
body.textContent = picture === null ? summary(manifest, attrs) : '';
|
|
421
420
|
body.hidden = picture !== null;
|
|
422
|
-
if (authoring !== undefined)
|
|
423
|
-
help.replaceChildren(...keptHelp(attrs.children, authoring));
|
|
424
421
|
};
|
|
425
422
|
render(node.attrs);
|
|
426
423
|
return {
|
|
@@ -445,36 +442,10 @@ const FLOW = `(${FLOW_NODES.join(' | ')})+`;
|
|
|
445
442
|
/**
|
|
446
443
|
* The group of the help for students (`<astl-hint>`, `<astl-explanation>`, `<astl-feedback>`): rich content in a page
|
|
447
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
|
|
448
|
-
* the editor, a labelled box; its select sets
|
|
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.
|
|
449
447
|
*/
|
|
450
448
|
const HELP = 'help';
|
|
451
|
-
/** The label of a help element in the UI language: what it is, with its cost or its verdict. */
|
|
452
|
-
function helpLabel(tag, attributes, t) {
|
|
453
|
-
if (tag === HINT_TAG) {
|
|
454
|
-
const percent = Math.round(Number(attributes.cost ?? 0) * 100);
|
|
455
|
-
return percent > 0 ? `${t('insert.hint')}, ${t('help.cost', { percent })}` : t('insert.hint');
|
|
456
|
-
}
|
|
457
|
-
if (tag === EXPLANATION_TAG)
|
|
458
|
-
return t('insert.explanation');
|
|
459
|
-
return `${t('insert.feedback')}: ${t(`help.when.${attributes.when ?? ''}`)}`;
|
|
460
|
-
}
|
|
461
|
-
/** An atom's help kept as data, as read-only labelled boxes. */
|
|
462
|
-
function keptHelp(children, authoring) {
|
|
463
|
-
const t = (key, params) => authoring.t(EDITOR_NAMESPACE, key, params);
|
|
464
|
-
return children.flatMap((c) => {
|
|
465
|
-
if (typeof c === 'string' || !HELP_TAGS.has(c.tag))
|
|
466
|
-
return [];
|
|
467
|
-
const box = document.createElement('div');
|
|
468
|
-
box.className = 'astl-help-box';
|
|
469
|
-
box.dataset.help = c.tag;
|
|
470
|
-
const head = box.appendChild(document.createElement('div'));
|
|
471
|
-
head.className = 'astl-help-head';
|
|
472
|
-
head.lang = authoring.locale;
|
|
473
|
-
head.textContent = helpLabel(c.tag, c.attributes, t);
|
|
474
|
-
box.appendChild(document.createElement('div')).textContent = textOf(c).trim();
|
|
475
|
-
return [box];
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
449
|
/** Costs a question's hint offers, as written in `cost` ('0': free, no attribute); a cost besides them stays. */
|
|
479
450
|
const COSTS = ['0', '0.1', '0.25', '0.5'];
|
|
480
451
|
/** Whether help in `parent` is the page's: directly on a page, in a column, or in a document without pages. */
|
|
@@ -577,6 +548,7 @@ function helpNode(tag, authoring) {
|
|
|
577
548
|
content: FLOW,
|
|
578
549
|
defining: true,
|
|
579
550
|
isolating: true,
|
|
551
|
+
selectable: false,
|
|
580
552
|
addAttributes: () => sourceAttributes,
|
|
581
553
|
parseHTML: () => [{ tag }],
|
|
582
554
|
renderHTML: ({ node }) => [tag, { ...node.attrs.attributes }, 0],
|
|
@@ -693,11 +665,14 @@ function helpNode(tag, authoring) {
|
|
|
693
665
|
attributes[tag === HINT_TAG ? 'cost' : 'when'] = select.value;
|
|
694
666
|
editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', attributes));
|
|
695
667
|
});
|
|
696
|
-
// "Remove" at the end of the head, named by the kind of box: a box the menu added empty has no other way
|
|
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.
|
|
697
670
|
const remove = head.appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
|
|
698
671
|
remove.className = 'astl-help-remove';
|
|
699
|
-
remove.
|
|
700
|
-
|
|
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;
|
|
701
676
|
remove.addEventListener('click', () => {
|
|
702
677
|
const pos = getPos();
|
|
703
678
|
if (typeof pos !== 'number')
|
|
@@ -782,6 +757,44 @@ function helpNode(tag, authoring) {
|
|
|
782
757
|
export function helpNodes(authoring) {
|
|
783
758
|
return [HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map((tag) => helpNode(tag, authoring));
|
|
784
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
|
+
}
|
|
785
798
|
/**
|
|
786
799
|
* A block with rich children, or with rich content (`content: 'flow'`), framed and labelled in the editor, with its
|
|
787
800
|
* picture when it has picture fields.
|
|
@@ -823,6 +836,9 @@ function blockNode(manifest, pictures, authoring) {
|
|
|
823
836
|
addNodeView: () => ({ node, getPos, editor }) => {
|
|
824
837
|
const dom = document.createElement('div');
|
|
825
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);
|
|
826
842
|
const picture = pictures?.layers(manifest.tag).length ? pictures.create(manifest.tag) : null;
|
|
827
843
|
const contentDOM = document.createElement('div');
|
|
828
844
|
// Inside the frame, not on it: an author's border must not hide what the frame says.
|
|
@@ -863,8 +879,21 @@ function blockNode(manifest, pictures, authoring) {
|
|
|
863
879
|
add.addEventListener('click', () => {
|
|
864
880
|
const pos = getPos();
|
|
865
881
|
const block = typeof pos === 'number' ? editor.state.doc.nodeAt(pos) : null;
|
|
882
|
+
if (typeof pos !== 'number' || block === null)
|
|
883
|
+
return;
|
|
884
|
+
// In a rich block the child is a mark in its text (Select words' word): a new word ends its last line.
|
|
885
|
+
const markType = editor.schema.marks[childType];
|
|
886
|
+
if (markType !== undefined) {
|
|
887
|
+
const word = authoring.t(EDITOR_NAMESPACE, 'surface.newWord');
|
|
888
|
+
const tr = addMarkedWord(editor.state, pos, block, markType, repeated.tag, word);
|
|
889
|
+
if (tr === null)
|
|
890
|
+
return;
|
|
891
|
+
editor.view.dispatch(tr.scrollIntoView());
|
|
892
|
+
editor.view.focus();
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
866
895
|
const type = editor.schema.nodes[childType];
|
|
867
|
-
if (
|
|
896
|
+
if (type === undefined)
|
|
868
897
|
return;
|
|
869
898
|
// After the last child of that tag (an option goes after the options, before anything after them).
|
|
870
899
|
let at = pos + block.nodeSize - 1;
|
|
@@ -926,6 +955,105 @@ function blockNode(manifest, pictures, authoring) {
|
|
|
926
955
|
},
|
|
927
956
|
});
|
|
928
957
|
}
|
|
958
|
+
/**
|
|
959
|
+
* A new child of a rich block that is a mark in its text (Select words' word, `markType`): `text` at the end of the
|
|
960
|
+
* block's last line (after a space), with a fresh id, selected so that typing replaces it and keeps the mark; one undo
|
|
961
|
+
* step. A block without a line of text gets one.
|
|
962
|
+
*/
|
|
963
|
+
function addMarkedWord(state, pos, block, markType, tag, text) {
|
|
964
|
+
const tr = closeHistory(state.tr);
|
|
965
|
+
let end = null;
|
|
966
|
+
block.descendants((node, offset) => {
|
|
967
|
+
if (node.type.name === 'paragraph' || node.type.name === 'heading')
|
|
968
|
+
end = pos + 2 + offset + node.content.size;
|
|
969
|
+
return !node.isTextblock;
|
|
970
|
+
});
|
|
971
|
+
if (end === null) {
|
|
972
|
+
const paragraph = state.schema.nodes.paragraph;
|
|
973
|
+
if (paragraph === undefined)
|
|
974
|
+
return null;
|
|
975
|
+
const at = pos + block.nodeSize - 1;
|
|
976
|
+
tr.insert(at, paragraph.create());
|
|
977
|
+
end = at + 1;
|
|
978
|
+
}
|
|
979
|
+
let at = end;
|
|
980
|
+
const $end = tr.doc.resolve(at);
|
|
981
|
+
const before = $end.parent.textBetween(0, $end.parentOffset, undefined, '\ufffc');
|
|
982
|
+
// A plain space before it: inserted text would take the mark of a word that ends the line.
|
|
983
|
+
if (before !== '' && !/\s$/u.test(before)) {
|
|
984
|
+
tr.insert(at, state.schema.text(' '));
|
|
985
|
+
at += 1;
|
|
986
|
+
}
|
|
987
|
+
const taken = idsIn(tr.doc);
|
|
988
|
+
tr.doc.descendants((node) => {
|
|
989
|
+
for (const mark of node.marks)
|
|
990
|
+
if (mark.type === markType)
|
|
991
|
+
taken.add(markId(mark));
|
|
992
|
+
return true;
|
|
993
|
+
});
|
|
994
|
+
const id = freshId(tag.replace(/^astl-/, ''), taken);
|
|
995
|
+
tr.insert(at, state.schema.text(text, [markType.create({ attributes: { id } })]));
|
|
996
|
+
// The word grows as the author types over it or at its end (`growingWordExtension`).
|
|
997
|
+
return tr
|
|
998
|
+
.setSelection(TextSelection.create(tr.doc, at, at + text.length))
|
|
999
|
+
.setMeta(GROWING, { id, type: markType });
|
|
1000
|
+
}
|
|
1001
|
+
const GROWING = new PluginKey('astlGrowingWord');
|
|
1002
|
+
/** Where the run of text marked `growing` is (one word), with its mark, or null. */
|
|
1003
|
+
function growingRun(doc, growing) {
|
|
1004
|
+
let run = null;
|
|
1005
|
+
doc.descendants((node, pos) => {
|
|
1006
|
+
if (!node.isText)
|
|
1007
|
+
return run === null;
|
|
1008
|
+
const mark = node.marks.find((m) => m.type === growing.type && markId(m) === growing.id);
|
|
1009
|
+
if (mark === undefined)
|
|
1010
|
+
return false;
|
|
1011
|
+
run = run === null ? { from: pos, to: pos + node.nodeSize, mark } : { ...run, to: pos + node.nodeSize };
|
|
1012
|
+
return false;
|
|
1013
|
+
});
|
|
1014
|
+
return run;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* A word "Add option" added keeps growing while the author types it: text typed over it (it comes selected) or at its
|
|
1018
|
+
* end joins it, though the words' marks end with them, until a space or the cursor leaves it. Other words keep their
|
|
1019
|
+
* marks as they are.
|
|
1020
|
+
*/
|
|
1021
|
+
const growingWordExtension = Extension.create({
|
|
1022
|
+
name: 'astlGrowingWord',
|
|
1023
|
+
addProseMirrorPlugins: () => [
|
|
1024
|
+
new Plugin({
|
|
1025
|
+
key: GROWING,
|
|
1026
|
+
state: {
|
|
1027
|
+
init: () => null,
|
|
1028
|
+
apply: (tr, value, _, state) => {
|
|
1029
|
+
const set = tr.getMeta(GROWING);
|
|
1030
|
+
const growing = set === undefined ? value : set;
|
|
1031
|
+
if (growing === null)
|
|
1032
|
+
return null;
|
|
1033
|
+
const run = growingRun(state.doc, growing);
|
|
1034
|
+
const { from, to } = state.selection;
|
|
1035
|
+
return run !== null && from >= run.from && to <= run.to ? growing : null;
|
|
1036
|
+
},
|
|
1037
|
+
},
|
|
1038
|
+
props: {
|
|
1039
|
+
handleTextInput: (view, from, to, text) => {
|
|
1040
|
+
const growing = GROWING.getState(view.state);
|
|
1041
|
+
if (growing === null || growing === undefined || !/^[\p{L}\p{N}\p{M}'’-]+$/u.test(text))
|
|
1042
|
+
return false;
|
|
1043
|
+
const run = growingRun(view.state.doc, growing);
|
|
1044
|
+
if (run === null)
|
|
1045
|
+
return false;
|
|
1046
|
+
const over = from === run.from && to === run.to;
|
|
1047
|
+
if (!over && !(from === to && from === run.to))
|
|
1048
|
+
return false;
|
|
1049
|
+
const tr = view.state.tr.insertText(text, from, to);
|
|
1050
|
+
view.dispatch(tr.addMark(from, from + text.length, run.mark));
|
|
1051
|
+
return true;
|
|
1052
|
+
},
|
|
1053
|
+
},
|
|
1054
|
+
}),
|
|
1055
|
+
],
|
|
1056
|
+
});
|
|
929
1057
|
/**
|
|
930
1058
|
* A rich child of a block (prompt, option): one line of rich text, in a plain element so no custom element claims
|
|
931
1059
|
* it. A child the manifest allows once (a prompt) is marked `single`.
|
|
@@ -2777,8 +2905,10 @@ export function astrolabeExtensions(authoring, manifests = authoring.engine.mani
|
|
|
2777
2905
|
referencesExtension(authoring, manifests),
|
|
2778
2906
|
childrenExtension(manifests),
|
|
2779
2907
|
richBlocksExtension(authoring, manifests),
|
|
2908
|
+
growingWordExtension,
|
|
2780
2909
|
pastedIdsExtension(authoring, manifests, hooks),
|
|
2781
|
-
blocksExtension(authoring, (node) => types.get(node.type.name) ?? null, nodeName, hooks?.style === undefined ? undefined : () => hooks.style?.('component')),
|
|
2910
|
+
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)),
|
|
2911
|
+
helpPlaceholderExtension(authoring),
|
|
2782
2912
|
];
|
|
2783
2913
|
}
|
|
2784
2914
|
//# sourceMappingURL=nodes.js.map
|