@tiptap/core 2.0.0-beta.98 → 2.0.0-rc.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/README.md +3 -3
- package/dist/index.cjs +4360 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{tiptap-core.esm.js → index.js} +2349 -1447
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +4358 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/packages/core/src/CommandManager.d.ts +14 -7
- package/dist/packages/core/src/Editor.d.ts +25 -11
- package/dist/packages/core/src/EventEmitter.d.ts +8 -4
- package/dist/packages/core/src/Extension.d.ts +63 -29
- package/dist/packages/core/src/ExtensionManager.d.ts +3 -4
- package/dist/packages/core/src/InputRule.d.ts +42 -0
- package/dist/packages/core/src/Mark.d.ts +94 -36
- package/dist/packages/core/src/Node.d.ts +104 -45
- package/dist/packages/core/src/NodeView.d.ts +8 -8
- package/dist/packages/core/src/PasteRule.d.ts +42 -0
- package/dist/packages/core/src/Tracker.d.ts +1 -1
- package/dist/packages/core/src/commands/deleteCurrentNode.d.ts +12 -0
- package/dist/packages/core/src/commands/deleteNode.d.ts +1 -1
- package/dist/packages/core/src/commands/deleteRange.d.ts +1 -1
- package/dist/packages/core/src/commands/extendMarkRange.d.ts +1 -1
- package/dist/packages/core/src/commands/focus.d.ts +4 -2
- package/dist/packages/core/src/commands/index.d.ts +50 -0
- package/dist/packages/core/src/commands/insertContent.d.ts +6 -3
- package/dist/packages/core/src/commands/insertContentAt.d.ts +6 -3
- package/dist/packages/core/src/commands/join.d.ts +33 -0
- package/dist/packages/core/src/commands/lift.d.ts +1 -1
- package/dist/packages/core/src/commands/liftListItem.d.ts +1 -1
- package/dist/packages/core/src/commands/resetAttributes.d.ts +1 -1
- package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +12 -0
- package/dist/packages/core/src/commands/selectTextblockStart.d.ts +12 -0
- package/dist/packages/core/src/commands/setContent.d.ts +2 -2
- package/dist/packages/core/src/commands/setMark.d.ts +1 -1
- package/dist/packages/core/src/commands/setNode.d.ts +1 -1
- package/dist/packages/core/src/commands/setTextSelection.d.ts +1 -1
- package/dist/packages/core/src/commands/sinkListItem.d.ts +1 -1
- package/dist/packages/core/src/commands/splitListItem.d.ts +1 -1
- package/dist/packages/core/src/commands/toggleList.d.ts +2 -2
- package/dist/packages/core/src/commands/toggleMark.d.ts +7 -2
- package/dist/packages/core/src/commands/toggleNode.d.ts +1 -1
- package/dist/packages/core/src/commands/toggleWrap.d.ts +1 -1
- package/dist/packages/core/src/commands/unsetMark.d.ts +7 -2
- package/dist/packages/core/src/commands/updateAttributes.d.ts +1 -1
- package/dist/packages/core/src/commands/wrapIn.d.ts +1 -1
- package/dist/packages/core/src/commands/wrapInList.d.ts +1 -1
- package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +1 -1
- package/dist/packages/core/src/extensions/commands.d.ts +2 -97
- package/dist/packages/core/src/extensions/editable.d.ts +1 -1
- package/dist/packages/core/src/extensions/focusEvents.d.ts +1 -1
- package/dist/packages/core/src/extensions/index.d.ts +1 -0
- package/dist/packages/core/src/extensions/keymap.d.ts +1 -1
- package/dist/packages/core/src/extensions/tabindex.d.ts +2 -0
- package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +7 -0
- package/dist/packages/core/src/helpers/createChainableState.d.ts +5 -0
- package/dist/packages/core/src/helpers/createDocument.d.ts +2 -2
- package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +2 -2
- package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +2 -0
- package/dist/packages/core/src/helpers/findChildren.d.ts +3 -3
- package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +3 -3
- package/dist/packages/core/src/helpers/findParentNode.d.ts +3 -4
- package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +3 -3
- package/dist/packages/core/src/helpers/generateHTML.d.ts +1 -1
- package/dist/packages/core/src/helpers/generateJSON.d.ts +1 -1
- package/dist/packages/core/src/helpers/generateText.d.ts +5 -0
- package/dist/packages/core/src/helpers/getAttributes.d.ts +3 -3
- package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +2 -2
- package/dist/packages/core/src/helpers/getChangedRanges.d.ts +11 -0
- package/dist/packages/core/src/helpers/getDebugJSON.d.ts +2 -5
- package/dist/packages/core/src/helpers/getExtensionField.d.ts +2 -2
- package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +2 -2
- package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +3 -3
- package/dist/packages/core/src/helpers/getMarkRange.d.ts +2 -2
- package/dist/packages/core/src/helpers/getMarkType.d.ts +2 -2
- package/dist/packages/core/src/helpers/getMarksBetween.d.ts +2 -2
- package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +3 -3
- package/dist/packages/core/src/helpers/getNodeType.d.ts +2 -2
- package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +2 -2
- package/dist/packages/core/src/helpers/getSchema.d.ts +2 -2
- package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +2 -2
- package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +2 -2
- package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +2 -2
- package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +1 -1
- package/dist/packages/core/src/helpers/getText.d.ts +6 -0
- package/dist/packages/core/src/helpers/getTextBetween.d.ts +6 -0
- package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +2 -0
- package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +3 -0
- package/dist/packages/core/src/helpers/index.d.ts +47 -0
- package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +2 -2
- package/dist/packages/core/src/helpers/isActive.d.ts +2 -2
- package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +2 -0
- package/dist/packages/core/src/helpers/isList.d.ts +1 -1
- package/dist/packages/core/src/helpers/isMarkActive.d.ts +3 -3
- package/dist/packages/core/src/helpers/isNodeActive.d.ts +3 -3
- package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +2 -2
- package/dist/packages/core/src/helpers/isNodeSelection.d.ts +2 -2
- package/dist/packages/core/src/helpers/isTextSelection.d.ts +2 -2
- package/dist/packages/core/src/helpers/posToDOMRect.d.ts +2 -2
- package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +4 -0
- package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +2 -2
- package/dist/packages/core/src/helpers/splitExtensions.d.ts +6 -6
- package/dist/packages/core/src/index.d.ts +12 -34
- package/dist/packages/core/src/inputRules/index.d.ts +5 -0
- package/dist/packages/core/src/inputRules/markInputRule.d.ts +12 -3
- package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +12 -3
- package/dist/packages/core/src/inputRules/textInputRule.d.ts +9 -0
- package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +14 -0
- package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +27 -0
- package/dist/packages/core/src/pasteRules/index.d.ts +3 -0
- package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +12 -3
- package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +12 -0
- package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +9 -0
- package/dist/packages/core/src/style.d.ts +1 -2
- package/dist/packages/core/src/types.d.ts +70 -40
- package/dist/packages/core/src/utilities/callOrReturn.d.ts +1 -1
- package/dist/packages/core/src/utilities/createStyleTag.d.ts +1 -1
- package/dist/packages/core/src/utilities/deleteProps.d.ts +1 -1
- package/dist/packages/core/src/utilities/elementFromString.d.ts +1 -1
- package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +1 -0
- package/dist/packages/core/src/utilities/findDuplicates.d.ts +1 -0
- package/dist/packages/core/src/utilities/fromString.d.ts +1 -1
- package/dist/packages/core/src/utilities/index.d.ts +20 -0
- package/dist/packages/core/src/utilities/isEmptyObject.d.ts +1 -1
- package/dist/packages/core/src/utilities/isFunction.d.ts +1 -0
- package/dist/packages/core/src/utilities/isMacOS.d.ts +1 -0
- package/dist/packages/core/src/utilities/isNumber.d.ts +1 -0
- package/dist/packages/core/src/utilities/isPlainObject.d.ts +1 -1
- package/dist/packages/core/src/utilities/isRegExp.d.ts +1 -0
- package/dist/packages/core/src/utilities/isString.d.ts +1 -0
- package/dist/packages/core/src/utilities/isiOS.d.ts +1 -1
- package/dist/packages/core/src/utilities/mergeAttributes.d.ts +1 -1
- package/dist/packages/core/src/utilities/mergeDeep.d.ts +1 -1
- package/dist/packages/core/src/utilities/minMax.d.ts +1 -1
- package/dist/packages/core/src/utilities/objectIncludes.d.ts +3 -1
- package/dist/packages/core/src/utilities/removeDuplicates.d.ts +8 -0
- package/package.json +25 -24
- package/src/CommandManager.ts +73 -83
- package/src/Editor.ts +101 -53
- package/src/EventEmitter.ts +14 -4
- package/src/Extension.ts +244 -138
- package/src/ExtensionManager.ts +153 -152
- package/src/InputRule.ts +260 -0
- package/src/Mark.ts +365 -204
- package/src/Node.ts +406 -253
- package/src/NodeView.ts +59 -38
- package/src/PasteRule.ts +240 -0
- package/src/Tracker.ts +4 -8
- package/src/commands/blur.ts +4 -0
- package/src/commands/clearNodes.ts +15 -9
- package/src/commands/createParagraphNear.ts +3 -2
- package/src/commands/deleteCurrentNode.ts +41 -0
- package/src/commands/deleteNode.ts +3 -2
- package/src/commands/deleteRange.ts +1 -1
- package/src/commands/deleteSelection.ts +3 -2
- package/src/commands/exitCode.ts +3 -2
- package/src/commands/extendMarkRange.ts +9 -5
- package/src/commands/focus.ts +31 -42
- package/src/commands/index.ts +50 -0
- package/src/commands/insertContent.ts +15 -4
- package/src/commands/insertContentAt.ts +71 -14
- package/src/commands/join.ts +53 -0
- package/src/commands/keyboardShortcut.ts +3 -3
- package/src/commands/lift.ts +6 -5
- package/src/commands/liftEmptyBlock.ts +2 -1
- package/src/commands/liftListItem.ts +5 -4
- package/src/commands/newlineInCode.ts +3 -2
- package/src/commands/resetAttributes.ts +16 -10
- package/src/commands/selectAll.ts +5 -3
- package/src/commands/selectNodeBackward.ts +3 -2
- package/src/commands/selectNodeForward.ts +3 -2
- package/src/commands/selectParentNode.ts +3 -2
- package/src/commands/selectTextblockEnd.ts +20 -0
- package/src/commands/selectTextblockStart.ts +20 -0
- package/src/commands/setContent.ts +6 -9
- package/src/commands/setMark.ts +66 -13
- package/src/commands/setNode.ts +30 -6
- package/src/commands/setNodeSelection.ts +6 -7
- package/src/commands/setTextSelection.ts +8 -9
- package/src/commands/sinkListItem.ts +5 -4
- package/src/commands/splitBlock.ts +23 -38
- package/src/commands/splitListItem.ts +30 -27
- package/src/commands/toggleList.ts +108 -19
- package/src/commands/toggleMark.ts +17 -6
- package/src/commands/toggleNode.ts +9 -4
- package/src/commands/toggleWrap.ts +8 -8
- package/src/commands/undoInputRule.ts +31 -2
- package/src/commands/unsetAllMarks.ts +4 -8
- package/src/commands/unsetMark.ts +34 -21
- package/src/commands/updateAttributes.ts +18 -12
- package/src/commands/wrapIn.ts +5 -10
- package/src/commands/wrapInList.ts +5 -4
- package/src/extensions/clipboardTextSerializer.ts +15 -36
- package/src/extensions/commands.ts +3 -144
- package/src/extensions/editable.ts +2 -1
- package/src/extensions/focusEvents.ts +4 -6
- package/src/extensions/index.ts +1 -0
- package/src/extensions/keymap.ts +111 -13
- package/src/extensions/tabindex.ts +18 -0
- package/src/helpers/combineTransactionSteps.ts +21 -0
- package/src/helpers/createChainableState.ts +38 -0
- package/src/helpers/createDocument.ts +4 -3
- package/src/helpers/createNodeFromContent.ts +10 -15
- package/src/helpers/defaultBlockAt.ts +13 -0
- package/src/helpers/findChildren.ts +4 -3
- package/src/helpers/findChildrenInRange.ts +8 -3
- package/src/helpers/findParentNode.ts +4 -3
- package/src/helpers/findParentNodeClosestToPos.ts +13 -7
- package/src/helpers/generateHTML.ts +6 -5
- package/src/helpers/generateJSON.ts +6 -7
- package/src/helpers/generateText.ts +27 -0
- package/src/helpers/getAttributes.ts +8 -9
- package/src/helpers/getAttributesFromExtensions.ts +25 -12
- package/src/helpers/getChangedRanges.ts +83 -0
- package/src/helpers/getDebugJSON.ts +42 -38
- package/src/helpers/getExtensionField.ts +3 -3
- package/src/helpers/getHTMLFromFragment.ts +5 -6
- package/src/helpers/getMarkAttributes.ts +18 -10
- package/src/helpers/getMarkRange.ts +13 -8
- package/src/helpers/getMarkType.ts +5 -3
- package/src/helpers/getMarksBetween.ts +34 -10
- package/src/helpers/getNodeAttributes.ts +14 -12
- package/src/helpers/getNodeType.ts +5 -3
- package/src/helpers/getRenderedAttributes.ts +8 -6
- package/src/helpers/getSchema.ts +5 -4
- package/src/helpers/getSchemaByResolvedExtensions.ts +165 -111
- package/src/helpers/getSchemaTypeByName.ts +3 -11
- package/src/helpers/getSchemaTypeNameByName.ts +2 -2
- package/src/helpers/getSplittedAttributes.ts +1 -1
- package/src/helpers/getText.ts +19 -0
- package/src/helpers/getTextBetween.ts +46 -0
- package/src/helpers/getTextContentFromNodes.ts +26 -0
- package/src/helpers/getTextSerializersFromSchema.ts +11 -0
- package/src/helpers/index.ts +47 -0
- package/src/helpers/injectExtensionAttributesToParseRule.ts +22 -23
- package/src/helpers/isActive.ts +10 -5
- package/src/helpers/isExtensionRulesEnabled.ts +15 -0
- package/src/helpers/isList.ts +6 -5
- package/src/helpers/isMarkActive.ts +32 -35
- package/src/helpers/isNodeActive.ts +27 -37
- package/src/helpers/isNodeEmpty.ts +2 -2
- package/src/helpers/isNodeSelection.ts +3 -4
- package/src/helpers/isTextSelection.ts +3 -4
- package/src/helpers/posToDOMRect.ts +10 -4
- package/src/helpers/resolveFocusPosition.ts +42 -0
- package/src/helpers/selectionToInsertionEnd.ts +3 -3
- package/src/helpers/splitExtensions.ts +3 -3
- package/src/index.ts +12 -37
- package/src/inputRules/index.ts +5 -0
- package/src/inputRules/markInputRule.ts +59 -40
- package/src/inputRules/nodeInputRule.ts +45 -12
- package/src/inputRules/textInputRule.ts +35 -0
- package/src/inputRules/textblockTypeInputRule.ts +37 -0
- package/src/inputRules/wrappingInputRule.ts +84 -0
- package/src/pasteRules/index.ts +3 -0
- package/src/pasteRules/markPasteRule.ts +49 -56
- package/src/pasteRules/nodePasteRule.ts +37 -0
- package/src/pasteRules/textPasteRule.ts +35 -0
- package/src/style.ts +12 -3
- package/src/types.ts +140 -106
- package/src/utilities/callOrReturn.ts +3 -2
- package/src/utilities/createStyleTag.ts +8 -4
- package/src/utilities/deleteProps.ts +1 -1
- package/src/utilities/elementFromString.ts +1 -1
- package/src/utilities/escapeForRegEx.ts +4 -0
- package/src/utilities/findDuplicates.ts +5 -0
- package/src/utilities/fromString.ts +2 -2
- package/src/utilities/index.ts +20 -0
- package/src/utilities/isEmptyObject.ts +2 -2
- package/src/utilities/isFunction.ts +3 -0
- package/src/utilities/isMacOS.ts +5 -0
- package/src/utilities/isNumber.ts +3 -0
- package/src/utilities/isPlainObject.ts +8 -5
- package/src/utilities/isRegExp.ts +3 -0
- package/src/utilities/isString.ts +3 -0
- package/src/utilities/isiOS.ts +1 -1
- package/src/utilities/mergeAttributes.ts +2 -1
- package/src/utilities/mergeDeep.ts +2 -2
- package/src/utilities/minMax.ts +1 -1
- package/src/utilities/objectIncludes.ts +18 -4
- package/src/utilities/removeDuplicates.ts +15 -0
- package/CHANGELOG.md +0 -1182
- package/LICENSE.md +0 -21
- package/dist/packages/core/src/commands/joinBackward.d.ts +0 -12
- package/dist/packages/core/src/commands/joinForward.d.ts +0 -12
- package/dist/packages/core/src/utilities/isClass.d.ts +0 -1
- package/dist/packages/core/src/utilities/isObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/removeElement.d.ts +0 -1
- package/dist/tiptap-core.cjs.js +0 -3408
- package/dist/tiptap-core.cjs.js.map +0 -1
- package/dist/tiptap-core.esm.js.map +0 -1
- package/dist/tiptap-core.umd.js +0 -3405
- package/dist/tiptap-core.umd.js.map +0 -1
- package/src/commands/joinBackward.ts +0 -17
- package/src/commands/joinForward.ts +0 -17
- package/src/utilities/isClass.ts +0 -7
- package/src/utilities/isObject.ts +0 -10
- package/src/utilities/removeElement.ts +0 -5
|
@@ -0,0 +1,4358 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tiptap/pm/state'), require('@tiptap/pm/view'), require('@tiptap/pm/keymap'), require('@tiptap/pm/model'), require('@tiptap/pm/transform'), require('@tiptap/pm/commands'), require('@tiptap/pm/schema-list')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@tiptap/pm/state', '@tiptap/pm/view', '@tiptap/pm/keymap', '@tiptap/pm/model', '@tiptap/pm/transform', '@tiptap/pm/commands', '@tiptap/pm/schema-list'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@tiptap/core"] = {}, global.state, global.view, global.keymap, global.model, global.transform, global.commands$1, global.schemaList));
|
|
5
|
+
})(this, (function (exports, state, view, keymap, model, transform, commands$1, schemaList) { 'use strict';
|
|
6
|
+
|
|
7
|
+
function createChainableState(config) {
|
|
8
|
+
const { state, transaction } = config;
|
|
9
|
+
let { selection } = transaction;
|
|
10
|
+
let { doc } = transaction;
|
|
11
|
+
let { storedMarks } = transaction;
|
|
12
|
+
return {
|
|
13
|
+
...state,
|
|
14
|
+
apply: state.apply.bind(state),
|
|
15
|
+
applyTransaction: state.applyTransaction.bind(state),
|
|
16
|
+
filterTransaction: state.filterTransaction,
|
|
17
|
+
plugins: state.plugins,
|
|
18
|
+
schema: state.schema,
|
|
19
|
+
reconfigure: state.reconfigure.bind(state),
|
|
20
|
+
toJSON: state.toJSON.bind(state),
|
|
21
|
+
get storedMarks() {
|
|
22
|
+
return storedMarks;
|
|
23
|
+
},
|
|
24
|
+
get selection() {
|
|
25
|
+
return selection;
|
|
26
|
+
},
|
|
27
|
+
get doc() {
|
|
28
|
+
return doc;
|
|
29
|
+
},
|
|
30
|
+
get tr() {
|
|
31
|
+
selection = transaction.selection;
|
|
32
|
+
doc = transaction.doc;
|
|
33
|
+
storedMarks = transaction.storedMarks;
|
|
34
|
+
return transaction;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class CommandManager {
|
|
40
|
+
constructor(props) {
|
|
41
|
+
this.editor = props.editor;
|
|
42
|
+
this.rawCommands = this.editor.extensionManager.commands;
|
|
43
|
+
this.customState = props.state;
|
|
44
|
+
}
|
|
45
|
+
get hasCustomState() {
|
|
46
|
+
return !!this.customState;
|
|
47
|
+
}
|
|
48
|
+
get state() {
|
|
49
|
+
return this.customState || this.editor.state;
|
|
50
|
+
}
|
|
51
|
+
get commands() {
|
|
52
|
+
const { rawCommands, editor, state } = this;
|
|
53
|
+
const { view } = editor;
|
|
54
|
+
const { tr } = state;
|
|
55
|
+
const props = this.buildProps(tr);
|
|
56
|
+
return Object.fromEntries(Object.entries(rawCommands).map(([name, command]) => {
|
|
57
|
+
const method = (...args) => {
|
|
58
|
+
const callback = command(...args)(props);
|
|
59
|
+
if (!tr.getMeta('preventDispatch') && !this.hasCustomState) {
|
|
60
|
+
view.dispatch(tr);
|
|
61
|
+
}
|
|
62
|
+
return callback;
|
|
63
|
+
};
|
|
64
|
+
return [name, method];
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
get chain() {
|
|
68
|
+
return () => this.createChain();
|
|
69
|
+
}
|
|
70
|
+
get can() {
|
|
71
|
+
return () => this.createCan();
|
|
72
|
+
}
|
|
73
|
+
createChain(startTr, shouldDispatch = true) {
|
|
74
|
+
const { rawCommands, editor, state } = this;
|
|
75
|
+
const { view } = editor;
|
|
76
|
+
const callbacks = [];
|
|
77
|
+
const hasStartTransaction = !!startTr;
|
|
78
|
+
const tr = startTr || state.tr;
|
|
79
|
+
const run = () => {
|
|
80
|
+
if (!hasStartTransaction
|
|
81
|
+
&& shouldDispatch
|
|
82
|
+
&& !tr.getMeta('preventDispatch')
|
|
83
|
+
&& !this.hasCustomState) {
|
|
84
|
+
view.dispatch(tr);
|
|
85
|
+
}
|
|
86
|
+
return callbacks.every(callback => callback === true);
|
|
87
|
+
};
|
|
88
|
+
const chain = {
|
|
89
|
+
...Object.fromEntries(Object.entries(rawCommands).map(([name, command]) => {
|
|
90
|
+
const chainedCommand = (...args) => {
|
|
91
|
+
const props = this.buildProps(tr, shouldDispatch);
|
|
92
|
+
const callback = command(...args)(props);
|
|
93
|
+
callbacks.push(callback);
|
|
94
|
+
return chain;
|
|
95
|
+
};
|
|
96
|
+
return [name, chainedCommand];
|
|
97
|
+
})),
|
|
98
|
+
run,
|
|
99
|
+
};
|
|
100
|
+
return chain;
|
|
101
|
+
}
|
|
102
|
+
createCan(startTr) {
|
|
103
|
+
const { rawCommands, state } = this;
|
|
104
|
+
const dispatch = false;
|
|
105
|
+
const tr = startTr || state.tr;
|
|
106
|
+
const props = this.buildProps(tr, dispatch);
|
|
107
|
+
const formattedCommands = Object.fromEntries(Object.entries(rawCommands).map(([name, command]) => {
|
|
108
|
+
return [name, (...args) => command(...args)({ ...props, dispatch: undefined })];
|
|
109
|
+
}));
|
|
110
|
+
return {
|
|
111
|
+
...formattedCommands,
|
|
112
|
+
chain: () => this.createChain(tr, dispatch),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
buildProps(tr, shouldDispatch = true) {
|
|
116
|
+
const { rawCommands, editor, state } = this;
|
|
117
|
+
const { view } = editor;
|
|
118
|
+
if (state.storedMarks) {
|
|
119
|
+
tr.setStoredMarks(state.storedMarks);
|
|
120
|
+
}
|
|
121
|
+
const props = {
|
|
122
|
+
tr,
|
|
123
|
+
editor,
|
|
124
|
+
view,
|
|
125
|
+
state: createChainableState({
|
|
126
|
+
state,
|
|
127
|
+
transaction: tr,
|
|
128
|
+
}),
|
|
129
|
+
dispatch: shouldDispatch ? () => undefined : undefined,
|
|
130
|
+
chain: () => this.createChain(tr),
|
|
131
|
+
can: () => this.createCan(tr),
|
|
132
|
+
get commands() {
|
|
133
|
+
return Object.fromEntries(Object.entries(rawCommands).map(([name, command]) => {
|
|
134
|
+
return [name, (...args) => command(...args)(props)];
|
|
135
|
+
}));
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
return props;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
class EventEmitter {
|
|
143
|
+
constructor() {
|
|
144
|
+
this.callbacks = {};
|
|
145
|
+
}
|
|
146
|
+
on(event, fn) {
|
|
147
|
+
if (!this.callbacks[event]) {
|
|
148
|
+
this.callbacks[event] = [];
|
|
149
|
+
}
|
|
150
|
+
this.callbacks[event].push(fn);
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
emit(event, ...args) {
|
|
154
|
+
const callbacks = this.callbacks[event];
|
|
155
|
+
if (callbacks) {
|
|
156
|
+
callbacks.forEach(callback => callback.apply(this, args));
|
|
157
|
+
}
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
off(event, fn) {
|
|
161
|
+
const callbacks = this.callbacks[event];
|
|
162
|
+
if (callbacks) {
|
|
163
|
+
if (fn) {
|
|
164
|
+
this.callbacks[event] = callbacks.filter(callback => callback !== fn);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
delete this.callbacks[event];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
removeAllListeners() {
|
|
173
|
+
this.callbacks = {};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function getExtensionField(extension, field, context) {
|
|
178
|
+
if (extension.config[field] === undefined && extension.parent) {
|
|
179
|
+
return getExtensionField(extension.parent, field, context);
|
|
180
|
+
}
|
|
181
|
+
if (typeof extension.config[field] === 'function') {
|
|
182
|
+
const value = extension.config[field].bind({
|
|
183
|
+
...context,
|
|
184
|
+
parent: extension.parent
|
|
185
|
+
? getExtensionField(extension.parent, field, context)
|
|
186
|
+
: null,
|
|
187
|
+
});
|
|
188
|
+
return value;
|
|
189
|
+
}
|
|
190
|
+
return extension.config[field];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function splitExtensions(extensions) {
|
|
194
|
+
const baseExtensions = extensions.filter(extension => extension.type === 'extension');
|
|
195
|
+
const nodeExtensions = extensions.filter(extension => extension.type === 'node');
|
|
196
|
+
const markExtensions = extensions.filter(extension => extension.type === 'mark');
|
|
197
|
+
return {
|
|
198
|
+
baseExtensions,
|
|
199
|
+
nodeExtensions,
|
|
200
|
+
markExtensions,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`.
|
|
206
|
+
* @param extensions List of extensions
|
|
207
|
+
*/
|
|
208
|
+
function getAttributesFromExtensions(extensions) {
|
|
209
|
+
const extensionAttributes = [];
|
|
210
|
+
const { nodeExtensions, markExtensions } = splitExtensions(extensions);
|
|
211
|
+
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions];
|
|
212
|
+
const defaultAttribute = {
|
|
213
|
+
default: null,
|
|
214
|
+
rendered: true,
|
|
215
|
+
renderHTML: null,
|
|
216
|
+
parseHTML: null,
|
|
217
|
+
keepOnSplit: true,
|
|
218
|
+
isRequired: false,
|
|
219
|
+
};
|
|
220
|
+
extensions.forEach(extension => {
|
|
221
|
+
const context = {
|
|
222
|
+
name: extension.name,
|
|
223
|
+
options: extension.options,
|
|
224
|
+
storage: extension.storage,
|
|
225
|
+
};
|
|
226
|
+
const addGlobalAttributes = getExtensionField(extension, 'addGlobalAttributes', context);
|
|
227
|
+
if (!addGlobalAttributes) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
// TODO: remove `as GlobalAttributes`
|
|
231
|
+
const globalAttributes = addGlobalAttributes();
|
|
232
|
+
globalAttributes.forEach(globalAttribute => {
|
|
233
|
+
globalAttribute.types.forEach(type => {
|
|
234
|
+
Object
|
|
235
|
+
.entries(globalAttribute.attributes)
|
|
236
|
+
.forEach(([name, attribute]) => {
|
|
237
|
+
extensionAttributes.push({
|
|
238
|
+
type,
|
|
239
|
+
name,
|
|
240
|
+
attribute: {
|
|
241
|
+
...defaultAttribute,
|
|
242
|
+
...attribute,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
nodeAndMarkExtensions.forEach(extension => {
|
|
250
|
+
const context = {
|
|
251
|
+
name: extension.name,
|
|
252
|
+
options: extension.options,
|
|
253
|
+
storage: extension.storage,
|
|
254
|
+
};
|
|
255
|
+
const addAttributes = getExtensionField(extension, 'addAttributes', context);
|
|
256
|
+
if (!addAttributes) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
// TODO: remove `as Attributes`
|
|
260
|
+
const attributes = addAttributes();
|
|
261
|
+
Object
|
|
262
|
+
.entries(attributes)
|
|
263
|
+
.forEach(([name, attribute]) => {
|
|
264
|
+
const mergedAttr = {
|
|
265
|
+
...defaultAttribute,
|
|
266
|
+
...attribute,
|
|
267
|
+
};
|
|
268
|
+
if (typeof (mergedAttr === null || mergedAttr === void 0 ? void 0 : mergedAttr.default) === 'function') {
|
|
269
|
+
mergedAttr.default = mergedAttr.default();
|
|
270
|
+
}
|
|
271
|
+
if ((mergedAttr === null || mergedAttr === void 0 ? void 0 : mergedAttr.isRequired) && (mergedAttr === null || mergedAttr === void 0 ? void 0 : mergedAttr.default) === undefined) {
|
|
272
|
+
delete mergedAttr.default;
|
|
273
|
+
}
|
|
274
|
+
extensionAttributes.push({
|
|
275
|
+
type: extension.name,
|
|
276
|
+
name,
|
|
277
|
+
attribute: mergedAttr,
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
return extensionAttributes;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function getNodeType(nameOrType, schema) {
|
|
285
|
+
if (typeof nameOrType === 'string') {
|
|
286
|
+
if (!schema.nodes[nameOrType]) {
|
|
287
|
+
throw Error(`There is no node type named '${nameOrType}'. Maybe you forgot to add the extension?`);
|
|
288
|
+
}
|
|
289
|
+
return schema.nodes[nameOrType];
|
|
290
|
+
}
|
|
291
|
+
return nameOrType;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function mergeAttributes(...objects) {
|
|
295
|
+
return objects
|
|
296
|
+
.filter(item => !!item)
|
|
297
|
+
.reduce((items, item) => {
|
|
298
|
+
const mergedAttributes = { ...items };
|
|
299
|
+
Object.entries(item).forEach(([key, value]) => {
|
|
300
|
+
const exists = mergedAttributes[key];
|
|
301
|
+
if (!exists) {
|
|
302
|
+
mergedAttributes[key] = value;
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (key === 'class') {
|
|
306
|
+
mergedAttributes[key] = [mergedAttributes[key], value].join(' ');
|
|
307
|
+
}
|
|
308
|
+
else if (key === 'style') {
|
|
309
|
+
mergedAttributes[key] = [mergedAttributes[key], value].join('; ');
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
mergedAttributes[key] = value;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
return mergedAttributes;
|
|
316
|
+
}, {});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function getRenderedAttributes(nodeOrMark, extensionAttributes) {
|
|
320
|
+
return extensionAttributes
|
|
321
|
+
.filter(item => item.attribute.rendered)
|
|
322
|
+
.map(item => {
|
|
323
|
+
if (!item.attribute.renderHTML) {
|
|
324
|
+
return {
|
|
325
|
+
[item.name]: nodeOrMark.attrs[item.name],
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
return item.attribute.renderHTML(nodeOrMark.attrs) || {};
|
|
329
|
+
})
|
|
330
|
+
.reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function isFunction(value) {
|
|
334
|
+
return typeof value === 'function';
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Optionally calls `value` as a function.
|
|
339
|
+
* Otherwise it is returned directly.
|
|
340
|
+
* @param value Function or any value.
|
|
341
|
+
* @param context Optional context to bind to function.
|
|
342
|
+
* @param props Optional props to pass to function.
|
|
343
|
+
*/
|
|
344
|
+
function callOrReturn(value, context = undefined, ...props) {
|
|
345
|
+
if (isFunction(value)) {
|
|
346
|
+
if (context) {
|
|
347
|
+
return value.bind(context)(...props);
|
|
348
|
+
}
|
|
349
|
+
return value(...props);
|
|
350
|
+
}
|
|
351
|
+
return value;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function isEmptyObject(value = {}) {
|
|
355
|
+
return Object.keys(value).length === 0 && value.constructor === Object;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function fromString(value) {
|
|
359
|
+
if (typeof value !== 'string') {
|
|
360
|
+
return value;
|
|
361
|
+
}
|
|
362
|
+
if (value.match(/^[+-]?(?:\d*\.)?\d+$/)) {
|
|
363
|
+
return Number(value);
|
|
364
|
+
}
|
|
365
|
+
if (value === 'true') {
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
if (value === 'false') {
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
return value;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* This function merges extension attributes into parserule attributes (`attrs` or `getAttrs`).
|
|
376
|
+
* Cancels when `getAttrs` returned `false`.
|
|
377
|
+
* @param parseRule ProseMirror ParseRule
|
|
378
|
+
* @param extensionAttributes List of attributes to inject
|
|
379
|
+
*/
|
|
380
|
+
function injectExtensionAttributesToParseRule(parseRule, extensionAttributes) {
|
|
381
|
+
if (parseRule.style) {
|
|
382
|
+
return parseRule;
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
...parseRule,
|
|
386
|
+
getAttrs: node => {
|
|
387
|
+
const oldAttributes = parseRule.getAttrs ? parseRule.getAttrs(node) : parseRule.attrs;
|
|
388
|
+
if (oldAttributes === false) {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
const newAttributes = extensionAttributes.reduce((items, item) => {
|
|
392
|
+
const value = item.attribute.parseHTML
|
|
393
|
+
? item.attribute.parseHTML(node)
|
|
394
|
+
: fromString(node.getAttribute(item.name));
|
|
395
|
+
if (value === null || value === undefined) {
|
|
396
|
+
return items;
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
...items,
|
|
400
|
+
[item.name]: value,
|
|
401
|
+
};
|
|
402
|
+
}, {});
|
|
403
|
+
return { ...oldAttributes, ...newAttributes };
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function cleanUpSchemaItem(data) {
|
|
409
|
+
return Object.fromEntries(Object.entries(data).filter(([key, value]) => {
|
|
410
|
+
if (key === 'attrs' && isEmptyObject(value)) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
return value !== null && value !== undefined;
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
function getSchemaByResolvedExtensions(extensions) {
|
|
417
|
+
var _a;
|
|
418
|
+
const allAttributes = getAttributesFromExtensions(extensions);
|
|
419
|
+
const { nodeExtensions, markExtensions } = splitExtensions(extensions);
|
|
420
|
+
const topNode = (_a = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))) === null || _a === void 0 ? void 0 : _a.name;
|
|
421
|
+
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
|
|
422
|
+
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name);
|
|
423
|
+
const context = {
|
|
424
|
+
name: extension.name,
|
|
425
|
+
options: extension.options,
|
|
426
|
+
storage: extension.storage,
|
|
427
|
+
};
|
|
428
|
+
const extraNodeFields = extensions.reduce((fields, e) => {
|
|
429
|
+
const extendNodeSchema = getExtensionField(e, 'extendNodeSchema', context);
|
|
430
|
+
return {
|
|
431
|
+
...fields,
|
|
432
|
+
...(extendNodeSchema ? extendNodeSchema(extension) : {}),
|
|
433
|
+
};
|
|
434
|
+
}, {});
|
|
435
|
+
const schema = cleanUpSchemaItem({
|
|
436
|
+
...extraNodeFields,
|
|
437
|
+
content: callOrReturn(getExtensionField(extension, 'content', context)),
|
|
438
|
+
marks: callOrReturn(getExtensionField(extension, 'marks', context)),
|
|
439
|
+
group: callOrReturn(getExtensionField(extension, 'group', context)),
|
|
440
|
+
inline: callOrReturn(getExtensionField(extension, 'inline', context)),
|
|
441
|
+
atom: callOrReturn(getExtensionField(extension, 'atom', context)),
|
|
442
|
+
selectable: callOrReturn(getExtensionField(extension, 'selectable', context)),
|
|
443
|
+
draggable: callOrReturn(getExtensionField(extension, 'draggable', context)),
|
|
444
|
+
code: callOrReturn(getExtensionField(extension, 'code', context)),
|
|
445
|
+
defining: callOrReturn(getExtensionField(extension, 'defining', context)),
|
|
446
|
+
isolating: callOrReturn(getExtensionField(extension, 'isolating', context)),
|
|
447
|
+
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
|
448
|
+
var _a;
|
|
449
|
+
return [extensionAttribute.name, { default: (_a = extensionAttribute === null || extensionAttribute === void 0 ? void 0 : extensionAttribute.attribute) === null || _a === void 0 ? void 0 : _a.default }];
|
|
450
|
+
})),
|
|
451
|
+
});
|
|
452
|
+
const parseHTML = callOrReturn(getExtensionField(extension, 'parseHTML', context));
|
|
453
|
+
if (parseHTML) {
|
|
454
|
+
schema.parseDOM = parseHTML.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes));
|
|
455
|
+
}
|
|
456
|
+
const renderHTML = getExtensionField(extension, 'renderHTML', context);
|
|
457
|
+
if (renderHTML) {
|
|
458
|
+
schema.toDOM = node => renderHTML({
|
|
459
|
+
node,
|
|
460
|
+
HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
const renderText = getExtensionField(extension, 'renderText', context);
|
|
464
|
+
if (renderText) {
|
|
465
|
+
schema.toText = renderText;
|
|
466
|
+
}
|
|
467
|
+
return [extension.name, schema];
|
|
468
|
+
}));
|
|
469
|
+
const marks = Object.fromEntries(markExtensions.map(extension => {
|
|
470
|
+
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name);
|
|
471
|
+
const context = {
|
|
472
|
+
name: extension.name,
|
|
473
|
+
options: extension.options,
|
|
474
|
+
storage: extension.storage,
|
|
475
|
+
};
|
|
476
|
+
const extraMarkFields = extensions.reduce((fields, e) => {
|
|
477
|
+
const extendMarkSchema = getExtensionField(e, 'extendMarkSchema', context);
|
|
478
|
+
return {
|
|
479
|
+
...fields,
|
|
480
|
+
...(extendMarkSchema ? extendMarkSchema(extension) : {}),
|
|
481
|
+
};
|
|
482
|
+
}, {});
|
|
483
|
+
const schema = cleanUpSchemaItem({
|
|
484
|
+
...extraMarkFields,
|
|
485
|
+
inclusive: callOrReturn(getExtensionField(extension, 'inclusive', context)),
|
|
486
|
+
excludes: callOrReturn(getExtensionField(extension, 'excludes', context)),
|
|
487
|
+
group: callOrReturn(getExtensionField(extension, 'group', context)),
|
|
488
|
+
spanning: callOrReturn(getExtensionField(extension, 'spanning', context)),
|
|
489
|
+
code: callOrReturn(getExtensionField(extension, 'code', context)),
|
|
490
|
+
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
|
491
|
+
var _a;
|
|
492
|
+
return [extensionAttribute.name, { default: (_a = extensionAttribute === null || extensionAttribute === void 0 ? void 0 : extensionAttribute.attribute) === null || _a === void 0 ? void 0 : _a.default }];
|
|
493
|
+
})),
|
|
494
|
+
});
|
|
495
|
+
const parseHTML = callOrReturn(getExtensionField(extension, 'parseHTML', context));
|
|
496
|
+
if (parseHTML) {
|
|
497
|
+
schema.parseDOM = parseHTML.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes));
|
|
498
|
+
}
|
|
499
|
+
const renderHTML = getExtensionField(extension, 'renderHTML', context);
|
|
500
|
+
if (renderHTML) {
|
|
501
|
+
schema.toDOM = mark => renderHTML({
|
|
502
|
+
mark,
|
|
503
|
+
HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
return [extension.name, schema];
|
|
507
|
+
}));
|
|
508
|
+
return new model.Schema({
|
|
509
|
+
topNode,
|
|
510
|
+
nodes,
|
|
511
|
+
marks,
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function getSchemaTypeByName(name, schema) {
|
|
516
|
+
return schema.nodes[name] || schema.marks[name] || null;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function isExtensionRulesEnabled(extension, enabled) {
|
|
520
|
+
if (Array.isArray(enabled)) {
|
|
521
|
+
return enabled.some(enabledExtension => {
|
|
522
|
+
const name = typeof enabledExtension === 'string'
|
|
523
|
+
? enabledExtension
|
|
524
|
+
: enabledExtension.name;
|
|
525
|
+
return name === extension.name;
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
return enabled;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const getTextContentFromNodes = ($from, maxMatch = 500) => {
|
|
532
|
+
let textBefore = '';
|
|
533
|
+
const sliceEndPos = $from.parentOffset;
|
|
534
|
+
$from.parent.nodesBetween(Math.max(0, sliceEndPos - maxMatch), sliceEndPos, (node, pos, parent, index) => {
|
|
535
|
+
var _a, _b;
|
|
536
|
+
const chunk = ((_b = (_a = node.type.spec).toText) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
537
|
+
node,
|
|
538
|
+
pos,
|
|
539
|
+
parent,
|
|
540
|
+
index,
|
|
541
|
+
}))
|
|
542
|
+
|| node.textContent
|
|
543
|
+
|| '%leaf%';
|
|
544
|
+
textBefore += chunk.slice(0, Math.max(0, sliceEndPos - pos));
|
|
545
|
+
});
|
|
546
|
+
return textBefore;
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
function isRegExp(value) {
|
|
550
|
+
return Object.prototype.toString.call(value) === '[object RegExp]';
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
class InputRule {
|
|
554
|
+
constructor(config) {
|
|
555
|
+
this.find = config.find;
|
|
556
|
+
this.handler = config.handler;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
const inputRuleMatcherHandler = (text, find) => {
|
|
560
|
+
if (isRegExp(find)) {
|
|
561
|
+
return find.exec(text);
|
|
562
|
+
}
|
|
563
|
+
const inputRuleMatch = find(text);
|
|
564
|
+
if (!inputRuleMatch) {
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
const result = [inputRuleMatch.text];
|
|
568
|
+
result.index = inputRuleMatch.index;
|
|
569
|
+
result.input = text;
|
|
570
|
+
result.data = inputRuleMatch.data;
|
|
571
|
+
if (inputRuleMatch.replaceWith) {
|
|
572
|
+
if (!inputRuleMatch.text.includes(inputRuleMatch.replaceWith)) {
|
|
573
|
+
console.warn('[tiptap warn]: "inputRuleMatch.replaceWith" must be part of "inputRuleMatch.text".');
|
|
574
|
+
}
|
|
575
|
+
result.push(inputRuleMatch.replaceWith);
|
|
576
|
+
}
|
|
577
|
+
return result;
|
|
578
|
+
};
|
|
579
|
+
function run$1(config) {
|
|
580
|
+
var _a;
|
|
581
|
+
const { editor, from, to, text, rules, plugin, } = config;
|
|
582
|
+
const { view } = editor;
|
|
583
|
+
if (view.composing) {
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
const $from = view.state.doc.resolve(from);
|
|
587
|
+
if (
|
|
588
|
+
// check for code node
|
|
589
|
+
$from.parent.type.spec.code
|
|
590
|
+
// check for code mark
|
|
591
|
+
|| !!((_a = ($from.nodeBefore || $from.nodeAfter)) === null || _a === void 0 ? void 0 : _a.marks.find(mark => mark.type.spec.code))) {
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
let matched = false;
|
|
595
|
+
const textBefore = getTextContentFromNodes($from) + text;
|
|
596
|
+
rules.forEach(rule => {
|
|
597
|
+
if (matched) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
const match = inputRuleMatcherHandler(textBefore, rule.find);
|
|
601
|
+
if (!match) {
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
const tr = view.state.tr;
|
|
605
|
+
const state = createChainableState({
|
|
606
|
+
state: view.state,
|
|
607
|
+
transaction: tr,
|
|
608
|
+
});
|
|
609
|
+
const range = {
|
|
610
|
+
from: from - (match[0].length - text.length),
|
|
611
|
+
to,
|
|
612
|
+
};
|
|
613
|
+
const { commands, chain, can } = new CommandManager({
|
|
614
|
+
editor,
|
|
615
|
+
state,
|
|
616
|
+
});
|
|
617
|
+
const handler = rule.handler({
|
|
618
|
+
state,
|
|
619
|
+
range,
|
|
620
|
+
match,
|
|
621
|
+
commands,
|
|
622
|
+
chain,
|
|
623
|
+
can,
|
|
624
|
+
});
|
|
625
|
+
// stop if there are no changes
|
|
626
|
+
if (handler === null || !tr.steps.length) {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
// store transform as meta data
|
|
630
|
+
// so we can undo input rules within the `undoInputRules` command
|
|
631
|
+
tr.setMeta(plugin, {
|
|
632
|
+
transform: tr,
|
|
633
|
+
from,
|
|
634
|
+
to,
|
|
635
|
+
text,
|
|
636
|
+
});
|
|
637
|
+
view.dispatch(tr);
|
|
638
|
+
matched = true;
|
|
639
|
+
});
|
|
640
|
+
return matched;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Create an input rules plugin. When enabled, it will cause text
|
|
644
|
+
* input that matches any of the given rules to trigger the rule’s
|
|
645
|
+
* action.
|
|
646
|
+
*/
|
|
647
|
+
function inputRulesPlugin(props) {
|
|
648
|
+
const { editor, rules } = props;
|
|
649
|
+
const plugin = new state.Plugin({
|
|
650
|
+
state: {
|
|
651
|
+
init() {
|
|
652
|
+
return null;
|
|
653
|
+
},
|
|
654
|
+
apply(tr, prev) {
|
|
655
|
+
const stored = tr.getMeta(plugin);
|
|
656
|
+
if (stored) {
|
|
657
|
+
return stored;
|
|
658
|
+
}
|
|
659
|
+
return tr.selectionSet || tr.docChanged ? null : prev;
|
|
660
|
+
},
|
|
661
|
+
},
|
|
662
|
+
props: {
|
|
663
|
+
handleTextInput(view, from, to, text) {
|
|
664
|
+
return run$1({
|
|
665
|
+
editor,
|
|
666
|
+
from,
|
|
667
|
+
to,
|
|
668
|
+
text,
|
|
669
|
+
rules,
|
|
670
|
+
plugin,
|
|
671
|
+
});
|
|
672
|
+
},
|
|
673
|
+
handleDOMEvents: {
|
|
674
|
+
compositionend: view => {
|
|
675
|
+
setTimeout(() => {
|
|
676
|
+
const { $cursor } = view.state.selection;
|
|
677
|
+
if ($cursor) {
|
|
678
|
+
run$1({
|
|
679
|
+
editor,
|
|
680
|
+
from: $cursor.pos,
|
|
681
|
+
to: $cursor.pos,
|
|
682
|
+
text: '',
|
|
683
|
+
rules,
|
|
684
|
+
plugin,
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
return false;
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
// add support for input rules to trigger on enter
|
|
692
|
+
// this is useful for example for code blocks
|
|
693
|
+
handleKeyDown(view, event) {
|
|
694
|
+
if (event.key !== 'Enter') {
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
const { $cursor } = view.state.selection;
|
|
698
|
+
if ($cursor) {
|
|
699
|
+
return run$1({
|
|
700
|
+
editor,
|
|
701
|
+
from: $cursor.pos,
|
|
702
|
+
to: $cursor.pos,
|
|
703
|
+
text: '\n',
|
|
704
|
+
rules,
|
|
705
|
+
plugin,
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
return false;
|
|
709
|
+
},
|
|
710
|
+
},
|
|
711
|
+
// @ts-ignore
|
|
712
|
+
isInputRules: true,
|
|
713
|
+
});
|
|
714
|
+
return plugin;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function isNumber(value) {
|
|
718
|
+
return typeof value === 'number';
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
class PasteRule {
|
|
722
|
+
constructor(config) {
|
|
723
|
+
this.find = config.find;
|
|
724
|
+
this.handler = config.handler;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
const pasteRuleMatcherHandler = (text, find) => {
|
|
728
|
+
if (isRegExp(find)) {
|
|
729
|
+
return [...text.matchAll(find)];
|
|
730
|
+
}
|
|
731
|
+
const matches = find(text);
|
|
732
|
+
if (!matches) {
|
|
733
|
+
return [];
|
|
734
|
+
}
|
|
735
|
+
return matches.map(pasteRuleMatch => {
|
|
736
|
+
const result = [pasteRuleMatch.text];
|
|
737
|
+
result.index = pasteRuleMatch.index;
|
|
738
|
+
result.input = text;
|
|
739
|
+
result.data = pasteRuleMatch.data;
|
|
740
|
+
if (pasteRuleMatch.replaceWith) {
|
|
741
|
+
if (!pasteRuleMatch.text.includes(pasteRuleMatch.replaceWith)) {
|
|
742
|
+
console.warn('[tiptap warn]: "pasteRuleMatch.replaceWith" must be part of "pasteRuleMatch.text".');
|
|
743
|
+
}
|
|
744
|
+
result.push(pasteRuleMatch.replaceWith);
|
|
745
|
+
}
|
|
746
|
+
return result;
|
|
747
|
+
});
|
|
748
|
+
};
|
|
749
|
+
function run(config) {
|
|
750
|
+
const { editor, state, from, to, rule, } = config;
|
|
751
|
+
const { commands, chain, can } = new CommandManager({
|
|
752
|
+
editor,
|
|
753
|
+
state,
|
|
754
|
+
});
|
|
755
|
+
const handlers = [];
|
|
756
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
757
|
+
if (!node.isTextblock || node.type.spec.code) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
const resolvedFrom = Math.max(from, pos);
|
|
761
|
+
const resolvedTo = Math.min(to, pos + node.content.size);
|
|
762
|
+
const textToMatch = node.textBetween(resolvedFrom - pos, resolvedTo - pos, undefined, '\ufffc');
|
|
763
|
+
const matches = pasteRuleMatcherHandler(textToMatch, rule.find);
|
|
764
|
+
matches.forEach(match => {
|
|
765
|
+
if (match.index === undefined) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
const start = resolvedFrom + match.index + 1;
|
|
769
|
+
const end = start + match[0].length;
|
|
770
|
+
const range = {
|
|
771
|
+
from: state.tr.mapping.map(start),
|
|
772
|
+
to: state.tr.mapping.map(end),
|
|
773
|
+
};
|
|
774
|
+
const handler = rule.handler({
|
|
775
|
+
state,
|
|
776
|
+
range,
|
|
777
|
+
match,
|
|
778
|
+
commands,
|
|
779
|
+
chain,
|
|
780
|
+
can,
|
|
781
|
+
});
|
|
782
|
+
handlers.push(handler);
|
|
783
|
+
});
|
|
784
|
+
});
|
|
785
|
+
const success = handlers.every(handler => handler !== null);
|
|
786
|
+
return success;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Create an paste rules plugin. When enabled, it will cause pasted
|
|
790
|
+
* text that matches any of the given rules to trigger the rule’s
|
|
791
|
+
* action.
|
|
792
|
+
*/
|
|
793
|
+
function pasteRulesPlugin(props) {
|
|
794
|
+
const { editor, rules } = props;
|
|
795
|
+
let dragSourceElement = null;
|
|
796
|
+
let isPastedFromProseMirror = false;
|
|
797
|
+
let isDroppedFromProseMirror = false;
|
|
798
|
+
const plugins = rules.map(rule => {
|
|
799
|
+
return new state.Plugin({
|
|
800
|
+
// we register a global drag handler to track the current drag source element
|
|
801
|
+
view(view) {
|
|
802
|
+
const handleDragstart = (event) => {
|
|
803
|
+
var _a;
|
|
804
|
+
dragSourceElement = ((_a = view.dom.parentElement) === null || _a === void 0 ? void 0 : _a.contains(event.target))
|
|
805
|
+
? view.dom.parentElement
|
|
806
|
+
: null;
|
|
807
|
+
};
|
|
808
|
+
window.addEventListener('dragstart', handleDragstart);
|
|
809
|
+
return {
|
|
810
|
+
destroy() {
|
|
811
|
+
window.removeEventListener('dragstart', handleDragstart);
|
|
812
|
+
},
|
|
813
|
+
};
|
|
814
|
+
},
|
|
815
|
+
props: {
|
|
816
|
+
handleDOMEvents: {
|
|
817
|
+
drop: view => {
|
|
818
|
+
isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement;
|
|
819
|
+
return false;
|
|
820
|
+
},
|
|
821
|
+
paste: (view, event) => {
|
|
822
|
+
var _a;
|
|
823
|
+
const html = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData('text/html');
|
|
824
|
+
isPastedFromProseMirror = !!(html === null || html === void 0 ? void 0 : html.includes('data-pm-slice'));
|
|
825
|
+
return false;
|
|
826
|
+
},
|
|
827
|
+
},
|
|
828
|
+
},
|
|
829
|
+
appendTransaction: (transactions, oldState, state) => {
|
|
830
|
+
const transaction = transactions[0];
|
|
831
|
+
const isPaste = transaction.getMeta('uiEvent') === 'paste' && !isPastedFromProseMirror;
|
|
832
|
+
const isDrop = transaction.getMeta('uiEvent') === 'drop' && !isDroppedFromProseMirror;
|
|
833
|
+
if (!isPaste && !isDrop) {
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
// stop if there is no changed range
|
|
837
|
+
const from = oldState.doc.content.findDiffStart(state.doc.content);
|
|
838
|
+
const to = oldState.doc.content.findDiffEnd(state.doc.content);
|
|
839
|
+
if (!isNumber(from) || !to || from === to.b) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
// build a chainable state
|
|
843
|
+
// so we can use a single transaction for all paste rules
|
|
844
|
+
const tr = state.tr;
|
|
845
|
+
const chainableState = createChainableState({
|
|
846
|
+
state,
|
|
847
|
+
transaction: tr,
|
|
848
|
+
});
|
|
849
|
+
const handler = run({
|
|
850
|
+
editor,
|
|
851
|
+
state: chainableState,
|
|
852
|
+
from: Math.max(from - 1, 0),
|
|
853
|
+
to: to.b - 1,
|
|
854
|
+
rule,
|
|
855
|
+
});
|
|
856
|
+
// stop if there are no changes
|
|
857
|
+
if (!handler || !tr.steps.length) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
return tr;
|
|
861
|
+
},
|
|
862
|
+
});
|
|
863
|
+
});
|
|
864
|
+
return plugins;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
function findDuplicates(items) {
|
|
868
|
+
const filtered = items.filter((el, index) => items.indexOf(el) !== index);
|
|
869
|
+
return [...new Set(filtered)];
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
class ExtensionManager {
|
|
873
|
+
constructor(extensions, editor) {
|
|
874
|
+
this.splittableMarks = [];
|
|
875
|
+
this.editor = editor;
|
|
876
|
+
this.extensions = ExtensionManager.resolve(extensions);
|
|
877
|
+
this.schema = getSchemaByResolvedExtensions(this.extensions);
|
|
878
|
+
this.extensions.forEach(extension => {
|
|
879
|
+
var _a;
|
|
880
|
+
// store extension storage in editor
|
|
881
|
+
this.editor.extensionStorage[extension.name] = extension.storage;
|
|
882
|
+
const context = {
|
|
883
|
+
name: extension.name,
|
|
884
|
+
options: extension.options,
|
|
885
|
+
storage: extension.storage,
|
|
886
|
+
editor: this.editor,
|
|
887
|
+
type: getSchemaTypeByName(extension.name, this.schema),
|
|
888
|
+
};
|
|
889
|
+
if (extension.type === 'mark') {
|
|
890
|
+
const keepOnSplit = (_a = callOrReturn(getExtensionField(extension, 'keepOnSplit', context))) !== null && _a !== void 0 ? _a : true;
|
|
891
|
+
if (keepOnSplit) {
|
|
892
|
+
this.splittableMarks.push(extension.name);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
const onBeforeCreate = getExtensionField(extension, 'onBeforeCreate', context);
|
|
896
|
+
if (onBeforeCreate) {
|
|
897
|
+
this.editor.on('beforeCreate', onBeforeCreate);
|
|
898
|
+
}
|
|
899
|
+
const onCreate = getExtensionField(extension, 'onCreate', context);
|
|
900
|
+
if (onCreate) {
|
|
901
|
+
this.editor.on('create', onCreate);
|
|
902
|
+
}
|
|
903
|
+
const onUpdate = getExtensionField(extension, 'onUpdate', context);
|
|
904
|
+
if (onUpdate) {
|
|
905
|
+
this.editor.on('update', onUpdate);
|
|
906
|
+
}
|
|
907
|
+
const onSelectionUpdate = getExtensionField(extension, 'onSelectionUpdate', context);
|
|
908
|
+
if (onSelectionUpdate) {
|
|
909
|
+
this.editor.on('selectionUpdate', onSelectionUpdate);
|
|
910
|
+
}
|
|
911
|
+
const onTransaction = getExtensionField(extension, 'onTransaction', context);
|
|
912
|
+
if (onTransaction) {
|
|
913
|
+
this.editor.on('transaction', onTransaction);
|
|
914
|
+
}
|
|
915
|
+
const onFocus = getExtensionField(extension, 'onFocus', context);
|
|
916
|
+
if (onFocus) {
|
|
917
|
+
this.editor.on('focus', onFocus);
|
|
918
|
+
}
|
|
919
|
+
const onBlur = getExtensionField(extension, 'onBlur', context);
|
|
920
|
+
if (onBlur) {
|
|
921
|
+
this.editor.on('blur', onBlur);
|
|
922
|
+
}
|
|
923
|
+
const onDestroy = getExtensionField(extension, 'onDestroy', context);
|
|
924
|
+
if (onDestroy) {
|
|
925
|
+
this.editor.on('destroy', onDestroy);
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
static resolve(extensions) {
|
|
930
|
+
const resolvedExtensions = ExtensionManager.sort(ExtensionManager.flatten(extensions));
|
|
931
|
+
const duplicatedNames = findDuplicates(resolvedExtensions.map(extension => extension.name));
|
|
932
|
+
if (duplicatedNames.length) {
|
|
933
|
+
console.warn(`[tiptap warn]: Duplicate extension names found: [${duplicatedNames
|
|
934
|
+
.map(item => `'${item}'`)
|
|
935
|
+
.join(', ')}]. This can lead to issues.`);
|
|
936
|
+
}
|
|
937
|
+
return resolvedExtensions;
|
|
938
|
+
}
|
|
939
|
+
static flatten(extensions) {
|
|
940
|
+
return (extensions
|
|
941
|
+
.map(extension => {
|
|
942
|
+
const context = {
|
|
943
|
+
name: extension.name,
|
|
944
|
+
options: extension.options,
|
|
945
|
+
storage: extension.storage,
|
|
946
|
+
};
|
|
947
|
+
const addExtensions = getExtensionField(extension, 'addExtensions', context);
|
|
948
|
+
if (addExtensions) {
|
|
949
|
+
return [extension, ...this.flatten(addExtensions())];
|
|
950
|
+
}
|
|
951
|
+
return extension;
|
|
952
|
+
})
|
|
953
|
+
// `Infinity` will break TypeScript so we set a number that is probably high enough
|
|
954
|
+
.flat(10));
|
|
955
|
+
}
|
|
956
|
+
static sort(extensions) {
|
|
957
|
+
const defaultPriority = 100;
|
|
958
|
+
return extensions.sort((a, b) => {
|
|
959
|
+
const priorityA = getExtensionField(a, 'priority') || defaultPriority;
|
|
960
|
+
const priorityB = getExtensionField(b, 'priority') || defaultPriority;
|
|
961
|
+
if (priorityA > priorityB) {
|
|
962
|
+
return -1;
|
|
963
|
+
}
|
|
964
|
+
if (priorityA < priorityB) {
|
|
965
|
+
return 1;
|
|
966
|
+
}
|
|
967
|
+
return 0;
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
get commands() {
|
|
971
|
+
return this.extensions.reduce((commands, extension) => {
|
|
972
|
+
const context = {
|
|
973
|
+
name: extension.name,
|
|
974
|
+
options: extension.options,
|
|
975
|
+
storage: extension.storage,
|
|
976
|
+
editor: this.editor,
|
|
977
|
+
type: getSchemaTypeByName(extension.name, this.schema),
|
|
978
|
+
};
|
|
979
|
+
const addCommands = getExtensionField(extension, 'addCommands', context);
|
|
980
|
+
if (!addCommands) {
|
|
981
|
+
return commands;
|
|
982
|
+
}
|
|
983
|
+
return {
|
|
984
|
+
...commands,
|
|
985
|
+
...addCommands(),
|
|
986
|
+
};
|
|
987
|
+
}, {});
|
|
988
|
+
}
|
|
989
|
+
get plugins() {
|
|
990
|
+
const { editor } = this;
|
|
991
|
+
// With ProseMirror, first plugins within an array are executed first.
|
|
992
|
+
// In tiptap, we provide the ability to override plugins,
|
|
993
|
+
// so it feels more natural to run plugins at the end of an array first.
|
|
994
|
+
// That’s why we have to reverse the `extensions` array and sort again
|
|
995
|
+
// based on the `priority` option.
|
|
996
|
+
const extensions = ExtensionManager.sort([...this.extensions].reverse());
|
|
997
|
+
const inputRules = [];
|
|
998
|
+
const pasteRules = [];
|
|
999
|
+
const allPlugins = extensions
|
|
1000
|
+
.map(extension => {
|
|
1001
|
+
const context = {
|
|
1002
|
+
name: extension.name,
|
|
1003
|
+
options: extension.options,
|
|
1004
|
+
storage: extension.storage,
|
|
1005
|
+
editor,
|
|
1006
|
+
type: getSchemaTypeByName(extension.name, this.schema),
|
|
1007
|
+
};
|
|
1008
|
+
const plugins = [];
|
|
1009
|
+
const addKeyboardShortcuts = getExtensionField(extension, 'addKeyboardShortcuts', context);
|
|
1010
|
+
let defaultBindings = {};
|
|
1011
|
+
// bind exit handling
|
|
1012
|
+
if (extension.type === 'mark' && extension.config.exitable) {
|
|
1013
|
+
defaultBindings.ArrowRight = () => Mark.handleExit({ editor, mark: extension });
|
|
1014
|
+
}
|
|
1015
|
+
if (addKeyboardShortcuts) {
|
|
1016
|
+
const bindings = Object.fromEntries(Object.entries(addKeyboardShortcuts()).map(([shortcut, method]) => {
|
|
1017
|
+
return [shortcut, () => method({ editor })];
|
|
1018
|
+
}));
|
|
1019
|
+
defaultBindings = { ...defaultBindings, ...bindings };
|
|
1020
|
+
}
|
|
1021
|
+
const keyMapPlugin = keymap.keymap(defaultBindings);
|
|
1022
|
+
plugins.push(keyMapPlugin);
|
|
1023
|
+
const addInputRules = getExtensionField(extension, 'addInputRules', context);
|
|
1024
|
+
if (isExtensionRulesEnabled(extension, editor.options.enableInputRules) && addInputRules) {
|
|
1025
|
+
inputRules.push(...addInputRules());
|
|
1026
|
+
}
|
|
1027
|
+
const addPasteRules = getExtensionField(extension, 'addPasteRules', context);
|
|
1028
|
+
if (isExtensionRulesEnabled(extension, editor.options.enablePasteRules) && addPasteRules) {
|
|
1029
|
+
pasteRules.push(...addPasteRules());
|
|
1030
|
+
}
|
|
1031
|
+
const addProseMirrorPlugins = getExtensionField(extension, 'addProseMirrorPlugins', context);
|
|
1032
|
+
if (addProseMirrorPlugins) {
|
|
1033
|
+
const proseMirrorPlugins = addProseMirrorPlugins();
|
|
1034
|
+
plugins.push(...proseMirrorPlugins);
|
|
1035
|
+
}
|
|
1036
|
+
return plugins;
|
|
1037
|
+
})
|
|
1038
|
+
.flat();
|
|
1039
|
+
return [
|
|
1040
|
+
inputRulesPlugin({
|
|
1041
|
+
editor,
|
|
1042
|
+
rules: inputRules,
|
|
1043
|
+
}),
|
|
1044
|
+
...pasteRulesPlugin({
|
|
1045
|
+
editor,
|
|
1046
|
+
rules: pasteRules,
|
|
1047
|
+
}),
|
|
1048
|
+
...allPlugins,
|
|
1049
|
+
];
|
|
1050
|
+
}
|
|
1051
|
+
get attributes() {
|
|
1052
|
+
return getAttributesFromExtensions(this.extensions);
|
|
1053
|
+
}
|
|
1054
|
+
get nodeViews() {
|
|
1055
|
+
const { editor } = this;
|
|
1056
|
+
const { nodeExtensions } = splitExtensions(this.extensions);
|
|
1057
|
+
return Object.fromEntries(nodeExtensions
|
|
1058
|
+
.filter(extension => !!getExtensionField(extension, 'addNodeView'))
|
|
1059
|
+
.map(extension => {
|
|
1060
|
+
const extensionAttributes = this.attributes.filter(attribute => attribute.type === extension.name);
|
|
1061
|
+
const context = {
|
|
1062
|
+
name: extension.name,
|
|
1063
|
+
options: extension.options,
|
|
1064
|
+
storage: extension.storage,
|
|
1065
|
+
editor,
|
|
1066
|
+
type: getNodeType(extension.name, this.schema),
|
|
1067
|
+
};
|
|
1068
|
+
const addNodeView = getExtensionField(extension, 'addNodeView', context);
|
|
1069
|
+
if (!addNodeView) {
|
|
1070
|
+
return [];
|
|
1071
|
+
}
|
|
1072
|
+
const nodeview = (node, view, getPos, decorations) => {
|
|
1073
|
+
const HTMLAttributes = getRenderedAttributes(node, extensionAttributes);
|
|
1074
|
+
return addNodeView()({
|
|
1075
|
+
editor,
|
|
1076
|
+
node,
|
|
1077
|
+
getPos,
|
|
1078
|
+
decorations,
|
|
1079
|
+
HTMLAttributes,
|
|
1080
|
+
extension,
|
|
1081
|
+
});
|
|
1082
|
+
};
|
|
1083
|
+
return [extension.name, nodeview];
|
|
1084
|
+
}));
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
// see: https://github.com/mesqueeb/is-what/blob/88d6e4ca92fb2baab6003c54e02eedf4e729e5ab/src/index.ts
|
|
1089
|
+
function getType(value) {
|
|
1090
|
+
return Object.prototype.toString.call(value).slice(8, -1);
|
|
1091
|
+
}
|
|
1092
|
+
function isPlainObject(value) {
|
|
1093
|
+
if (getType(value) !== 'Object') {
|
|
1094
|
+
return false;
|
|
1095
|
+
}
|
|
1096
|
+
return value.constructor === Object && Object.getPrototypeOf(value) === Object.prototype;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function mergeDeep(target, source) {
|
|
1100
|
+
const output = { ...target };
|
|
1101
|
+
if (isPlainObject(target) && isPlainObject(source)) {
|
|
1102
|
+
Object.keys(source).forEach(key => {
|
|
1103
|
+
if (isPlainObject(source[key])) {
|
|
1104
|
+
if (!(key in target)) {
|
|
1105
|
+
Object.assign(output, { [key]: source[key] });
|
|
1106
|
+
}
|
|
1107
|
+
else {
|
|
1108
|
+
output[key] = mergeDeep(target[key], source[key]);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
else {
|
|
1112
|
+
Object.assign(output, { [key]: source[key] });
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
return output;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
class Extension {
|
|
1120
|
+
constructor(config = {}) {
|
|
1121
|
+
this.type = 'extension';
|
|
1122
|
+
this.name = 'extension';
|
|
1123
|
+
this.parent = null;
|
|
1124
|
+
this.child = null;
|
|
1125
|
+
this.config = {
|
|
1126
|
+
name: this.name,
|
|
1127
|
+
defaultOptions: {},
|
|
1128
|
+
};
|
|
1129
|
+
this.config = {
|
|
1130
|
+
...this.config,
|
|
1131
|
+
...config,
|
|
1132
|
+
};
|
|
1133
|
+
this.name = this.config.name;
|
|
1134
|
+
if (config.defaultOptions) {
|
|
1135
|
+
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
|
|
1136
|
+
}
|
|
1137
|
+
// TODO: remove `addOptions` fallback
|
|
1138
|
+
this.options = this.config.defaultOptions;
|
|
1139
|
+
if (this.config.addOptions) {
|
|
1140
|
+
this.options = callOrReturn(getExtensionField(this, 'addOptions', {
|
|
1141
|
+
name: this.name,
|
|
1142
|
+
}));
|
|
1143
|
+
}
|
|
1144
|
+
this.storage = callOrReturn(getExtensionField(this, 'addStorage', {
|
|
1145
|
+
name: this.name,
|
|
1146
|
+
options: this.options,
|
|
1147
|
+
})) || {};
|
|
1148
|
+
}
|
|
1149
|
+
static create(config = {}) {
|
|
1150
|
+
return new Extension(config);
|
|
1151
|
+
}
|
|
1152
|
+
configure(options = {}) {
|
|
1153
|
+
// return a new instance so we can use the same extension
|
|
1154
|
+
// with different calls of `configure`
|
|
1155
|
+
const extension = this.extend();
|
|
1156
|
+
extension.options = mergeDeep(this.options, options);
|
|
1157
|
+
extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
|
|
1158
|
+
name: extension.name,
|
|
1159
|
+
options: extension.options,
|
|
1160
|
+
}));
|
|
1161
|
+
return extension;
|
|
1162
|
+
}
|
|
1163
|
+
extend(extendedConfig = {}) {
|
|
1164
|
+
const extension = new Extension(extendedConfig);
|
|
1165
|
+
extension.parent = this;
|
|
1166
|
+
this.child = extension;
|
|
1167
|
+
extension.name = extendedConfig.name ? extendedConfig.name : extension.parent.name;
|
|
1168
|
+
if (extendedConfig.defaultOptions) {
|
|
1169
|
+
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`);
|
|
1170
|
+
}
|
|
1171
|
+
extension.options = callOrReturn(getExtensionField(extension, 'addOptions', {
|
|
1172
|
+
name: extension.name,
|
|
1173
|
+
}));
|
|
1174
|
+
extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
|
|
1175
|
+
name: extension.name,
|
|
1176
|
+
options: extension.options,
|
|
1177
|
+
}));
|
|
1178
|
+
return extension;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
function getTextBetween(startNode, range, options) {
|
|
1183
|
+
const { from, to } = range;
|
|
1184
|
+
const { blockSeparator = '\n\n', textSerializers = {} } = options || {};
|
|
1185
|
+
let text = '';
|
|
1186
|
+
let separated = true;
|
|
1187
|
+
startNode.nodesBetween(from, to, (node, pos, parent, index) => {
|
|
1188
|
+
var _a;
|
|
1189
|
+
const textSerializer = textSerializers === null || textSerializers === void 0 ? void 0 : textSerializers[node.type.name];
|
|
1190
|
+
if (textSerializer) {
|
|
1191
|
+
if (node.isBlock && !separated) {
|
|
1192
|
+
text += blockSeparator;
|
|
1193
|
+
separated = true;
|
|
1194
|
+
}
|
|
1195
|
+
if (parent) {
|
|
1196
|
+
text += textSerializer({
|
|
1197
|
+
node,
|
|
1198
|
+
pos,
|
|
1199
|
+
parent,
|
|
1200
|
+
index,
|
|
1201
|
+
range,
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
else if (node.isText) {
|
|
1206
|
+
text += (_a = node === null || node === void 0 ? void 0 : node.text) === null || _a === void 0 ? void 0 : _a.slice(Math.max(from, pos) - pos, to - pos); // eslint-disable-line
|
|
1207
|
+
separated = false;
|
|
1208
|
+
}
|
|
1209
|
+
else if (node.isBlock && !separated) {
|
|
1210
|
+
text += blockSeparator;
|
|
1211
|
+
separated = true;
|
|
1212
|
+
}
|
|
1213
|
+
});
|
|
1214
|
+
return text;
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
function getTextSerializersFromSchema(schema) {
|
|
1218
|
+
return Object.fromEntries(Object.entries(schema.nodes)
|
|
1219
|
+
.filter(([, node]) => node.spec.toText)
|
|
1220
|
+
.map(([name, node]) => [name, node.spec.toText]));
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
const ClipboardTextSerializer = Extension.create({
|
|
1224
|
+
name: 'clipboardTextSerializer',
|
|
1225
|
+
addProseMirrorPlugins() {
|
|
1226
|
+
return [
|
|
1227
|
+
new state.Plugin({
|
|
1228
|
+
key: new state.PluginKey('clipboardTextSerializer'),
|
|
1229
|
+
props: {
|
|
1230
|
+
clipboardTextSerializer: () => {
|
|
1231
|
+
const { editor } = this;
|
|
1232
|
+
const { state, schema } = editor;
|
|
1233
|
+
const { doc, selection } = state;
|
|
1234
|
+
const { ranges } = selection;
|
|
1235
|
+
const from = Math.min(...ranges.map(range => range.$from.pos));
|
|
1236
|
+
const to = Math.max(...ranges.map(range => range.$to.pos));
|
|
1237
|
+
const textSerializers = getTextSerializersFromSchema(schema);
|
|
1238
|
+
const range = { from, to };
|
|
1239
|
+
return getTextBetween(doc, range, {
|
|
1240
|
+
textSerializers,
|
|
1241
|
+
});
|
|
1242
|
+
},
|
|
1243
|
+
},
|
|
1244
|
+
}),
|
|
1245
|
+
];
|
|
1246
|
+
},
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
const blur = () => ({ editor, view }) => {
|
|
1250
|
+
requestAnimationFrame(() => {
|
|
1251
|
+
var _a;
|
|
1252
|
+
if (!editor.isDestroyed) {
|
|
1253
|
+
view.dom.blur();
|
|
1254
|
+
// Browsers should remove the caret on blur but safari does not.
|
|
1255
|
+
// See: https://github.com/ueberdosis/tiptap/issues/2405
|
|
1256
|
+
(_a = window === null || window === void 0 ? void 0 : window.getSelection()) === null || _a === void 0 ? void 0 : _a.removeAllRanges();
|
|
1257
|
+
}
|
|
1258
|
+
});
|
|
1259
|
+
return true;
|
|
1260
|
+
};
|
|
1261
|
+
|
|
1262
|
+
const clearContent = (emitUpdate = false) => ({ commands }) => {
|
|
1263
|
+
return commands.setContent('', emitUpdate);
|
|
1264
|
+
};
|
|
1265
|
+
|
|
1266
|
+
const clearNodes = () => ({ state, tr, dispatch }) => {
|
|
1267
|
+
const { selection } = tr;
|
|
1268
|
+
const { ranges } = selection;
|
|
1269
|
+
if (!dispatch) {
|
|
1270
|
+
return true;
|
|
1271
|
+
}
|
|
1272
|
+
ranges.forEach(({ $from, $to }) => {
|
|
1273
|
+
state.doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
|
|
1274
|
+
if (node.type.isText) {
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
const { doc, mapping } = tr;
|
|
1278
|
+
const $mappedFrom = doc.resolve(mapping.map(pos));
|
|
1279
|
+
const $mappedTo = doc.resolve(mapping.map(pos + node.nodeSize));
|
|
1280
|
+
const nodeRange = $mappedFrom.blockRange($mappedTo);
|
|
1281
|
+
if (!nodeRange) {
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
const targetLiftDepth = transform.liftTarget(nodeRange);
|
|
1285
|
+
if (node.type.isTextblock) {
|
|
1286
|
+
const { defaultType } = $mappedFrom.parent.contentMatchAt($mappedFrom.index());
|
|
1287
|
+
tr.setNodeMarkup(nodeRange.start, defaultType);
|
|
1288
|
+
}
|
|
1289
|
+
if (targetLiftDepth || targetLiftDepth === 0) {
|
|
1290
|
+
tr.lift(nodeRange, targetLiftDepth);
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1293
|
+
});
|
|
1294
|
+
return true;
|
|
1295
|
+
};
|
|
1296
|
+
|
|
1297
|
+
const command = fn => props => {
|
|
1298
|
+
return fn(props);
|
|
1299
|
+
};
|
|
1300
|
+
|
|
1301
|
+
const createParagraphNear = () => ({ state, dispatch }) => {
|
|
1302
|
+
return commands$1.createParagraphNear(state, dispatch);
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
const deleteCurrentNode = () => ({ tr, dispatch }) => {
|
|
1306
|
+
const { selection } = tr;
|
|
1307
|
+
const currentNode = selection.$anchor.node();
|
|
1308
|
+
// if there is content inside the current node, break out of this command
|
|
1309
|
+
if (currentNode.content.size > 0) {
|
|
1310
|
+
return false;
|
|
1311
|
+
}
|
|
1312
|
+
const $pos = tr.selection.$anchor;
|
|
1313
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
1314
|
+
const node = $pos.node(depth);
|
|
1315
|
+
if (node.type === currentNode.type) {
|
|
1316
|
+
if (dispatch) {
|
|
1317
|
+
const from = $pos.before(depth);
|
|
1318
|
+
const to = $pos.after(depth);
|
|
1319
|
+
tr.delete(from, to).scrollIntoView();
|
|
1320
|
+
}
|
|
1321
|
+
return true;
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
return false;
|
|
1325
|
+
};
|
|
1326
|
+
|
|
1327
|
+
const deleteNode = typeOrName => ({ tr, state, dispatch }) => {
|
|
1328
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
1329
|
+
const $pos = tr.selection.$anchor;
|
|
1330
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
1331
|
+
const node = $pos.node(depth);
|
|
1332
|
+
if (node.type === type) {
|
|
1333
|
+
if (dispatch) {
|
|
1334
|
+
const from = $pos.before(depth);
|
|
1335
|
+
const to = $pos.after(depth);
|
|
1336
|
+
tr.delete(from, to).scrollIntoView();
|
|
1337
|
+
}
|
|
1338
|
+
return true;
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
return false;
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
const deleteRange = range => ({ tr, dispatch }) => {
|
|
1345
|
+
const { from, to } = range;
|
|
1346
|
+
if (dispatch) {
|
|
1347
|
+
tr.delete(from, to);
|
|
1348
|
+
}
|
|
1349
|
+
return true;
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
const deleteSelection = () => ({ state, dispatch }) => {
|
|
1353
|
+
return commands$1.deleteSelection(state, dispatch);
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
const enter = () => ({ commands }) => {
|
|
1357
|
+
return commands.keyboardShortcut('Enter');
|
|
1358
|
+
};
|
|
1359
|
+
|
|
1360
|
+
const exitCode = () => ({ state, dispatch }) => {
|
|
1361
|
+
return commands$1.exitCode(state, dispatch);
|
|
1362
|
+
};
|
|
1363
|
+
|
|
1364
|
+
/**
|
|
1365
|
+
* Check if object1 includes object2
|
|
1366
|
+
* @param object1 Object
|
|
1367
|
+
* @param object2 Object
|
|
1368
|
+
*/
|
|
1369
|
+
function objectIncludes(object1, object2, options = { strict: true }) {
|
|
1370
|
+
const keys = Object.keys(object2);
|
|
1371
|
+
if (!keys.length) {
|
|
1372
|
+
return true;
|
|
1373
|
+
}
|
|
1374
|
+
return keys.every(key => {
|
|
1375
|
+
if (options.strict) {
|
|
1376
|
+
return object2[key] === object1[key];
|
|
1377
|
+
}
|
|
1378
|
+
if (isRegExp(object2[key])) {
|
|
1379
|
+
return object2[key].test(object1[key]);
|
|
1380
|
+
}
|
|
1381
|
+
return object2[key] === object1[key];
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
function findMarkInSet(marks, type, attributes = {}) {
|
|
1386
|
+
return marks.find(item => {
|
|
1387
|
+
return item.type === type && objectIncludes(item.attrs, attributes);
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
function isMarkInSet(marks, type, attributes = {}) {
|
|
1391
|
+
return !!findMarkInSet(marks, type, attributes);
|
|
1392
|
+
}
|
|
1393
|
+
function getMarkRange($pos, type, attributes = {}) {
|
|
1394
|
+
if (!$pos || !type) {
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
let start = $pos.parent.childAfter($pos.parentOffset);
|
|
1398
|
+
if ($pos.parentOffset === start.offset && start.offset !== 0) {
|
|
1399
|
+
start = $pos.parent.childBefore($pos.parentOffset);
|
|
1400
|
+
}
|
|
1401
|
+
if (!start.node) {
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
const mark = findMarkInSet([...start.node.marks], type, attributes);
|
|
1405
|
+
if (!mark) {
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
let startIndex = start.index;
|
|
1409
|
+
let startPos = $pos.start() + start.offset;
|
|
1410
|
+
let endIndex = startIndex + 1;
|
|
1411
|
+
let endPos = startPos + start.node.nodeSize;
|
|
1412
|
+
findMarkInSet([...start.node.marks], type, attributes);
|
|
1413
|
+
while (startIndex > 0 && mark.isInSet($pos.parent.child(startIndex - 1).marks)) {
|
|
1414
|
+
startIndex -= 1;
|
|
1415
|
+
startPos -= $pos.parent.child(startIndex).nodeSize;
|
|
1416
|
+
}
|
|
1417
|
+
while (endIndex < $pos.parent.childCount
|
|
1418
|
+
&& isMarkInSet([...$pos.parent.child(endIndex).marks], type, attributes)) {
|
|
1419
|
+
endPos += $pos.parent.child(endIndex).nodeSize;
|
|
1420
|
+
endIndex += 1;
|
|
1421
|
+
}
|
|
1422
|
+
return {
|
|
1423
|
+
from: startPos,
|
|
1424
|
+
to: endPos,
|
|
1425
|
+
};
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
function getMarkType(nameOrType, schema) {
|
|
1429
|
+
if (typeof nameOrType === 'string') {
|
|
1430
|
+
if (!schema.marks[nameOrType]) {
|
|
1431
|
+
throw Error(`There is no mark type named '${nameOrType}'. Maybe you forgot to add the extension?`);
|
|
1432
|
+
}
|
|
1433
|
+
return schema.marks[nameOrType];
|
|
1434
|
+
}
|
|
1435
|
+
return nameOrType;
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
const extendMarkRange = (typeOrName, attributes = {}) => ({ tr, state: state$1, dispatch }) => {
|
|
1439
|
+
const type = getMarkType(typeOrName, state$1.schema);
|
|
1440
|
+
const { doc, selection } = tr;
|
|
1441
|
+
const { $from, from, to } = selection;
|
|
1442
|
+
if (dispatch) {
|
|
1443
|
+
const range = getMarkRange($from, type, attributes);
|
|
1444
|
+
if (range && range.from <= from && range.to >= to) {
|
|
1445
|
+
const newSelection = state.TextSelection.create(doc, range.from, range.to);
|
|
1446
|
+
tr.setSelection(newSelection);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
return true;
|
|
1450
|
+
};
|
|
1451
|
+
|
|
1452
|
+
const first = commands => props => {
|
|
1453
|
+
const items = typeof commands === 'function'
|
|
1454
|
+
? commands(props)
|
|
1455
|
+
: commands;
|
|
1456
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
1457
|
+
if (items[i](props)) {
|
|
1458
|
+
return true;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
return false;
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
function isTextSelection(value) {
|
|
1465
|
+
return value instanceof state.TextSelection;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
function minMax(value = 0, min = 0, max = 0) {
|
|
1469
|
+
return Math.min(Math.max(value, min), max);
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
function resolveFocusPosition(doc, position = null) {
|
|
1473
|
+
if (!position) {
|
|
1474
|
+
return null;
|
|
1475
|
+
}
|
|
1476
|
+
const selectionAtStart = state.Selection.atStart(doc);
|
|
1477
|
+
const selectionAtEnd = state.Selection.atEnd(doc);
|
|
1478
|
+
if (position === 'start' || position === true) {
|
|
1479
|
+
return selectionAtStart;
|
|
1480
|
+
}
|
|
1481
|
+
if (position === 'end') {
|
|
1482
|
+
return selectionAtEnd;
|
|
1483
|
+
}
|
|
1484
|
+
const minPos = selectionAtStart.from;
|
|
1485
|
+
const maxPos = selectionAtEnd.to;
|
|
1486
|
+
if (position === 'all') {
|
|
1487
|
+
return state.TextSelection.create(doc, minMax(0, minPos, maxPos), minMax(doc.content.size, minPos, maxPos));
|
|
1488
|
+
}
|
|
1489
|
+
return state.TextSelection.create(doc, minMax(position, minPos, maxPos), minMax(position, minPos, maxPos));
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
function isiOS() {
|
|
1493
|
+
return [
|
|
1494
|
+
'iPad Simulator',
|
|
1495
|
+
'iPhone Simulator',
|
|
1496
|
+
'iPod Simulator',
|
|
1497
|
+
'iPad',
|
|
1498
|
+
'iPhone',
|
|
1499
|
+
'iPod',
|
|
1500
|
+
].includes(navigator.platform)
|
|
1501
|
+
// iPad on iOS 13 detection
|
|
1502
|
+
|| (navigator.userAgent.includes('Mac') && 'ontouchend' in document);
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
const focus = (position = null, options = {}) => ({ editor, view, tr, dispatch, }) => {
|
|
1506
|
+
options = {
|
|
1507
|
+
scrollIntoView: true,
|
|
1508
|
+
...options,
|
|
1509
|
+
};
|
|
1510
|
+
const delayedFocus = () => {
|
|
1511
|
+
// focus within `requestAnimationFrame` breaks focus on iOS
|
|
1512
|
+
// so we have to call this
|
|
1513
|
+
if (isiOS()) {
|
|
1514
|
+
view.dom.focus();
|
|
1515
|
+
}
|
|
1516
|
+
// For React we have to focus asynchronously. Otherwise wild things happen.
|
|
1517
|
+
// see: https://github.com/ueberdosis/tiptap/issues/1520
|
|
1518
|
+
requestAnimationFrame(() => {
|
|
1519
|
+
if (!editor.isDestroyed) {
|
|
1520
|
+
view.focus();
|
|
1521
|
+
if (options === null || options === void 0 ? void 0 : options.scrollIntoView) {
|
|
1522
|
+
editor.commands.scrollIntoView();
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
});
|
|
1526
|
+
};
|
|
1527
|
+
if ((view.hasFocus() && position === null) || position === false) {
|
|
1528
|
+
return true;
|
|
1529
|
+
}
|
|
1530
|
+
// we don’t try to resolve a NodeSelection or CellSelection
|
|
1531
|
+
if (dispatch && position === null && !isTextSelection(editor.state.selection)) {
|
|
1532
|
+
delayedFocus();
|
|
1533
|
+
return true;
|
|
1534
|
+
}
|
|
1535
|
+
// pass through tr.doc instead of editor.state.doc
|
|
1536
|
+
// since transactions could change the editors state before this command has been run
|
|
1537
|
+
const selection = resolveFocusPosition(tr.doc, position) || editor.state.selection;
|
|
1538
|
+
const isSameSelection = editor.state.selection.eq(selection);
|
|
1539
|
+
if (dispatch) {
|
|
1540
|
+
if (!isSameSelection) {
|
|
1541
|
+
tr.setSelection(selection);
|
|
1542
|
+
}
|
|
1543
|
+
// `tr.setSelection` resets the stored marks
|
|
1544
|
+
// so we’ll restore them if the selection is the same as before
|
|
1545
|
+
if (isSameSelection && tr.storedMarks) {
|
|
1546
|
+
tr.setStoredMarks(tr.storedMarks);
|
|
1547
|
+
}
|
|
1548
|
+
delayedFocus();
|
|
1549
|
+
}
|
|
1550
|
+
return true;
|
|
1551
|
+
};
|
|
1552
|
+
|
|
1553
|
+
const forEach = (items, fn) => props => {
|
|
1554
|
+
return items.every((item, index) => fn(item, { ...props, index }));
|
|
1555
|
+
};
|
|
1556
|
+
|
|
1557
|
+
const insertContent = (value, options) => ({ tr, commands }) => {
|
|
1558
|
+
return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
|
|
1559
|
+
};
|
|
1560
|
+
|
|
1561
|
+
function elementFromString(value) {
|
|
1562
|
+
// add a wrapper to preserve leading and trailing whitespace
|
|
1563
|
+
const wrappedValue = `<body>${value}</body>`;
|
|
1564
|
+
return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
function createNodeFromContent(content, schema, options) {
|
|
1568
|
+
options = {
|
|
1569
|
+
slice: true,
|
|
1570
|
+
parseOptions: {},
|
|
1571
|
+
...options,
|
|
1572
|
+
};
|
|
1573
|
+
if (typeof content === 'object' && content !== null) {
|
|
1574
|
+
try {
|
|
1575
|
+
if (Array.isArray(content) && content.length > 0) {
|
|
1576
|
+
return model.Fragment.fromArray(content.map(item => schema.nodeFromJSON(item)));
|
|
1577
|
+
}
|
|
1578
|
+
return schema.nodeFromJSON(content);
|
|
1579
|
+
}
|
|
1580
|
+
catch (error) {
|
|
1581
|
+
console.warn('[tiptap warn]: Invalid content.', 'Passed value:', content, 'Error:', error);
|
|
1582
|
+
return createNodeFromContent('', schema, options);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
if (typeof content === 'string') {
|
|
1586
|
+
const parser = model.DOMParser.fromSchema(schema);
|
|
1587
|
+
return options.slice
|
|
1588
|
+
? parser.parseSlice(elementFromString(content), options.parseOptions).content
|
|
1589
|
+
: parser.parse(elementFromString(content), options.parseOptions);
|
|
1590
|
+
}
|
|
1591
|
+
return createNodeFromContent('', schema, options);
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
// source: https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
|
|
1595
|
+
function selectionToInsertionEnd(tr, startLen, bias) {
|
|
1596
|
+
const last = tr.steps.length - 1;
|
|
1597
|
+
if (last < startLen) {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
const step = tr.steps[last];
|
|
1601
|
+
if (!(step instanceof transform.ReplaceStep || step instanceof transform.ReplaceAroundStep)) {
|
|
1602
|
+
return;
|
|
1603
|
+
}
|
|
1604
|
+
const map = tr.mapping.maps[last];
|
|
1605
|
+
let end = 0;
|
|
1606
|
+
map.forEach((_from, _to, _newFrom, newTo) => {
|
|
1607
|
+
if (end === 0) {
|
|
1608
|
+
end = newTo;
|
|
1609
|
+
}
|
|
1610
|
+
});
|
|
1611
|
+
tr.setSelection(state.Selection.near(tr.doc.resolve(end), bias));
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
const isFragment = (nodeOrFragment) => {
|
|
1615
|
+
return nodeOrFragment.toString().startsWith('<');
|
|
1616
|
+
};
|
|
1617
|
+
const insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) => {
|
|
1618
|
+
if (dispatch) {
|
|
1619
|
+
options = {
|
|
1620
|
+
parseOptions: {},
|
|
1621
|
+
updateSelection: true,
|
|
1622
|
+
...options,
|
|
1623
|
+
};
|
|
1624
|
+
const content = createNodeFromContent(value, editor.schema, {
|
|
1625
|
+
parseOptions: {
|
|
1626
|
+
preserveWhitespace: 'full',
|
|
1627
|
+
...options.parseOptions,
|
|
1628
|
+
},
|
|
1629
|
+
});
|
|
1630
|
+
// don’t dispatch an empty fragment because this can lead to strange errors
|
|
1631
|
+
if (content.toString() === '<>') {
|
|
1632
|
+
return true;
|
|
1633
|
+
}
|
|
1634
|
+
let { from, to } = typeof position === 'number' ? { from: position, to: position } : position;
|
|
1635
|
+
let isOnlyTextContent = true;
|
|
1636
|
+
let isOnlyBlockContent = true;
|
|
1637
|
+
const nodes = isFragment(content) ? content : [content];
|
|
1638
|
+
nodes.forEach(node => {
|
|
1639
|
+
// check if added node is valid
|
|
1640
|
+
node.check();
|
|
1641
|
+
isOnlyTextContent = isOnlyTextContent ? node.isText && node.marks.length === 0 : false;
|
|
1642
|
+
isOnlyBlockContent = isOnlyBlockContent ? node.isBlock : false;
|
|
1643
|
+
});
|
|
1644
|
+
// check if we can replace the wrapping node by
|
|
1645
|
+
// the newly inserted content
|
|
1646
|
+
// example:
|
|
1647
|
+
// replace an empty paragraph by an inserted image
|
|
1648
|
+
// instead of inserting the image below the paragraph
|
|
1649
|
+
if (from === to && isOnlyBlockContent) {
|
|
1650
|
+
const { parent } = tr.doc.resolve(from);
|
|
1651
|
+
const isEmptyTextBlock = parent.isTextblock && !parent.type.spec.code && !parent.childCount;
|
|
1652
|
+
if (isEmptyTextBlock) {
|
|
1653
|
+
from -= 1;
|
|
1654
|
+
to += 1;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
// if there is only plain text we have to use `insertText`
|
|
1658
|
+
// because this will keep the current marks
|
|
1659
|
+
if (isOnlyTextContent) {
|
|
1660
|
+
// if value is string, we can use it directly
|
|
1661
|
+
// otherwise if it is an array, we have to join it
|
|
1662
|
+
if (Array.isArray(value)) {
|
|
1663
|
+
tr.insertText(value.map(v => v.text || '').join(''), from, to);
|
|
1664
|
+
}
|
|
1665
|
+
else if (typeof value === 'object' && !!value && !!value.text) {
|
|
1666
|
+
tr.insertText(value.text, from, to);
|
|
1667
|
+
}
|
|
1668
|
+
else {
|
|
1669
|
+
tr.insertText(value, from, to);
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
else {
|
|
1673
|
+
tr.replaceWith(from, to, content);
|
|
1674
|
+
}
|
|
1675
|
+
// set cursor at end of inserted content
|
|
1676
|
+
if (options.updateSelection) {
|
|
1677
|
+
selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
return true;
|
|
1681
|
+
};
|
|
1682
|
+
|
|
1683
|
+
const joinUp = () => ({ state, dispatch }) => {
|
|
1684
|
+
return commands$1.joinUp(state, dispatch);
|
|
1685
|
+
};
|
|
1686
|
+
const joinDown = () => ({ state, dispatch }) => {
|
|
1687
|
+
return commands$1.joinDown(state, dispatch);
|
|
1688
|
+
};
|
|
1689
|
+
const joinBackward = () => ({ state, dispatch }) => {
|
|
1690
|
+
return commands$1.joinBackward(state, dispatch);
|
|
1691
|
+
};
|
|
1692
|
+
const joinForward = () => ({ state, dispatch }) => {
|
|
1693
|
+
return commands$1.joinForward(state, dispatch);
|
|
1694
|
+
};
|
|
1695
|
+
|
|
1696
|
+
function isMacOS() {
|
|
1697
|
+
return typeof navigator !== 'undefined'
|
|
1698
|
+
? /Mac/.test(navigator.platform)
|
|
1699
|
+
: false;
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
function normalizeKeyName(name) {
|
|
1703
|
+
const parts = name.split(/-(?!$)/);
|
|
1704
|
+
let result = parts[parts.length - 1];
|
|
1705
|
+
if (result === 'Space') {
|
|
1706
|
+
result = ' ';
|
|
1707
|
+
}
|
|
1708
|
+
let alt;
|
|
1709
|
+
let ctrl;
|
|
1710
|
+
let shift;
|
|
1711
|
+
let meta;
|
|
1712
|
+
for (let i = 0; i < parts.length - 1; i += 1) {
|
|
1713
|
+
const mod = parts[i];
|
|
1714
|
+
if (/^(cmd|meta|m)$/i.test(mod)) {
|
|
1715
|
+
meta = true;
|
|
1716
|
+
}
|
|
1717
|
+
else if (/^a(lt)?$/i.test(mod)) {
|
|
1718
|
+
alt = true;
|
|
1719
|
+
}
|
|
1720
|
+
else if (/^(c|ctrl|control)$/i.test(mod)) {
|
|
1721
|
+
ctrl = true;
|
|
1722
|
+
}
|
|
1723
|
+
else if (/^s(hift)?$/i.test(mod)) {
|
|
1724
|
+
shift = true;
|
|
1725
|
+
}
|
|
1726
|
+
else if (/^mod$/i.test(mod)) {
|
|
1727
|
+
if (isiOS() || isMacOS()) {
|
|
1728
|
+
meta = true;
|
|
1729
|
+
}
|
|
1730
|
+
else {
|
|
1731
|
+
ctrl = true;
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
else {
|
|
1735
|
+
throw new Error(`Unrecognized modifier name: ${mod}`);
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
if (alt) {
|
|
1739
|
+
result = `Alt-${result}`;
|
|
1740
|
+
}
|
|
1741
|
+
if (ctrl) {
|
|
1742
|
+
result = `Ctrl-${result}`;
|
|
1743
|
+
}
|
|
1744
|
+
if (meta) {
|
|
1745
|
+
result = `Meta-${result}`;
|
|
1746
|
+
}
|
|
1747
|
+
if (shift) {
|
|
1748
|
+
result = `Shift-${result}`;
|
|
1749
|
+
}
|
|
1750
|
+
return result;
|
|
1751
|
+
}
|
|
1752
|
+
const keyboardShortcut = name => ({ editor, view, tr, dispatch, }) => {
|
|
1753
|
+
const keys = normalizeKeyName(name).split(/-(?!$)/);
|
|
1754
|
+
const key = keys.find(item => !['Alt', 'Ctrl', 'Meta', 'Shift'].includes(item));
|
|
1755
|
+
const event = new KeyboardEvent('keydown', {
|
|
1756
|
+
key: key === 'Space'
|
|
1757
|
+
? ' '
|
|
1758
|
+
: key,
|
|
1759
|
+
altKey: keys.includes('Alt'),
|
|
1760
|
+
ctrlKey: keys.includes('Ctrl'),
|
|
1761
|
+
metaKey: keys.includes('Meta'),
|
|
1762
|
+
shiftKey: keys.includes('Shift'),
|
|
1763
|
+
bubbles: true,
|
|
1764
|
+
cancelable: true,
|
|
1765
|
+
});
|
|
1766
|
+
const capturedTransaction = editor.captureTransaction(() => {
|
|
1767
|
+
view.someProp('handleKeyDown', f => f(view, event));
|
|
1768
|
+
});
|
|
1769
|
+
capturedTransaction === null || capturedTransaction === void 0 ? void 0 : capturedTransaction.steps.forEach(step => {
|
|
1770
|
+
const newStep = step.map(tr.mapping);
|
|
1771
|
+
if (newStep && dispatch) {
|
|
1772
|
+
tr.maybeStep(newStep);
|
|
1773
|
+
}
|
|
1774
|
+
});
|
|
1775
|
+
return true;
|
|
1776
|
+
};
|
|
1777
|
+
|
|
1778
|
+
function isNodeActive(state, typeOrName, attributes = {}) {
|
|
1779
|
+
const { from, to, empty } = state.selection;
|
|
1780
|
+
const type = typeOrName ? getNodeType(typeOrName, state.schema) : null;
|
|
1781
|
+
const nodeRanges = [];
|
|
1782
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
1783
|
+
if (node.isText) {
|
|
1784
|
+
return;
|
|
1785
|
+
}
|
|
1786
|
+
const relativeFrom = Math.max(from, pos);
|
|
1787
|
+
const relativeTo = Math.min(to, pos + node.nodeSize);
|
|
1788
|
+
nodeRanges.push({
|
|
1789
|
+
node,
|
|
1790
|
+
from: relativeFrom,
|
|
1791
|
+
to: relativeTo,
|
|
1792
|
+
});
|
|
1793
|
+
});
|
|
1794
|
+
const selectionRange = to - from;
|
|
1795
|
+
const matchedNodeRanges = nodeRanges
|
|
1796
|
+
.filter(nodeRange => {
|
|
1797
|
+
if (!type) {
|
|
1798
|
+
return true;
|
|
1799
|
+
}
|
|
1800
|
+
return type.name === nodeRange.node.type.name;
|
|
1801
|
+
})
|
|
1802
|
+
.filter(nodeRange => objectIncludes(nodeRange.node.attrs, attributes, { strict: false }));
|
|
1803
|
+
if (empty) {
|
|
1804
|
+
return !!matchedNodeRanges.length;
|
|
1805
|
+
}
|
|
1806
|
+
const range = matchedNodeRanges.reduce((sum, nodeRange) => sum + nodeRange.to - nodeRange.from, 0);
|
|
1807
|
+
return range >= selectionRange;
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
const lift = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
|
|
1811
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
1812
|
+
const isActive = isNodeActive(state, type, attributes);
|
|
1813
|
+
if (!isActive) {
|
|
1814
|
+
return false;
|
|
1815
|
+
}
|
|
1816
|
+
return commands$1.lift(state, dispatch);
|
|
1817
|
+
};
|
|
1818
|
+
|
|
1819
|
+
const liftEmptyBlock = () => ({ state, dispatch }) => {
|
|
1820
|
+
return commands$1.liftEmptyBlock(state, dispatch);
|
|
1821
|
+
};
|
|
1822
|
+
|
|
1823
|
+
const liftListItem = typeOrName => ({ state, dispatch }) => {
|
|
1824
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
1825
|
+
return schemaList.liftListItem(type)(state, dispatch);
|
|
1826
|
+
};
|
|
1827
|
+
|
|
1828
|
+
const newlineInCode = () => ({ state, dispatch }) => {
|
|
1829
|
+
return commands$1.newlineInCode(state, dispatch);
|
|
1830
|
+
};
|
|
1831
|
+
|
|
1832
|
+
function getSchemaTypeNameByName(name, schema) {
|
|
1833
|
+
if (schema.nodes[name]) {
|
|
1834
|
+
return 'node';
|
|
1835
|
+
}
|
|
1836
|
+
if (schema.marks[name]) {
|
|
1837
|
+
return 'mark';
|
|
1838
|
+
}
|
|
1839
|
+
return null;
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
/**
|
|
1843
|
+
* Remove a property or an array of properties from an object
|
|
1844
|
+
* @param obj Object
|
|
1845
|
+
* @param key Key to remove
|
|
1846
|
+
*/
|
|
1847
|
+
function deleteProps(obj, propOrProps) {
|
|
1848
|
+
const props = typeof propOrProps === 'string'
|
|
1849
|
+
? [propOrProps]
|
|
1850
|
+
: propOrProps;
|
|
1851
|
+
return Object
|
|
1852
|
+
.keys(obj)
|
|
1853
|
+
.reduce((newObj, prop) => {
|
|
1854
|
+
if (!props.includes(prop)) {
|
|
1855
|
+
newObj[prop] = obj[prop];
|
|
1856
|
+
}
|
|
1857
|
+
return newObj;
|
|
1858
|
+
}, {});
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
const resetAttributes = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
|
|
1862
|
+
let nodeType = null;
|
|
1863
|
+
let markType = null;
|
|
1864
|
+
const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string' ? typeOrName : typeOrName.name, state.schema);
|
|
1865
|
+
if (!schemaType) {
|
|
1866
|
+
return false;
|
|
1867
|
+
}
|
|
1868
|
+
if (schemaType === 'node') {
|
|
1869
|
+
nodeType = getNodeType(typeOrName, state.schema);
|
|
1870
|
+
}
|
|
1871
|
+
if (schemaType === 'mark') {
|
|
1872
|
+
markType = getMarkType(typeOrName, state.schema);
|
|
1873
|
+
}
|
|
1874
|
+
if (dispatch) {
|
|
1875
|
+
tr.selection.ranges.forEach(range => {
|
|
1876
|
+
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
|
|
1877
|
+
if (nodeType && nodeType === node.type) {
|
|
1878
|
+
tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes));
|
|
1879
|
+
}
|
|
1880
|
+
if (markType && node.marks.length) {
|
|
1881
|
+
node.marks.forEach(mark => {
|
|
1882
|
+
if (markType === mark.type) {
|
|
1883
|
+
tr.addMark(pos, pos + node.nodeSize, markType.create(deleteProps(mark.attrs, attributes)));
|
|
1884
|
+
}
|
|
1885
|
+
});
|
|
1886
|
+
}
|
|
1887
|
+
});
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1890
|
+
return true;
|
|
1891
|
+
};
|
|
1892
|
+
|
|
1893
|
+
const scrollIntoView = () => ({ tr, dispatch }) => {
|
|
1894
|
+
if (dispatch) {
|
|
1895
|
+
tr.scrollIntoView();
|
|
1896
|
+
}
|
|
1897
|
+
return true;
|
|
1898
|
+
};
|
|
1899
|
+
|
|
1900
|
+
const selectAll = () => ({ tr, commands }) => {
|
|
1901
|
+
return commands.setTextSelection({
|
|
1902
|
+
from: 0,
|
|
1903
|
+
to: tr.doc.content.size,
|
|
1904
|
+
});
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1907
|
+
const selectNodeBackward = () => ({ state, dispatch }) => {
|
|
1908
|
+
return commands$1.selectNodeBackward(state, dispatch);
|
|
1909
|
+
};
|
|
1910
|
+
|
|
1911
|
+
const selectNodeForward = () => ({ state, dispatch }) => {
|
|
1912
|
+
return commands$1.selectNodeForward(state, dispatch);
|
|
1913
|
+
};
|
|
1914
|
+
|
|
1915
|
+
const selectParentNode = () => ({ state, dispatch }) => {
|
|
1916
|
+
return commands$1.selectParentNode(state, dispatch);
|
|
1917
|
+
};
|
|
1918
|
+
|
|
1919
|
+
// @ts-ignore
|
|
1920
|
+
const selectTextblockEnd = () => ({ state, dispatch }) => {
|
|
1921
|
+
return commands$1.selectTextblockEnd(state, dispatch);
|
|
1922
|
+
};
|
|
1923
|
+
|
|
1924
|
+
// @ts-ignore
|
|
1925
|
+
const selectTextblockStart = () => ({ state, dispatch }) => {
|
|
1926
|
+
return commands$1.selectTextblockStart(state, dispatch);
|
|
1927
|
+
};
|
|
1928
|
+
|
|
1929
|
+
function createDocument(content, schema, parseOptions = {}) {
|
|
1930
|
+
return createNodeFromContent(content, schema, { slice: false, parseOptions });
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
const setContent = (content, emitUpdate = false, parseOptions = {}) => ({ tr, editor, dispatch }) => {
|
|
1934
|
+
const { doc } = tr;
|
|
1935
|
+
const document = createDocument(content, editor.schema, parseOptions);
|
|
1936
|
+
if (dispatch) {
|
|
1937
|
+
tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', !emitUpdate);
|
|
1938
|
+
}
|
|
1939
|
+
return true;
|
|
1940
|
+
};
|
|
1941
|
+
|
|
1942
|
+
/**
|
|
1943
|
+
* Returns a new `Transform` based on all steps of the passed transactions.
|
|
1944
|
+
*/
|
|
1945
|
+
function combineTransactionSteps(oldDoc, transactions) {
|
|
1946
|
+
const transform$1 = new transform.Transform(oldDoc);
|
|
1947
|
+
transactions.forEach(transaction => {
|
|
1948
|
+
transaction.steps.forEach(step => {
|
|
1949
|
+
transform$1.step(step);
|
|
1950
|
+
});
|
|
1951
|
+
});
|
|
1952
|
+
return transform$1;
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
function defaultBlockAt(match) {
|
|
1956
|
+
for (let i = 0; i < match.edgeCount; i += 1) {
|
|
1957
|
+
const { type } = match.edge(i);
|
|
1958
|
+
if (type.isTextblock && !type.hasRequiredAttrs()) {
|
|
1959
|
+
return type;
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
return null;
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
function findChildren(node, predicate) {
|
|
1966
|
+
const nodesWithPos = [];
|
|
1967
|
+
node.descendants((child, pos) => {
|
|
1968
|
+
if (predicate(child)) {
|
|
1969
|
+
nodesWithPos.push({
|
|
1970
|
+
node: child,
|
|
1971
|
+
pos,
|
|
1972
|
+
});
|
|
1973
|
+
}
|
|
1974
|
+
});
|
|
1975
|
+
return nodesWithPos;
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
/**
|
|
1979
|
+
* Same as `findChildren` but searches only within a `range`.
|
|
1980
|
+
*/
|
|
1981
|
+
function findChildrenInRange(node, range, predicate) {
|
|
1982
|
+
const nodesWithPos = [];
|
|
1983
|
+
// if (range.from === range.to) {
|
|
1984
|
+
// const nodeAt = node.nodeAt(range.from)
|
|
1985
|
+
// if (nodeAt) {
|
|
1986
|
+
// nodesWithPos.push({
|
|
1987
|
+
// node: nodeAt,
|
|
1988
|
+
// pos: range.from,
|
|
1989
|
+
// })
|
|
1990
|
+
// }
|
|
1991
|
+
// }
|
|
1992
|
+
node.nodesBetween(range.from, range.to, (child, pos) => {
|
|
1993
|
+
if (predicate(child)) {
|
|
1994
|
+
nodesWithPos.push({
|
|
1995
|
+
node: child,
|
|
1996
|
+
pos,
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
});
|
|
2000
|
+
return nodesWithPos;
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
function findParentNodeClosestToPos($pos, predicate) {
|
|
2004
|
+
for (let i = $pos.depth; i > 0; i -= 1) {
|
|
2005
|
+
const node = $pos.node(i);
|
|
2006
|
+
if (predicate(node)) {
|
|
2007
|
+
return {
|
|
2008
|
+
pos: i > 0 ? $pos.before(i) : 0,
|
|
2009
|
+
start: $pos.start(i),
|
|
2010
|
+
depth: i,
|
|
2011
|
+
node,
|
|
2012
|
+
};
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
function findParentNode(predicate) {
|
|
2018
|
+
return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
function getHTMLFromFragment(fragment, schema) {
|
|
2022
|
+
const documentFragment = model.DOMSerializer.fromSchema(schema).serializeFragment(fragment);
|
|
2023
|
+
const temporaryDocument = document.implementation.createHTMLDocument();
|
|
2024
|
+
const container = temporaryDocument.createElement('div');
|
|
2025
|
+
container.appendChild(documentFragment);
|
|
2026
|
+
return container.innerHTML;
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
function getSchema(extensions) {
|
|
2030
|
+
const resolvedExtensions = ExtensionManager.resolve(extensions);
|
|
2031
|
+
return getSchemaByResolvedExtensions(resolvedExtensions);
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
function generateHTML(doc, extensions) {
|
|
2035
|
+
const schema = getSchema(extensions);
|
|
2036
|
+
const contentNode = model.Node.fromJSON(schema, doc);
|
|
2037
|
+
return getHTMLFromFragment(contentNode.content, schema);
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
function generateJSON(html, extensions) {
|
|
2041
|
+
const schema = getSchema(extensions);
|
|
2042
|
+
const dom = elementFromString(html);
|
|
2043
|
+
return model.DOMParser.fromSchema(schema).parse(dom).toJSON();
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
function getText(node, options) {
|
|
2047
|
+
const range = {
|
|
2048
|
+
from: 0,
|
|
2049
|
+
to: node.content.size,
|
|
2050
|
+
};
|
|
2051
|
+
return getTextBetween(node, range, options);
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
function generateText(doc, extensions, options) {
|
|
2055
|
+
const { blockSeparator = '\n\n', textSerializers = {} } = options || {};
|
|
2056
|
+
const schema = getSchema(extensions);
|
|
2057
|
+
const contentNode = model.Node.fromJSON(schema, doc);
|
|
2058
|
+
return getText(contentNode, {
|
|
2059
|
+
blockSeparator,
|
|
2060
|
+
textSerializers: {
|
|
2061
|
+
...getTextSerializersFromSchema(schema),
|
|
2062
|
+
...textSerializers,
|
|
2063
|
+
},
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
function getMarkAttributes(state, typeOrName) {
|
|
2068
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
2069
|
+
const { from, to, empty } = state.selection;
|
|
2070
|
+
const marks = [];
|
|
2071
|
+
if (empty) {
|
|
2072
|
+
if (state.storedMarks) {
|
|
2073
|
+
marks.push(...state.storedMarks);
|
|
2074
|
+
}
|
|
2075
|
+
marks.push(...state.selection.$head.marks());
|
|
2076
|
+
}
|
|
2077
|
+
else {
|
|
2078
|
+
state.doc.nodesBetween(from, to, node => {
|
|
2079
|
+
marks.push(...node.marks);
|
|
2080
|
+
});
|
|
2081
|
+
}
|
|
2082
|
+
const mark = marks.find(markItem => markItem.type.name === type.name);
|
|
2083
|
+
if (!mark) {
|
|
2084
|
+
return {};
|
|
2085
|
+
}
|
|
2086
|
+
return { ...mark.attrs };
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
function getNodeAttributes(state, typeOrName) {
|
|
2090
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2091
|
+
const { from, to } = state.selection;
|
|
2092
|
+
const nodes = [];
|
|
2093
|
+
state.doc.nodesBetween(from, to, node => {
|
|
2094
|
+
nodes.push(node);
|
|
2095
|
+
});
|
|
2096
|
+
const node = nodes.reverse().find(nodeItem => nodeItem.type.name === type.name);
|
|
2097
|
+
if (!node) {
|
|
2098
|
+
return {};
|
|
2099
|
+
}
|
|
2100
|
+
return { ...node.attrs };
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
function getAttributes(state, typeOrName) {
|
|
2104
|
+
const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string' ? typeOrName : typeOrName.name, state.schema);
|
|
2105
|
+
if (schemaType === 'node') {
|
|
2106
|
+
return getNodeAttributes(state, typeOrName);
|
|
2107
|
+
}
|
|
2108
|
+
if (schemaType === 'mark') {
|
|
2109
|
+
return getMarkAttributes(state, typeOrName);
|
|
2110
|
+
}
|
|
2111
|
+
return {};
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
/**
|
|
2115
|
+
* Removes duplicated values within an array.
|
|
2116
|
+
* Supports numbers, strings and objects.
|
|
2117
|
+
*/
|
|
2118
|
+
function removeDuplicates(array, by = JSON.stringify) {
|
|
2119
|
+
const seen = {};
|
|
2120
|
+
return array.filter(item => {
|
|
2121
|
+
const key = by(item);
|
|
2122
|
+
return Object.prototype.hasOwnProperty.call(seen, key)
|
|
2123
|
+
? false
|
|
2124
|
+
: (seen[key] = true);
|
|
2125
|
+
});
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
/**
|
|
2129
|
+
* Removes duplicated ranges and ranges that are
|
|
2130
|
+
* fully captured by other ranges.
|
|
2131
|
+
*/
|
|
2132
|
+
function simplifyChangedRanges(changes) {
|
|
2133
|
+
const uniqueChanges = removeDuplicates(changes);
|
|
2134
|
+
return uniqueChanges.length === 1
|
|
2135
|
+
? uniqueChanges
|
|
2136
|
+
: uniqueChanges.filter((change, index) => {
|
|
2137
|
+
const rest = uniqueChanges.filter((_, i) => i !== index);
|
|
2138
|
+
return !rest.some(otherChange => {
|
|
2139
|
+
return change.oldRange.from >= otherChange.oldRange.from
|
|
2140
|
+
&& change.oldRange.to <= otherChange.oldRange.to
|
|
2141
|
+
&& change.newRange.from >= otherChange.newRange.from
|
|
2142
|
+
&& change.newRange.to <= otherChange.newRange.to;
|
|
2143
|
+
});
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
/**
|
|
2147
|
+
* Returns a list of changed ranges
|
|
2148
|
+
* based on the first and last state of all steps.
|
|
2149
|
+
*/
|
|
2150
|
+
function getChangedRanges(transform) {
|
|
2151
|
+
const { mapping, steps } = transform;
|
|
2152
|
+
const changes = [];
|
|
2153
|
+
mapping.maps.forEach((stepMap, index) => {
|
|
2154
|
+
const ranges = [];
|
|
2155
|
+
// This accounts for step changes where no range was actually altered
|
|
2156
|
+
// e.g. when setting a mark, node attribute, etc.
|
|
2157
|
+
// @ts-ignore
|
|
2158
|
+
if (!stepMap.ranges.length) {
|
|
2159
|
+
const { from, to } = steps[index];
|
|
2160
|
+
if (from === undefined || to === undefined) {
|
|
2161
|
+
return;
|
|
2162
|
+
}
|
|
2163
|
+
ranges.push({ from, to });
|
|
2164
|
+
}
|
|
2165
|
+
else {
|
|
2166
|
+
stepMap.forEach((from, to) => {
|
|
2167
|
+
ranges.push({ from, to });
|
|
2168
|
+
});
|
|
2169
|
+
}
|
|
2170
|
+
ranges.forEach(({ from, to }) => {
|
|
2171
|
+
const newStart = mapping.slice(index).map(from, -1);
|
|
2172
|
+
const newEnd = mapping.slice(index).map(to);
|
|
2173
|
+
const oldStart = mapping.invert().map(newStart, -1);
|
|
2174
|
+
const oldEnd = mapping.invert().map(newEnd);
|
|
2175
|
+
changes.push({
|
|
2176
|
+
oldRange: {
|
|
2177
|
+
from: oldStart,
|
|
2178
|
+
to: oldEnd,
|
|
2179
|
+
},
|
|
2180
|
+
newRange: {
|
|
2181
|
+
from: newStart,
|
|
2182
|
+
to: newEnd,
|
|
2183
|
+
},
|
|
2184
|
+
});
|
|
2185
|
+
});
|
|
2186
|
+
});
|
|
2187
|
+
return simplifyChangedRanges(changes);
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
function getDebugJSON(node, startOffset = 0) {
|
|
2191
|
+
const isTopNode = node.type === node.type.schema.topNodeType;
|
|
2192
|
+
const increment = isTopNode ? 0 : 1;
|
|
2193
|
+
const from = startOffset;
|
|
2194
|
+
const to = from + node.nodeSize;
|
|
2195
|
+
const marks = node.marks.map(mark => {
|
|
2196
|
+
const output = {
|
|
2197
|
+
type: mark.type.name,
|
|
2198
|
+
};
|
|
2199
|
+
if (Object.keys(mark.attrs).length) {
|
|
2200
|
+
output.attrs = { ...mark.attrs };
|
|
2201
|
+
}
|
|
2202
|
+
return output;
|
|
2203
|
+
});
|
|
2204
|
+
const attrs = { ...node.attrs };
|
|
2205
|
+
const output = {
|
|
2206
|
+
type: node.type.name,
|
|
2207
|
+
from,
|
|
2208
|
+
to,
|
|
2209
|
+
};
|
|
2210
|
+
if (Object.keys(attrs).length) {
|
|
2211
|
+
output.attrs = attrs;
|
|
2212
|
+
}
|
|
2213
|
+
if (marks.length) {
|
|
2214
|
+
output.marks = marks;
|
|
2215
|
+
}
|
|
2216
|
+
if (node.content.childCount) {
|
|
2217
|
+
output.content = [];
|
|
2218
|
+
node.forEach((child, offset) => {
|
|
2219
|
+
var _a;
|
|
2220
|
+
(_a = output.content) === null || _a === void 0 ? void 0 : _a.push(getDebugJSON(child, startOffset + offset + increment));
|
|
2221
|
+
});
|
|
2222
|
+
}
|
|
2223
|
+
if (node.text) {
|
|
2224
|
+
output.text = node.text;
|
|
2225
|
+
}
|
|
2226
|
+
return output;
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
function getMarksBetween(from, to, doc) {
|
|
2230
|
+
const marks = [];
|
|
2231
|
+
// get all inclusive marks on empty selection
|
|
2232
|
+
if (from === to) {
|
|
2233
|
+
doc
|
|
2234
|
+
.resolve(from)
|
|
2235
|
+
.marks()
|
|
2236
|
+
.forEach(mark => {
|
|
2237
|
+
const $pos = doc.resolve(from - 1);
|
|
2238
|
+
const range = getMarkRange($pos, mark.type);
|
|
2239
|
+
if (!range) {
|
|
2240
|
+
return;
|
|
2241
|
+
}
|
|
2242
|
+
marks.push({
|
|
2243
|
+
mark,
|
|
2244
|
+
...range,
|
|
2245
|
+
});
|
|
2246
|
+
});
|
|
2247
|
+
}
|
|
2248
|
+
else {
|
|
2249
|
+
doc.nodesBetween(from, to, (node, pos) => {
|
|
2250
|
+
marks.push(...node.marks.map(mark => ({
|
|
2251
|
+
from: pos,
|
|
2252
|
+
to: pos + node.nodeSize,
|
|
2253
|
+
mark,
|
|
2254
|
+
})));
|
|
2255
|
+
});
|
|
2256
|
+
}
|
|
2257
|
+
return marks;
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
function getSplittedAttributes(extensionAttributes, typeName, attributes) {
|
|
2261
|
+
return Object.fromEntries(Object
|
|
2262
|
+
.entries(attributes)
|
|
2263
|
+
.filter(([name]) => {
|
|
2264
|
+
const extensionAttribute = extensionAttributes.find(item => {
|
|
2265
|
+
return item.type === typeName && item.name === name;
|
|
2266
|
+
});
|
|
2267
|
+
if (!extensionAttribute) {
|
|
2268
|
+
return false;
|
|
2269
|
+
}
|
|
2270
|
+
return extensionAttribute.attribute.keepOnSplit;
|
|
2271
|
+
}));
|
|
2272
|
+
}
|
|
2273
|
+
|
|
2274
|
+
function isMarkActive(state, typeOrName, attributes = {}) {
|
|
2275
|
+
const { empty, ranges } = state.selection;
|
|
2276
|
+
const type = typeOrName ? getMarkType(typeOrName, state.schema) : null;
|
|
2277
|
+
if (empty) {
|
|
2278
|
+
return !!(state.storedMarks || state.selection.$from.marks())
|
|
2279
|
+
.filter(mark => {
|
|
2280
|
+
if (!type) {
|
|
2281
|
+
return true;
|
|
2282
|
+
}
|
|
2283
|
+
return type.name === mark.type.name;
|
|
2284
|
+
})
|
|
2285
|
+
.find(mark => objectIncludes(mark.attrs, attributes, { strict: false }));
|
|
2286
|
+
}
|
|
2287
|
+
let selectionRange = 0;
|
|
2288
|
+
const markRanges = [];
|
|
2289
|
+
ranges.forEach(({ $from, $to }) => {
|
|
2290
|
+
const from = $from.pos;
|
|
2291
|
+
const to = $to.pos;
|
|
2292
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
2293
|
+
if (!node.isText && !node.marks.length) {
|
|
2294
|
+
return;
|
|
2295
|
+
}
|
|
2296
|
+
const relativeFrom = Math.max(from, pos);
|
|
2297
|
+
const relativeTo = Math.min(to, pos + node.nodeSize);
|
|
2298
|
+
const range = relativeTo - relativeFrom;
|
|
2299
|
+
selectionRange += range;
|
|
2300
|
+
markRanges.push(...node.marks.map(mark => ({
|
|
2301
|
+
mark,
|
|
2302
|
+
from: relativeFrom,
|
|
2303
|
+
to: relativeTo,
|
|
2304
|
+
})));
|
|
2305
|
+
});
|
|
2306
|
+
});
|
|
2307
|
+
if (selectionRange === 0) {
|
|
2308
|
+
return false;
|
|
2309
|
+
}
|
|
2310
|
+
// calculate range of matched mark
|
|
2311
|
+
const matchedRange = markRanges
|
|
2312
|
+
.filter(markRange => {
|
|
2313
|
+
if (!type) {
|
|
2314
|
+
return true;
|
|
2315
|
+
}
|
|
2316
|
+
return type.name === markRange.mark.type.name;
|
|
2317
|
+
})
|
|
2318
|
+
.filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
|
|
2319
|
+
.reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
|
|
2320
|
+
// calculate range of marks that excludes the searched mark
|
|
2321
|
+
// for example `code` doesn’t allow any other marks
|
|
2322
|
+
const excludedRange = markRanges
|
|
2323
|
+
.filter(markRange => {
|
|
2324
|
+
if (!type) {
|
|
2325
|
+
return true;
|
|
2326
|
+
}
|
|
2327
|
+
return markRange.mark.type !== type && markRange.mark.type.excludes(type);
|
|
2328
|
+
})
|
|
2329
|
+
.reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
|
|
2330
|
+
// we only include the result of `excludedRange`
|
|
2331
|
+
// if there is a match at all
|
|
2332
|
+
const range = matchedRange > 0 ? matchedRange + excludedRange : matchedRange;
|
|
2333
|
+
return range >= selectionRange;
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
function isActive(state, name, attributes = {}) {
|
|
2337
|
+
if (!name) {
|
|
2338
|
+
return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
|
|
2339
|
+
}
|
|
2340
|
+
const schemaType = getSchemaTypeNameByName(name, state.schema);
|
|
2341
|
+
if (schemaType === 'node') {
|
|
2342
|
+
return isNodeActive(state, name, attributes);
|
|
2343
|
+
}
|
|
2344
|
+
if (schemaType === 'mark') {
|
|
2345
|
+
return isMarkActive(state, name, attributes);
|
|
2346
|
+
}
|
|
2347
|
+
return false;
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
function isList(name, extensions) {
|
|
2351
|
+
const { nodeExtensions } = splitExtensions(extensions);
|
|
2352
|
+
const extension = nodeExtensions.find(item => item.name === name);
|
|
2353
|
+
if (!extension) {
|
|
2354
|
+
return false;
|
|
2355
|
+
}
|
|
2356
|
+
const context = {
|
|
2357
|
+
name: extension.name,
|
|
2358
|
+
options: extension.options,
|
|
2359
|
+
storage: extension.storage,
|
|
2360
|
+
};
|
|
2361
|
+
const group = callOrReturn(getExtensionField(extension, 'group', context));
|
|
2362
|
+
if (typeof group !== 'string') {
|
|
2363
|
+
return false;
|
|
2364
|
+
}
|
|
2365
|
+
return group.split(' ').includes('list');
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2368
|
+
function isNodeEmpty(node) {
|
|
2369
|
+
var _a;
|
|
2370
|
+
const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
|
|
2371
|
+
const content = node.toJSON();
|
|
2372
|
+
return JSON.stringify(defaultContent) === JSON.stringify(content);
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
function isNodeSelection(value) {
|
|
2376
|
+
return value instanceof state.NodeSelection;
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
function posToDOMRect(view, from, to) {
|
|
2380
|
+
const minPos = 0;
|
|
2381
|
+
const maxPos = view.state.doc.content.size;
|
|
2382
|
+
const resolvedFrom = minMax(from, minPos, maxPos);
|
|
2383
|
+
const resolvedEnd = minMax(to, minPos, maxPos);
|
|
2384
|
+
const start = view.coordsAtPos(resolvedFrom);
|
|
2385
|
+
const end = view.coordsAtPos(resolvedEnd, -1);
|
|
2386
|
+
const top = Math.min(start.top, end.top);
|
|
2387
|
+
const bottom = Math.max(start.bottom, end.bottom);
|
|
2388
|
+
const left = Math.min(start.left, end.left);
|
|
2389
|
+
const right = Math.max(start.right, end.right);
|
|
2390
|
+
const width = right - left;
|
|
2391
|
+
const height = bottom - top;
|
|
2392
|
+
const x = left;
|
|
2393
|
+
const y = top;
|
|
2394
|
+
const data = {
|
|
2395
|
+
top,
|
|
2396
|
+
bottom,
|
|
2397
|
+
left,
|
|
2398
|
+
right,
|
|
2399
|
+
width,
|
|
2400
|
+
height,
|
|
2401
|
+
x,
|
|
2402
|
+
y,
|
|
2403
|
+
};
|
|
2404
|
+
return {
|
|
2405
|
+
...data,
|
|
2406
|
+
toJSON: () => data,
|
|
2407
|
+
};
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
function canSetMark(state, tr, newMarkType) {
|
|
2411
|
+
var _a;
|
|
2412
|
+
const { selection } = tr;
|
|
2413
|
+
let cursor = null;
|
|
2414
|
+
if (isTextSelection(selection)) {
|
|
2415
|
+
cursor = selection.$cursor;
|
|
2416
|
+
}
|
|
2417
|
+
if (cursor) {
|
|
2418
|
+
const currentMarks = (_a = state.storedMarks) !== null && _a !== void 0 ? _a : cursor.marks();
|
|
2419
|
+
// There can be no current marks that exclude the new mark
|
|
2420
|
+
return (!!newMarkType.isInSet(currentMarks)
|
|
2421
|
+
|| !currentMarks.some(mark => mark.type.excludes(newMarkType)));
|
|
2422
|
+
}
|
|
2423
|
+
const { ranges } = selection;
|
|
2424
|
+
return ranges.some(({ $from, $to }) => {
|
|
2425
|
+
let someNodeSupportsMark = $from.depth === 0
|
|
2426
|
+
? state.doc.inlineContent && state.doc.type.allowsMarkType(newMarkType)
|
|
2427
|
+
: false;
|
|
2428
|
+
state.doc.nodesBetween($from.pos, $to.pos, (node, _pos, parent) => {
|
|
2429
|
+
// If we already found a mark that we can enable, return false to bypass the remaining search
|
|
2430
|
+
if (someNodeSupportsMark) {
|
|
2431
|
+
return false;
|
|
2432
|
+
}
|
|
2433
|
+
if (node.isInline) {
|
|
2434
|
+
const parentAllowsMarkType = !parent || parent.type.allowsMarkType(newMarkType);
|
|
2435
|
+
const currentMarksAllowMarkType = !!newMarkType.isInSet(node.marks)
|
|
2436
|
+
|| !node.marks.some(otherMark => otherMark.type.excludes(newMarkType));
|
|
2437
|
+
someNodeSupportsMark = parentAllowsMarkType && currentMarksAllowMarkType;
|
|
2438
|
+
}
|
|
2439
|
+
return !someNodeSupportsMark;
|
|
2440
|
+
});
|
|
2441
|
+
return someNodeSupportsMark;
|
|
2442
|
+
});
|
|
2443
|
+
}
|
|
2444
|
+
const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
|
2445
|
+
const { selection } = tr;
|
|
2446
|
+
const { empty, ranges } = selection;
|
|
2447
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
2448
|
+
if (dispatch) {
|
|
2449
|
+
if (empty) {
|
|
2450
|
+
const oldAttributes = getMarkAttributes(state, type);
|
|
2451
|
+
tr.addStoredMark(type.create({
|
|
2452
|
+
...oldAttributes,
|
|
2453
|
+
...attributes,
|
|
2454
|
+
}));
|
|
2455
|
+
}
|
|
2456
|
+
else {
|
|
2457
|
+
ranges.forEach(range => {
|
|
2458
|
+
const from = range.$from.pos;
|
|
2459
|
+
const to = range.$to.pos;
|
|
2460
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
2461
|
+
const trimmedFrom = Math.max(pos, from);
|
|
2462
|
+
const trimmedTo = Math.min(pos + node.nodeSize, to);
|
|
2463
|
+
const someHasMark = node.marks.find(mark => mark.type === type);
|
|
2464
|
+
// if there is already a mark of this type
|
|
2465
|
+
// we know that we have to merge its attributes
|
|
2466
|
+
// otherwise we add a fresh new mark
|
|
2467
|
+
if (someHasMark) {
|
|
2468
|
+
node.marks.forEach(mark => {
|
|
2469
|
+
if (type === mark.type) {
|
|
2470
|
+
tr.addMark(trimmedFrom, trimmedTo, type.create({
|
|
2471
|
+
...mark.attrs,
|
|
2472
|
+
...attributes,
|
|
2473
|
+
}));
|
|
2474
|
+
}
|
|
2475
|
+
});
|
|
2476
|
+
}
|
|
2477
|
+
else {
|
|
2478
|
+
tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
|
|
2479
|
+
}
|
|
2480
|
+
});
|
|
2481
|
+
});
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
return canSetMark(state, tr, type);
|
|
2485
|
+
};
|
|
2486
|
+
|
|
2487
|
+
const setMeta = (key, value) => ({ tr }) => {
|
|
2488
|
+
tr.setMeta(key, value);
|
|
2489
|
+
return true;
|
|
2490
|
+
};
|
|
2491
|
+
|
|
2492
|
+
const setNode = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
|
|
2493
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2494
|
+
// TODO: use a fallback like insertContent?
|
|
2495
|
+
if (!type.isTextblock) {
|
|
2496
|
+
console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.');
|
|
2497
|
+
return false;
|
|
2498
|
+
}
|
|
2499
|
+
return (chain()
|
|
2500
|
+
// try to convert node to default node if needed
|
|
2501
|
+
.command(({ commands }) => {
|
|
2502
|
+
const canSetBlock = commands$1.setBlockType(type, attributes)(state);
|
|
2503
|
+
if (canSetBlock) {
|
|
2504
|
+
return true;
|
|
2505
|
+
}
|
|
2506
|
+
return commands.clearNodes();
|
|
2507
|
+
})
|
|
2508
|
+
.command(({ state: updatedState }) => {
|
|
2509
|
+
return commands$1.setBlockType(type, attributes)(updatedState, dispatch);
|
|
2510
|
+
})
|
|
2511
|
+
.run());
|
|
2512
|
+
};
|
|
2513
|
+
|
|
2514
|
+
const setNodeSelection = position => ({ tr, dispatch }) => {
|
|
2515
|
+
if (dispatch) {
|
|
2516
|
+
const { doc } = tr;
|
|
2517
|
+
const from = minMax(position, 0, doc.content.size);
|
|
2518
|
+
const selection = state.NodeSelection.create(doc, from);
|
|
2519
|
+
tr.setSelection(selection);
|
|
2520
|
+
}
|
|
2521
|
+
return true;
|
|
2522
|
+
};
|
|
2523
|
+
|
|
2524
|
+
const setTextSelection = position => ({ tr, dispatch }) => {
|
|
2525
|
+
if (dispatch) {
|
|
2526
|
+
const { doc } = tr;
|
|
2527
|
+
const { from, to } = typeof position === 'number' ? { from: position, to: position } : position;
|
|
2528
|
+
const minPos = state.TextSelection.atStart(doc).from;
|
|
2529
|
+
const maxPos = state.TextSelection.atEnd(doc).to;
|
|
2530
|
+
const resolvedFrom = minMax(from, minPos, maxPos);
|
|
2531
|
+
const resolvedEnd = minMax(to, minPos, maxPos);
|
|
2532
|
+
const selection = state.TextSelection.create(doc, resolvedFrom, resolvedEnd);
|
|
2533
|
+
tr.setSelection(selection);
|
|
2534
|
+
}
|
|
2535
|
+
return true;
|
|
2536
|
+
};
|
|
2537
|
+
|
|
2538
|
+
const sinkListItem = typeOrName => ({ state, dispatch }) => {
|
|
2539
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2540
|
+
return schemaList.sinkListItem(type)(state, dispatch);
|
|
2541
|
+
};
|
|
2542
|
+
|
|
2543
|
+
function ensureMarks(state, splittableMarks) {
|
|
2544
|
+
const marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());
|
|
2545
|
+
if (marks) {
|
|
2546
|
+
const filteredMarks = marks.filter(mark => splittableMarks === null || splittableMarks === void 0 ? void 0 : splittableMarks.includes(mark.type.name));
|
|
2547
|
+
state.tr.ensureMarks(filteredMarks);
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
const splitBlock = ({ keepMarks = true } = {}) => ({ tr, state: state$1, dispatch, editor, }) => {
|
|
2551
|
+
const { selection, doc } = tr;
|
|
2552
|
+
const { $from, $to } = selection;
|
|
2553
|
+
const extensionAttributes = editor.extensionManager.attributes;
|
|
2554
|
+
const newAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
|
|
2555
|
+
if (selection instanceof state.NodeSelection && selection.node.isBlock) {
|
|
2556
|
+
if (!$from.parentOffset || !transform.canSplit(doc, $from.pos)) {
|
|
2557
|
+
return false;
|
|
2558
|
+
}
|
|
2559
|
+
if (dispatch) {
|
|
2560
|
+
if (keepMarks) {
|
|
2561
|
+
ensureMarks(state$1, editor.extensionManager.splittableMarks);
|
|
2562
|
+
}
|
|
2563
|
+
tr.split($from.pos).scrollIntoView();
|
|
2564
|
+
}
|
|
2565
|
+
return true;
|
|
2566
|
+
}
|
|
2567
|
+
if (!$from.parent.isBlock) {
|
|
2568
|
+
return false;
|
|
2569
|
+
}
|
|
2570
|
+
if (dispatch) {
|
|
2571
|
+
const atEnd = $to.parentOffset === $to.parent.content.size;
|
|
2572
|
+
if (selection instanceof state.TextSelection) {
|
|
2573
|
+
tr.deleteSelection();
|
|
2574
|
+
}
|
|
2575
|
+
const deflt = $from.depth === 0
|
|
2576
|
+
? undefined
|
|
2577
|
+
: defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)));
|
|
2578
|
+
let types = atEnd && deflt
|
|
2579
|
+
? [
|
|
2580
|
+
{
|
|
2581
|
+
type: deflt,
|
|
2582
|
+
attrs: newAttributes,
|
|
2583
|
+
},
|
|
2584
|
+
]
|
|
2585
|
+
: undefined;
|
|
2586
|
+
let can = transform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, types);
|
|
2587
|
+
if (!types
|
|
2588
|
+
&& !can
|
|
2589
|
+
&& transform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : undefined)) {
|
|
2590
|
+
can = true;
|
|
2591
|
+
types = deflt
|
|
2592
|
+
? [
|
|
2593
|
+
{
|
|
2594
|
+
type: deflt,
|
|
2595
|
+
attrs: newAttributes,
|
|
2596
|
+
},
|
|
2597
|
+
]
|
|
2598
|
+
: undefined;
|
|
2599
|
+
}
|
|
2600
|
+
if (can) {
|
|
2601
|
+
tr.split(tr.mapping.map($from.pos), 1, types);
|
|
2602
|
+
if (deflt && !atEnd && !$from.parentOffset && $from.parent.type !== deflt) {
|
|
2603
|
+
const first = tr.mapping.map($from.before());
|
|
2604
|
+
const $first = tr.doc.resolve(first);
|
|
2605
|
+
if ($from.node(-1).canReplaceWith($first.index(), $first.index() + 1, deflt)) {
|
|
2606
|
+
tr.setNodeMarkup(tr.mapping.map($from.before()), deflt);
|
|
2607
|
+
}
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
if (keepMarks) {
|
|
2611
|
+
ensureMarks(state$1, editor.extensionManager.splittableMarks);
|
|
2612
|
+
}
|
|
2613
|
+
tr.scrollIntoView();
|
|
2614
|
+
}
|
|
2615
|
+
return true;
|
|
2616
|
+
};
|
|
2617
|
+
|
|
2618
|
+
const splitListItem = typeOrName => ({ tr, state: state$1, dispatch, editor, }) => {
|
|
2619
|
+
var _a;
|
|
2620
|
+
const type = getNodeType(typeOrName, state$1.schema);
|
|
2621
|
+
const { $from, $to } = state$1.selection;
|
|
2622
|
+
// @ts-ignore
|
|
2623
|
+
// eslint-disable-next-line
|
|
2624
|
+
const node = state$1.selection.node;
|
|
2625
|
+
if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) {
|
|
2626
|
+
return false;
|
|
2627
|
+
}
|
|
2628
|
+
const grandParent = $from.node(-1);
|
|
2629
|
+
if (grandParent.type !== type) {
|
|
2630
|
+
return false;
|
|
2631
|
+
}
|
|
2632
|
+
const extensionAttributes = editor.extensionManager.attributes;
|
|
2633
|
+
if ($from.parent.content.size === 0 && $from.node(-1).childCount === $from.indexAfter(-1)) {
|
|
2634
|
+
// In an empty block. If this is a nested list, the wrapping
|
|
2635
|
+
// list item should be split. Otherwise, bail out and let next
|
|
2636
|
+
// command handle lifting.
|
|
2637
|
+
if ($from.depth === 2
|
|
2638
|
+
|| $from.node(-3).type !== type
|
|
2639
|
+
|| $from.index(-2) !== $from.node(-2).childCount - 1) {
|
|
2640
|
+
return false;
|
|
2641
|
+
}
|
|
2642
|
+
if (dispatch) {
|
|
2643
|
+
let wrap = model.Fragment.empty;
|
|
2644
|
+
// eslint-disable-next-line
|
|
2645
|
+
const depthBefore = $from.index(-1) ? 1 : $from.index(-2) ? 2 : 3;
|
|
2646
|
+
// Build a fragment containing empty versions of the structure
|
|
2647
|
+
// from the outer list item to the parent node of the cursor
|
|
2648
|
+
for (let d = $from.depth - depthBefore; d >= $from.depth - 3; d -= 1) {
|
|
2649
|
+
wrap = model.Fragment.from($from.node(d).copy(wrap));
|
|
2650
|
+
}
|
|
2651
|
+
// eslint-disable-next-line
|
|
2652
|
+
const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3;
|
|
2653
|
+
// Add a second list item with an empty default start node
|
|
2654
|
+
const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
|
|
2655
|
+
const nextType = ((_a = type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.createAndFill(newNextTypeAttributes)) || undefined;
|
|
2656
|
+
wrap = wrap.append(model.Fragment.from(type.createAndFill(null, nextType) || undefined));
|
|
2657
|
+
const start = $from.before($from.depth - (depthBefore - 1));
|
|
2658
|
+
tr.replace(start, $from.after(-depthAfter), new model.Slice(wrap, 4 - depthBefore, 0));
|
|
2659
|
+
let sel = -1;
|
|
2660
|
+
tr.doc.nodesBetween(start, tr.doc.content.size, (n, pos) => {
|
|
2661
|
+
if (sel > -1) {
|
|
2662
|
+
return false;
|
|
2663
|
+
}
|
|
2664
|
+
if (n.isTextblock && n.content.size === 0) {
|
|
2665
|
+
sel = pos + 1;
|
|
2666
|
+
}
|
|
2667
|
+
});
|
|
2668
|
+
if (sel > -1) {
|
|
2669
|
+
tr.setSelection(state.TextSelection.near(tr.doc.resolve(sel)));
|
|
2670
|
+
}
|
|
2671
|
+
tr.scrollIntoView();
|
|
2672
|
+
}
|
|
2673
|
+
return true;
|
|
2674
|
+
}
|
|
2675
|
+
const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null;
|
|
2676
|
+
const newTypeAttributes = getSplittedAttributes(extensionAttributes, grandParent.type.name, grandParent.attrs);
|
|
2677
|
+
const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
|
|
2678
|
+
tr.delete($from.pos, $to.pos);
|
|
2679
|
+
const types = nextType
|
|
2680
|
+
? [
|
|
2681
|
+
{ type, attrs: newTypeAttributes },
|
|
2682
|
+
{ type: nextType, attrs: newNextTypeAttributes },
|
|
2683
|
+
]
|
|
2684
|
+
: [{ type, attrs: newTypeAttributes }];
|
|
2685
|
+
if (!transform.canSplit(tr.doc, $from.pos, 2)) {
|
|
2686
|
+
return false;
|
|
2687
|
+
}
|
|
2688
|
+
if (dispatch) {
|
|
2689
|
+
const { selection, storedMarks } = state$1;
|
|
2690
|
+
const { splittableMarks } = editor.extensionManager;
|
|
2691
|
+
const marks = storedMarks || (selection.$to.parentOffset && selection.$from.marks());
|
|
2692
|
+
tr.split($from.pos, 2, types).scrollIntoView();
|
|
2693
|
+
if (!marks || !dispatch) {
|
|
2694
|
+
return true;
|
|
2695
|
+
}
|
|
2696
|
+
const filteredMarks = marks.filter(mark => splittableMarks.includes(mark.type.name));
|
|
2697
|
+
tr.ensureMarks(filteredMarks);
|
|
2698
|
+
}
|
|
2699
|
+
return true;
|
|
2700
|
+
};
|
|
2701
|
+
|
|
2702
|
+
const joinListBackwards = (tr, listType) => {
|
|
2703
|
+
const list = findParentNode(node => node.type === listType)(tr.selection);
|
|
2704
|
+
if (!list) {
|
|
2705
|
+
return true;
|
|
2706
|
+
}
|
|
2707
|
+
const before = tr.doc.resolve(Math.max(0, list.pos - 1)).before(list.depth);
|
|
2708
|
+
if (before === undefined) {
|
|
2709
|
+
return true;
|
|
2710
|
+
}
|
|
2711
|
+
const nodeBefore = tr.doc.nodeAt(before);
|
|
2712
|
+
const canJoinBackwards = list.node.type === (nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.type) && transform.canJoin(tr.doc, list.pos);
|
|
2713
|
+
if (!canJoinBackwards) {
|
|
2714
|
+
return true;
|
|
2715
|
+
}
|
|
2716
|
+
tr.join(list.pos);
|
|
2717
|
+
return true;
|
|
2718
|
+
};
|
|
2719
|
+
const joinListForwards = (tr, listType) => {
|
|
2720
|
+
const list = findParentNode(node => node.type === listType)(tr.selection);
|
|
2721
|
+
if (!list) {
|
|
2722
|
+
return true;
|
|
2723
|
+
}
|
|
2724
|
+
const after = tr.doc.resolve(list.start).after(list.depth);
|
|
2725
|
+
if (after === undefined) {
|
|
2726
|
+
return true;
|
|
2727
|
+
}
|
|
2728
|
+
const nodeAfter = tr.doc.nodeAt(after);
|
|
2729
|
+
const canJoinForwards = list.node.type === (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.type) && transform.canJoin(tr.doc, after);
|
|
2730
|
+
if (!canJoinForwards) {
|
|
2731
|
+
return true;
|
|
2732
|
+
}
|
|
2733
|
+
tr.join(after);
|
|
2734
|
+
return true;
|
|
2735
|
+
};
|
|
2736
|
+
const toggleList = (listTypeOrName, itemTypeOrName, keepMarks, attributes = {}) => ({ editor, tr, state, dispatch, chain, commands, can, }) => {
|
|
2737
|
+
const { extensions, splittableMarks } = editor.extensionManager;
|
|
2738
|
+
const listType = getNodeType(listTypeOrName, state.schema);
|
|
2739
|
+
const itemType = getNodeType(itemTypeOrName, state.schema);
|
|
2740
|
+
const { selection, storedMarks } = state;
|
|
2741
|
+
const { $from, $to } = selection;
|
|
2742
|
+
const range = $from.blockRange($to);
|
|
2743
|
+
const marks = storedMarks || (selection.$to.parentOffset && selection.$from.marks());
|
|
2744
|
+
if (!range) {
|
|
2745
|
+
return false;
|
|
2746
|
+
}
|
|
2747
|
+
const parentList = findParentNode(node => isList(node.type.name, extensions))(selection);
|
|
2748
|
+
if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
|
|
2749
|
+
// remove list
|
|
2750
|
+
if (parentList.node.type === listType) {
|
|
2751
|
+
return commands.liftListItem(itemType);
|
|
2752
|
+
}
|
|
2753
|
+
// change list type
|
|
2754
|
+
if (isList(parentList.node.type.name, extensions)
|
|
2755
|
+
&& listType.validContent(parentList.node.content)
|
|
2756
|
+
&& dispatch) {
|
|
2757
|
+
return chain()
|
|
2758
|
+
.command(() => {
|
|
2759
|
+
tr.setNodeMarkup(parentList.pos, listType);
|
|
2760
|
+
return true;
|
|
2761
|
+
})
|
|
2762
|
+
.command(() => joinListBackwards(tr, listType))
|
|
2763
|
+
.command(() => joinListForwards(tr, listType))
|
|
2764
|
+
.run();
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
if (!keepMarks || !marks || !dispatch) {
|
|
2768
|
+
return chain()
|
|
2769
|
+
// try to convert node to default node if needed
|
|
2770
|
+
.command(() => {
|
|
2771
|
+
const canWrapInList = can().wrapInList(listType, attributes);
|
|
2772
|
+
if (canWrapInList) {
|
|
2773
|
+
return true;
|
|
2774
|
+
}
|
|
2775
|
+
return commands.clearNodes();
|
|
2776
|
+
})
|
|
2777
|
+
.wrapInList(listType, attributes)
|
|
2778
|
+
.command(() => joinListBackwards(tr, listType))
|
|
2779
|
+
.command(() => joinListForwards(tr, listType))
|
|
2780
|
+
.run();
|
|
2781
|
+
}
|
|
2782
|
+
return (chain()
|
|
2783
|
+
// try to convert node to default node if needed
|
|
2784
|
+
.command(() => {
|
|
2785
|
+
const canWrapInList = can().wrapInList(listType, attributes);
|
|
2786
|
+
const filteredMarks = marks.filter(mark => splittableMarks.includes(mark.type.name));
|
|
2787
|
+
tr.ensureMarks(filteredMarks);
|
|
2788
|
+
if (canWrapInList) {
|
|
2789
|
+
return true;
|
|
2790
|
+
}
|
|
2791
|
+
return commands.clearNodes();
|
|
2792
|
+
})
|
|
2793
|
+
.wrapInList(listType, attributes)
|
|
2794
|
+
.command(() => joinListBackwards(tr, listType))
|
|
2795
|
+
.command(() => joinListForwards(tr, listType))
|
|
2796
|
+
.run());
|
|
2797
|
+
};
|
|
2798
|
+
|
|
2799
|
+
const toggleMark = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
|
|
2800
|
+
const { extendEmptyMarkRange = false } = options;
|
|
2801
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
2802
|
+
const isActive = isMarkActive(state, type, attributes);
|
|
2803
|
+
if (isActive) {
|
|
2804
|
+
return commands.unsetMark(type, { extendEmptyMarkRange });
|
|
2805
|
+
}
|
|
2806
|
+
return commands.setMark(type, attributes);
|
|
2807
|
+
};
|
|
2808
|
+
|
|
2809
|
+
const toggleNode = (typeOrName, toggleTypeOrName, attributes = {}) => ({ state, commands }) => {
|
|
2810
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2811
|
+
const toggleType = getNodeType(toggleTypeOrName, state.schema);
|
|
2812
|
+
const isActive = isNodeActive(state, type, attributes);
|
|
2813
|
+
if (isActive) {
|
|
2814
|
+
return commands.setNode(toggleType);
|
|
2815
|
+
}
|
|
2816
|
+
return commands.setNode(type, attributes);
|
|
2817
|
+
};
|
|
2818
|
+
|
|
2819
|
+
const toggleWrap = (typeOrName, attributes = {}) => ({ state, commands }) => {
|
|
2820
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2821
|
+
const isActive = isNodeActive(state, type, attributes);
|
|
2822
|
+
if (isActive) {
|
|
2823
|
+
return commands.lift(type);
|
|
2824
|
+
}
|
|
2825
|
+
return commands.wrapIn(type, attributes);
|
|
2826
|
+
};
|
|
2827
|
+
|
|
2828
|
+
const undoInputRule = () => ({ state, dispatch }) => {
|
|
2829
|
+
const plugins = state.plugins;
|
|
2830
|
+
for (let i = 0; i < plugins.length; i += 1) {
|
|
2831
|
+
const plugin = plugins[i];
|
|
2832
|
+
let undoable;
|
|
2833
|
+
// @ts-ignore
|
|
2834
|
+
// eslint-disable-next-line
|
|
2835
|
+
if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
|
|
2836
|
+
if (dispatch) {
|
|
2837
|
+
const tr = state.tr;
|
|
2838
|
+
const toUndo = undoable.transform;
|
|
2839
|
+
for (let j = toUndo.steps.length - 1; j >= 0; j -= 1) {
|
|
2840
|
+
tr.step(toUndo.steps[j].invert(toUndo.docs[j]));
|
|
2841
|
+
}
|
|
2842
|
+
if (undoable.text) {
|
|
2843
|
+
const marks = tr.doc.resolve(undoable.from).marks();
|
|
2844
|
+
tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks));
|
|
2845
|
+
}
|
|
2846
|
+
else {
|
|
2847
|
+
tr.delete(undoable.from, undoable.to);
|
|
2848
|
+
}
|
|
2849
|
+
}
|
|
2850
|
+
return true;
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
return false;
|
|
2854
|
+
};
|
|
2855
|
+
|
|
2856
|
+
const unsetAllMarks = () => ({ tr, dispatch }) => {
|
|
2857
|
+
const { selection } = tr;
|
|
2858
|
+
const { empty, ranges } = selection;
|
|
2859
|
+
if (empty) {
|
|
2860
|
+
return true;
|
|
2861
|
+
}
|
|
2862
|
+
if (dispatch) {
|
|
2863
|
+
ranges.forEach(range => {
|
|
2864
|
+
tr.removeMark(range.$from.pos, range.$to.pos);
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
return true;
|
|
2868
|
+
};
|
|
2869
|
+
|
|
2870
|
+
const unsetMark = (typeOrName, options = {}) => ({ tr, state, dispatch }) => {
|
|
2871
|
+
var _a;
|
|
2872
|
+
const { extendEmptyMarkRange = false } = options;
|
|
2873
|
+
const { selection } = tr;
|
|
2874
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
2875
|
+
const { $from, empty, ranges } = selection;
|
|
2876
|
+
if (!dispatch) {
|
|
2877
|
+
return true;
|
|
2878
|
+
}
|
|
2879
|
+
if (empty && extendEmptyMarkRange) {
|
|
2880
|
+
let { from, to } = selection;
|
|
2881
|
+
const attrs = (_a = $from.marks().find(mark => mark.type === type)) === null || _a === void 0 ? void 0 : _a.attrs;
|
|
2882
|
+
const range = getMarkRange($from, type, attrs);
|
|
2883
|
+
if (range) {
|
|
2884
|
+
from = range.from;
|
|
2885
|
+
to = range.to;
|
|
2886
|
+
}
|
|
2887
|
+
tr.removeMark(from, to, type);
|
|
2888
|
+
}
|
|
2889
|
+
else {
|
|
2890
|
+
ranges.forEach(range => {
|
|
2891
|
+
tr.removeMark(range.$from.pos, range.$to.pos, type);
|
|
2892
|
+
});
|
|
2893
|
+
}
|
|
2894
|
+
tr.removeStoredMark(type);
|
|
2895
|
+
return true;
|
|
2896
|
+
};
|
|
2897
|
+
|
|
2898
|
+
const updateAttributes = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
|
2899
|
+
let nodeType = null;
|
|
2900
|
+
let markType = null;
|
|
2901
|
+
const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string' ? typeOrName : typeOrName.name, state.schema);
|
|
2902
|
+
if (!schemaType) {
|
|
2903
|
+
return false;
|
|
2904
|
+
}
|
|
2905
|
+
if (schemaType === 'node') {
|
|
2906
|
+
nodeType = getNodeType(typeOrName, state.schema);
|
|
2907
|
+
}
|
|
2908
|
+
if (schemaType === 'mark') {
|
|
2909
|
+
markType = getMarkType(typeOrName, state.schema);
|
|
2910
|
+
}
|
|
2911
|
+
if (dispatch) {
|
|
2912
|
+
tr.selection.ranges.forEach(range => {
|
|
2913
|
+
const from = range.$from.pos;
|
|
2914
|
+
const to = range.$to.pos;
|
|
2915
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
2916
|
+
if (nodeType && nodeType === node.type) {
|
|
2917
|
+
tr.setNodeMarkup(pos, undefined, {
|
|
2918
|
+
...node.attrs,
|
|
2919
|
+
...attributes,
|
|
2920
|
+
});
|
|
2921
|
+
}
|
|
2922
|
+
if (markType && node.marks.length) {
|
|
2923
|
+
node.marks.forEach(mark => {
|
|
2924
|
+
if (markType === mark.type) {
|
|
2925
|
+
const trimmedFrom = Math.max(pos, from);
|
|
2926
|
+
const trimmedTo = Math.min(pos + node.nodeSize, to);
|
|
2927
|
+
tr.addMark(trimmedFrom, trimmedTo, markType.create({
|
|
2928
|
+
...mark.attrs,
|
|
2929
|
+
...attributes,
|
|
2930
|
+
}));
|
|
2931
|
+
}
|
|
2932
|
+
});
|
|
2933
|
+
}
|
|
2934
|
+
});
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
return true;
|
|
2938
|
+
};
|
|
2939
|
+
|
|
2940
|
+
const wrapIn = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
|
|
2941
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2942
|
+
return commands$1.wrapIn(type, attributes)(state, dispatch);
|
|
2943
|
+
};
|
|
2944
|
+
|
|
2945
|
+
const wrapInList = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
|
|
2946
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2947
|
+
return schemaList.wrapInList(type, attributes)(state, dispatch);
|
|
2948
|
+
};
|
|
2949
|
+
|
|
2950
|
+
var commands = /*#__PURE__*/Object.freeze({
|
|
2951
|
+
__proto__: null,
|
|
2952
|
+
blur: blur,
|
|
2953
|
+
clearContent: clearContent,
|
|
2954
|
+
clearNodes: clearNodes,
|
|
2955
|
+
command: command,
|
|
2956
|
+
createParagraphNear: createParagraphNear,
|
|
2957
|
+
deleteCurrentNode: deleteCurrentNode,
|
|
2958
|
+
deleteNode: deleteNode,
|
|
2959
|
+
deleteRange: deleteRange,
|
|
2960
|
+
deleteSelection: deleteSelection,
|
|
2961
|
+
enter: enter,
|
|
2962
|
+
exitCode: exitCode,
|
|
2963
|
+
extendMarkRange: extendMarkRange,
|
|
2964
|
+
first: first,
|
|
2965
|
+
focus: focus,
|
|
2966
|
+
forEach: forEach,
|
|
2967
|
+
insertContent: insertContent,
|
|
2968
|
+
insertContentAt: insertContentAt,
|
|
2969
|
+
joinUp: joinUp,
|
|
2970
|
+
joinDown: joinDown,
|
|
2971
|
+
joinBackward: joinBackward,
|
|
2972
|
+
joinForward: joinForward,
|
|
2973
|
+
keyboardShortcut: keyboardShortcut,
|
|
2974
|
+
lift: lift,
|
|
2975
|
+
liftEmptyBlock: liftEmptyBlock,
|
|
2976
|
+
liftListItem: liftListItem,
|
|
2977
|
+
newlineInCode: newlineInCode,
|
|
2978
|
+
resetAttributes: resetAttributes,
|
|
2979
|
+
scrollIntoView: scrollIntoView,
|
|
2980
|
+
selectAll: selectAll,
|
|
2981
|
+
selectNodeBackward: selectNodeBackward,
|
|
2982
|
+
selectNodeForward: selectNodeForward,
|
|
2983
|
+
selectParentNode: selectParentNode,
|
|
2984
|
+
selectTextblockEnd: selectTextblockEnd,
|
|
2985
|
+
selectTextblockStart: selectTextblockStart,
|
|
2986
|
+
setContent: setContent,
|
|
2987
|
+
setMark: setMark,
|
|
2988
|
+
setMeta: setMeta,
|
|
2989
|
+
setNode: setNode,
|
|
2990
|
+
setNodeSelection: setNodeSelection,
|
|
2991
|
+
setTextSelection: setTextSelection,
|
|
2992
|
+
sinkListItem: sinkListItem,
|
|
2993
|
+
splitBlock: splitBlock,
|
|
2994
|
+
splitListItem: splitListItem,
|
|
2995
|
+
toggleList: toggleList,
|
|
2996
|
+
toggleMark: toggleMark,
|
|
2997
|
+
toggleNode: toggleNode,
|
|
2998
|
+
toggleWrap: toggleWrap,
|
|
2999
|
+
undoInputRule: undoInputRule,
|
|
3000
|
+
unsetAllMarks: unsetAllMarks,
|
|
3001
|
+
unsetMark: unsetMark,
|
|
3002
|
+
updateAttributes: updateAttributes,
|
|
3003
|
+
wrapIn: wrapIn,
|
|
3004
|
+
wrapInList: wrapInList
|
|
3005
|
+
});
|
|
3006
|
+
|
|
3007
|
+
const Commands = Extension.create({
|
|
3008
|
+
name: 'commands',
|
|
3009
|
+
addCommands() {
|
|
3010
|
+
return {
|
|
3011
|
+
...commands,
|
|
3012
|
+
};
|
|
3013
|
+
},
|
|
3014
|
+
});
|
|
3015
|
+
|
|
3016
|
+
const Editable = Extension.create({
|
|
3017
|
+
name: 'editable',
|
|
3018
|
+
addProseMirrorPlugins() {
|
|
3019
|
+
return [
|
|
3020
|
+
new state.Plugin({
|
|
3021
|
+
key: new state.PluginKey('editable'),
|
|
3022
|
+
props: {
|
|
3023
|
+
editable: () => this.editor.options.editable,
|
|
3024
|
+
},
|
|
3025
|
+
}),
|
|
3026
|
+
];
|
|
3027
|
+
},
|
|
3028
|
+
});
|
|
3029
|
+
|
|
3030
|
+
const FocusEvents = Extension.create({
|
|
3031
|
+
name: 'focusEvents',
|
|
3032
|
+
addProseMirrorPlugins() {
|
|
3033
|
+
const { editor } = this;
|
|
3034
|
+
return [
|
|
3035
|
+
new state.Plugin({
|
|
3036
|
+
key: new state.PluginKey('focusEvents'),
|
|
3037
|
+
props: {
|
|
3038
|
+
handleDOMEvents: {
|
|
3039
|
+
focus: (view, event) => {
|
|
3040
|
+
editor.isFocused = true;
|
|
3041
|
+
const transaction = editor.state.tr
|
|
3042
|
+
.setMeta('focus', { event })
|
|
3043
|
+
.setMeta('addToHistory', false);
|
|
3044
|
+
view.dispatch(transaction);
|
|
3045
|
+
return false;
|
|
3046
|
+
},
|
|
3047
|
+
blur: (view, event) => {
|
|
3048
|
+
editor.isFocused = false;
|
|
3049
|
+
const transaction = editor.state.tr
|
|
3050
|
+
.setMeta('blur', { event })
|
|
3051
|
+
.setMeta('addToHistory', false);
|
|
3052
|
+
view.dispatch(transaction);
|
|
3053
|
+
return false;
|
|
3054
|
+
},
|
|
3055
|
+
},
|
|
3056
|
+
},
|
|
3057
|
+
}),
|
|
3058
|
+
];
|
|
3059
|
+
},
|
|
3060
|
+
});
|
|
3061
|
+
|
|
3062
|
+
const Keymap = Extension.create({
|
|
3063
|
+
name: 'keymap',
|
|
3064
|
+
addKeyboardShortcuts() {
|
|
3065
|
+
const handleBackspace = () => this.editor.commands.first(({ commands }) => [
|
|
3066
|
+
() => commands.undoInputRule(),
|
|
3067
|
+
// maybe convert first text block node to default node
|
|
3068
|
+
() => commands.command(({ tr }) => {
|
|
3069
|
+
const { selection, doc } = tr;
|
|
3070
|
+
const { empty, $anchor } = selection;
|
|
3071
|
+
const { pos, parent } = $anchor;
|
|
3072
|
+
const isAtStart = state.Selection.atStart(doc).from === pos;
|
|
3073
|
+
if (!empty || !isAtStart || !parent.type.isTextblock || parent.textContent.length) {
|
|
3074
|
+
return false;
|
|
3075
|
+
}
|
|
3076
|
+
return commands.clearNodes();
|
|
3077
|
+
}),
|
|
3078
|
+
() => commands.deleteSelection(),
|
|
3079
|
+
() => commands.joinBackward(),
|
|
3080
|
+
() => commands.selectNodeBackward(),
|
|
3081
|
+
]);
|
|
3082
|
+
const handleDelete = () => this.editor.commands.first(({ commands }) => [
|
|
3083
|
+
() => commands.deleteSelection(),
|
|
3084
|
+
() => commands.deleteCurrentNode(),
|
|
3085
|
+
() => commands.joinForward(),
|
|
3086
|
+
() => commands.selectNodeForward(),
|
|
3087
|
+
]);
|
|
3088
|
+
const handleEnter = () => this.editor.commands.first(({ commands }) => [
|
|
3089
|
+
() => commands.newlineInCode(),
|
|
3090
|
+
() => commands.createParagraphNear(),
|
|
3091
|
+
() => commands.liftEmptyBlock(),
|
|
3092
|
+
() => commands.splitBlock(),
|
|
3093
|
+
]);
|
|
3094
|
+
const baseKeymap = {
|
|
3095
|
+
Enter: handleEnter,
|
|
3096
|
+
'Mod-Enter': () => this.editor.commands.exitCode(),
|
|
3097
|
+
Backspace: handleBackspace,
|
|
3098
|
+
'Mod-Backspace': handleBackspace,
|
|
3099
|
+
'Shift-Backspace': handleBackspace,
|
|
3100
|
+
Delete: handleDelete,
|
|
3101
|
+
'Mod-Delete': handleDelete,
|
|
3102
|
+
'Mod-a': () => this.editor.commands.selectAll(),
|
|
3103
|
+
};
|
|
3104
|
+
const pcKeymap = {
|
|
3105
|
+
...baseKeymap,
|
|
3106
|
+
};
|
|
3107
|
+
const macKeymap = {
|
|
3108
|
+
...baseKeymap,
|
|
3109
|
+
'Ctrl-h': handleBackspace,
|
|
3110
|
+
'Alt-Backspace': handleBackspace,
|
|
3111
|
+
'Ctrl-d': handleDelete,
|
|
3112
|
+
'Ctrl-Alt-Backspace': handleDelete,
|
|
3113
|
+
'Alt-Delete': handleDelete,
|
|
3114
|
+
'Alt-d': handleDelete,
|
|
3115
|
+
'Ctrl-a': () => this.editor.commands.selectTextblockStart(),
|
|
3116
|
+
'Ctrl-e': () => this.editor.commands.selectTextblockEnd(),
|
|
3117
|
+
};
|
|
3118
|
+
if (isiOS() || isMacOS()) {
|
|
3119
|
+
return macKeymap;
|
|
3120
|
+
}
|
|
3121
|
+
return pcKeymap;
|
|
3122
|
+
},
|
|
3123
|
+
addProseMirrorPlugins() {
|
|
3124
|
+
return [
|
|
3125
|
+
// With this plugin we check if the whole document was selected and deleted.
|
|
3126
|
+
// In this case we will additionally call `clearNodes()` to convert e.g. a heading
|
|
3127
|
+
// to a paragraph if necessary.
|
|
3128
|
+
// This is an alternative to ProseMirror's `AllSelection`, which doesn’t work well
|
|
3129
|
+
// with many other commands.
|
|
3130
|
+
new state.Plugin({
|
|
3131
|
+
key: new state.PluginKey('clearDocument'),
|
|
3132
|
+
appendTransaction: (transactions, oldState, newState) => {
|
|
3133
|
+
const docChanges = transactions.some(transaction => transaction.docChanged)
|
|
3134
|
+
&& !oldState.doc.eq(newState.doc);
|
|
3135
|
+
if (!docChanges) {
|
|
3136
|
+
return;
|
|
3137
|
+
}
|
|
3138
|
+
const { empty, from, to } = oldState.selection;
|
|
3139
|
+
const allFrom = state.Selection.atStart(oldState.doc).from;
|
|
3140
|
+
const allEnd = state.Selection.atEnd(oldState.doc).to;
|
|
3141
|
+
const allWasSelected = from === allFrom && to === allEnd;
|
|
3142
|
+
if (empty || !allWasSelected) {
|
|
3143
|
+
return;
|
|
3144
|
+
}
|
|
3145
|
+
const isEmpty = newState.doc.textBetween(0, newState.doc.content.size, ' ', ' ').length === 0;
|
|
3146
|
+
if (!isEmpty) {
|
|
3147
|
+
return;
|
|
3148
|
+
}
|
|
3149
|
+
const tr = newState.tr;
|
|
3150
|
+
const state$1 = createChainableState({
|
|
3151
|
+
state: newState,
|
|
3152
|
+
transaction: tr,
|
|
3153
|
+
});
|
|
3154
|
+
const { commands } = new CommandManager({
|
|
3155
|
+
editor: this.editor,
|
|
3156
|
+
state: state$1,
|
|
3157
|
+
});
|
|
3158
|
+
commands.clearNodes();
|
|
3159
|
+
if (!tr.steps.length) {
|
|
3160
|
+
return;
|
|
3161
|
+
}
|
|
3162
|
+
return tr;
|
|
3163
|
+
},
|
|
3164
|
+
}),
|
|
3165
|
+
];
|
|
3166
|
+
},
|
|
3167
|
+
});
|
|
3168
|
+
|
|
3169
|
+
const Tabindex = Extension.create({
|
|
3170
|
+
name: 'tabindex',
|
|
3171
|
+
addProseMirrorPlugins() {
|
|
3172
|
+
return [
|
|
3173
|
+
new state.Plugin({
|
|
3174
|
+
key: new state.PluginKey('tabindex'),
|
|
3175
|
+
props: {
|
|
3176
|
+
attributes: this.editor.isEditable ? { tabindex: '0' } : {},
|
|
3177
|
+
},
|
|
3178
|
+
}),
|
|
3179
|
+
];
|
|
3180
|
+
},
|
|
3181
|
+
});
|
|
3182
|
+
|
|
3183
|
+
var extensions = /*#__PURE__*/Object.freeze({
|
|
3184
|
+
__proto__: null,
|
|
3185
|
+
ClipboardTextSerializer: ClipboardTextSerializer,
|
|
3186
|
+
Commands: Commands,
|
|
3187
|
+
Editable: Editable,
|
|
3188
|
+
FocusEvents: FocusEvents,
|
|
3189
|
+
Keymap: Keymap,
|
|
3190
|
+
Tabindex: Tabindex
|
|
3191
|
+
});
|
|
3192
|
+
|
|
3193
|
+
const style = `.ProseMirror {
|
|
3194
|
+
position: relative;
|
|
3195
|
+
}
|
|
3196
|
+
|
|
3197
|
+
.ProseMirror {
|
|
3198
|
+
word-wrap: break-word;
|
|
3199
|
+
white-space: pre-wrap;
|
|
3200
|
+
white-space: break-spaces;
|
|
3201
|
+
-webkit-font-variant-ligatures: none;
|
|
3202
|
+
font-variant-ligatures: none;
|
|
3203
|
+
font-feature-settings: "liga" 0; /* the above doesn't seem to work in Edge */
|
|
3204
|
+
}
|
|
3205
|
+
|
|
3206
|
+
.ProseMirror [contenteditable="false"] {
|
|
3207
|
+
white-space: normal;
|
|
3208
|
+
}
|
|
3209
|
+
|
|
3210
|
+
.ProseMirror [contenteditable="false"] [contenteditable="true"] {
|
|
3211
|
+
white-space: pre-wrap;
|
|
3212
|
+
}
|
|
3213
|
+
|
|
3214
|
+
.ProseMirror pre {
|
|
3215
|
+
white-space: pre-wrap;
|
|
3216
|
+
}
|
|
3217
|
+
|
|
3218
|
+
img.ProseMirror-separator {
|
|
3219
|
+
display: inline !important;
|
|
3220
|
+
border: none !important;
|
|
3221
|
+
margin: 0 !important;
|
|
3222
|
+
width: 1px !important;
|
|
3223
|
+
height: 1px !important;
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
.ProseMirror-gapcursor {
|
|
3227
|
+
display: none;
|
|
3228
|
+
pointer-events: none;
|
|
3229
|
+
position: absolute;
|
|
3230
|
+
margin: 0;
|
|
3231
|
+
}
|
|
3232
|
+
|
|
3233
|
+
.ProseMirror-gapcursor:after {
|
|
3234
|
+
content: "";
|
|
3235
|
+
display: block;
|
|
3236
|
+
position: absolute;
|
|
3237
|
+
top: -2px;
|
|
3238
|
+
width: 20px;
|
|
3239
|
+
border-top: 1px solid black;
|
|
3240
|
+
animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite;
|
|
3241
|
+
}
|
|
3242
|
+
|
|
3243
|
+
@keyframes ProseMirror-cursor-blink {
|
|
3244
|
+
to {
|
|
3245
|
+
visibility: hidden;
|
|
3246
|
+
}
|
|
3247
|
+
}
|
|
3248
|
+
|
|
3249
|
+
.ProseMirror-hideselection *::selection {
|
|
3250
|
+
background: transparent;
|
|
3251
|
+
}
|
|
3252
|
+
|
|
3253
|
+
.ProseMirror-hideselection *::-moz-selection {
|
|
3254
|
+
background: transparent;
|
|
3255
|
+
}
|
|
3256
|
+
|
|
3257
|
+
.ProseMirror-hideselection * {
|
|
3258
|
+
caret-color: transparent;
|
|
3259
|
+
}
|
|
3260
|
+
|
|
3261
|
+
.ProseMirror-focused .ProseMirror-gapcursor {
|
|
3262
|
+
display: block;
|
|
3263
|
+
}
|
|
3264
|
+
|
|
3265
|
+
.tippy-box[data-animation=fade][data-state=hidden] {
|
|
3266
|
+
opacity: 0
|
|
3267
|
+
}`;
|
|
3268
|
+
|
|
3269
|
+
function createStyleTag(style, nonce) {
|
|
3270
|
+
const tiptapStyleTag = document.querySelector('style[data-tiptap-style]');
|
|
3271
|
+
if (tiptapStyleTag !== null) {
|
|
3272
|
+
return tiptapStyleTag;
|
|
3273
|
+
}
|
|
3274
|
+
const styleNode = document.createElement('style');
|
|
3275
|
+
if (nonce) {
|
|
3276
|
+
styleNode.setAttribute('nonce', nonce);
|
|
3277
|
+
}
|
|
3278
|
+
styleNode.setAttribute('data-tiptap-style', '');
|
|
3279
|
+
styleNode.innerHTML = style;
|
|
3280
|
+
document.getElementsByTagName('head')[0].appendChild(styleNode);
|
|
3281
|
+
return styleNode;
|
|
3282
|
+
}
|
|
3283
|
+
|
|
3284
|
+
class Editor extends EventEmitter {
|
|
3285
|
+
constructor(options = {}) {
|
|
3286
|
+
super();
|
|
3287
|
+
this.isFocused = false;
|
|
3288
|
+
this.extensionStorage = {};
|
|
3289
|
+
this.options = {
|
|
3290
|
+
element: document.createElement('div'),
|
|
3291
|
+
content: '',
|
|
3292
|
+
injectCSS: true,
|
|
3293
|
+
injectNonce: undefined,
|
|
3294
|
+
extensions: [],
|
|
3295
|
+
autofocus: false,
|
|
3296
|
+
editable: true,
|
|
3297
|
+
editorProps: {},
|
|
3298
|
+
parseOptions: {},
|
|
3299
|
+
enableInputRules: true,
|
|
3300
|
+
enablePasteRules: true,
|
|
3301
|
+
enableCoreExtensions: true,
|
|
3302
|
+
onBeforeCreate: () => null,
|
|
3303
|
+
onCreate: () => null,
|
|
3304
|
+
onUpdate: () => null,
|
|
3305
|
+
onSelectionUpdate: () => null,
|
|
3306
|
+
onTransaction: () => null,
|
|
3307
|
+
onFocus: () => null,
|
|
3308
|
+
onBlur: () => null,
|
|
3309
|
+
onDestroy: () => null,
|
|
3310
|
+
};
|
|
3311
|
+
this.isCapturingTransaction = false;
|
|
3312
|
+
this.capturedTransaction = null;
|
|
3313
|
+
this.setOptions(options);
|
|
3314
|
+
this.createExtensionManager();
|
|
3315
|
+
this.createCommandManager();
|
|
3316
|
+
this.createSchema();
|
|
3317
|
+
this.on('beforeCreate', this.options.onBeforeCreate);
|
|
3318
|
+
this.emit('beforeCreate', { editor: this });
|
|
3319
|
+
this.createView();
|
|
3320
|
+
this.injectCSS();
|
|
3321
|
+
this.on('create', this.options.onCreate);
|
|
3322
|
+
this.on('update', this.options.onUpdate);
|
|
3323
|
+
this.on('selectionUpdate', this.options.onSelectionUpdate);
|
|
3324
|
+
this.on('transaction', this.options.onTransaction);
|
|
3325
|
+
this.on('focus', this.options.onFocus);
|
|
3326
|
+
this.on('blur', this.options.onBlur);
|
|
3327
|
+
this.on('destroy', this.options.onDestroy);
|
|
3328
|
+
window.setTimeout(() => {
|
|
3329
|
+
if (this.isDestroyed) {
|
|
3330
|
+
return;
|
|
3331
|
+
}
|
|
3332
|
+
this.commands.focus(this.options.autofocus);
|
|
3333
|
+
this.emit('create', { editor: this });
|
|
3334
|
+
}, 0);
|
|
3335
|
+
}
|
|
3336
|
+
/**
|
|
3337
|
+
* Returns the editor storage.
|
|
3338
|
+
*/
|
|
3339
|
+
get storage() {
|
|
3340
|
+
return this.extensionStorage;
|
|
3341
|
+
}
|
|
3342
|
+
/**
|
|
3343
|
+
* An object of all registered commands.
|
|
3344
|
+
*/
|
|
3345
|
+
get commands() {
|
|
3346
|
+
return this.commandManager.commands;
|
|
3347
|
+
}
|
|
3348
|
+
/**
|
|
3349
|
+
* Create a command chain to call multiple commands at once.
|
|
3350
|
+
*/
|
|
3351
|
+
chain() {
|
|
3352
|
+
return this.commandManager.chain();
|
|
3353
|
+
}
|
|
3354
|
+
/**
|
|
3355
|
+
* Check if a command or a command chain can be executed. Without executing it.
|
|
3356
|
+
*/
|
|
3357
|
+
can() {
|
|
3358
|
+
return this.commandManager.can();
|
|
3359
|
+
}
|
|
3360
|
+
/**
|
|
3361
|
+
* Inject CSS styles.
|
|
3362
|
+
*/
|
|
3363
|
+
injectCSS() {
|
|
3364
|
+
if (this.options.injectCSS && document) {
|
|
3365
|
+
this.css = createStyleTag(style, this.options.injectNonce);
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
/**
|
|
3369
|
+
* Update editor options.
|
|
3370
|
+
*
|
|
3371
|
+
* @param options A list of options
|
|
3372
|
+
*/
|
|
3373
|
+
setOptions(options = {}) {
|
|
3374
|
+
this.options = {
|
|
3375
|
+
...this.options,
|
|
3376
|
+
...options,
|
|
3377
|
+
};
|
|
3378
|
+
if (!this.view || !this.state || this.isDestroyed) {
|
|
3379
|
+
return;
|
|
3380
|
+
}
|
|
3381
|
+
if (this.options.editorProps) {
|
|
3382
|
+
this.view.setProps(this.options.editorProps);
|
|
3383
|
+
}
|
|
3384
|
+
this.view.updateState(this.state);
|
|
3385
|
+
}
|
|
3386
|
+
/**
|
|
3387
|
+
* Update editable state of the editor.
|
|
3388
|
+
*/
|
|
3389
|
+
setEditable(editable, emitUpdate = true) {
|
|
3390
|
+
this.setOptions({ editable });
|
|
3391
|
+
if (emitUpdate) {
|
|
3392
|
+
this.emit('update', { editor: this, transaction: this.state.tr });
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
/**
|
|
3396
|
+
* Returns whether the editor is editable.
|
|
3397
|
+
*/
|
|
3398
|
+
get isEditable() {
|
|
3399
|
+
// since plugins are applied after creating the view
|
|
3400
|
+
// `editable` is always `true` for one tick.
|
|
3401
|
+
// that’s why we also have to check for `options.editable`
|
|
3402
|
+
return this.options.editable && this.view && this.view.editable;
|
|
3403
|
+
}
|
|
3404
|
+
/**
|
|
3405
|
+
* Returns the editor state.
|
|
3406
|
+
*/
|
|
3407
|
+
get state() {
|
|
3408
|
+
return this.view.state;
|
|
3409
|
+
}
|
|
3410
|
+
/**
|
|
3411
|
+
* Register a ProseMirror plugin.
|
|
3412
|
+
*
|
|
3413
|
+
* @param plugin A ProseMirror plugin
|
|
3414
|
+
* @param handlePlugins Control how to merge the plugin into the existing plugins.
|
|
3415
|
+
*/
|
|
3416
|
+
registerPlugin(plugin, handlePlugins) {
|
|
3417
|
+
const plugins = isFunction(handlePlugins)
|
|
3418
|
+
? handlePlugins(plugin, [...this.state.plugins])
|
|
3419
|
+
: [...this.state.plugins, plugin];
|
|
3420
|
+
const state = this.state.reconfigure({ plugins });
|
|
3421
|
+
this.view.updateState(state);
|
|
3422
|
+
}
|
|
3423
|
+
/**
|
|
3424
|
+
* Unregister a ProseMirror plugin.
|
|
3425
|
+
*
|
|
3426
|
+
* @param nameOrPluginKey The plugins name
|
|
3427
|
+
*/
|
|
3428
|
+
unregisterPlugin(nameOrPluginKey) {
|
|
3429
|
+
if (this.isDestroyed) {
|
|
3430
|
+
return;
|
|
3431
|
+
}
|
|
3432
|
+
// @ts-ignore
|
|
3433
|
+
const name = typeof nameOrPluginKey === 'string' ? `${nameOrPluginKey}$` : nameOrPluginKey.key;
|
|
3434
|
+
const state = this.state.reconfigure({
|
|
3435
|
+
// @ts-ignore
|
|
3436
|
+
plugins: this.state.plugins.filter(plugin => !plugin.key.startsWith(name)),
|
|
3437
|
+
});
|
|
3438
|
+
this.view.updateState(state);
|
|
3439
|
+
}
|
|
3440
|
+
/**
|
|
3441
|
+
* Creates an extension manager.
|
|
3442
|
+
*/
|
|
3443
|
+
createExtensionManager() {
|
|
3444
|
+
const coreExtensions = this.options.enableCoreExtensions ? Object.values(extensions) : [];
|
|
3445
|
+
const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
|
|
3446
|
+
return ['extension', 'node', 'mark'].includes(extension === null || extension === void 0 ? void 0 : extension.type);
|
|
3447
|
+
});
|
|
3448
|
+
this.extensionManager = new ExtensionManager(allExtensions, this);
|
|
3449
|
+
}
|
|
3450
|
+
/**
|
|
3451
|
+
* Creates an command manager.
|
|
3452
|
+
*/
|
|
3453
|
+
createCommandManager() {
|
|
3454
|
+
this.commandManager = new CommandManager({
|
|
3455
|
+
editor: this,
|
|
3456
|
+
});
|
|
3457
|
+
}
|
|
3458
|
+
/**
|
|
3459
|
+
* Creates a ProseMirror schema.
|
|
3460
|
+
*/
|
|
3461
|
+
createSchema() {
|
|
3462
|
+
this.schema = this.extensionManager.schema;
|
|
3463
|
+
}
|
|
3464
|
+
/**
|
|
3465
|
+
* Creates a ProseMirror view.
|
|
3466
|
+
*/
|
|
3467
|
+
createView() {
|
|
3468
|
+
const doc = createDocument(this.options.content, this.schema, this.options.parseOptions);
|
|
3469
|
+
const selection = resolveFocusPosition(doc, this.options.autofocus);
|
|
3470
|
+
this.view = new view.EditorView(this.options.element, {
|
|
3471
|
+
...this.options.editorProps,
|
|
3472
|
+
dispatchTransaction: this.dispatchTransaction.bind(this),
|
|
3473
|
+
state: state.EditorState.create({
|
|
3474
|
+
doc,
|
|
3475
|
+
selection: selection || undefined,
|
|
3476
|
+
}),
|
|
3477
|
+
});
|
|
3478
|
+
// `editor.view` is not yet available at this time.
|
|
3479
|
+
// Therefore we will add all plugins and node views directly afterwards.
|
|
3480
|
+
const newState = this.state.reconfigure({
|
|
3481
|
+
plugins: this.extensionManager.plugins,
|
|
3482
|
+
});
|
|
3483
|
+
this.view.updateState(newState);
|
|
3484
|
+
this.createNodeViews();
|
|
3485
|
+
// Let’s store the editor instance in the DOM element.
|
|
3486
|
+
// So we’ll have access to it for tests.
|
|
3487
|
+
const dom = this.view.dom;
|
|
3488
|
+
dom.editor = this;
|
|
3489
|
+
}
|
|
3490
|
+
/**
|
|
3491
|
+
* Creates all node views.
|
|
3492
|
+
*/
|
|
3493
|
+
createNodeViews() {
|
|
3494
|
+
this.view.setProps({
|
|
3495
|
+
nodeViews: this.extensionManager.nodeViews,
|
|
3496
|
+
});
|
|
3497
|
+
}
|
|
3498
|
+
captureTransaction(fn) {
|
|
3499
|
+
this.isCapturingTransaction = true;
|
|
3500
|
+
fn();
|
|
3501
|
+
this.isCapturingTransaction = false;
|
|
3502
|
+
const tr = this.capturedTransaction;
|
|
3503
|
+
this.capturedTransaction = null;
|
|
3504
|
+
return tr;
|
|
3505
|
+
}
|
|
3506
|
+
/**
|
|
3507
|
+
* The callback over which to send transactions (state updates) produced by the view.
|
|
3508
|
+
*
|
|
3509
|
+
* @param transaction An editor state transaction
|
|
3510
|
+
*/
|
|
3511
|
+
dispatchTransaction(transaction) {
|
|
3512
|
+
// if the editor / the view of the editor was destroyed
|
|
3513
|
+
// the transaction should not be dispatched as there is no view anymore.
|
|
3514
|
+
if (this.view.isDestroyed) {
|
|
3515
|
+
return;
|
|
3516
|
+
}
|
|
3517
|
+
if (this.isCapturingTransaction) {
|
|
3518
|
+
if (!this.capturedTransaction) {
|
|
3519
|
+
this.capturedTransaction = transaction;
|
|
3520
|
+
return;
|
|
3521
|
+
}
|
|
3522
|
+
transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
|
|
3523
|
+
return;
|
|
3524
|
+
}
|
|
3525
|
+
const state = this.state.apply(transaction);
|
|
3526
|
+
const selectionHasChanged = !this.state.selection.eq(state.selection);
|
|
3527
|
+
this.view.updateState(state);
|
|
3528
|
+
this.emit('transaction', {
|
|
3529
|
+
editor: this,
|
|
3530
|
+
transaction,
|
|
3531
|
+
});
|
|
3532
|
+
if (selectionHasChanged) {
|
|
3533
|
+
this.emit('selectionUpdate', {
|
|
3534
|
+
editor: this,
|
|
3535
|
+
transaction,
|
|
3536
|
+
});
|
|
3537
|
+
}
|
|
3538
|
+
const focus = transaction.getMeta('focus');
|
|
3539
|
+
const blur = transaction.getMeta('blur');
|
|
3540
|
+
if (focus) {
|
|
3541
|
+
this.emit('focus', {
|
|
3542
|
+
editor: this,
|
|
3543
|
+
event: focus.event,
|
|
3544
|
+
transaction,
|
|
3545
|
+
});
|
|
3546
|
+
}
|
|
3547
|
+
if (blur) {
|
|
3548
|
+
this.emit('blur', {
|
|
3549
|
+
editor: this,
|
|
3550
|
+
event: blur.event,
|
|
3551
|
+
transaction,
|
|
3552
|
+
});
|
|
3553
|
+
}
|
|
3554
|
+
if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
|
|
3555
|
+
return;
|
|
3556
|
+
}
|
|
3557
|
+
this.emit('update', {
|
|
3558
|
+
editor: this,
|
|
3559
|
+
transaction,
|
|
3560
|
+
});
|
|
3561
|
+
}
|
|
3562
|
+
/**
|
|
3563
|
+
* Get attributes of the currently selected node or mark.
|
|
3564
|
+
*/
|
|
3565
|
+
getAttributes(nameOrType) {
|
|
3566
|
+
return getAttributes(this.state, nameOrType);
|
|
3567
|
+
}
|
|
3568
|
+
isActive(nameOrAttributes, attributesOrUndefined) {
|
|
3569
|
+
const name = typeof nameOrAttributes === 'string' ? nameOrAttributes : null;
|
|
3570
|
+
const attributes = typeof nameOrAttributes === 'string' ? attributesOrUndefined : nameOrAttributes;
|
|
3571
|
+
return isActive(this.state, name, attributes);
|
|
3572
|
+
}
|
|
3573
|
+
/**
|
|
3574
|
+
* Get the document as JSON.
|
|
3575
|
+
*/
|
|
3576
|
+
getJSON() {
|
|
3577
|
+
return this.state.doc.toJSON();
|
|
3578
|
+
}
|
|
3579
|
+
/**
|
|
3580
|
+
* Get the document as HTML.
|
|
3581
|
+
*/
|
|
3582
|
+
getHTML() {
|
|
3583
|
+
return getHTMLFromFragment(this.state.doc.content, this.schema);
|
|
3584
|
+
}
|
|
3585
|
+
/**
|
|
3586
|
+
* Get the document as text.
|
|
3587
|
+
*/
|
|
3588
|
+
getText(options) {
|
|
3589
|
+
const { blockSeparator = '\n\n', textSerializers = {} } = options || {};
|
|
3590
|
+
return getText(this.state.doc, {
|
|
3591
|
+
blockSeparator,
|
|
3592
|
+
textSerializers: {
|
|
3593
|
+
...getTextSerializersFromSchema(this.schema),
|
|
3594
|
+
...textSerializers,
|
|
3595
|
+
},
|
|
3596
|
+
});
|
|
3597
|
+
}
|
|
3598
|
+
/**
|
|
3599
|
+
* Check if there is no content.
|
|
3600
|
+
*/
|
|
3601
|
+
get isEmpty() {
|
|
3602
|
+
return isNodeEmpty(this.state.doc);
|
|
3603
|
+
}
|
|
3604
|
+
/**
|
|
3605
|
+
* Get the number of characters for the current document.
|
|
3606
|
+
*
|
|
3607
|
+
* @deprecated
|
|
3608
|
+
*/
|
|
3609
|
+
getCharacterCount() {
|
|
3610
|
+
console.warn('[tiptap warn]: "editor.getCharacterCount()" is deprecated. Please use "editor.storage.characterCount.characters()" instead.');
|
|
3611
|
+
return this.state.doc.content.size - 2;
|
|
3612
|
+
}
|
|
3613
|
+
/**
|
|
3614
|
+
* Destroy the editor.
|
|
3615
|
+
*/
|
|
3616
|
+
destroy() {
|
|
3617
|
+
this.emit('destroy');
|
|
3618
|
+
if (this.view) {
|
|
3619
|
+
this.view.destroy();
|
|
3620
|
+
}
|
|
3621
|
+
this.removeAllListeners();
|
|
3622
|
+
}
|
|
3623
|
+
/**
|
|
3624
|
+
* Check if the editor is already destroyed.
|
|
3625
|
+
*/
|
|
3626
|
+
get isDestroyed() {
|
|
3627
|
+
var _a;
|
|
3628
|
+
// @ts-ignore
|
|
3629
|
+
return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3632
|
+
|
|
3633
|
+
/**
|
|
3634
|
+
* Build an input rule that adds a mark when the
|
|
3635
|
+
* matched text is typed into it.
|
|
3636
|
+
*/
|
|
3637
|
+
function markInputRule(config) {
|
|
3638
|
+
return new InputRule({
|
|
3639
|
+
find: config.find,
|
|
3640
|
+
handler: ({ state, range, match }) => {
|
|
3641
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match);
|
|
3642
|
+
if (attributes === false || attributes === null) {
|
|
3643
|
+
return null;
|
|
3644
|
+
}
|
|
3645
|
+
const { tr } = state;
|
|
3646
|
+
const captureGroup = match[match.length - 1];
|
|
3647
|
+
const fullMatch = match[0];
|
|
3648
|
+
let markEnd = range.to;
|
|
3649
|
+
if (captureGroup) {
|
|
3650
|
+
const startSpaces = fullMatch.search(/\S/);
|
|
3651
|
+
const textStart = range.from + fullMatch.indexOf(captureGroup);
|
|
3652
|
+
const textEnd = textStart + captureGroup.length;
|
|
3653
|
+
const excludedMarks = getMarksBetween(range.from, range.to, state.doc)
|
|
3654
|
+
.filter(item => {
|
|
3655
|
+
// @ts-ignore
|
|
3656
|
+
const excluded = item.mark.type.excluded;
|
|
3657
|
+
return excluded.find(type => type === config.type && type !== item.mark.type);
|
|
3658
|
+
})
|
|
3659
|
+
.filter(item => item.to > textStart);
|
|
3660
|
+
if (excludedMarks.length) {
|
|
3661
|
+
return null;
|
|
3662
|
+
}
|
|
3663
|
+
if (textEnd < range.to) {
|
|
3664
|
+
tr.delete(textEnd, range.to);
|
|
3665
|
+
}
|
|
3666
|
+
if (textStart > range.from) {
|
|
3667
|
+
tr.delete(range.from + startSpaces, textStart);
|
|
3668
|
+
}
|
|
3669
|
+
markEnd = range.from + startSpaces + captureGroup.length;
|
|
3670
|
+
tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}));
|
|
3671
|
+
tr.removeStoredMark(config.type);
|
|
3672
|
+
}
|
|
3673
|
+
},
|
|
3674
|
+
});
|
|
3675
|
+
}
|
|
3676
|
+
|
|
3677
|
+
/**
|
|
3678
|
+
* Build an input rule that adds a node when the
|
|
3679
|
+
* matched text is typed into it.
|
|
3680
|
+
*/
|
|
3681
|
+
function nodeInputRule(config) {
|
|
3682
|
+
return new InputRule({
|
|
3683
|
+
find: config.find,
|
|
3684
|
+
handler: ({ state, range, match }) => {
|
|
3685
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
|
|
3686
|
+
const { tr } = state;
|
|
3687
|
+
const start = range.from;
|
|
3688
|
+
let end = range.to;
|
|
3689
|
+
if (match[1]) {
|
|
3690
|
+
const offset = match[0].lastIndexOf(match[1]);
|
|
3691
|
+
let matchStart = start + offset;
|
|
3692
|
+
if (matchStart > end) {
|
|
3693
|
+
matchStart = end;
|
|
3694
|
+
}
|
|
3695
|
+
else {
|
|
3696
|
+
end = matchStart + match[1].length;
|
|
3697
|
+
}
|
|
3698
|
+
// insert last typed character
|
|
3699
|
+
const lastChar = match[0][match[0].length - 1];
|
|
3700
|
+
tr.insertText(lastChar, start + match[0].length - 1);
|
|
3701
|
+
// insert node from input rule
|
|
3702
|
+
tr.replaceWith(matchStart, end, config.type.create(attributes));
|
|
3703
|
+
}
|
|
3704
|
+
else if (match[0]) {
|
|
3705
|
+
tr.replaceWith(start, end, config.type.create(attributes));
|
|
3706
|
+
}
|
|
3707
|
+
},
|
|
3708
|
+
});
|
|
3709
|
+
}
|
|
3710
|
+
|
|
3711
|
+
/**
|
|
3712
|
+
* Build an input rule that changes the type of a textblock when the
|
|
3713
|
+
* matched text is typed into it. When using a regular expresion you’ll
|
|
3714
|
+
* probably want the regexp to start with `^`, so that the pattern can
|
|
3715
|
+
* only occur at the start of a textblock.
|
|
3716
|
+
*/
|
|
3717
|
+
function textblockTypeInputRule(config) {
|
|
3718
|
+
return new InputRule({
|
|
3719
|
+
find: config.find,
|
|
3720
|
+
handler: ({ state, range, match }) => {
|
|
3721
|
+
const $start = state.doc.resolve(range.from);
|
|
3722
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
|
|
3723
|
+
if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), config.type)) {
|
|
3724
|
+
return null;
|
|
3725
|
+
}
|
|
3726
|
+
state.tr
|
|
3727
|
+
.delete(range.from, range.to)
|
|
3728
|
+
.setBlockType(range.from, range.from, config.type, attributes);
|
|
3729
|
+
},
|
|
3730
|
+
});
|
|
3731
|
+
}
|
|
3732
|
+
|
|
3733
|
+
/**
|
|
3734
|
+
* Build an input rule that replaces text when the
|
|
3735
|
+
* matched text is typed into it.
|
|
3736
|
+
*/
|
|
3737
|
+
function textInputRule(config) {
|
|
3738
|
+
return new InputRule({
|
|
3739
|
+
find: config.find,
|
|
3740
|
+
handler: ({ state, range, match }) => {
|
|
3741
|
+
let insert = config.replace;
|
|
3742
|
+
let start = range.from;
|
|
3743
|
+
const end = range.to;
|
|
3744
|
+
if (match[1]) {
|
|
3745
|
+
const offset = match[0].lastIndexOf(match[1]);
|
|
3746
|
+
insert += match[0].slice(offset + match[1].length);
|
|
3747
|
+
start += offset;
|
|
3748
|
+
const cutOff = start - end;
|
|
3749
|
+
if (cutOff > 0) {
|
|
3750
|
+
insert = match[0].slice(offset - cutOff, offset) + insert;
|
|
3751
|
+
start = end;
|
|
3752
|
+
}
|
|
3753
|
+
}
|
|
3754
|
+
state.tr.insertText(insert, start, end);
|
|
3755
|
+
},
|
|
3756
|
+
});
|
|
3757
|
+
}
|
|
3758
|
+
|
|
3759
|
+
/**
|
|
3760
|
+
* Build an input rule for automatically wrapping a textblock when a
|
|
3761
|
+
* given string is typed. When using a regular expresion you’ll
|
|
3762
|
+
* probably want the regexp to start with `^`, so that the pattern can
|
|
3763
|
+
* only occur at the start of a textblock.
|
|
3764
|
+
*
|
|
3765
|
+
* `type` is the type of node to wrap in.
|
|
3766
|
+
*
|
|
3767
|
+
* By default, if there’s a node with the same type above the newly
|
|
3768
|
+
* wrapped node, the rule will try to join those
|
|
3769
|
+
* two nodes. You can pass a join predicate, which takes a regular
|
|
3770
|
+
* expression match and the node before the wrapped node, and can
|
|
3771
|
+
* return a boolean to indicate whether a join should happen.
|
|
3772
|
+
*/
|
|
3773
|
+
function wrappingInputRule(config) {
|
|
3774
|
+
return new InputRule({
|
|
3775
|
+
find: config.find,
|
|
3776
|
+
handler: ({ state, range, match, chain, }) => {
|
|
3777
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
|
|
3778
|
+
const tr = state.tr.delete(range.from, range.to);
|
|
3779
|
+
const $start = tr.doc.resolve(range.from);
|
|
3780
|
+
const blockRange = $start.blockRange();
|
|
3781
|
+
const wrapping = blockRange && transform.findWrapping(blockRange, config.type, attributes);
|
|
3782
|
+
if (!wrapping) {
|
|
3783
|
+
return null;
|
|
3784
|
+
}
|
|
3785
|
+
tr.wrap(blockRange, wrapping);
|
|
3786
|
+
if (config.keepMarks && config.editor) {
|
|
3787
|
+
const { selection, storedMarks } = state;
|
|
3788
|
+
const { splittableMarks } = config.editor.extensionManager;
|
|
3789
|
+
const marks = storedMarks || (selection.$to.parentOffset && selection.$from.marks());
|
|
3790
|
+
if (marks) {
|
|
3791
|
+
const filteredMarks = marks.filter(mark => splittableMarks.includes(mark.type.name));
|
|
3792
|
+
tr.ensureMarks(filteredMarks);
|
|
3793
|
+
}
|
|
3794
|
+
}
|
|
3795
|
+
if (config.keepAttributes) {
|
|
3796
|
+
/** If the nodeType is `bulletList` or `orderedList` set the `nodeType` as `listItem` */
|
|
3797
|
+
const nodeType = config.type.name === 'bulletList' || config.type.name === 'orderedList' ? 'listItem' : 'taskList';
|
|
3798
|
+
chain().updateAttributes(nodeType, attributes).run();
|
|
3799
|
+
}
|
|
3800
|
+
const before = tr.doc.resolve(range.from - 1).nodeBefore;
|
|
3801
|
+
if (before
|
|
3802
|
+
&& before.type === config.type
|
|
3803
|
+
&& transform.canJoin(tr.doc, range.from - 1)
|
|
3804
|
+
&& (!config.joinPredicate || config.joinPredicate(match, before))) {
|
|
3805
|
+
tr.join(range.from - 1);
|
|
3806
|
+
}
|
|
3807
|
+
},
|
|
3808
|
+
});
|
|
3809
|
+
}
|
|
3810
|
+
|
|
3811
|
+
class Mark {
|
|
3812
|
+
constructor(config = {}) {
|
|
3813
|
+
this.type = 'mark';
|
|
3814
|
+
this.name = 'mark';
|
|
3815
|
+
this.parent = null;
|
|
3816
|
+
this.child = null;
|
|
3817
|
+
this.config = {
|
|
3818
|
+
name: this.name,
|
|
3819
|
+
defaultOptions: {},
|
|
3820
|
+
};
|
|
3821
|
+
this.config = {
|
|
3822
|
+
...this.config,
|
|
3823
|
+
...config,
|
|
3824
|
+
};
|
|
3825
|
+
this.name = this.config.name;
|
|
3826
|
+
if (config.defaultOptions) {
|
|
3827
|
+
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
|
|
3828
|
+
}
|
|
3829
|
+
// TODO: remove `addOptions` fallback
|
|
3830
|
+
this.options = this.config.defaultOptions;
|
|
3831
|
+
if (this.config.addOptions) {
|
|
3832
|
+
this.options = callOrReturn(getExtensionField(this, 'addOptions', {
|
|
3833
|
+
name: this.name,
|
|
3834
|
+
}));
|
|
3835
|
+
}
|
|
3836
|
+
this.storage = callOrReturn(getExtensionField(this, 'addStorage', {
|
|
3837
|
+
name: this.name,
|
|
3838
|
+
options: this.options,
|
|
3839
|
+
})) || {};
|
|
3840
|
+
}
|
|
3841
|
+
static create(config = {}) {
|
|
3842
|
+
return new Mark(config);
|
|
3843
|
+
}
|
|
3844
|
+
configure(options = {}) {
|
|
3845
|
+
// return a new instance so we can use the same extension
|
|
3846
|
+
// with different calls of `configure`
|
|
3847
|
+
const extension = this.extend();
|
|
3848
|
+
extension.options = mergeDeep(this.options, options);
|
|
3849
|
+
extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
|
|
3850
|
+
name: extension.name,
|
|
3851
|
+
options: extension.options,
|
|
3852
|
+
}));
|
|
3853
|
+
return extension;
|
|
3854
|
+
}
|
|
3855
|
+
extend(extendedConfig = {}) {
|
|
3856
|
+
const extension = new Mark(extendedConfig);
|
|
3857
|
+
extension.parent = this;
|
|
3858
|
+
this.child = extension;
|
|
3859
|
+
extension.name = extendedConfig.name ? extendedConfig.name : extension.parent.name;
|
|
3860
|
+
if (extendedConfig.defaultOptions) {
|
|
3861
|
+
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`);
|
|
3862
|
+
}
|
|
3863
|
+
extension.options = callOrReturn(getExtensionField(extension, 'addOptions', {
|
|
3864
|
+
name: extension.name,
|
|
3865
|
+
}));
|
|
3866
|
+
extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
|
|
3867
|
+
name: extension.name,
|
|
3868
|
+
options: extension.options,
|
|
3869
|
+
}));
|
|
3870
|
+
return extension;
|
|
3871
|
+
}
|
|
3872
|
+
static handleExit({ editor, mark }) {
|
|
3873
|
+
const { tr } = editor.state;
|
|
3874
|
+
const currentPos = editor.state.selection.$from;
|
|
3875
|
+
const isAtEnd = currentPos.pos === currentPos.end();
|
|
3876
|
+
if (isAtEnd) {
|
|
3877
|
+
const currentMarks = currentPos.marks();
|
|
3878
|
+
const isInMark = !!currentMarks.find(m => (m === null || m === void 0 ? void 0 : m.type.name) === mark.name);
|
|
3879
|
+
if (!isInMark) {
|
|
3880
|
+
return false;
|
|
3881
|
+
}
|
|
3882
|
+
const removeMark = currentMarks.find(m => (m === null || m === void 0 ? void 0 : m.type.name) === mark.name);
|
|
3883
|
+
if (removeMark) {
|
|
3884
|
+
tr.removeStoredMark(removeMark);
|
|
3885
|
+
}
|
|
3886
|
+
tr.insertText(' ', currentPos.pos);
|
|
3887
|
+
editor.view.dispatch(tr);
|
|
3888
|
+
return true;
|
|
3889
|
+
}
|
|
3890
|
+
return false;
|
|
3891
|
+
}
|
|
3892
|
+
}
|
|
3893
|
+
|
|
3894
|
+
class Node {
|
|
3895
|
+
constructor(config = {}) {
|
|
3896
|
+
this.type = 'node';
|
|
3897
|
+
this.name = 'node';
|
|
3898
|
+
this.parent = null;
|
|
3899
|
+
this.child = null;
|
|
3900
|
+
this.config = {
|
|
3901
|
+
name: this.name,
|
|
3902
|
+
defaultOptions: {},
|
|
3903
|
+
};
|
|
3904
|
+
this.config = {
|
|
3905
|
+
...this.config,
|
|
3906
|
+
...config,
|
|
3907
|
+
};
|
|
3908
|
+
this.name = this.config.name;
|
|
3909
|
+
if (config.defaultOptions) {
|
|
3910
|
+
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
|
|
3911
|
+
}
|
|
3912
|
+
// TODO: remove `addOptions` fallback
|
|
3913
|
+
this.options = this.config.defaultOptions;
|
|
3914
|
+
if (this.config.addOptions) {
|
|
3915
|
+
this.options = callOrReturn(getExtensionField(this, 'addOptions', {
|
|
3916
|
+
name: this.name,
|
|
3917
|
+
}));
|
|
3918
|
+
}
|
|
3919
|
+
this.storage = callOrReturn(getExtensionField(this, 'addStorage', {
|
|
3920
|
+
name: this.name,
|
|
3921
|
+
options: this.options,
|
|
3922
|
+
})) || {};
|
|
3923
|
+
}
|
|
3924
|
+
static create(config = {}) {
|
|
3925
|
+
return new Node(config);
|
|
3926
|
+
}
|
|
3927
|
+
configure(options = {}) {
|
|
3928
|
+
// return a new instance so we can use the same extension
|
|
3929
|
+
// with different calls of `configure`
|
|
3930
|
+
const extension = this.extend();
|
|
3931
|
+
extension.options = mergeDeep(this.options, options);
|
|
3932
|
+
extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
|
|
3933
|
+
name: extension.name,
|
|
3934
|
+
options: extension.options,
|
|
3935
|
+
}));
|
|
3936
|
+
return extension;
|
|
3937
|
+
}
|
|
3938
|
+
extend(extendedConfig = {}) {
|
|
3939
|
+
const extension = new Node(extendedConfig);
|
|
3940
|
+
extension.parent = this;
|
|
3941
|
+
this.child = extension;
|
|
3942
|
+
extension.name = extendedConfig.name ? extendedConfig.name : extension.parent.name;
|
|
3943
|
+
if (extendedConfig.defaultOptions) {
|
|
3944
|
+
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`);
|
|
3945
|
+
}
|
|
3946
|
+
extension.options = callOrReturn(getExtensionField(extension, 'addOptions', {
|
|
3947
|
+
name: extension.name,
|
|
3948
|
+
}));
|
|
3949
|
+
extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
|
|
3950
|
+
name: extension.name,
|
|
3951
|
+
options: extension.options,
|
|
3952
|
+
}));
|
|
3953
|
+
return extension;
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
|
|
3957
|
+
class NodeView {
|
|
3958
|
+
constructor(component, props, options) {
|
|
3959
|
+
this.isDragging = false;
|
|
3960
|
+
this.component = component;
|
|
3961
|
+
this.editor = props.editor;
|
|
3962
|
+
this.options = {
|
|
3963
|
+
stopEvent: null,
|
|
3964
|
+
ignoreMutation: null,
|
|
3965
|
+
...options,
|
|
3966
|
+
};
|
|
3967
|
+
this.extension = props.extension;
|
|
3968
|
+
this.node = props.node;
|
|
3969
|
+
this.decorations = props.decorations;
|
|
3970
|
+
this.getPos = props.getPos;
|
|
3971
|
+
this.mount();
|
|
3972
|
+
}
|
|
3973
|
+
mount() {
|
|
3974
|
+
// eslint-disable-next-line
|
|
3975
|
+
return;
|
|
3976
|
+
}
|
|
3977
|
+
get dom() {
|
|
3978
|
+
return this.editor.view.dom;
|
|
3979
|
+
}
|
|
3980
|
+
get contentDOM() {
|
|
3981
|
+
return null;
|
|
3982
|
+
}
|
|
3983
|
+
onDragStart(event) {
|
|
3984
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
3985
|
+
const { view } = this.editor;
|
|
3986
|
+
const target = event.target;
|
|
3987
|
+
// get the drag handle element
|
|
3988
|
+
// `closest` is not available for text nodes so we may have to use its parent
|
|
3989
|
+
const dragHandle = target.nodeType === 3
|
|
3990
|
+
? (_a = target.parentElement) === null || _a === void 0 ? void 0 : _a.closest('[data-drag-handle]')
|
|
3991
|
+
: target.closest('[data-drag-handle]');
|
|
3992
|
+
if (!this.dom || ((_b = this.contentDOM) === null || _b === void 0 ? void 0 : _b.contains(target)) || !dragHandle) {
|
|
3993
|
+
return;
|
|
3994
|
+
}
|
|
3995
|
+
let x = 0;
|
|
3996
|
+
let y = 0;
|
|
3997
|
+
// calculate offset for drag element if we use a different drag handle element
|
|
3998
|
+
if (this.dom !== dragHandle) {
|
|
3999
|
+
const domBox = this.dom.getBoundingClientRect();
|
|
4000
|
+
const handleBox = dragHandle.getBoundingClientRect();
|
|
4001
|
+
// In React, we have to go through nativeEvent to reach offsetX/offsetY.
|
|
4002
|
+
const offsetX = (_c = event.offsetX) !== null && _c !== void 0 ? _c : (_d = event.nativeEvent) === null || _d === void 0 ? void 0 : _d.offsetX;
|
|
4003
|
+
const offsetY = (_e = event.offsetY) !== null && _e !== void 0 ? _e : (_f = event.nativeEvent) === null || _f === void 0 ? void 0 : _f.offsetY;
|
|
4004
|
+
x = handleBox.x - domBox.x + offsetX;
|
|
4005
|
+
y = handleBox.y - domBox.y + offsetY;
|
|
4006
|
+
}
|
|
4007
|
+
(_g = event.dataTransfer) === null || _g === void 0 ? void 0 : _g.setDragImage(this.dom, x, y);
|
|
4008
|
+
// we need to tell ProseMirror that we want to move the whole node
|
|
4009
|
+
// so we create a NodeSelection
|
|
4010
|
+
const selection = state.NodeSelection.create(view.state.doc, this.getPos());
|
|
4011
|
+
const transaction = view.state.tr.setSelection(selection);
|
|
4012
|
+
view.dispatch(transaction);
|
|
4013
|
+
}
|
|
4014
|
+
stopEvent(event) {
|
|
4015
|
+
var _a;
|
|
4016
|
+
if (!this.dom) {
|
|
4017
|
+
return false;
|
|
4018
|
+
}
|
|
4019
|
+
if (typeof this.options.stopEvent === 'function') {
|
|
4020
|
+
return this.options.stopEvent({ event });
|
|
4021
|
+
}
|
|
4022
|
+
const target = event.target;
|
|
4023
|
+
const isInElement = this.dom.contains(target) && !((_a = this.contentDOM) === null || _a === void 0 ? void 0 : _a.contains(target));
|
|
4024
|
+
// any event from child nodes should be handled by ProseMirror
|
|
4025
|
+
if (!isInElement) {
|
|
4026
|
+
return false;
|
|
4027
|
+
}
|
|
4028
|
+
const isDragEvent = event.type.startsWith('drag');
|
|
4029
|
+
const isDropEvent = event.type === 'drop';
|
|
4030
|
+
const isInput = ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA'].includes(target.tagName) || target.isContentEditable;
|
|
4031
|
+
// any input event within node views should be ignored by ProseMirror
|
|
4032
|
+
if (isInput && !isDropEvent && !isDragEvent) {
|
|
4033
|
+
return true;
|
|
4034
|
+
}
|
|
4035
|
+
const { isEditable } = this.editor;
|
|
4036
|
+
const { isDragging } = this;
|
|
4037
|
+
const isDraggable = !!this.node.type.spec.draggable;
|
|
4038
|
+
const isSelectable = state.NodeSelection.isSelectable(this.node);
|
|
4039
|
+
const isCopyEvent = event.type === 'copy';
|
|
4040
|
+
const isPasteEvent = event.type === 'paste';
|
|
4041
|
+
const isCutEvent = event.type === 'cut';
|
|
4042
|
+
const isClickEvent = event.type === 'mousedown';
|
|
4043
|
+
// ProseMirror tries to drag selectable nodes
|
|
4044
|
+
// even if `draggable` is set to `false`
|
|
4045
|
+
// this fix prevents that
|
|
4046
|
+
if (!isDraggable && isSelectable && isDragEvent) {
|
|
4047
|
+
event.preventDefault();
|
|
4048
|
+
}
|
|
4049
|
+
if (isDraggable && isDragEvent && !isDragging) {
|
|
4050
|
+
event.preventDefault();
|
|
4051
|
+
return false;
|
|
4052
|
+
}
|
|
4053
|
+
// we have to store that dragging started
|
|
4054
|
+
if (isDraggable && isEditable && !isDragging && isClickEvent) {
|
|
4055
|
+
const dragHandle = target.closest('[data-drag-handle]');
|
|
4056
|
+
const isValidDragHandle = dragHandle && (this.dom === dragHandle || this.dom.contains(dragHandle));
|
|
4057
|
+
if (isValidDragHandle) {
|
|
4058
|
+
this.isDragging = true;
|
|
4059
|
+
document.addEventListener('dragend', () => {
|
|
4060
|
+
this.isDragging = false;
|
|
4061
|
+
}, { once: true });
|
|
4062
|
+
document.addEventListener('drop', () => {
|
|
4063
|
+
this.isDragging = false;
|
|
4064
|
+
}, { once: true });
|
|
4065
|
+
document.addEventListener('mouseup', () => {
|
|
4066
|
+
this.isDragging = false;
|
|
4067
|
+
}, { once: true });
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
// these events are handled by prosemirror
|
|
4071
|
+
if (isDragging
|
|
4072
|
+
|| isDropEvent
|
|
4073
|
+
|| isCopyEvent
|
|
4074
|
+
|| isPasteEvent
|
|
4075
|
+
|| isCutEvent
|
|
4076
|
+
|| (isClickEvent && isSelectable)) {
|
|
4077
|
+
return false;
|
|
4078
|
+
}
|
|
4079
|
+
return true;
|
|
4080
|
+
}
|
|
4081
|
+
ignoreMutation(mutation) {
|
|
4082
|
+
if (!this.dom || !this.contentDOM) {
|
|
4083
|
+
return true;
|
|
4084
|
+
}
|
|
4085
|
+
if (typeof this.options.ignoreMutation === 'function') {
|
|
4086
|
+
return this.options.ignoreMutation({ mutation });
|
|
4087
|
+
}
|
|
4088
|
+
// a leaf/atom node is like a black box for ProseMirror
|
|
4089
|
+
// and should be fully handled by the node view
|
|
4090
|
+
if (this.node.isLeaf || this.node.isAtom) {
|
|
4091
|
+
return true;
|
|
4092
|
+
}
|
|
4093
|
+
// ProseMirror should handle any selections
|
|
4094
|
+
if (mutation.type === 'selection') {
|
|
4095
|
+
return false;
|
|
4096
|
+
}
|
|
4097
|
+
// try to prevent a bug on iOS that will break node views on enter
|
|
4098
|
+
// this is because ProseMirror can’t preventDispatch on enter
|
|
4099
|
+
// this will lead to a re-render of the node view on enter
|
|
4100
|
+
// see: https://github.com/ueberdosis/tiptap/issues/1214
|
|
4101
|
+
if (this.dom.contains(mutation.target)
|
|
4102
|
+
&& mutation.type === 'childList'
|
|
4103
|
+
&& isiOS()
|
|
4104
|
+
&& this.editor.isFocused) {
|
|
4105
|
+
const changedNodes = [
|
|
4106
|
+
...Array.from(mutation.addedNodes),
|
|
4107
|
+
...Array.from(mutation.removedNodes),
|
|
4108
|
+
];
|
|
4109
|
+
// we’ll check if every changed node is contentEditable
|
|
4110
|
+
// to make sure it’s probably mutated by ProseMirror
|
|
4111
|
+
if (changedNodes.every(node => node.isContentEditable)) {
|
|
4112
|
+
return false;
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
4115
|
+
// we will allow mutation contentDOM with attributes
|
|
4116
|
+
// so we can for example adding classes within our node view
|
|
4117
|
+
if (this.contentDOM === mutation.target && mutation.type === 'attributes') {
|
|
4118
|
+
return true;
|
|
4119
|
+
}
|
|
4120
|
+
// ProseMirror should handle any changes within contentDOM
|
|
4121
|
+
if (this.contentDOM.contains(mutation.target)) {
|
|
4122
|
+
return false;
|
|
4123
|
+
}
|
|
4124
|
+
return true;
|
|
4125
|
+
}
|
|
4126
|
+
updateAttributes(attributes) {
|
|
4127
|
+
this.editor.commands.command(({ tr }) => {
|
|
4128
|
+
const pos = this.getPos();
|
|
4129
|
+
tr.setNodeMarkup(pos, undefined, {
|
|
4130
|
+
...this.node.attrs,
|
|
4131
|
+
...attributes,
|
|
4132
|
+
});
|
|
4133
|
+
return true;
|
|
4134
|
+
});
|
|
4135
|
+
}
|
|
4136
|
+
deleteNode() {
|
|
4137
|
+
const from = this.getPos();
|
|
4138
|
+
const to = from + this.node.nodeSize;
|
|
4139
|
+
this.editor.commands.deleteRange({ from, to });
|
|
4140
|
+
}
|
|
4141
|
+
}
|
|
4142
|
+
|
|
4143
|
+
/**
|
|
4144
|
+
* Build an paste rule that adds a mark when the
|
|
4145
|
+
* matched text is pasted into it.
|
|
4146
|
+
*/
|
|
4147
|
+
function markPasteRule(config) {
|
|
4148
|
+
return new PasteRule({
|
|
4149
|
+
find: config.find,
|
|
4150
|
+
handler: ({ state, range, match }) => {
|
|
4151
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match);
|
|
4152
|
+
if (attributes === false || attributes === null) {
|
|
4153
|
+
return null;
|
|
4154
|
+
}
|
|
4155
|
+
const { tr } = state;
|
|
4156
|
+
const captureGroup = match[match.length - 1];
|
|
4157
|
+
const fullMatch = match[0];
|
|
4158
|
+
let markEnd = range.to;
|
|
4159
|
+
if (captureGroup) {
|
|
4160
|
+
const startSpaces = fullMatch.search(/\S/);
|
|
4161
|
+
const textStart = range.from + fullMatch.indexOf(captureGroup);
|
|
4162
|
+
const textEnd = textStart + captureGroup.length;
|
|
4163
|
+
const excludedMarks = getMarksBetween(range.from, range.to, state.doc)
|
|
4164
|
+
.filter(item => {
|
|
4165
|
+
// @ts-ignore
|
|
4166
|
+
const excluded = item.mark.type.excluded;
|
|
4167
|
+
return excluded.find(type => type === config.type && type !== item.mark.type);
|
|
4168
|
+
})
|
|
4169
|
+
.filter(item => item.to > textStart);
|
|
4170
|
+
if (excludedMarks.length) {
|
|
4171
|
+
return null;
|
|
4172
|
+
}
|
|
4173
|
+
if (textEnd < range.to) {
|
|
4174
|
+
tr.delete(textEnd, range.to);
|
|
4175
|
+
}
|
|
4176
|
+
if (textStart > range.from) {
|
|
4177
|
+
tr.delete(range.from + startSpaces, textStart);
|
|
4178
|
+
}
|
|
4179
|
+
markEnd = range.from + startSpaces + captureGroup.length;
|
|
4180
|
+
tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}));
|
|
4181
|
+
tr.removeStoredMark(config.type);
|
|
4182
|
+
}
|
|
4183
|
+
},
|
|
4184
|
+
});
|
|
4185
|
+
}
|
|
4186
|
+
|
|
4187
|
+
// source: https://stackoverflow.com/a/6969486
|
|
4188
|
+
function escapeForRegEx(string) {
|
|
4189
|
+
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
4190
|
+
}
|
|
4191
|
+
|
|
4192
|
+
function isString(value) {
|
|
4193
|
+
return typeof value === 'string';
|
|
4194
|
+
}
|
|
4195
|
+
|
|
4196
|
+
/**
|
|
4197
|
+
* Build an paste rule that adds a node when the
|
|
4198
|
+
* matched text is pasted into it.
|
|
4199
|
+
*/
|
|
4200
|
+
function nodePasteRule(config) {
|
|
4201
|
+
return new PasteRule({
|
|
4202
|
+
find: config.find,
|
|
4203
|
+
handler({ match, chain, range }) {
|
|
4204
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match);
|
|
4205
|
+
if (attributes === false || attributes === null) {
|
|
4206
|
+
return null;
|
|
4207
|
+
}
|
|
4208
|
+
if (match.input) {
|
|
4209
|
+
chain().deleteRange(range).insertContentAt(range.from, {
|
|
4210
|
+
type: config.type.name,
|
|
4211
|
+
attrs: attributes,
|
|
4212
|
+
});
|
|
4213
|
+
}
|
|
4214
|
+
},
|
|
4215
|
+
});
|
|
4216
|
+
}
|
|
4217
|
+
|
|
4218
|
+
/**
|
|
4219
|
+
* Build an paste rule that replaces text when the
|
|
4220
|
+
* matched text is pasted into it.
|
|
4221
|
+
*/
|
|
4222
|
+
function textPasteRule(config) {
|
|
4223
|
+
return new PasteRule({
|
|
4224
|
+
find: config.find,
|
|
4225
|
+
handler: ({ state, range, match }) => {
|
|
4226
|
+
let insert = config.replace;
|
|
4227
|
+
let start = range.from;
|
|
4228
|
+
const end = range.to;
|
|
4229
|
+
if (match[1]) {
|
|
4230
|
+
const offset = match[0].lastIndexOf(match[1]);
|
|
4231
|
+
insert += match[0].slice(offset + match[1].length);
|
|
4232
|
+
start += offset;
|
|
4233
|
+
const cutOff = start - end;
|
|
4234
|
+
if (cutOff > 0) {
|
|
4235
|
+
insert = match[0].slice(offset - cutOff, offset) + insert;
|
|
4236
|
+
start = end;
|
|
4237
|
+
}
|
|
4238
|
+
}
|
|
4239
|
+
state.tr.insertText(insert, start, end);
|
|
4240
|
+
},
|
|
4241
|
+
});
|
|
4242
|
+
}
|
|
4243
|
+
|
|
4244
|
+
class Tracker {
|
|
4245
|
+
constructor(transaction) {
|
|
4246
|
+
this.transaction = transaction;
|
|
4247
|
+
this.currentStep = this.transaction.steps.length;
|
|
4248
|
+
}
|
|
4249
|
+
map(position) {
|
|
4250
|
+
let deleted = false;
|
|
4251
|
+
const mappedPosition = this.transaction.steps
|
|
4252
|
+
.slice(this.currentStep)
|
|
4253
|
+
.reduce((newPosition, step) => {
|
|
4254
|
+
const mapResult = step.getMap().mapResult(newPosition);
|
|
4255
|
+
if (mapResult.deleted) {
|
|
4256
|
+
deleted = true;
|
|
4257
|
+
}
|
|
4258
|
+
return mapResult.pos;
|
|
4259
|
+
}, position);
|
|
4260
|
+
return {
|
|
4261
|
+
position: mappedPosition,
|
|
4262
|
+
deleted,
|
|
4263
|
+
};
|
|
4264
|
+
}
|
|
4265
|
+
}
|
|
4266
|
+
|
|
4267
|
+
exports.CommandManager = CommandManager;
|
|
4268
|
+
exports.Editor = Editor;
|
|
4269
|
+
exports.Extension = Extension;
|
|
4270
|
+
exports.InputRule = InputRule;
|
|
4271
|
+
exports.Mark = Mark;
|
|
4272
|
+
exports.Node = Node;
|
|
4273
|
+
exports.NodeView = NodeView;
|
|
4274
|
+
exports.PasteRule = PasteRule;
|
|
4275
|
+
exports.Tracker = Tracker;
|
|
4276
|
+
exports.callOrReturn = callOrReturn;
|
|
4277
|
+
exports.combineTransactionSteps = combineTransactionSteps;
|
|
4278
|
+
exports.createChainableState = createChainableState;
|
|
4279
|
+
exports.createDocument = createDocument;
|
|
4280
|
+
exports.createNodeFromContent = createNodeFromContent;
|
|
4281
|
+
exports.createStyleTag = createStyleTag;
|
|
4282
|
+
exports.defaultBlockAt = defaultBlockAt;
|
|
4283
|
+
exports.deleteProps = deleteProps;
|
|
4284
|
+
exports.elementFromString = elementFromString;
|
|
4285
|
+
exports.escapeForRegEx = escapeForRegEx;
|
|
4286
|
+
exports.extensions = extensions;
|
|
4287
|
+
exports.findChildren = findChildren;
|
|
4288
|
+
exports.findChildrenInRange = findChildrenInRange;
|
|
4289
|
+
exports.findDuplicates = findDuplicates;
|
|
4290
|
+
exports.findParentNode = findParentNode;
|
|
4291
|
+
exports.findParentNodeClosestToPos = findParentNodeClosestToPos;
|
|
4292
|
+
exports.fromString = fromString;
|
|
4293
|
+
exports.generateHTML = generateHTML;
|
|
4294
|
+
exports.generateJSON = generateJSON;
|
|
4295
|
+
exports.generateText = generateText;
|
|
4296
|
+
exports.getAttributes = getAttributes;
|
|
4297
|
+
exports.getAttributesFromExtensions = getAttributesFromExtensions;
|
|
4298
|
+
exports.getChangedRanges = getChangedRanges;
|
|
4299
|
+
exports.getDebugJSON = getDebugJSON;
|
|
4300
|
+
exports.getExtensionField = getExtensionField;
|
|
4301
|
+
exports.getHTMLFromFragment = getHTMLFromFragment;
|
|
4302
|
+
exports.getMarkAttributes = getMarkAttributes;
|
|
4303
|
+
exports.getMarkRange = getMarkRange;
|
|
4304
|
+
exports.getMarkType = getMarkType;
|
|
4305
|
+
exports.getMarksBetween = getMarksBetween;
|
|
4306
|
+
exports.getNodeAttributes = getNodeAttributes;
|
|
4307
|
+
exports.getNodeType = getNodeType;
|
|
4308
|
+
exports.getRenderedAttributes = getRenderedAttributes;
|
|
4309
|
+
exports.getSchema = getSchema;
|
|
4310
|
+
exports.getSchemaByResolvedExtensions = getSchemaByResolvedExtensions;
|
|
4311
|
+
exports.getSchemaTypeByName = getSchemaTypeByName;
|
|
4312
|
+
exports.getSchemaTypeNameByName = getSchemaTypeNameByName;
|
|
4313
|
+
exports.getSplittedAttributes = getSplittedAttributes;
|
|
4314
|
+
exports.getText = getText;
|
|
4315
|
+
exports.getTextBetween = getTextBetween;
|
|
4316
|
+
exports.getTextContentFromNodes = getTextContentFromNodes;
|
|
4317
|
+
exports.getTextSerializersFromSchema = getTextSerializersFromSchema;
|
|
4318
|
+
exports.injectExtensionAttributesToParseRule = injectExtensionAttributesToParseRule;
|
|
4319
|
+
exports.inputRulesPlugin = inputRulesPlugin;
|
|
4320
|
+
exports.isActive = isActive;
|
|
4321
|
+
exports.isEmptyObject = isEmptyObject;
|
|
4322
|
+
exports.isExtensionRulesEnabled = isExtensionRulesEnabled;
|
|
4323
|
+
exports.isFunction = isFunction;
|
|
4324
|
+
exports.isList = isList;
|
|
4325
|
+
exports.isMacOS = isMacOS;
|
|
4326
|
+
exports.isMarkActive = isMarkActive;
|
|
4327
|
+
exports.isNodeActive = isNodeActive;
|
|
4328
|
+
exports.isNodeEmpty = isNodeEmpty;
|
|
4329
|
+
exports.isNodeSelection = isNodeSelection;
|
|
4330
|
+
exports.isNumber = isNumber;
|
|
4331
|
+
exports.isPlainObject = isPlainObject;
|
|
4332
|
+
exports.isRegExp = isRegExp;
|
|
4333
|
+
exports.isString = isString;
|
|
4334
|
+
exports.isTextSelection = isTextSelection;
|
|
4335
|
+
exports.isiOS = isiOS;
|
|
4336
|
+
exports.markInputRule = markInputRule;
|
|
4337
|
+
exports.markPasteRule = markPasteRule;
|
|
4338
|
+
exports.mergeAttributes = mergeAttributes;
|
|
4339
|
+
exports.mergeDeep = mergeDeep;
|
|
4340
|
+
exports.minMax = minMax;
|
|
4341
|
+
exports.nodeInputRule = nodeInputRule;
|
|
4342
|
+
exports.nodePasteRule = nodePasteRule;
|
|
4343
|
+
exports.objectIncludes = objectIncludes;
|
|
4344
|
+
exports.pasteRulesPlugin = pasteRulesPlugin;
|
|
4345
|
+
exports.posToDOMRect = posToDOMRect;
|
|
4346
|
+
exports.removeDuplicates = removeDuplicates;
|
|
4347
|
+
exports.resolveFocusPosition = resolveFocusPosition;
|
|
4348
|
+
exports.selectionToInsertionEnd = selectionToInsertionEnd;
|
|
4349
|
+
exports.splitExtensions = splitExtensions;
|
|
4350
|
+
exports.textInputRule = textInputRule;
|
|
4351
|
+
exports.textPasteRule = textPasteRule;
|
|
4352
|
+
exports.textblockTypeInputRule = textblockTypeInputRule;
|
|
4353
|
+
exports.wrappingInputRule = wrappingInputRule;
|
|
4354
|
+
|
|
4355
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4356
|
+
|
|
4357
|
+
}));
|
|
4358
|
+
//# sourceMappingURL=index.umd.js.map
|