@tiptap/core 2.0.0-beta.21 → 2.0.0-beta.210

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.
Files changed (289) hide show
  1. package/README.md +2 -2
  2. package/dist/index.cjs +4311 -0
  3. package/dist/index.d.ts +2330 -0
  4. package/dist/index.js +4311 -0
  5. package/package.json +39 -25
  6. package/src/CommandManager.ts +76 -86
  7. package/src/Editor.ts +120 -81
  8. package/src/EventEmitter.ts +14 -4
  9. package/src/Extension.ts +280 -108
  10. package/src/ExtensionManager.ts +254 -108
  11. package/src/InputRule.ts +260 -0
  12. package/src/Mark.ts +398 -147
  13. package/src/Node.ts +437 -171
  14. package/src/NodeView.ts +132 -63
  15. package/src/PasteRule.ts +240 -0
  16. package/src/Tracker.ts +38 -0
  17. package/src/commands/blur.ts +12 -6
  18. package/src/commands/clearContent.ts +3 -3
  19. package/src/commands/clearNodes.ts +31 -19
  20. package/src/commands/command.ts +2 -2
  21. package/src/commands/createParagraphNear.ts +5 -4
  22. package/src/commands/deleteCurrentNode.ts +41 -0
  23. package/src/commands/deleteNode.ts +37 -0
  24. package/src/commands/deleteRange.ts +3 -3
  25. package/src/commands/deleteSelection.ts +5 -4
  26. package/src/commands/enter.ts +3 -3
  27. package/src/commands/exitCode.ts +5 -4
  28. package/src/commands/extendMarkRange.ts +16 -12
  29. package/src/commands/first.ts +3 -3
  30. package/src/commands/focus.ts +47 -44
  31. package/src/commands/forEach.ts +24 -0
  32. package/src/commands/index.ts +50 -0
  33. package/src/commands/insertContent.ts +17 -24
  34. package/src/commands/insertContentAt.ts +94 -0
  35. package/src/commands/join.ts +53 -0
  36. package/src/commands/keyboardShortcut.ts +6 -6
  37. package/src/commands/lift.ts +8 -7
  38. package/src/commands/liftEmptyBlock.ts +5 -4
  39. package/src/commands/liftListItem.ts +7 -6
  40. package/src/commands/newlineInCode.ts +5 -4
  41. package/src/commands/resetAttributes.ts +18 -12
  42. package/src/commands/scrollIntoView.ts +3 -3
  43. package/src/commands/selectAll.ts +8 -6
  44. package/src/commands/selectNodeBackward.ts +5 -4
  45. package/src/commands/selectNodeForward.ts +5 -4
  46. package/src/commands/selectParentNode.ts +5 -4
  47. package/src/commands/selectTextblockEnd.ts +20 -0
  48. package/src/commands/selectTextblockStart.ts +20 -0
  49. package/src/commands/setContent.ts +9 -16
  50. package/src/commands/setMark.ts +90 -14
  51. package/src/commands/setMeta.ts +18 -0
  52. package/src/commands/setNode.ts +32 -8
  53. package/src/commands/setNodeSelection.ts +27 -0
  54. package/src/commands/setTextSelection.ts +31 -0
  55. package/src/commands/sinkListItem.ts +7 -6
  56. package/src/commands/splitBlock.ts +31 -41
  57. package/src/commands/splitListItem.ts +46 -29
  58. package/src/commands/toggleList.ts +88 -20
  59. package/src/commands/toggleMark.ts +19 -8
  60. package/src/commands/toggleNode.ts +11 -6
  61. package/src/commands/toggleWrap.ts +10 -10
  62. package/src/commands/undoInputRule.ts +34 -5
  63. package/src/commands/unsetAllMarks.ts +7 -11
  64. package/src/commands/unsetMark.ts +36 -23
  65. package/src/commands/updateAttributes.ts +27 -15
  66. package/src/commands/wrapIn.ts +7 -12
  67. package/src/commands/wrapInList.ts +7 -6
  68. package/src/extensions/clipboardTextSerializer.ts +15 -36
  69. package/src/extensions/commands.ts +3 -147
  70. package/src/extensions/editable.ts +2 -1
  71. package/src/extensions/focusEvents.ts +4 -6
  72. package/src/extensions/index.ts +1 -0
  73. package/src/extensions/keymap.ts +106 -13
  74. package/src/extensions/tabindex.ts +18 -0
  75. package/src/helpers/combineTransactionSteps.ts +21 -0
  76. package/src/helpers/createChainableState.ts +38 -0
  77. package/src/helpers/createDocument.ts +5 -6
  78. package/src/helpers/createNodeFromContent.ts +20 -28
  79. package/src/helpers/defaultBlockAt.ts +13 -0
  80. package/src/helpers/findChildren.ts +18 -0
  81. package/src/helpers/findChildrenInRange.ts +36 -0
  82. package/src/helpers/findParentNode.ts +4 -3
  83. package/src/helpers/findParentNodeClosestToPos.ts +13 -7
  84. package/src/helpers/generateHTML.ts +7 -6
  85. package/src/helpers/generateJSON.ts +12 -0
  86. package/src/helpers/generateText.ts +27 -0
  87. package/src/helpers/getAttributes.ts +26 -0
  88. package/src/helpers/getAttributesFromExtensions.ts +42 -14
  89. package/src/helpers/getChangedRanges.ts +83 -0
  90. package/src/helpers/getDebugJSON.ts +54 -0
  91. package/src/helpers/getExtensionField.ts +25 -0
  92. package/src/helpers/getHTMLFromFragment.ts +5 -6
  93. package/src/helpers/getMarkAttributes.ts +18 -11
  94. package/src/helpers/getMarkRange.ts +41 -8
  95. package/src/helpers/getMarkType.ts +8 -2
  96. package/src/helpers/getMarksBetween.ts +34 -10
  97. package/src/helpers/getNodeAttributes.ts +14 -13
  98. package/src/helpers/getNodeType.ts +8 -2
  99. package/src/helpers/getRenderedAttributes.ts +9 -7
  100. package/src/helpers/getSchema.ts +7 -133
  101. package/src/helpers/getSchemaByResolvedExtensions.ts +192 -0
  102. package/src/helpers/getSchemaTypeByName.ts +3 -11
  103. package/src/helpers/getSchemaTypeNameByName.ts +2 -2
  104. package/src/helpers/getSplittedAttributes.ts +4 -4
  105. package/src/helpers/getText.ts +19 -0
  106. package/src/helpers/getTextBetween.ts +46 -0
  107. package/src/helpers/getTextContentFromNodes.ts +26 -0
  108. package/src/helpers/getTextSerializersFromSchema.ts +11 -0
  109. package/src/helpers/index.ts +33 -0
  110. package/src/helpers/injectExtensionAttributesToParseRule.ts +22 -23
  111. package/src/helpers/isActive.ts +10 -6
  112. package/src/helpers/isExtensionRulesEnabled.ts +15 -0
  113. package/src/helpers/isList.ts +14 -7
  114. package/src/helpers/isMarkActive.ts +49 -27
  115. package/src/helpers/isNodeActive.ts +29 -39
  116. package/src/helpers/isNodeEmpty.ts +2 -2
  117. package/src/helpers/isNodeSelection.ts +3 -4
  118. package/src/helpers/isTextSelection.ts +3 -4
  119. package/src/helpers/posToDOMRect.ts +35 -0
  120. package/src/helpers/resolveFocusPosition.ts +42 -0
  121. package/src/helpers/selectionToInsertionEnd.ts +3 -3
  122. package/src/helpers/splitExtensions.ts +3 -3
  123. package/src/index.ts +15 -24
  124. package/src/inputRules/index.ts +5 -0
  125. package/src/inputRules/markInputRule.ts +59 -40
  126. package/src/inputRules/nodeInputRule.ts +45 -12
  127. package/src/inputRules/textInputRule.ts +35 -0
  128. package/src/inputRules/textblockTypeInputRule.ts +37 -0
  129. package/src/inputRules/wrappingInputRule.ts +59 -0
  130. package/src/pasteRules/index.ts +3 -0
  131. package/src/pasteRules/markPasteRule.ts +61 -53
  132. package/src/pasteRules/nodePasteRule.ts +37 -0
  133. package/src/pasteRules/textPasteRule.ts +35 -0
  134. package/src/style.ts +16 -3
  135. package/src/types.ts +170 -97
  136. package/src/utilities/callOrReturn.ts +6 -3
  137. package/src/utilities/createStyleTag.ts +12 -1
  138. package/src/utilities/deleteProps.ts +2 -4
  139. package/src/utilities/elementFromString.ts +4 -5
  140. package/src/utilities/escapeForRegEx.ts +4 -0
  141. package/src/utilities/findDuplicates.ts +5 -0
  142. package/src/utilities/fromString.ts +2 -2
  143. package/src/utilities/index.ts +20 -0
  144. package/src/utilities/isEmptyObject.ts +2 -2
  145. package/src/utilities/isFunction.ts +3 -0
  146. package/src/utilities/isMacOS.ts +5 -0
  147. package/src/utilities/isNumber.ts +3 -0
  148. package/src/utilities/isPlainObject.ts +8 -5
  149. package/src/utilities/isRegExp.ts +3 -0
  150. package/src/utilities/isString.ts +3 -0
  151. package/src/utilities/isiOS.ts +12 -0
  152. package/src/utilities/mergeAttributes.ts +2 -3
  153. package/src/utilities/mergeDeep.ts +2 -3
  154. package/src/utilities/minMax.ts +1 -1
  155. package/src/utilities/objectIncludes.ts +17 -5
  156. package/src/utilities/removeDuplicates.ts +15 -0
  157. package/CHANGELOG.md +0 -365
  158. package/LICENSE.md +0 -21
  159. package/dist/packages/core/src/CommandManager.d.ts +0 -13
  160. package/dist/packages/core/src/Editor.d.ts +0 -142
  161. package/dist/packages/core/src/EventEmitter.d.ts +0 -7
  162. package/dist/packages/core/src/Extension.d.ts +0 -148
  163. package/dist/packages/core/src/ExtensionManager.d.ts +0 -24
  164. package/dist/packages/core/src/Mark.d.ts +0 -211
  165. package/dist/packages/core/src/Node.d.ts +0 -265
  166. package/dist/packages/core/src/NodeView.d.ts +0 -31
  167. package/dist/packages/core/src/commands/blur.d.ts +0 -12
  168. package/dist/packages/core/src/commands/clearContent.d.ts +0 -12
  169. package/dist/packages/core/src/commands/clearNodes.d.ts +0 -12
  170. package/dist/packages/core/src/commands/command.d.ts +0 -12
  171. package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -12
  172. package/dist/packages/core/src/commands/deleteRange.d.ts +0 -12
  173. package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -12
  174. package/dist/packages/core/src/commands/enter.d.ts +0 -12
  175. package/dist/packages/core/src/commands/exitCode.d.ts +0 -12
  176. package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -13
  177. package/dist/packages/core/src/commands/first.d.ts +0 -12
  178. package/dist/packages/core/src/commands/focus.d.ts +0 -12
  179. package/dist/packages/core/src/commands/insertContent.d.ts +0 -12
  180. package/dist/packages/core/src/commands/insertHTML.d.ts +0 -12
  181. package/dist/packages/core/src/commands/insertNode.d.ts +0 -13
  182. package/dist/packages/core/src/commands/insertText.d.ts +0 -12
  183. package/dist/packages/core/src/commands/joinBackward.d.ts +0 -12
  184. package/dist/packages/core/src/commands/joinForward.d.ts +0 -12
  185. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -12
  186. package/dist/packages/core/src/commands/lift.d.ts +0 -13
  187. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -12
  188. package/dist/packages/core/src/commands/liftListItem.d.ts +0 -13
  189. package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -12
  190. package/dist/packages/core/src/commands/replace.d.ts +0 -13
  191. package/dist/packages/core/src/commands/replaceRange.d.ts +0 -13
  192. package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -13
  193. package/dist/packages/core/src/commands/resetNodeAttributes.d.ts +0 -13
  194. package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -12
  195. package/dist/packages/core/src/commands/selectAll.d.ts +0 -12
  196. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -12
  197. package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -12
  198. package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -12
  199. package/dist/packages/core/src/commands/setContent.d.ts +0 -12
  200. package/dist/packages/core/src/commands/setMark.d.ts +0 -13
  201. package/dist/packages/core/src/commands/setNode.d.ts +0 -13
  202. package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -13
  203. package/dist/packages/core/src/commands/splitBlock.d.ts +0 -14
  204. package/dist/packages/core/src/commands/splitListItem.d.ts +0 -13
  205. package/dist/packages/core/src/commands/toggleList.d.ts +0 -13
  206. package/dist/packages/core/src/commands/toggleMark.d.ts +0 -13
  207. package/dist/packages/core/src/commands/toggleNode.d.ts +0 -13
  208. package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -13
  209. package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -12
  210. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -12
  211. package/dist/packages/core/src/commands/unsetMark.d.ts +0 -13
  212. package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -13
  213. package/dist/packages/core/src/commands/updateNodeAttributes.d.ts +0 -13
  214. package/dist/packages/core/src/commands/wrapIn.d.ts +0 -13
  215. package/dist/packages/core/src/commands/wrapInList.d.ts +0 -13
  216. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -2
  217. package/dist/packages/core/src/extensions/commands.d.ts +0 -100
  218. package/dist/packages/core/src/extensions/editable.d.ts +0 -2
  219. package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
  220. package/dist/packages/core/src/extensions/index.d.ts +0 -5
  221. package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
  222. package/dist/packages/core/src/helpers/createDocument.d.ts +0 -4
  223. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -8
  224. package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -9
  225. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -8
  226. package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -2
  227. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
  228. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
  229. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -4
  230. package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
  231. package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
  232. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
  233. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -4
  234. package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
  235. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
  236. package/dist/packages/core/src/helpers/getSchema.d.ts +0 -3
  237. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -2
  238. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -2
  239. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -2
  240. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
  241. package/dist/packages/core/src/helpers/isActive.d.ts +0 -3
  242. package/dist/packages/core/src/helpers/isList.d.ts +0 -2
  243. package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -4
  244. package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -4
  245. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -2
  246. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
  247. package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
  248. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
  249. package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
  250. package/dist/packages/core/src/index.d.ts +0 -30
  251. package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -3
  252. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -3
  253. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -3
  254. package/dist/packages/core/src/style.d.ts +0 -2
  255. package/dist/packages/core/src/types.d.ts +0 -154
  256. package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -8
  257. package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
  258. package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -7
  259. package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
  260. package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
  261. package/dist/packages/core/src/utilities/isClass.d.ts +0 -1
  262. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
  263. package/dist/packages/core/src/utilities/isObject.d.ts +0 -1
  264. package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
  265. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -2
  266. package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -2
  267. package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
  268. package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -7
  269. package/dist/packages/core/src/utilities/removeElement.d.ts +0 -1
  270. package/dist/tiptap-core.bundle.umd.min.js +0 -17
  271. package/dist/tiptap-core.bundle.umd.min.js.map +0 -1
  272. package/dist/tiptap-core.cjs.js +0 -3027
  273. package/dist/tiptap-core.cjs.js.map +0 -1
  274. package/dist/tiptap-core.esm.js +0 -3002
  275. package/dist/tiptap-core.esm.js.map +0 -1
  276. package/dist/tiptap-core.umd.js +0 -3024
  277. package/dist/tiptap-core.umd.js.map +0 -1
  278. package/src/commands/insertHTML.ts +0 -30
  279. package/src/commands/insertNode.ts +0 -33
  280. package/src/commands/insertText.ts +0 -22
  281. package/src/commands/joinBackward.ts +0 -17
  282. package/src/commands/joinForward.ts +0 -17
  283. package/src/commands/replace.ts +0 -20
  284. package/src/commands/replaceRange.ts +0 -36
  285. package/src/commands/resetNodeAttributes.ts +0 -33
  286. package/src/commands/updateNodeAttributes.ts +0 -35
  287. package/src/utilities/isClass.ts +0 -7
  288. package/src/utilities/isObject.ts +0 -10
  289. package/src/utilities/removeElement.ts +0 -5
@@ -1,3027 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var prosemirrorState = require('prosemirror-state');
6
- var prosemirrorView = require('prosemirror-view');
7
- var prosemirrorModel = require('prosemirror-model');
8
- var prosemirrorKeymap = require('prosemirror-keymap');
9
- var prosemirrorInputrules = require('prosemirror-inputrules');
10
- var prosemirrorTransform = require('prosemirror-transform');
11
- var prosemirrorCommands = require('prosemirror-commands');
12
- var prosemirrorSchemaList = require('prosemirror-schema-list');
13
-
14
- function getNodeType(nameOrType, schema) {
15
- if (typeof nameOrType === 'string') {
16
- return schema.nodes[nameOrType];
17
- }
18
- return nameOrType;
19
- }
20
-
21
- function getNodeAttributes(state, typeOrName) {
22
- const type = getNodeType(typeOrName, state.schema);
23
- const { from, to } = state.selection;
24
- let nodes = [];
25
- state.doc.nodesBetween(from, to, node => {
26
- nodes = [...nodes, node];
27
- });
28
- const node = nodes
29
- .reverse()
30
- .find(nodeItem => nodeItem.type.name === type.name);
31
- if (node) {
32
- return { ...node.attrs };
33
- }
34
- return {};
35
- }
36
-
37
- function getMarkType(nameOrType, schema) {
38
- if (typeof nameOrType === 'string') {
39
- return schema.marks[nameOrType];
40
- }
41
- return nameOrType;
42
- }
43
-
44
- function getMarkAttributes(state, typeOrName) {
45
- const type = getMarkType(typeOrName, state.schema);
46
- const { from, to, empty } = state.selection;
47
- let marks = [];
48
- if (empty) {
49
- marks = state.selection.$head.marks();
50
- }
51
- else {
52
- state.doc.nodesBetween(from, to, node => {
53
- marks = [...marks, ...node.marks];
54
- });
55
- }
56
- const mark = marks.find(markItem => markItem.type.name === type.name);
57
- if (mark) {
58
- return { ...mark.attrs };
59
- }
60
- return {};
61
- }
62
-
63
- /**
64
- * Check if object1 includes object2
65
- * @param object1 Object
66
- * @param object2 Object
67
- */
68
- function objectIncludes(object1, object2) {
69
- const keys = Object.keys(object2);
70
- if (!keys.length) {
71
- return true;
72
- }
73
- return !!keys
74
- .filter(key => object2[key] === object1[key])
75
- .length;
76
- }
77
-
78
- function isNodeActive(state, typeOrName, attributes = {}) {
79
- const { from, to, empty } = state.selection;
80
- const type = typeOrName
81
- ? getNodeType(typeOrName, state.schema)
82
- : null;
83
- let nodeRanges = [];
84
- state.doc.nodesBetween(from, to, (node, pos) => {
85
- if (!node.isText) {
86
- const relativeFrom = Math.max(from, pos);
87
- const relativeTo = Math.min(to, pos + node.nodeSize);
88
- nodeRanges = [...nodeRanges, {
89
- node,
90
- from: relativeFrom,
91
- to: relativeTo,
92
- }];
93
- }
94
- });
95
- if (empty) {
96
- return !!nodeRanges
97
- .filter(nodeRange => {
98
- if (!type) {
99
- return true;
100
- }
101
- return type.name === nodeRange.node.type.name;
102
- })
103
- .find(nodeRange => objectIncludes(nodeRange.node.attrs, attributes));
104
- }
105
- const selectionRange = to - from;
106
- const range = nodeRanges
107
- .filter(nodeRange => {
108
- if (!type) {
109
- return true;
110
- }
111
- return type.name === nodeRange.node.type.name;
112
- })
113
- .filter(nodeRange => objectIncludes(nodeRange.node.attrs, attributes))
114
- .reduce((sum, nodeRange) => {
115
- const size = nodeRange.to - nodeRange.from;
116
- return sum + size;
117
- }, 0);
118
- return range >= selectionRange;
119
- }
120
-
121
- function isMarkActive(state, typeOrName, attributes = {}) {
122
- const { from, to, empty } = state.selection;
123
- const type = typeOrName
124
- ? getMarkType(typeOrName, state.schema)
125
- : null;
126
- if (empty) {
127
- return !!(state.storedMarks || state.selection.$from.marks())
128
- .filter(mark => {
129
- if (!type) {
130
- return true;
131
- }
132
- return type.name === mark.type.name;
133
- })
134
- .find(mark => objectIncludes(mark.attrs, attributes));
135
- }
136
- let selectionRange = 0;
137
- let markRanges = [];
138
- state.doc.nodesBetween(from, to, (node, pos) => {
139
- if (node.isText) {
140
- const relativeFrom = Math.max(from, pos);
141
- const relativeTo = Math.min(to, pos + node.nodeSize);
142
- const range = relativeTo - relativeFrom;
143
- selectionRange += range;
144
- markRanges = [...markRanges, ...node.marks.map(mark => ({
145
- mark,
146
- from: relativeFrom,
147
- to: relativeTo,
148
- }))];
149
- }
150
- });
151
- if (selectionRange === 0) {
152
- return false;
153
- }
154
- const range = markRanges
155
- .filter(markRange => {
156
- if (!type) {
157
- return true;
158
- }
159
- return type.name === markRange.mark.type.name;
160
- })
161
- .filter(markRange => objectIncludes(markRange.mark.attrs, attributes))
162
- .reduce((sum, markRange) => {
163
- const size = markRange.to - markRange.from;
164
- return sum + size;
165
- }, 0);
166
- return range >= selectionRange;
167
- }
168
-
169
- function getSchemaTypeNameByName(name, schema) {
170
- if (schema.nodes[name]) {
171
- return 'node';
172
- }
173
- if (schema.marks[name]) {
174
- return 'mark';
175
- }
176
- return null;
177
- }
178
-
179
- function isActive(state, name, attributes = {}) {
180
- if (!name) {
181
- return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
182
- }
183
- const schemaType = getSchemaTypeNameByName(name, state.schema);
184
- if (schemaType === 'node') {
185
- return isNodeActive(state, name, attributes);
186
- }
187
- if (schemaType === 'mark') {
188
- return isMarkActive(state, name, attributes);
189
- }
190
- return false;
191
- }
192
-
193
- function removeElement(element) {
194
- if (element && element.parentNode) {
195
- element.parentNode.removeChild(element);
196
- }
197
- }
198
-
199
- function elementFromString(value) {
200
- const htmlString = `<div>${value}</div>`;
201
- const parser = new window.DOMParser();
202
- const element = parser.parseFromString(htmlString, 'text/html').body;
203
- return element;
204
- }
205
-
206
- function createNodeFromContent(content, schema, options) {
207
- options = {
208
- slice: true,
209
- parseOptions: {},
210
- ...options,
211
- };
212
- if (content && typeof content === 'object') {
213
- try {
214
- return schema.nodeFromJSON(content);
215
- }
216
- catch (error) {
217
- console.warn('[tiptap warn]: Invalid content.', 'Passed value:', content, 'Error:', error);
218
- return createNodeFromContent('', schema, options);
219
- }
220
- }
221
- if (typeof content === 'string') {
222
- const isHTML = content.trim().startsWith('<') && content.trim().endsWith('>');
223
- if (isHTML || !options.slice) {
224
- const parser = prosemirrorModel.DOMParser.fromSchema(schema);
225
- return options.slice
226
- ? parser.parseSlice(elementFromString(content), options.parseOptions).content
227
- : parser.parse(elementFromString(content), options.parseOptions);
228
- }
229
- return content;
230
- }
231
- return createNodeFromContent('', schema, options);
232
- }
233
-
234
- function createDocument(content, schema, parseOptions = {}) {
235
- return createNodeFromContent(content, schema, { slice: false, parseOptions });
236
- }
237
-
238
- function getHTMLFromFragment(doc, schema) {
239
- const fragment = prosemirrorModel.DOMSerializer
240
- .fromSchema(schema)
241
- .serializeFragment(doc.content);
242
- const temporaryDocument = document.implementation.createHTMLDocument();
243
- const container = temporaryDocument.createElement('div');
244
- container.appendChild(fragment);
245
- return container.innerHTML;
246
- }
247
-
248
- function isNodeEmpty(node) {
249
- var _a;
250
- const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
251
- const content = node.toJSON();
252
- return JSON.stringify(defaultContent) === JSON.stringify(content);
253
- }
254
-
255
- function createStyleTag(style) {
256
- const styleNode = document.createElement('style');
257
- styleNode.innerHTML = style;
258
- document.getElementsByTagName('head')[0].appendChild(styleNode);
259
- return styleNode;
260
- }
261
-
262
- class CommandManager {
263
- constructor(editor, commands) {
264
- this.editor = editor;
265
- this.commands = commands;
266
- }
267
- createCommands() {
268
- const { commands, editor } = this;
269
- const { state, view } = editor;
270
- const { tr } = state;
271
- const props = this.buildProps(tr);
272
- return Object.fromEntries(Object
273
- .entries(commands)
274
- .map(([name, command]) => {
275
- const method = (...args) => {
276
- const callback = command(...args)(props);
277
- if (!tr.getMeta('preventDispatch')) {
278
- view.dispatch(tr);
279
- }
280
- return callback;
281
- };
282
- return [name, method];
283
- }));
284
- }
285
- createChain(startTr, shouldDispatch = true) {
286
- const { commands, editor } = this;
287
- const { state, view } = editor;
288
- const callbacks = [];
289
- const hasStartTransaction = !!startTr;
290
- const tr = startTr || state.tr;
291
- const run = () => {
292
- if (!hasStartTransaction && shouldDispatch && !tr.getMeta('preventDispatch')) {
293
- view.dispatch(tr);
294
- }
295
- return () => callbacks.every(callback => callback === true);
296
- };
297
- const chain = {
298
- ...Object.fromEntries(Object.entries(commands).map(([name, command]) => {
299
- const chainedCommand = (...args) => {
300
- const props = this.buildProps(tr, shouldDispatch);
301
- const callback = command(...args)(props);
302
- callbacks.push(callback);
303
- return chain;
304
- };
305
- return [name, chainedCommand];
306
- })),
307
- run,
308
- };
309
- return chain;
310
- }
311
- createCan(startTr) {
312
- const { commands, editor } = this;
313
- const { state } = editor;
314
- const dispatch = undefined;
315
- const tr = startTr || state.tr;
316
- const props = this.buildProps(tr, dispatch);
317
- const formattedCommands = Object.fromEntries(Object
318
- .entries(commands)
319
- .map(([name, command]) => {
320
- return [name, (...args) => command(...args)({ ...props, dispatch })];
321
- }));
322
- return {
323
- ...formattedCommands,
324
- chain: () => this.createChain(tr, dispatch),
325
- };
326
- }
327
- buildProps(tr, shouldDispatch = true) {
328
- const { editor, commands } = this;
329
- const { state, view } = editor;
330
- if (state.storedMarks) {
331
- tr.setStoredMarks(state.storedMarks);
332
- }
333
- const props = {
334
- tr,
335
- editor,
336
- view,
337
- state: this.chainableState(tr, state),
338
- dispatch: shouldDispatch
339
- ? () => undefined
340
- : undefined,
341
- chain: () => this.createChain(tr),
342
- can: () => this.createCan(tr),
343
- get commands() {
344
- return Object.fromEntries(Object
345
- .entries(commands)
346
- .map(([name, command]) => {
347
- return [name, (...args) => command(...args)(props)];
348
- }));
349
- },
350
- };
351
- return props;
352
- }
353
- chainableState(tr, state) {
354
- let { selection } = tr;
355
- let { doc } = tr;
356
- let { storedMarks } = tr;
357
- return {
358
- ...state,
359
- schema: state.schema,
360
- plugins: state.plugins,
361
- apply: state.apply.bind(state),
362
- applyTransaction: state.applyTransaction.bind(state),
363
- reconfigure: state.reconfigure.bind(state),
364
- toJSON: state.toJSON.bind(state),
365
- get storedMarks() {
366
- return storedMarks;
367
- },
368
- get selection() {
369
- return selection;
370
- },
371
- get doc() {
372
- return doc;
373
- },
374
- get tr() {
375
- selection = tr.selection;
376
- doc = tr.doc;
377
- storedMarks = tr.storedMarks;
378
- return tr;
379
- },
380
- };
381
- }
382
- }
383
-
384
- function splitExtensions(extensions) {
385
- const baseExtensions = extensions.filter(extension => extension.type === 'extension');
386
- const nodeExtensions = extensions.filter(extension => extension.type === 'node');
387
- const markExtensions = extensions.filter(extension => extension.type === 'mark');
388
- return {
389
- baseExtensions,
390
- nodeExtensions,
391
- markExtensions,
392
- };
393
- }
394
-
395
- /**
396
- * Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`.
397
- * @param extensions List of extensions
398
- */
399
- function getAttributesFromExtensions(extensions) {
400
- const extensionAttributes = [];
401
- const { nodeExtensions, markExtensions } = splitExtensions(extensions);
402
- const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions];
403
- const defaultAttribute = {
404
- default: null,
405
- rendered: true,
406
- renderHTML: null,
407
- parseHTML: null,
408
- keepOnSplit: true,
409
- };
410
- extensions.forEach(extension => {
411
- const context = {
412
- options: extension.options,
413
- };
414
- if (!extension.config.addGlobalAttributes) {
415
- return;
416
- }
417
- const globalAttributes = extension.config.addGlobalAttributes.bind(context)();
418
- globalAttributes.forEach(globalAttribute => {
419
- globalAttribute.types.forEach(type => {
420
- Object
421
- .entries(globalAttribute.attributes)
422
- .forEach(([name, attribute]) => {
423
- extensionAttributes.push({
424
- type,
425
- name,
426
- attribute: {
427
- ...defaultAttribute,
428
- ...attribute,
429
- },
430
- });
431
- });
432
- });
433
- });
434
- });
435
- nodeAndMarkExtensions.forEach(extension => {
436
- const context = {
437
- options: extension.options,
438
- };
439
- if (!extension.config.addAttributes) {
440
- return;
441
- }
442
- const attributes = extension.config.addAttributes.bind(context)();
443
- Object
444
- .entries(attributes)
445
- .forEach(([name, attribute]) => {
446
- extensionAttributes.push({
447
- type: extension.config.name,
448
- name,
449
- attribute: {
450
- ...defaultAttribute,
451
- ...attribute,
452
- },
453
- });
454
- });
455
- });
456
- return extensionAttributes;
457
- }
458
-
459
- function mergeAttributes(...objects) {
460
- return objects
461
- .filter(item => !!item)
462
- .reduce((items, item) => {
463
- const mergedAttributes = { ...items };
464
- Object.entries(item).forEach(([key, value]) => {
465
- const exists = mergedAttributes[key];
466
- if (!exists) {
467
- mergedAttributes[key] = value;
468
- return;
469
- }
470
- if (key === 'class') {
471
- mergedAttributes[key] = [mergedAttributes[key], value].join(' ');
472
- }
473
- else if (key === 'style') {
474
- mergedAttributes[key] = [mergedAttributes[key], value].join('; ');
475
- }
476
- else {
477
- mergedAttributes[key] = value;
478
- }
479
- });
480
- return mergedAttributes;
481
- }, {});
482
- }
483
-
484
- function getRenderedAttributes(nodeOrMark, extensionAttributes) {
485
- return extensionAttributes
486
- .filter(item => item.attribute.rendered)
487
- .map(item => {
488
- if (!item.attribute.renderHTML) {
489
- return {
490
- [item.name]: nodeOrMark.attrs[item.name],
491
- };
492
- }
493
- return item.attribute.renderHTML(nodeOrMark.attrs) || {};
494
- })
495
- .reduce((attributes, attribute) => {
496
- return mergeAttributes(attributes, attribute);
497
- }, {});
498
- }
499
-
500
- function isEmptyObject(object = {}) {
501
- return Object.keys(object).length === 0 && object.constructor === Object;
502
- }
503
-
504
- function fromString(value) {
505
- if (typeof value !== 'string') {
506
- return value;
507
- }
508
- if (value.match(/^\d*(\.\d+)?$/)) {
509
- return Number(value);
510
- }
511
- if (value === 'true') {
512
- return true;
513
- }
514
- if (value === 'false') {
515
- return false;
516
- }
517
- return value;
518
- }
519
-
520
- /**
521
- * This function merges extension attributes into parserule attributes (`attrs` or `getAttrs`).
522
- * Cancels when `getAttrs` returned `false`.
523
- * @param parseRule ProseMirror ParseRule
524
- * @param extensionAttributes List of attributes to inject
525
- */
526
- function injectExtensionAttributesToParseRule(parseRule, extensionAttributes) {
527
- if (parseRule.style) {
528
- return parseRule;
529
- }
530
- return {
531
- ...parseRule,
532
- getAttrs: node => {
533
- const oldAttributes = parseRule.getAttrs
534
- ? parseRule.getAttrs(node)
535
- : parseRule.attrs;
536
- if (oldAttributes === false) {
537
- return false;
538
- }
539
- const newAttributes = extensionAttributes
540
- .filter(item => item.attribute.rendered)
541
- .reduce((items, item) => {
542
- const attributes = item.attribute.parseHTML
543
- ? item.attribute.parseHTML(node) || {}
544
- : {
545
- [item.name]: fromString(node.getAttribute(item.name)),
546
- };
547
- const filteredAttributes = Object.fromEntries(Object.entries(attributes)
548
- .filter(([, value]) => value !== undefined && value !== null));
549
- return {
550
- ...items,
551
- ...filteredAttributes,
552
- };
553
- }, {});
554
- return { ...oldAttributes, ...newAttributes };
555
- },
556
- };
557
- }
558
-
559
- /**
560
- * Optionally calls `value` as a function.
561
- * Otherwise it is returned directly.
562
- * @param value Function or any value.
563
- * @param context Optional context to bind to function.
564
- * @param props Optional props to pass to function.
565
- */
566
- function callOrReturn(value, context = undefined, ...props) {
567
- if (typeof value === 'function') {
568
- if (context) {
569
- return value.bind(context)(...props);
570
- }
571
- return value(...props);
572
- }
573
- return value;
574
- }
575
-
576
- function cleanUpSchemaItem(data) {
577
- return Object.fromEntries(Object.entries(data).filter(([key, value]) => {
578
- if (key === 'attrs' && isEmptyObject(value)) {
579
- return false;
580
- }
581
- return value !== null && value !== undefined;
582
- }));
583
- }
584
- function getSchema(extensions) {
585
- var _a;
586
- const allAttributes = getAttributesFromExtensions(extensions);
587
- const { nodeExtensions, markExtensions } = splitExtensions(extensions);
588
- const topNode = (_a = nodeExtensions.find(extension => extension.config.topNode)) === null || _a === void 0 ? void 0 : _a.config.name;
589
- const nodeSchemaExtenders = [];
590
- const markSchemaExtenders = [];
591
- extensions.forEach(extension => {
592
- if (typeof extension.config.extendNodeSchema === 'function') {
593
- nodeSchemaExtenders.push(extension.config.extendNodeSchema);
594
- }
595
- if (typeof extension.config.extendMarkSchema === 'function') {
596
- markSchemaExtenders.push(extension.config.extendMarkSchema);
597
- }
598
- });
599
- const nodes = Object.fromEntries(nodeExtensions.map(extension => {
600
- var _a;
601
- const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name);
602
- const context = { options: extension.options };
603
- const extraNodeFields = nodeSchemaExtenders.reduce((fields, nodeSchemaExtender) => {
604
- const extraFields = callOrReturn(nodeSchemaExtender, context, extension);
605
- return {
606
- ...fields,
607
- ...extraFields,
608
- };
609
- }, {});
610
- const schema = cleanUpSchemaItem({
611
- ...extraNodeFields,
612
- content: callOrReturn(extension.config.content, context),
613
- marks: callOrReturn(extension.config.marks, context),
614
- group: callOrReturn(extension.config.group, context),
615
- inline: callOrReturn(extension.config.inline, context),
616
- atom: callOrReturn(extension.config.atom, context),
617
- selectable: callOrReturn(extension.config.selectable, context),
618
- draggable: callOrReturn(extension.config.draggable, context),
619
- code: callOrReturn(extension.config.code, context),
620
- defining: callOrReturn(extension.config.defining, context),
621
- isolating: callOrReturn(extension.config.isolating, context),
622
- attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
623
- var _a;
624
- return [extensionAttribute.name, { default: (_a = extensionAttribute === null || extensionAttribute === void 0 ? void 0 : extensionAttribute.attribute) === null || _a === void 0 ? void 0 : _a.default }];
625
- })),
626
- });
627
- if (extension.config.parseHTML) {
628
- schema.parseDOM = (_a = extension.config.parseHTML
629
- .bind(context)()) === null || _a === void 0 ? void 0 : _a.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes));
630
- }
631
- if (extension.config.renderHTML) {
632
- schema.toDOM = node => {
633
- var _a;
634
- return (_a = extension.config.renderHTML) === null || _a === void 0 ? void 0 : _a.bind(context)({
635
- node,
636
- HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
637
- });
638
- };
639
- }
640
- return [extension.config.name, schema];
641
- }));
642
- const marks = Object.fromEntries(markExtensions.map(extension => {
643
- var _a;
644
- const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name);
645
- const context = { options: extension.options };
646
- const extraMarkFields = markSchemaExtenders.reduce((fields, markSchemaExtender) => {
647
- const extraFields = callOrReturn(markSchemaExtender, context, extension);
648
- return {
649
- ...fields,
650
- ...extraFields,
651
- };
652
- }, {});
653
- const schema = cleanUpSchemaItem({
654
- ...extraMarkFields,
655
- inclusive: callOrReturn(extension.config.inclusive, context),
656
- excludes: callOrReturn(extension.config.excludes, context),
657
- group: callOrReturn(extension.config.group, context),
658
- spanning: callOrReturn(extension.config.spanning, context),
659
- attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
660
- var _a;
661
- return [extensionAttribute.name, { default: (_a = extensionAttribute === null || extensionAttribute === void 0 ? void 0 : extensionAttribute.attribute) === null || _a === void 0 ? void 0 : _a.default }];
662
- })),
663
- });
664
- if (extension.config.parseHTML) {
665
- schema.parseDOM = (_a = extension.config.parseHTML
666
- .bind(context)()) === null || _a === void 0 ? void 0 : _a.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes));
667
- }
668
- if (extension.config.renderHTML) {
669
- schema.toDOM = mark => {
670
- var _a;
671
- return (_a = extension.config.renderHTML) === null || _a === void 0 ? void 0 : _a.bind(context)({
672
- mark,
673
- HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
674
- });
675
- };
676
- }
677
- return [extension.config.name, schema];
678
- }));
679
- return new prosemirrorModel.Schema({
680
- topNode,
681
- nodes,
682
- marks,
683
- });
684
- }
685
-
686
- function getSchemaTypeByName(name, schema) {
687
- if (schema.nodes[name]) {
688
- return schema.nodes[name];
689
- }
690
- if (schema.marks[name]) {
691
- return schema.marks[name];
692
- }
693
- return null;
694
- }
695
-
696
- class ExtensionManager {
697
- constructor(extensions, editor) {
698
- this.splittableMarks = [];
699
- this.editor = editor;
700
- this.extensions = this.sort(extensions);
701
- this.schema = getSchema(this.extensions);
702
- this.extensions.forEach(extension => {
703
- var _a;
704
- const context = {
705
- options: extension.options,
706
- editor: this.editor,
707
- type: getSchemaTypeByName(extension.config.name, this.schema),
708
- };
709
- if (extension.type === 'mark') {
710
- const keepOnSplit = (_a = callOrReturn(extension.config.keepOnSplit, context)) !== null && _a !== void 0 ? _a : true;
711
- if (keepOnSplit) {
712
- this.splittableMarks.push(extension.config.name);
713
- }
714
- }
715
- if (typeof extension.config.onCreate === 'function') {
716
- this.editor.on('create', extension.config.onCreate.bind(context));
717
- }
718
- if (typeof extension.config.onUpdate === 'function') {
719
- this.editor.on('update', extension.config.onUpdate.bind(context));
720
- }
721
- if (typeof extension.config.onSelectionUpdate === 'function') {
722
- this.editor.on('selectionUpdate', extension.config.onSelectionUpdate.bind(context));
723
- }
724
- if (typeof extension.config.onTransaction === 'function') {
725
- this.editor.on('transaction', extension.config.onTransaction.bind(context));
726
- }
727
- if (typeof extension.config.onFocus === 'function') {
728
- this.editor.on('focus', extension.config.onFocus.bind(context));
729
- }
730
- if (typeof extension.config.onBlur === 'function') {
731
- this.editor.on('blur', extension.config.onBlur.bind(context));
732
- }
733
- if (typeof extension.config.onDestroy === 'function') {
734
- this.editor.on('destroy', extension.config.onDestroy.bind(context));
735
- }
736
- });
737
- }
738
- sort(extensions) {
739
- const defaultPriority = 100;
740
- return extensions.sort((a, b) => {
741
- if ((a.config.priority || defaultPriority) > (b.config.priority || defaultPriority)) {
742
- return -1;
743
- }
744
- if ((a.config.priority || defaultPriority) < (b.config.priority || defaultPriority)) {
745
- return 1;
746
- }
747
- return 0;
748
- });
749
- }
750
- get commands() {
751
- return this.extensions.reduce((commands, extension) => {
752
- const context = {
753
- options: extension.options,
754
- editor: this.editor,
755
- type: getSchemaTypeByName(extension.config.name, this.schema),
756
- };
757
- if (!extension.config.addCommands) {
758
- return commands;
759
- }
760
- return {
761
- ...commands,
762
- ...extension.config.addCommands.bind(context)(),
763
- };
764
- }, {});
765
- }
766
- get plugins() {
767
- return [...this.extensions]
768
- .reverse()
769
- .map(extension => {
770
- const context = {
771
- options: extension.options,
772
- editor: this.editor,
773
- type: getSchemaTypeByName(extension.config.name, this.schema),
774
- };
775
- const plugins = [];
776
- if (extension.config.addKeyboardShortcuts) {
777
- const keyMapPlugin = prosemirrorKeymap.keymap(extension.config.addKeyboardShortcuts.bind(context)());
778
- plugins.push(keyMapPlugin);
779
- }
780
- if (this.editor.options.enableInputRules && extension.config.addInputRules) {
781
- const inputRules = extension.config.addInputRules.bind(context)();
782
- const inputRulePlugins = inputRules.length
783
- ? [prosemirrorInputrules.inputRules({ rules: inputRules })]
784
- : [];
785
- plugins.push(...inputRulePlugins);
786
- }
787
- if (this.editor.options.enablePasteRules && extension.config.addPasteRules) {
788
- const pasteRulePlugins = extension.config.addPasteRules.bind(context)();
789
- plugins.push(...pasteRulePlugins);
790
- }
791
- if (extension.config.addProseMirrorPlugins) {
792
- const proseMirrorPlugins = extension.config.addProseMirrorPlugins.bind(context)();
793
- plugins.push(...proseMirrorPlugins);
794
- }
795
- return plugins;
796
- })
797
- .flat();
798
- }
799
- get attributes() {
800
- return getAttributesFromExtensions(this.extensions);
801
- }
802
- get nodeViews() {
803
- const { editor } = this;
804
- const { nodeExtensions } = splitExtensions(this.extensions);
805
- return Object.fromEntries(nodeExtensions
806
- .filter(extension => !!extension.config.addNodeView)
807
- .map(extension => {
808
- var _a;
809
- const extensionAttributes = this.attributes.filter(attribute => attribute.type === extension.config.name);
810
- const context = {
811
- options: extension.options,
812
- editor,
813
- type: getNodeType(extension.config.name, this.schema),
814
- };
815
- const renderer = (_a = extension.config.addNodeView) === null || _a === void 0 ? void 0 : _a.call(context);
816
- const nodeview = (node, view, getPos, decorations) => {
817
- const HTMLAttributes = getRenderedAttributes(node, extensionAttributes);
818
- return renderer({
819
- editor,
820
- node,
821
- getPos,
822
- decorations,
823
- HTMLAttributes,
824
- extension,
825
- });
826
- };
827
- return [extension.config.name, nodeview];
828
- }));
829
- }
830
- get textSerializers() {
831
- const { editor } = this;
832
- const { nodeExtensions } = splitExtensions(this.extensions);
833
- return Object.fromEntries(nodeExtensions
834
- .filter(extension => !!extension.config.renderText)
835
- .map(extension => {
836
- const context = {
837
- options: extension.options,
838
- editor,
839
- type: getNodeType(extension.config.name, this.schema),
840
- };
841
- const textSerializer = (props) => { var _a; return (_a = extension.config.renderText) === null || _a === void 0 ? void 0 : _a.call(context, props); };
842
- return [extension.config.name, textSerializer];
843
- }));
844
- }
845
- }
846
-
847
- class EventEmitter {
848
- constructor() {
849
- this.callbacks = {};
850
- }
851
- on(event, fn) {
852
- if (!this.callbacks[event]) {
853
- this.callbacks[event] = [];
854
- }
855
- this.callbacks[event].push(fn);
856
- return this;
857
- }
858
- emit(event, ...args) {
859
- const callbacks = this.callbacks[event];
860
- if (callbacks) {
861
- callbacks.forEach(callback => callback.apply(this, args));
862
- }
863
- return this;
864
- }
865
- off(event, fn) {
866
- const callbacks = this.callbacks[event];
867
- if (callbacks) {
868
- if (fn) {
869
- this.callbacks[event] = callbacks.filter(callback => callback !== fn);
870
- }
871
- else {
872
- delete this.callbacks[event];
873
- }
874
- }
875
- return this;
876
- }
877
- removeAllListeners() {
878
- this.callbacks = {};
879
- }
880
- }
881
-
882
- /*! *****************************************************************************
883
- Copyright (c) Microsoft Corporation.
884
-
885
- Permission to use, copy, modify, and/or distribute this software for any
886
- purpose with or without fee is hereby granted.
887
-
888
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
889
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
890
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
891
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
892
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
893
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
894
- PERFORMANCE OF THIS SOFTWARE.
895
- ***************************************************************************** */
896
-
897
- function __classPrivateFieldGet(receiver, state, kind, f) {
898
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
899
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
900
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
901
- }
902
-
903
- // see: https://github.com/mesqueeb/is-what/blob/88d6e4ca92fb2baab6003c54e02eedf4e729e5ab/src/index.ts
904
- function getType(payload) {
905
- return Object.prototype.toString.call(payload).slice(8, -1);
906
- }
907
- function isPlainObject(payload) {
908
- if (getType(payload) !== 'Object')
909
- return false;
910
- return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype;
911
- }
912
-
913
- function mergeDeep(target, source) {
914
- const output = { ...target };
915
- if (isPlainObject(target) && isPlainObject(source)) {
916
- Object.keys(source).forEach(key => {
917
- if (isPlainObject(source[key])) {
918
- if (!(key in target)) {
919
- Object.assign(output, { [key]: source[key] });
920
- }
921
- else {
922
- output[key] = mergeDeep(target[key], source[key]);
923
- }
924
- }
925
- else {
926
- Object.assign(output, { [key]: source[key] });
927
- }
928
- });
929
- }
930
- return output;
931
- }
932
-
933
- var _configure$2;
934
- class Extension {
935
- constructor(config) {
936
- this.type = 'extension';
937
- this.config = {
938
- name: 'extension',
939
- priority: 100,
940
- defaultOptions: {},
941
- };
942
- _configure$2.set(this, (options) => {
943
- this.options = mergeDeep(this.config.defaultOptions, options);
944
- return this;
945
- });
946
- this.config = {
947
- ...this.config,
948
- ...config,
949
- };
950
- this.options = this.config.defaultOptions;
951
- }
952
- static create(config) {
953
- return new Extension(config);
954
- }
955
- configure(options = {}) {
956
- var _a;
957
- return __classPrivateFieldGet((_a = Extension
958
- .create(this.config)), _configure$2).call(_a, options);
959
- }
960
- extend(extendedConfig) {
961
- return new Extension({
962
- ...this.config,
963
- ...extendedConfig,
964
- });
965
- }
966
- }
967
- _configure$2 = new WeakMap();
968
-
969
- const textBetween = (editor, from, to, blockSeparator, leafText) => {
970
- let text = '';
971
- let separated = true;
972
- editor.state.doc.nodesBetween(from, to, (node, pos) => {
973
- var _a;
974
- const textSerializer = editor.extensionManager.textSerializers[node.type.name];
975
- if (textSerializer) {
976
- text += textSerializer({ node });
977
- separated = !blockSeparator;
978
- }
979
- else if (node.isText) {
980
- 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);
981
- separated = !blockSeparator;
982
- }
983
- else if (node.isLeaf && leafText) {
984
- text += leafText;
985
- separated = !blockSeparator;
986
- }
987
- else if (!separated && node.isBlock) {
988
- text += blockSeparator;
989
- separated = true;
990
- }
991
- }, 0);
992
- return text;
993
- };
994
- const ClipboardTextSerializer = Extension.create({
995
- name: 'editable',
996
- addProseMirrorPlugins() {
997
- return [
998
- new prosemirrorState.Plugin({
999
- key: new prosemirrorState.PluginKey('clipboardTextSerializer'),
1000
- props: {
1001
- clipboardTextSerializer: () => {
1002
- const { editor } = this;
1003
- const { from, to } = editor.state.selection;
1004
- return textBetween(editor, from, to, '\n');
1005
- },
1006
- },
1007
- }),
1008
- ];
1009
- },
1010
- });
1011
-
1012
- const blur = () => ({ view }) => {
1013
- const element = view.dom;
1014
- element.blur();
1015
- return true;
1016
- };
1017
-
1018
- var blur$1 = /*#__PURE__*/Object.freeze({
1019
- __proto__: null,
1020
- blur: blur
1021
- });
1022
-
1023
- const clearContent = (emitUpdate = false) => ({ commands }) => {
1024
- return commands.setContent('', emitUpdate);
1025
- };
1026
-
1027
- var clearContent$1 = /*#__PURE__*/Object.freeze({
1028
- __proto__: null,
1029
- clearContent: clearContent
1030
- });
1031
-
1032
- const clearNodes = () => ({ state, tr, dispatch }) => {
1033
- const { selection } = tr;
1034
- const { ranges } = selection;
1035
- ranges.forEach(range => {
1036
- state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
1037
- if (!node.type.isText) {
1038
- const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1));
1039
- const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1));
1040
- const nodeRange = fromPos.blockRange(toPos);
1041
- if (nodeRange) {
1042
- const targetLiftDepth = prosemirrorTransform.liftTarget(nodeRange);
1043
- if (node.type.isTextblock && dispatch) {
1044
- tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType);
1045
- }
1046
- if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
1047
- tr.lift(nodeRange, targetLiftDepth);
1048
- }
1049
- }
1050
- }
1051
- });
1052
- });
1053
- return true;
1054
- };
1055
-
1056
- var clearNodes$1 = /*#__PURE__*/Object.freeze({
1057
- __proto__: null,
1058
- clearNodes: clearNodes
1059
- });
1060
-
1061
- const command = fn => props => {
1062
- return fn(props);
1063
- };
1064
-
1065
- var command$1 = /*#__PURE__*/Object.freeze({
1066
- __proto__: null,
1067
- command: command
1068
- });
1069
-
1070
- const createParagraphNear = () => ({ state, dispatch }) => {
1071
- return prosemirrorCommands.createParagraphNear(state, dispatch);
1072
- };
1073
-
1074
- var createParagraphNear$1 = /*#__PURE__*/Object.freeze({
1075
- __proto__: null,
1076
- createParagraphNear: createParagraphNear
1077
- });
1078
-
1079
- const deleteRange = range => ({ tr, dispatch }) => {
1080
- const { from, to } = range;
1081
- if (dispatch) {
1082
- tr.delete(from, to);
1083
- }
1084
- return true;
1085
- };
1086
-
1087
- var deleteRange$1 = /*#__PURE__*/Object.freeze({
1088
- __proto__: null,
1089
- deleteRange: deleteRange
1090
- });
1091
-
1092
- const deleteSelection = () => ({ state, dispatch }) => {
1093
- return prosemirrorCommands.deleteSelection(state, dispatch);
1094
- };
1095
-
1096
- var deleteSelection$1 = /*#__PURE__*/Object.freeze({
1097
- __proto__: null,
1098
- deleteSelection: deleteSelection
1099
- });
1100
-
1101
- const enter = () => ({ commands }) => {
1102
- return commands.keyboardShortcut('Enter');
1103
- };
1104
-
1105
- var enter$1 = /*#__PURE__*/Object.freeze({
1106
- __proto__: null,
1107
- enter: enter
1108
- });
1109
-
1110
- const exitCode = () => ({ state, dispatch }) => {
1111
- return prosemirrorCommands.exitCode(state, dispatch);
1112
- };
1113
-
1114
- var exitCode$1 = /*#__PURE__*/Object.freeze({
1115
- __proto__: null,
1116
- exitCode: exitCode
1117
- });
1118
-
1119
- function getMarkRange($pos, type) {
1120
- if (!$pos || !type) {
1121
- return;
1122
- }
1123
- const start = $pos.parent.childAfter($pos.parentOffset);
1124
- if (!start.node) {
1125
- return;
1126
- }
1127
- const link = start.node.marks.find(mark => mark.type === type);
1128
- if (!link) {
1129
- return;
1130
- }
1131
- let startIndex = $pos.index();
1132
- let startPos = $pos.start() + start.offset;
1133
- let endIndex = startIndex + 1;
1134
- let endPos = startPos + start.node.nodeSize;
1135
- while (startIndex > 0 && link.isInSet($pos.parent.child(startIndex - 1).marks)) {
1136
- startIndex -= 1;
1137
- startPos -= $pos.parent.child(startIndex).nodeSize;
1138
- }
1139
- while (endIndex < $pos.parent.childCount && link.isInSet($pos.parent.child(endIndex).marks)) {
1140
- endPos += $pos.parent.child(endIndex).nodeSize;
1141
- endIndex += 1;
1142
- }
1143
- return {
1144
- from: startPos,
1145
- to: endPos,
1146
- };
1147
- }
1148
-
1149
- const extendMarkRange = typeOrName => ({ tr, state, dispatch }) => {
1150
- const type = getMarkType(typeOrName, state.schema);
1151
- const { doc, selection } = tr;
1152
- const { $from, empty } = selection;
1153
- if (empty && dispatch) {
1154
- const range = getMarkRange($from, type);
1155
- if (range) {
1156
- const newSelection = prosemirrorState.TextSelection.create(doc, range.from, range.to);
1157
- tr.setSelection(newSelection);
1158
- }
1159
- }
1160
- return true;
1161
- };
1162
-
1163
- var extendMarkRange$1 = /*#__PURE__*/Object.freeze({
1164
- __proto__: null,
1165
- extendMarkRange: extendMarkRange
1166
- });
1167
-
1168
- const first = commands => props => {
1169
- const items = typeof commands === 'function'
1170
- ? commands(props)
1171
- : commands;
1172
- for (let i = 0; i < items.length; i += 1) {
1173
- if (items[i](props)) {
1174
- return true;
1175
- }
1176
- }
1177
- return false;
1178
- };
1179
-
1180
- var first$1 = /*#__PURE__*/Object.freeze({
1181
- __proto__: null,
1182
- first: first
1183
- });
1184
-
1185
- function minMax(value = 0, min = 0, max = 0) {
1186
- return Math.min(Math.max(value, min), max);
1187
- }
1188
-
1189
- function isClass(item) {
1190
- var _a;
1191
- if (((_a = item.constructor) === null || _a === void 0 ? void 0 : _a.toString().substring(0, 5)) !== 'class') {
1192
- return false;
1193
- }
1194
- return true;
1195
- }
1196
-
1197
- function isObject(item) {
1198
- return (item
1199
- && typeof item === 'object'
1200
- && !Array.isArray(item)
1201
- && !isClass(item));
1202
- }
1203
-
1204
- function isTextSelection(value) {
1205
- return isObject(value) && value instanceof prosemirrorState.TextSelection;
1206
- }
1207
-
1208
- function resolveSelection(state, position = null) {
1209
- if (!position) {
1210
- return null;
1211
- }
1212
- if (position === 'start' || position === true) {
1213
- return {
1214
- from: 0,
1215
- to: 0,
1216
- };
1217
- }
1218
- if (position === 'end') {
1219
- const { size } = state.doc.content;
1220
- return {
1221
- from: size,
1222
- to: size,
1223
- };
1224
- }
1225
- return {
1226
- from: position,
1227
- to: position,
1228
- };
1229
- }
1230
- const focus = (position = null) => ({ editor, view, tr, dispatch, }) => {
1231
- if ((view.hasFocus() && position === null) || position === false) {
1232
- return true;
1233
- }
1234
- // we don’t try to resolve a NodeSelection or CellSelection
1235
- if (dispatch && position === null && !isTextSelection(editor.state.selection)) {
1236
- view.focus();
1237
- return true;
1238
- }
1239
- const { from, to } = resolveSelection(editor.state, position) || editor.state.selection;
1240
- const { doc, storedMarks } = tr;
1241
- const resolvedFrom = minMax(from, 0, doc.content.size);
1242
- const resolvedEnd = minMax(to, 0, doc.content.size);
1243
- const selection = prosemirrorState.TextSelection.create(doc, resolvedFrom, resolvedEnd);
1244
- const isSameSelection = editor.state.selection.eq(selection);
1245
- if (dispatch) {
1246
- tr.setSelection(selection);
1247
- // `tr.setSelection` resets the stored marks
1248
- // so we’ll restore them if the selection is the same as before
1249
- if (isSameSelection && storedMarks) {
1250
- tr.setStoredMarks(storedMarks);
1251
- }
1252
- view.focus();
1253
- }
1254
- return true;
1255
- };
1256
-
1257
- var focus$1 = /*#__PURE__*/Object.freeze({
1258
- __proto__: null,
1259
- focus: focus
1260
- });
1261
-
1262
- // source: https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
1263
- function selectionToInsertionEnd(tr, startLen, bias) {
1264
- const last = tr.steps.length - 1;
1265
- if (last < startLen) {
1266
- return;
1267
- }
1268
- const step = tr.steps[last];
1269
- if (!(step instanceof prosemirrorTransform.ReplaceStep || step instanceof prosemirrorTransform.ReplaceAroundStep)) {
1270
- return;
1271
- }
1272
- const map = tr.mapping.maps[last];
1273
- let end = 0;
1274
- map.forEach((_from, _to, _newFrom, newTo) => {
1275
- if (end === 0) {
1276
- end = newTo;
1277
- }
1278
- });
1279
- tr.setSelection(prosemirrorState.Selection.near(tr.doc.resolve(end), bias));
1280
- }
1281
-
1282
- const insertContent = value => ({ tr, dispatch, editor }) => {
1283
- if (dispatch) {
1284
- const content = createNodeFromContent(value, editor.schema);
1285
- if (typeof content === 'string') {
1286
- tr.insertText(content);
1287
- return true;
1288
- }
1289
- if (!tr.selection.empty) {
1290
- tr.deleteSelection();
1291
- }
1292
- tr.insert(tr.selection.anchor, content);
1293
- selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
1294
- }
1295
- return true;
1296
- };
1297
-
1298
- var insertContent$1 = /*#__PURE__*/Object.freeze({
1299
- __proto__: null,
1300
- insertContent: insertContent
1301
- });
1302
-
1303
- const insertHTML = value => ({ tr, state, dispatch }) => {
1304
- console.warn('[tiptap warn]: insertHTML() is deprecated. please use insertContent() instead.');
1305
- const { selection } = tr;
1306
- const element = elementFromString(value);
1307
- const slice = prosemirrorModel.DOMParser.fromSchema(state.schema).parseSlice(element);
1308
- if (dispatch) {
1309
- tr.insert(selection.anchor, slice.content);
1310
- selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
1311
- }
1312
- return true;
1313
- };
1314
-
1315
- var insertHTML$1 = /*#__PURE__*/Object.freeze({
1316
- __proto__: null,
1317
- insertHTML: insertHTML
1318
- });
1319
-
1320
- const insertNode = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1321
- console.warn('[tiptap warn]: insertNode() is deprecated. please use insertContent() instead.');
1322
- const { selection } = tr;
1323
- const type = getNodeType(typeOrName, state.schema);
1324
- if (!type) {
1325
- return false;
1326
- }
1327
- const node = type.create(attributes);
1328
- if (dispatch) {
1329
- tr.insert(selection.anchor, node);
1330
- }
1331
- return true;
1332
- };
1333
-
1334
- var insertNode$1 = /*#__PURE__*/Object.freeze({
1335
- __proto__: null,
1336
- insertNode: insertNode
1337
- });
1338
-
1339
- const insertText = value => ({ tr, dispatch }) => {
1340
- console.warn('[tiptap warn]: insertText() is deprecated. please use insertContent() instead.');
1341
- if (dispatch) {
1342
- tr.insertText(value);
1343
- }
1344
- return true;
1345
- };
1346
-
1347
- var insertText$1 = /*#__PURE__*/Object.freeze({
1348
- __proto__: null,
1349
- insertText: insertText
1350
- });
1351
-
1352
- const joinBackward = () => ({ state, dispatch }) => {
1353
- return prosemirrorCommands.joinBackward(state, dispatch);
1354
- };
1355
-
1356
- var joinBackward$1 = /*#__PURE__*/Object.freeze({
1357
- __proto__: null,
1358
- joinBackward: joinBackward
1359
- });
1360
-
1361
- const joinForward = () => ({ state, dispatch }) => {
1362
- return prosemirrorCommands.joinForward(state, dispatch);
1363
- };
1364
-
1365
- var joinForward$1 = /*#__PURE__*/Object.freeze({
1366
- __proto__: null,
1367
- joinForward: joinForward
1368
- });
1369
-
1370
- const mac = typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false;
1371
- function normalizeKeyName(name) {
1372
- const parts = name.split(/-(?!$)/);
1373
- let result = parts[parts.length - 1];
1374
- if (result === 'Space') {
1375
- result = ' ';
1376
- }
1377
- let alt;
1378
- let ctrl;
1379
- let shift;
1380
- let meta;
1381
- for (let i = 0; i < parts.length - 1; i += 1) {
1382
- const mod = parts[i];
1383
- if (/^(cmd|meta|m)$/i.test(mod)) {
1384
- meta = true;
1385
- }
1386
- else if (/^a(lt)?$/i.test(mod)) {
1387
- alt = true;
1388
- }
1389
- else if (/^(c|ctrl|control)$/i.test(mod)) {
1390
- ctrl = true;
1391
- }
1392
- else if (/^s(hift)?$/i.test(mod)) {
1393
- shift = true;
1394
- }
1395
- else if (/^mod$/i.test(mod)) {
1396
- if (mac) {
1397
- meta = true;
1398
- }
1399
- else {
1400
- ctrl = true;
1401
- }
1402
- }
1403
- else {
1404
- throw new Error(`Unrecognized modifier name: ${mod}`);
1405
- }
1406
- }
1407
- if (alt) {
1408
- result = `Alt-${result}`;
1409
- }
1410
- if (ctrl) {
1411
- result = `Ctrl-${result}`;
1412
- }
1413
- if (meta) {
1414
- result = `Meta-${result}`;
1415
- }
1416
- if (shift) {
1417
- result = `Shift-${result}`;
1418
- }
1419
- return result;
1420
- }
1421
- const keyboardShortcut = name => ({ editor, view, tr, dispatch, }) => {
1422
- const keys = normalizeKeyName(name).split(/-(?!$)/);
1423
- const key = keys.find(item => !['Alt', 'Ctrl', 'Meta', 'Shift'].includes(item));
1424
- const event = new KeyboardEvent('keydown', {
1425
- key: key === 'Space'
1426
- ? ' '
1427
- : key,
1428
- altKey: keys.includes('Alt'),
1429
- ctrlKey: keys.includes('Ctrl'),
1430
- metaKey: keys.includes('Meta'),
1431
- shiftKey: keys.includes('Shift'),
1432
- bubbles: true,
1433
- cancelable: true,
1434
- });
1435
- const capturedTransaction = editor.captureTransaction(() => {
1436
- view.someProp('handleKeyDown', f => f(view, event));
1437
- });
1438
- capturedTransaction === null || capturedTransaction === void 0 ? void 0 : capturedTransaction.steps.forEach(step => {
1439
- const newStep = step.map(tr.mapping);
1440
- if (newStep && dispatch) {
1441
- tr.maybeStep(newStep);
1442
- }
1443
- });
1444
- return true;
1445
- };
1446
-
1447
- var keyboardShortcut$1 = /*#__PURE__*/Object.freeze({
1448
- __proto__: null,
1449
- keyboardShortcut: keyboardShortcut
1450
- });
1451
-
1452
- const lift = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1453
- const type = getNodeType(typeOrName, state.schema);
1454
- const isActive = isNodeActive(state, type, attributes);
1455
- if (!isActive) {
1456
- return false;
1457
- }
1458
- return prosemirrorCommands.lift(state, dispatch);
1459
- };
1460
-
1461
- var lift$1 = /*#__PURE__*/Object.freeze({
1462
- __proto__: null,
1463
- lift: lift
1464
- });
1465
-
1466
- const liftEmptyBlock = () => ({ state, dispatch }) => {
1467
- return prosemirrorCommands.liftEmptyBlock(state, dispatch);
1468
- };
1469
-
1470
- var liftEmptyBlock$1 = /*#__PURE__*/Object.freeze({
1471
- __proto__: null,
1472
- liftEmptyBlock: liftEmptyBlock
1473
- });
1474
-
1475
- const liftListItem = typeOrName => ({ state, dispatch }) => {
1476
- const type = getNodeType(typeOrName, state.schema);
1477
- return prosemirrorSchemaList.liftListItem(type)(state, dispatch);
1478
- };
1479
-
1480
- var liftListItem$1 = /*#__PURE__*/Object.freeze({
1481
- __proto__: null,
1482
- liftListItem: liftListItem
1483
- });
1484
-
1485
- const newlineInCode = () => ({ state, dispatch }) => {
1486
- return prosemirrorCommands.newlineInCode(state, dispatch);
1487
- };
1488
-
1489
- var newlineInCode$1 = /*#__PURE__*/Object.freeze({
1490
- __proto__: null,
1491
- newlineInCode: newlineInCode
1492
- });
1493
-
1494
- const replace = (typeOrName, attributes = {}) => ({ state, commands }) => {
1495
- const { from, to } = state.selection;
1496
- const range = { from, to };
1497
- return commands.replaceRange(range, typeOrName, attributes);
1498
- };
1499
-
1500
- var replace$1 = /*#__PURE__*/Object.freeze({
1501
- __proto__: null,
1502
- replace: replace
1503
- });
1504
-
1505
- const replaceRange = (range, typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1506
- const type = getNodeType(typeOrName, state.schema);
1507
- const { from, to } = range;
1508
- const $from = tr.doc.resolve(from);
1509
- const index = $from.index();
1510
- if (!$from.parent.canReplaceWith(index, index, type)) {
1511
- return false;
1512
- }
1513
- if (dispatch) {
1514
- tr.replaceWith(from, to, type.create(attributes));
1515
- }
1516
- return true;
1517
- };
1518
-
1519
- var replaceRange$1 = /*#__PURE__*/Object.freeze({
1520
- __proto__: null,
1521
- replaceRange: replaceRange
1522
- });
1523
-
1524
- /**
1525
- * Remove a property or an array of properties from an object
1526
- * @param obj Object
1527
- * @param key Key to remove
1528
- */
1529
- function deleteProps(obj, propOrProps) {
1530
- const props = typeof propOrProps === 'string'
1531
- ? [propOrProps]
1532
- : propOrProps;
1533
- return Object
1534
- .keys(obj)
1535
- .reduce((newObj, prop) => {
1536
- if (!props.includes(prop)) {
1537
- newObj[prop] = obj[prop];
1538
- }
1539
- return newObj;
1540
- }, {});
1541
- }
1542
-
1543
- const resetAttributes = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
1544
- let nodeType = null;
1545
- let markType = null;
1546
- const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
1547
- ? typeOrName
1548
- : typeOrName.name, state.schema);
1549
- if (!schemaType) {
1550
- return false;
1551
- }
1552
- if (schemaType === 'node') {
1553
- nodeType = getNodeType(typeOrName, state.schema);
1554
- }
1555
- if (schemaType === 'mark') {
1556
- markType = getMarkType(typeOrName, state.schema);
1557
- }
1558
- if (dispatch) {
1559
- tr.selection.ranges.forEach(range => {
1560
- state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
1561
- if (nodeType && nodeType === node.type) {
1562
- tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes));
1563
- }
1564
- if (markType && node.marks.length) {
1565
- node.marks.forEach(mark => {
1566
- if (markType === mark.type) {
1567
- tr.addMark(pos, pos + node.nodeSize, markType.create(deleteProps(mark.attrs, attributes)));
1568
- }
1569
- });
1570
- }
1571
- });
1572
- });
1573
- }
1574
- return true;
1575
- };
1576
-
1577
- var resetAttributes$1 = /*#__PURE__*/Object.freeze({
1578
- __proto__: null,
1579
- resetAttributes: resetAttributes
1580
- });
1581
-
1582
- const resetNodeAttributes = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
1583
- console.warn('[tiptap warn]: resetNodeAttributes() is deprecated. please use resetAttributes() instead.');
1584
- const type = getNodeType(typeOrName, state.schema);
1585
- const { selection } = tr;
1586
- const { ranges } = selection;
1587
- ranges.forEach(range => {
1588
- state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
1589
- if (node.type === type && dispatch) {
1590
- tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes));
1591
- }
1592
- });
1593
- });
1594
- return true;
1595
- };
1596
-
1597
- var resetNodeAttributes$1 = /*#__PURE__*/Object.freeze({
1598
- __proto__: null,
1599
- resetNodeAttributes: resetNodeAttributes
1600
- });
1601
-
1602
- const scrollIntoView = () => ({ tr, dispatch }) => {
1603
- if (dispatch) {
1604
- tr.scrollIntoView();
1605
- }
1606
- return true;
1607
- };
1608
-
1609
- var scrollIntoView$1 = /*#__PURE__*/Object.freeze({
1610
- __proto__: null,
1611
- scrollIntoView: scrollIntoView
1612
- });
1613
-
1614
- const selectAll = () => ({ state, dispatch }) => {
1615
- return prosemirrorCommands.selectAll(state, dispatch);
1616
- };
1617
-
1618
- var selectAll$1 = /*#__PURE__*/Object.freeze({
1619
- __proto__: null,
1620
- selectAll: selectAll
1621
- });
1622
-
1623
- const selectNodeBackward = () => ({ state, dispatch }) => {
1624
- return prosemirrorCommands.selectNodeBackward(state, dispatch);
1625
- };
1626
-
1627
- var selectNodeBackward$1 = /*#__PURE__*/Object.freeze({
1628
- __proto__: null,
1629
- selectNodeBackward: selectNodeBackward
1630
- });
1631
-
1632
- const selectNodeForward = () => ({ state, dispatch }) => {
1633
- return prosemirrorCommands.selectNodeForward(state, dispatch);
1634
- };
1635
-
1636
- var selectNodeForward$1 = /*#__PURE__*/Object.freeze({
1637
- __proto__: null,
1638
- selectNodeForward: selectNodeForward
1639
- });
1640
-
1641
- const selectParentNode = () => ({ state, dispatch }) => {
1642
- return prosemirrorCommands.selectParentNode(state, dispatch);
1643
- };
1644
-
1645
- var selectParentNode$1 = /*#__PURE__*/Object.freeze({
1646
- __proto__: null,
1647
- selectParentNode: selectParentNode
1648
- });
1649
-
1650
- const setContent = (content, emitUpdate = false, parseOptions = {}) => ({ tr, editor, dispatch }) => {
1651
- const { doc } = tr;
1652
- const document = createDocument(content, editor.schema, parseOptions);
1653
- const selection = prosemirrorState.TextSelection.create(doc, 0, doc.content.size);
1654
- if (dispatch) {
1655
- tr.setSelection(selection)
1656
- .replaceSelectionWith(document, false)
1657
- .setMeta('preventUpdate', !emitUpdate);
1658
- }
1659
- return true;
1660
- };
1661
-
1662
- var setContent$1 = /*#__PURE__*/Object.freeze({
1663
- __proto__: null,
1664
- setContent: setContent
1665
- });
1666
-
1667
- const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1668
- const { selection } = tr;
1669
- const { empty, ranges } = selection;
1670
- const type = getMarkType(typeOrName, state.schema);
1671
- const oldAttributes = getMarkAttributes(state, type);
1672
- const newAttributes = {
1673
- ...oldAttributes,
1674
- ...attributes,
1675
- };
1676
- if (dispatch) {
1677
- if (empty) {
1678
- tr.addStoredMark(type.create(newAttributes));
1679
- }
1680
- else {
1681
- ranges.forEach(range => {
1682
- tr.addMark(range.$from.pos, range.$to.pos, type.create(newAttributes));
1683
- });
1684
- }
1685
- }
1686
- return true;
1687
- };
1688
-
1689
- var setMark$1 = /*#__PURE__*/Object.freeze({
1690
- __proto__: null,
1691
- setMark: setMark
1692
- });
1693
-
1694
- const setNode = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1695
- const type = getNodeType(typeOrName, state.schema);
1696
- return prosemirrorCommands.setBlockType(type, attributes)(state, dispatch);
1697
- };
1698
-
1699
- var setNode$1 = /*#__PURE__*/Object.freeze({
1700
- __proto__: null,
1701
- setNode: setNode
1702
- });
1703
-
1704
- const sinkListItem = typeOrName => ({ state, dispatch }) => {
1705
- const type = getNodeType(typeOrName, state.schema);
1706
- return prosemirrorSchemaList.sinkListItem(type)(state, dispatch);
1707
- };
1708
-
1709
- var sinkListItem$1 = /*#__PURE__*/Object.freeze({
1710
- __proto__: null,
1711
- sinkListItem: sinkListItem
1712
- });
1713
-
1714
- function getSplittedAttributes(extensionAttributes, typeName, attributes) {
1715
- return Object.fromEntries(Object
1716
- .entries(attributes)
1717
- .filter(([name]) => {
1718
- const extensionAttribute = extensionAttributes.find(item => {
1719
- return item.type === typeName && item.name === name;
1720
- });
1721
- if (!extensionAttribute) {
1722
- return false;
1723
- }
1724
- return extensionAttribute.attribute.keepOnSplit;
1725
- }));
1726
- }
1727
-
1728
- function defaultBlockAt(match) {
1729
- for (let i = 0; i < match.edgeCount; i + 1) {
1730
- const { type } = match.edge(i);
1731
- if (type.isTextblock && !type.hasRequiredAttrs()) {
1732
- return type;
1733
- }
1734
- }
1735
- return null;
1736
- }
1737
- function ensureMarks(state, splittableMarks) {
1738
- const marks = state.storedMarks
1739
- || (state.selection.$to.parentOffset && state.selection.$from.marks());
1740
- if (marks) {
1741
- const filteredMarks = marks.filter(mark => splittableMarks === null || splittableMarks === void 0 ? void 0 : splittableMarks.includes(mark.type.name));
1742
- state.tr.ensureMarks(filteredMarks);
1743
- }
1744
- }
1745
- const splitBlock = ({ keepMarks = true } = {}) => ({ tr, state, dispatch, editor, }) => {
1746
- const { selection, doc } = tr;
1747
- const { $from, $to } = selection;
1748
- const extensionAttributes = editor.extensionManager.attributes;
1749
- const newAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
1750
- if (selection instanceof prosemirrorState.NodeSelection && selection.node.isBlock) {
1751
- if (!$from.parentOffset || !prosemirrorTransform.canSplit(doc, $from.pos)) {
1752
- return false;
1753
- }
1754
- if (dispatch) {
1755
- if (keepMarks) {
1756
- ensureMarks(state, editor.extensionManager.splittableMarks);
1757
- }
1758
- tr.split($from.pos).scrollIntoView();
1759
- }
1760
- return true;
1761
- }
1762
- if (!$from.parent.isBlock) {
1763
- return false;
1764
- }
1765
- if (dispatch) {
1766
- const atEnd = $to.parentOffset === $to.parent.content.size;
1767
- if (selection instanceof prosemirrorState.TextSelection) {
1768
- tr.deleteSelection();
1769
- }
1770
- const deflt = $from.depth === 0
1771
- ? undefined
1772
- : defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)));
1773
- let types = atEnd && deflt
1774
- ? [{
1775
- type: deflt,
1776
- attrs: newAttributes,
1777
- }]
1778
- : undefined;
1779
- let can = prosemirrorTransform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, types);
1780
- if (!types
1781
- && !can
1782
- && prosemirrorTransform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : undefined)) {
1783
- can = true;
1784
- types = deflt
1785
- ? [{
1786
- type: deflt,
1787
- attrs: newAttributes,
1788
- }]
1789
- : undefined;
1790
- }
1791
- if (can) {
1792
- tr.split(tr.mapping.map($from.pos), 1, types);
1793
- if (!atEnd
1794
- && !$from.parentOffset
1795
- && $from.parent.type !== deflt
1796
- && $from.node(-1).canReplace($from.index(-1), $from.indexAfter(-1), prosemirrorModel.Fragment.from(deflt === null || deflt === void 0 ? void 0 : deflt.create()))) {
1797
- tr.setNodeMarkup(tr.mapping.map($from.before()), deflt || undefined);
1798
- }
1799
- }
1800
- if (keepMarks) {
1801
- ensureMarks(state, editor.extensionManager.splittableMarks);
1802
- }
1803
- tr.scrollIntoView();
1804
- }
1805
- return true;
1806
- };
1807
-
1808
- var splitBlock$1 = /*#__PURE__*/Object.freeze({
1809
- __proto__: null,
1810
- splitBlock: splitBlock
1811
- });
1812
-
1813
- const splitListItem = typeOrName => ({ tr, state, dispatch, editor, }) => {
1814
- var _a;
1815
- const type = getNodeType(typeOrName, state.schema);
1816
- const { $from, $to } = state.selection;
1817
- // @ts-ignore
1818
- // eslint-disable-next-line
1819
- const node = state.selection.node;
1820
- if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) {
1821
- return false;
1822
- }
1823
- const grandParent = $from.node(-1);
1824
- if (grandParent.type !== type) {
1825
- return false;
1826
- }
1827
- const extensionAttributes = editor.extensionManager.attributes;
1828
- if ($from.parent.content.size === 0 && $from.node(-1).childCount === $from.indexAfter(-1)) {
1829
- // In an empty block. If this is a nested list, the wrapping
1830
- // list item should be split. Otherwise, bail out and let next
1831
- // command handle lifting.
1832
- if ($from.depth === 2
1833
- || $from.node(-3).type !== type
1834
- || $from.index(-2) !== $from.node(-2).childCount - 1) {
1835
- return false;
1836
- }
1837
- if (dispatch) {
1838
- let wrap = prosemirrorModel.Fragment.empty;
1839
- const keepItem = $from.index(-1) > 0;
1840
- // Build a fragment containing empty versions of the structure
1841
- // from the outer list item to the parent node of the cursor
1842
- for (let d = $from.depth - (keepItem ? 1 : 2); d >= $from.depth - 3; d -= 1) {
1843
- wrap = prosemirrorModel.Fragment.from($from.node(d).copy(wrap));
1844
- }
1845
- // Add a second list item with an empty default start node
1846
- const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
1847
- const nextType = ((_a = type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.createAndFill(newNextTypeAttributes)) || undefined;
1848
- wrap = wrap.append(prosemirrorModel.Fragment.from(type.createAndFill(null, nextType) || undefined));
1849
- tr
1850
- .replace($from.before(keepItem ? undefined : -1), $from.after(-3), new prosemirrorModel.Slice(wrap, keepItem ? 3 : 2, 2))
1851
- .setSelection(prosemirrorState.TextSelection.near(tr.doc.resolve($from.pos + (keepItem ? 3 : 2))))
1852
- .scrollIntoView();
1853
- }
1854
- return true;
1855
- }
1856
- const nextType = $to.pos === $from.end()
1857
- ? grandParent.contentMatchAt(0).defaultType
1858
- : null;
1859
- const newTypeAttributes = getSplittedAttributes(extensionAttributes, grandParent.type.name, grandParent.attrs);
1860
- const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
1861
- tr.delete($from.pos, $to.pos);
1862
- const types = nextType
1863
- ? [{ type, attrs: newTypeAttributes }, { type: nextType, attrs: newNextTypeAttributes }]
1864
- : [{ type, attrs: newTypeAttributes }];
1865
- if (!prosemirrorTransform.canSplit(tr.doc, $from.pos, 2)) {
1866
- return false;
1867
- }
1868
- if (dispatch) {
1869
- tr.split($from.pos, 2, types).scrollIntoView();
1870
- }
1871
- return true;
1872
- };
1873
-
1874
- var splitListItem$1 = /*#__PURE__*/Object.freeze({
1875
- __proto__: null,
1876
- splitListItem: splitListItem
1877
- });
1878
-
1879
- function findParentNodeClosestToPos($pos, predicate) {
1880
- for (let i = $pos.depth; i > 0; i -= 1) {
1881
- const node = $pos.node(i);
1882
- if (predicate(node)) {
1883
- return {
1884
- pos: i > 0 ? $pos.before(i) : 0,
1885
- start: $pos.start(i),
1886
- depth: i,
1887
- node,
1888
- };
1889
- }
1890
- }
1891
- }
1892
-
1893
- function findParentNode(predicate) {
1894
- return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
1895
- }
1896
-
1897
- function isList(name, extensions) {
1898
- const { nodeExtensions } = splitExtensions(extensions);
1899
- const extension = nodeExtensions.find(item => item.config.name === name);
1900
- if (!extension) {
1901
- return false;
1902
- }
1903
- const groups = callOrReturn(extension.config.group, { options: extension.options });
1904
- if (typeof groups !== 'string') {
1905
- return false;
1906
- }
1907
- return groups.split(' ').includes('list');
1908
- }
1909
-
1910
- const toggleList = (listTypeOrName, itemTypeOrName) => ({ editor, tr, state, dispatch, chain, commands, can, }) => {
1911
- const { extensions } = editor.options;
1912
- const listType = getNodeType(listTypeOrName, state.schema);
1913
- const itemType = getNodeType(itemTypeOrName, state.schema);
1914
- const { selection } = state;
1915
- const { $from, $to } = selection;
1916
- const range = $from.blockRange($to);
1917
- if (!range) {
1918
- return false;
1919
- }
1920
- const parentList = findParentNode(node => isList(node.type.name, extensions))(selection);
1921
- if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
1922
- // remove list
1923
- if (parentList.node.type === listType) {
1924
- return commands.liftListItem(itemType);
1925
- }
1926
- // change list type
1927
- if (isList(parentList.node.type.name, extensions)
1928
- && listType.validContent(parentList.node.content)
1929
- && dispatch) {
1930
- tr.setNodeMarkup(parentList.pos, listType);
1931
- return true;
1932
- }
1933
- }
1934
- const canWrapInList = can().wrapInList(listType);
1935
- // try to convert node to paragraph if needed
1936
- if (!canWrapInList) {
1937
- return chain()
1938
- .clearNodes()
1939
- .wrapInList(listType)
1940
- .run();
1941
- }
1942
- return commands.wrapInList(listType);
1943
- };
1944
-
1945
- var toggleList$1 = /*#__PURE__*/Object.freeze({
1946
- __proto__: null,
1947
- toggleList: toggleList
1948
- });
1949
-
1950
- const toggleMark = (typeOrName, attributes = {}) => ({ state, commands }) => {
1951
- const type = getMarkType(typeOrName, state.schema);
1952
- const isActive = isMarkActive(state, type, attributes);
1953
- if (isActive) {
1954
- return commands.unsetMark(type);
1955
- }
1956
- return commands.setMark(type, attributes);
1957
- };
1958
-
1959
- var toggleMark$1 = /*#__PURE__*/Object.freeze({
1960
- __proto__: null,
1961
- toggleMark: toggleMark
1962
- });
1963
-
1964
- const toggleNode = (typeOrName, toggleTypeOrName, attributes = {}) => ({ state, commands }) => {
1965
- const type = getNodeType(typeOrName, state.schema);
1966
- const toggleType = getNodeType(toggleTypeOrName, state.schema);
1967
- const isActive = isNodeActive(state, type, attributes);
1968
- if (isActive) {
1969
- return commands.setNode(toggleType);
1970
- }
1971
- return commands.setNode(type, attributes);
1972
- };
1973
-
1974
- var toggleNode$1 = /*#__PURE__*/Object.freeze({
1975
- __proto__: null,
1976
- toggleNode: toggleNode
1977
- });
1978
-
1979
- const toggleWrap = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1980
- const type = getNodeType(typeOrName, state.schema);
1981
- const isActive = isNodeActive(state, type, attributes);
1982
- if (isActive) {
1983
- return prosemirrorCommands.lift(state, dispatch);
1984
- }
1985
- return prosemirrorCommands.wrapIn(type, attributes)(state, dispatch);
1986
- };
1987
-
1988
- var toggleWrap$1 = /*#__PURE__*/Object.freeze({
1989
- __proto__: null,
1990
- toggleWrap: toggleWrap
1991
- });
1992
-
1993
- const undoInputRule = () => ({ state, dispatch }) => {
1994
- return prosemirrorInputrules.undoInputRule(state, dispatch);
1995
- };
1996
-
1997
- var undoInputRule$1 = /*#__PURE__*/Object.freeze({
1998
- __proto__: null,
1999
- undoInputRule: undoInputRule
2000
- });
2001
-
2002
- const unsetAllMarks = () => ({ tr, state, dispatch }) => {
2003
- const { selection } = tr;
2004
- const { empty, ranges } = selection;
2005
- if (empty) {
2006
- return true;
2007
- }
2008
- if (dispatch) {
2009
- Object
2010
- .entries(state.schema.marks)
2011
- .forEach(([, mark]) => {
2012
- ranges.forEach(range => {
2013
- tr.removeMark(range.$from.pos, range.$to.pos, mark);
2014
- });
2015
- });
2016
- }
2017
- return true;
2018
- };
2019
-
2020
- var unsetAllMarks$1 = /*#__PURE__*/Object.freeze({
2021
- __proto__: null,
2022
- unsetAllMarks: unsetAllMarks
2023
- });
2024
-
2025
- const unsetMark = typeOrName => ({ tr, state, dispatch }) => {
2026
- const { selection } = tr;
2027
- const type = getMarkType(typeOrName, state.schema);
2028
- const { $from, empty, ranges } = selection;
2029
- if (dispatch) {
2030
- if (empty) {
2031
- let { from, to } = selection;
2032
- const range = getMarkRange($from, type);
2033
- if (range) {
2034
- from = range.from;
2035
- to = range.to;
2036
- }
2037
- tr.removeMark(from, to, type);
2038
- }
2039
- else {
2040
- ranges.forEach(range => {
2041
- tr.removeMark(range.$from.pos, range.$to.pos, type);
2042
- });
2043
- }
2044
- tr.removeStoredMark(type);
2045
- }
2046
- return true;
2047
- };
2048
-
2049
- var unsetMark$1 = /*#__PURE__*/Object.freeze({
2050
- __proto__: null,
2051
- unsetMark: unsetMark
2052
- });
2053
-
2054
- const updateAttributes = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
2055
- let nodeType = null;
2056
- let markType = null;
2057
- const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2058
- ? typeOrName
2059
- : typeOrName.name, state.schema);
2060
- if (!schemaType) {
2061
- return false;
2062
- }
2063
- if (schemaType === 'node') {
2064
- nodeType = getNodeType(typeOrName, state.schema);
2065
- }
2066
- if (schemaType === 'mark') {
2067
- markType = getMarkType(typeOrName, state.schema);
2068
- }
2069
- if (dispatch) {
2070
- tr.selection.ranges.forEach(range => {
2071
- state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
2072
- if (nodeType && nodeType === node.type) {
2073
- tr.setNodeMarkup(pos, undefined, {
2074
- ...node.attrs,
2075
- ...attributes,
2076
- });
2077
- }
2078
- if (markType && node.marks.length) {
2079
- node.marks.forEach(mark => {
2080
- if (markType === mark.type) {
2081
- tr.addMark(pos, pos + node.nodeSize, markType.create({
2082
- ...mark.attrs,
2083
- ...attributes,
2084
- }));
2085
- }
2086
- });
2087
- }
2088
- });
2089
- });
2090
- }
2091
- return true;
2092
- };
2093
-
2094
- var updateAttributes$1 = /*#__PURE__*/Object.freeze({
2095
- __proto__: null,
2096
- updateAttributes: updateAttributes
2097
- });
2098
-
2099
- const updateNodeAttributes = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
2100
- console.warn('[tiptap warn]: updateNodeAttributes() is deprecated. please use updateAttributes() instead.');
2101
- const type = getNodeType(typeOrName, state.schema);
2102
- const { selection } = tr;
2103
- const { ranges } = selection;
2104
- ranges.forEach(range => {
2105
- state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
2106
- if (node.type === type && dispatch) {
2107
- tr.setNodeMarkup(pos, undefined, {
2108
- ...node.attrs,
2109
- ...attributes,
2110
- });
2111
- }
2112
- });
2113
- });
2114
- return true;
2115
- };
2116
-
2117
- var updateNodeAttributes$1 = /*#__PURE__*/Object.freeze({
2118
- __proto__: null,
2119
- updateNodeAttributes: updateNodeAttributes
2120
- });
2121
-
2122
- const wrapIn = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
2123
- const type = getNodeType(typeOrName, state.schema);
2124
- const isActive = isNodeActive(state, type, attributes);
2125
- if (isActive) {
2126
- return false;
2127
- }
2128
- return prosemirrorCommands.wrapIn(type, attributes)(state, dispatch);
2129
- };
2130
-
2131
- var wrapIn$1 = /*#__PURE__*/Object.freeze({
2132
- __proto__: null,
2133
- wrapIn: wrapIn
2134
- });
2135
-
2136
- const wrapInList = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
2137
- const type = getNodeType(typeOrName, state.schema);
2138
- return prosemirrorSchemaList.wrapInList(type, attributes)(state, dispatch);
2139
- };
2140
-
2141
- var wrapInList$1 = /*#__PURE__*/Object.freeze({
2142
- __proto__: null,
2143
- wrapInList: wrapInList
2144
- });
2145
-
2146
- const Commands = Extension.create({
2147
- name: 'commands',
2148
- addCommands() {
2149
- return {
2150
- ...blur$1,
2151
- ...clearContent$1,
2152
- ...clearNodes$1,
2153
- ...command$1,
2154
- ...createParagraphNear$1,
2155
- ...deleteRange$1,
2156
- ...deleteSelection$1,
2157
- ...enter$1,
2158
- ...exitCode$1,
2159
- ...extendMarkRange$1,
2160
- ...first$1,
2161
- ...focus$1,
2162
- ...insertContent$1,
2163
- ...insertHTML$1,
2164
- ...insertNode$1,
2165
- ...insertText$1,
2166
- ...joinBackward$1,
2167
- ...joinForward$1,
2168
- ...keyboardShortcut$1,
2169
- ...lift$1,
2170
- ...liftEmptyBlock$1,
2171
- ...liftListItem$1,
2172
- ...newlineInCode$1,
2173
- ...replace$1,
2174
- ...replaceRange$1,
2175
- ...resetAttributes$1,
2176
- ...resetNodeAttributes$1,
2177
- ...scrollIntoView$1,
2178
- ...selectAll$1,
2179
- ...selectNodeBackward$1,
2180
- ...selectNodeForward$1,
2181
- ...selectParentNode$1,
2182
- ...setContent$1,
2183
- ...setMark$1,
2184
- ...setNode$1,
2185
- ...sinkListItem$1,
2186
- ...splitBlock$1,
2187
- ...splitListItem$1,
2188
- ...toggleList$1,
2189
- ...toggleMark$1,
2190
- ...toggleNode$1,
2191
- ...toggleWrap$1,
2192
- ...undoInputRule$1,
2193
- ...unsetAllMarks$1,
2194
- ...unsetMark$1,
2195
- ...updateAttributes$1,
2196
- ...updateNodeAttributes$1,
2197
- ...wrapIn$1,
2198
- ...wrapInList$1,
2199
- };
2200
- },
2201
- });
2202
-
2203
- const Editable = Extension.create({
2204
- name: 'editable',
2205
- addProseMirrorPlugins() {
2206
- return [
2207
- new prosemirrorState.Plugin({
2208
- key: new prosemirrorState.PluginKey('editable'),
2209
- props: {
2210
- editable: () => this.editor.options.editable,
2211
- },
2212
- }),
2213
- ];
2214
- },
2215
- });
2216
-
2217
- const FocusEvents = Extension.create({
2218
- name: 'focusEvents',
2219
- addProseMirrorPlugins() {
2220
- const { editor } = this;
2221
- return [
2222
- new prosemirrorState.Plugin({
2223
- key: new prosemirrorState.PluginKey('focusEvents'),
2224
- props: {
2225
- attributes: {
2226
- tabindex: '0',
2227
- },
2228
- handleDOMEvents: {
2229
- focus: (view, event) => {
2230
- editor.isFocused = true;
2231
- const transaction = editor.state.tr
2232
- .setMeta('focus', { event })
2233
- .setMeta('addToHistory', false);
2234
- view.dispatch(transaction);
2235
- return false;
2236
- },
2237
- blur: (view, event) => {
2238
- editor.isFocused = false;
2239
- const transaction = editor.state.tr
2240
- .setMeta('blur', { event })
2241
- .setMeta('addToHistory', false);
2242
- view.dispatch(transaction);
2243
- return false;
2244
- },
2245
- },
2246
- },
2247
- }),
2248
- ];
2249
- },
2250
- });
2251
-
2252
- const Keymap = Extension.create({
2253
- name: 'keymap',
2254
- addKeyboardShortcuts() {
2255
- const handleBackspace = () => this.editor.commands.first(({ commands }) => [
2256
- () => commands.undoInputRule(),
2257
- () => commands.deleteSelection(),
2258
- () => commands.joinBackward(),
2259
- () => commands.selectNodeBackward(),
2260
- ]);
2261
- const handleDelete = () => this.editor.commands.first(({ commands }) => [
2262
- () => commands.deleteSelection(),
2263
- () => commands.joinForward(),
2264
- () => commands.selectNodeForward(),
2265
- ]);
2266
- return {
2267
- Enter: () => this.editor.commands.first(({ commands }) => [
2268
- () => commands.newlineInCode(),
2269
- () => commands.createParagraphNear(),
2270
- () => commands.liftEmptyBlock(),
2271
- () => commands.splitBlock(),
2272
- ]),
2273
- 'Mod-Enter': () => this.editor.commands.exitCode(),
2274
- Backspace: () => handleBackspace(),
2275
- 'Mod-Backspace': () => handleBackspace(),
2276
- Delete: () => handleDelete(),
2277
- 'Mod-Delete': () => handleDelete(),
2278
- // we don’t need a custom `selectAll` for now
2279
- // 'Mod-a': () => this.editor.commands.selectAll(),
2280
- };
2281
- },
2282
- });
2283
-
2284
- var extensions = /*#__PURE__*/Object.freeze({
2285
- __proto__: null,
2286
- ClipboardTextSerializer: ClipboardTextSerializer,
2287
- Commands: Commands,
2288
- Editable: Editable,
2289
- FocusEvents: FocusEvents,
2290
- Keymap: Keymap
2291
- });
2292
-
2293
- const style = `.ProseMirror {
2294
- position: relative;
2295
- }
2296
-
2297
- .ProseMirror {
2298
- word-wrap: break-word;
2299
- white-space: pre-wrap;
2300
- -webkit-font-variant-ligatures: none;
2301
- font-variant-ligatures: none;
2302
- }
2303
-
2304
- .ProseMirror [contenteditable="false"] {
2305
- white-space: normal;
2306
- }
2307
-
2308
- .ProseMirror [contenteditable="false"] [contenteditable="true"] {
2309
- white-space: pre-wrap;
2310
- }
2311
-
2312
- .ProseMirror pre {
2313
- white-space: pre-wrap;
2314
- }
2315
-
2316
- .ProseMirror-gapcursor {
2317
- display: none;
2318
- pointer-events: none;
2319
- position: absolute;
2320
- }
2321
-
2322
- .ProseMirror-gapcursor:after {
2323
- content: "";
2324
- display: block;
2325
- position: absolute;
2326
- top: -2px;
2327
- width: 20px;
2328
- border-top: 1px solid black;
2329
- animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite;
2330
- }
2331
-
2332
- @keyframes ProseMirror-cursor-blink {
2333
- to {
2334
- visibility: hidden;
2335
- }
2336
- }
2337
-
2338
- .ProseMirror-hideselection *::selection {
2339
- background: transparent;
2340
- }
2341
-
2342
- .ProseMirror-hideselection *::-moz-selection {
2343
- background: transparent;
2344
- }
2345
-
2346
- .ProseMirror-hideselection * {
2347
- caret-color: transparent;
2348
- }
2349
-
2350
- .ProseMirror-focused .ProseMirror-gapcursor {
2351
- display: block;
2352
- }`;
2353
-
2354
- class Editor extends EventEmitter {
2355
- constructor(options = {}) {
2356
- super();
2357
- this.isFocused = false;
2358
- this.options = {
2359
- element: document.createElement('div'),
2360
- content: '',
2361
- injectCSS: true,
2362
- extensions: [],
2363
- autofocus: false,
2364
- editable: true,
2365
- editorProps: {},
2366
- parseOptions: {},
2367
- enableInputRules: true,
2368
- enablePasteRules: true,
2369
- onCreate: () => null,
2370
- onUpdate: () => null,
2371
- onSelectionUpdate: () => null,
2372
- onTransaction: () => null,
2373
- onFocus: () => null,
2374
- onBlur: () => null,
2375
- onResize: () => null,
2376
- onDestroy: () => null,
2377
- };
2378
- this.isCapturingTransaction = false;
2379
- this.capturedTransaction = null;
2380
- this.setOptions(options);
2381
- this.createExtensionManager();
2382
- this.createCommandManager();
2383
- this.createSchema();
2384
- this.createView();
2385
- this.injectCSS();
2386
- this.on('create', this.options.onCreate);
2387
- this.on('update', this.options.onUpdate);
2388
- this.on('selectionUpdate', this.options.onSelectionUpdate);
2389
- this.on('transaction', this.options.onTransaction);
2390
- this.on('focus', this.options.onFocus);
2391
- this.on('blur', this.options.onBlur);
2392
- this.on('destroy', this.options.onDestroy);
2393
- window.setTimeout(() => {
2394
- this.commands.focus(this.options.autofocus);
2395
- this.emit('create', { editor: this });
2396
- if (window.ResizeObserver) {
2397
- this.resizeObserver = new ResizeObserver(() => {
2398
- this.emit('resize', { editor: this });
2399
- });
2400
- this.resizeObserver.observe(this.view.dom);
2401
- }
2402
- }, 0);
2403
- }
2404
- /**
2405
- * An object of all registered commands.
2406
- */
2407
- get commands() {
2408
- return this.commandManager.createCommands();
2409
- }
2410
- /**
2411
- * Create a command chain to call multiple commands at once.
2412
- */
2413
- chain() {
2414
- return this.commandManager.createChain();
2415
- }
2416
- /**
2417
- * Check if a command or a command chain can be executed. Without executing it.
2418
- */
2419
- can() {
2420
- return this.commandManager.createCan();
2421
- }
2422
- /**
2423
- * Inject CSS styles.
2424
- */
2425
- injectCSS() {
2426
- if (this.options.injectCSS && document) {
2427
- this.css = createStyleTag(style);
2428
- }
2429
- }
2430
- /**
2431
- * Update editor options.
2432
- *
2433
- * @param options A list of options
2434
- */
2435
- setOptions(options = {}) {
2436
- this.options = { ...this.options, ...options };
2437
- }
2438
- /**
2439
- * Update editable state of the editor.
2440
- */
2441
- setEditable(editable) {
2442
- this.setOptions({ editable });
2443
- if (this.view && this.state && !this.isDestroyed) {
2444
- this.view.updateState(this.state);
2445
- }
2446
- }
2447
- /**
2448
- * Returns whether the editor is editable.
2449
- */
2450
- get isEditable() {
2451
- return this.view && this.view.editable;
2452
- }
2453
- /**
2454
- * Returns the editor state.
2455
- */
2456
- get state() {
2457
- return this.view.state;
2458
- }
2459
- /**
2460
- * Register a ProseMirror plugin.
2461
- *
2462
- * @param plugin A ProseMirror plugin
2463
- * @param handlePlugins Control how to merge the plugin into the existing plugins.
2464
- */
2465
- registerPlugin(plugin, handlePlugins) {
2466
- const plugins = typeof handlePlugins === 'function'
2467
- ? handlePlugins(plugin, this.state.plugins)
2468
- : [...this.state.plugins, plugin];
2469
- const state = this.state.reconfigure({ plugins });
2470
- this.view.updateState(state);
2471
- }
2472
- /**
2473
- * Unregister a ProseMirror plugin.
2474
- *
2475
- * @param name The plugins name
2476
- */
2477
- unregisterPlugin(nameOrPluginKey) {
2478
- if (this.isDestroyed) {
2479
- return;
2480
- }
2481
- const name = typeof nameOrPluginKey === 'string'
2482
- ? `${nameOrPluginKey}$`
2483
- // @ts-ignore
2484
- : nameOrPluginKey.key;
2485
- const state = this.state.reconfigure({
2486
- // @ts-ignore
2487
- plugins: this.state.plugins.filter(plugin => !plugin.key.startsWith(name)),
2488
- });
2489
- this.view.updateState(state);
2490
- }
2491
- /**
2492
- * Creates an extension manager.
2493
- */
2494
- createExtensionManager() {
2495
- const coreExtensions = Object.entries(extensions).map(([, extension]) => extension);
2496
- const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
2497
- return ['extension', 'node', 'mark'].includes(extension === null || extension === void 0 ? void 0 : extension.type);
2498
- });
2499
- this.extensionManager = new ExtensionManager(allExtensions, this);
2500
- }
2501
- /**
2502
- * Creates an command manager.
2503
- */
2504
- createCommandManager() {
2505
- this.commandManager = new CommandManager(this, this.extensionManager.commands);
2506
- }
2507
- /**
2508
- * Creates a ProseMirror schema.
2509
- */
2510
- createSchema() {
2511
- this.schema = this.extensionManager.schema;
2512
- }
2513
- /**
2514
- * Creates a ProseMirror view.
2515
- */
2516
- createView() {
2517
- this.view = new prosemirrorView.EditorView(this.options.element, {
2518
- ...this.options.editorProps,
2519
- dispatchTransaction: this.dispatchTransaction.bind(this),
2520
- state: prosemirrorState.EditorState.create({
2521
- doc: createDocument(this.options.content, this.schema, this.options.parseOptions),
2522
- }),
2523
- });
2524
- // `editor.view` is not yet available at this time.
2525
- // Therefore we will add all plugins and node views directly afterwards.
2526
- const newState = this.state.reconfigure({
2527
- plugins: this.extensionManager.plugins,
2528
- });
2529
- this.view.updateState(newState);
2530
- this.createNodeViews();
2531
- // Let’s store the editor instance in the DOM element.
2532
- // So we’ll have access to it for tests.
2533
- const dom = this.view.dom;
2534
- dom.editor = this;
2535
- }
2536
- /**
2537
- * Creates all node views.
2538
- */
2539
- createNodeViews() {
2540
- this.view.setProps({
2541
- nodeViews: this.extensionManager.nodeViews,
2542
- });
2543
- }
2544
- captureTransaction(fn) {
2545
- this.isCapturingTransaction = true;
2546
- fn();
2547
- this.isCapturingTransaction = false;
2548
- const tr = this.capturedTransaction;
2549
- this.capturedTransaction = null;
2550
- return tr;
2551
- }
2552
- /**
2553
- * The callback over which to send transactions (state updates) produced by the view.
2554
- *
2555
- * @param transaction An editor state transaction
2556
- */
2557
- dispatchTransaction(transaction) {
2558
- if (transaction.docChanged && !this.isEditable) {
2559
- return;
2560
- }
2561
- if (this.isCapturingTransaction) {
2562
- if (!this.capturedTransaction) {
2563
- this.capturedTransaction = transaction;
2564
- return;
2565
- }
2566
- transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
2567
- return;
2568
- }
2569
- const state = this.state.apply(transaction);
2570
- const selectionHasChanged = !this.state.selection.eq(state.selection);
2571
- this.view.updateState(state);
2572
- this.emit('transaction', {
2573
- editor: this,
2574
- transaction,
2575
- });
2576
- if (selectionHasChanged) {
2577
- this.emit('selectionUpdate', {
2578
- editor: this,
2579
- });
2580
- }
2581
- const focus = transaction.getMeta('focus');
2582
- const blur = transaction.getMeta('blur');
2583
- if (focus) {
2584
- this.emit('focus', {
2585
- editor: this,
2586
- event: focus.event,
2587
- });
2588
- }
2589
- if (blur) {
2590
- this.emit('blur', {
2591
- editor: this,
2592
- event: blur.event,
2593
- });
2594
- }
2595
- if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
2596
- return;
2597
- }
2598
- this.emit('update', {
2599
- editor: this,
2600
- transaction,
2601
- });
2602
- }
2603
- /**
2604
- * Get attributes of the currently selected node.
2605
- *
2606
- * @param name Name of the node
2607
- */
2608
- getNodeAttributes(name) {
2609
- return getNodeAttributes(this.state, name);
2610
- }
2611
- /**
2612
- * Get attributes of the currently selected mark.
2613
- *
2614
- * @param name Name of the mark
2615
- */
2616
- getMarkAttributes(name) {
2617
- return getMarkAttributes(this.state, name);
2618
- }
2619
- isActive(nameOrAttributes, attributesOrUndefined) {
2620
- const name = typeof nameOrAttributes === 'string'
2621
- ? nameOrAttributes
2622
- : null;
2623
- const attributes = typeof nameOrAttributes === 'string'
2624
- ? attributesOrUndefined
2625
- : nameOrAttributes;
2626
- return isActive(this.state, name, attributes);
2627
- }
2628
- /**
2629
- * Get the document as JSON.
2630
- */
2631
- getJSON() {
2632
- return this.state.doc.toJSON();
2633
- }
2634
- /**
2635
- * Get the document as HTML.
2636
- */
2637
- getHTML() {
2638
- return getHTMLFromFragment(this.state.doc, this.schema);
2639
- }
2640
- /**
2641
- * Check if there is no content.
2642
- */
2643
- get isEmpty() {
2644
- return isNodeEmpty(this.state.doc);
2645
- }
2646
- /**
2647
- * Get the number of characters for the current document.
2648
- */
2649
- getCharacterCount() {
2650
- return this.state.doc.content.size - 2;
2651
- }
2652
- /**
2653
- * Destroy the editor.
2654
- */
2655
- destroy() {
2656
- var _a;
2657
- (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.unobserve(this.view.dom);
2658
- this.emit('destroy');
2659
- if (this.view) {
2660
- this.view.destroy();
2661
- }
2662
- this.removeAllListeners();
2663
- removeElement(this.css);
2664
- }
2665
- /**
2666
- * Check if the editor is already destroyed.
2667
- */
2668
- get isDestroyed() {
2669
- var _a;
2670
- // @ts-ignore
2671
- return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
2672
- }
2673
- }
2674
-
2675
- var _configure$1;
2676
- class Node {
2677
- constructor(config) {
2678
- this.type = 'node';
2679
- this.config = {
2680
- name: 'node',
2681
- priority: 100,
2682
- defaultOptions: {},
2683
- };
2684
- _configure$1.set(this, (options) => {
2685
- this.options = mergeDeep(this.config.defaultOptions, options);
2686
- return this;
2687
- });
2688
- this.config = {
2689
- ...this.config,
2690
- ...config,
2691
- };
2692
- this.options = this.config.defaultOptions;
2693
- }
2694
- static create(config) {
2695
- return new Node(config);
2696
- }
2697
- configure(options = {}) {
2698
- var _a;
2699
- return __classPrivateFieldGet((_a = Node
2700
- .create(this.config)), _configure$1).call(_a, options);
2701
- }
2702
- extend(extendedConfig) {
2703
- return new Node({
2704
- ...this.config,
2705
- ...extendedConfig,
2706
- });
2707
- }
2708
- }
2709
- _configure$1 = new WeakMap();
2710
-
2711
- var _configure;
2712
- class Mark {
2713
- constructor(config) {
2714
- this.type = 'mark';
2715
- this.config = {
2716
- name: 'mark',
2717
- priority: 100,
2718
- defaultOptions: {},
2719
- };
2720
- _configure.set(this, (options) => {
2721
- this.options = mergeDeep(this.config.defaultOptions, options);
2722
- return this;
2723
- });
2724
- this.config = {
2725
- ...this.config,
2726
- ...config,
2727
- };
2728
- this.options = this.config.defaultOptions;
2729
- }
2730
- static create(config) {
2731
- return new Mark(config);
2732
- }
2733
- configure(options = {}) {
2734
- var _a;
2735
- return __classPrivateFieldGet((_a = Mark
2736
- .create(this.config)), _configure).call(_a, options);
2737
- }
2738
- extend(extendedConfig) {
2739
- return new Mark({
2740
- ...this.config,
2741
- ...extendedConfig,
2742
- });
2743
- }
2744
- }
2745
- _configure = new WeakMap();
2746
-
2747
- class NodeView {
2748
- constructor(component, props, options) {
2749
- this.isDragging = false;
2750
- this.options = {
2751
- stopEvent: null,
2752
- update: null,
2753
- };
2754
- this.component = component;
2755
- this.options = { ...this.options, ...options };
2756
- this.editor = props.editor;
2757
- this.extension = props.extension;
2758
- this.node = props.node;
2759
- this.decorations = props.decorations;
2760
- this.getPos = props.getPos;
2761
- this.mount();
2762
- }
2763
- mount() {
2764
- // eslint-disable-next-line
2765
- return;
2766
- }
2767
- get dom() {
2768
- return null;
2769
- }
2770
- get contentDOM() {
2771
- return null;
2772
- }
2773
- onDragStart(event) {
2774
- var _a, _b;
2775
- if (!this.dom) {
2776
- return;
2777
- }
2778
- const { view } = this.editor;
2779
- const target = event.target;
2780
- if ((_a = this.contentDOM) === null || _a === void 0 ? void 0 : _a.contains(target)) {
2781
- return;
2782
- }
2783
- // sometimes `event.target` is not the `dom` element
2784
- (_b = event.dataTransfer) === null || _b === void 0 ? void 0 : _b.setDragImage(this.dom, 0, 0);
2785
- const selection = prosemirrorState.NodeSelection.create(view.state.doc, this.getPos());
2786
- const transaction = view.state.tr.setSelection(selection);
2787
- view.dispatch(transaction);
2788
- }
2789
- stopEvent(event) {
2790
- var _a;
2791
- if (!this.dom) {
2792
- return false;
2793
- }
2794
- if (typeof this.options.stopEvent === 'function') {
2795
- return this.options.stopEvent(event);
2796
- }
2797
- const target = event.target;
2798
- const isInElement = this.dom.contains(target) && !((_a = this.contentDOM) === null || _a === void 0 ? void 0 : _a.contains(target));
2799
- // any event from child nodes should be handled by ProseMirror
2800
- if (!isInElement) {
2801
- return false;
2802
- }
2803
- const isInput = ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA'].includes(target.tagName)
2804
- || target.isContentEditable;
2805
- // any input event within node views should be ignored by ProseMirror
2806
- if (isInput) {
2807
- return true;
2808
- }
2809
- const { isEditable } = this.editor;
2810
- const { isDragging } = this;
2811
- const isDraggable = !!this.node.type.spec.draggable;
2812
- const isSelectable = prosemirrorState.NodeSelection.isSelectable(this.node);
2813
- const isCopyEvent = event.type === 'copy';
2814
- const isPasteEvent = event.type === 'paste';
2815
- const isCutEvent = event.type === 'cut';
2816
- const isClickEvent = event.type === 'mousedown';
2817
- const isDragEvent = event.type.startsWith('drag') || event.type === 'drop';
2818
- // ProseMirror tries to drag selectable nodes
2819
- // even if `draggable` is set to `false`
2820
- // this fix prevents that
2821
- if (!isDraggable && isSelectable && isDragEvent) {
2822
- event.preventDefault();
2823
- }
2824
- if (isDraggable && isDragEvent && !isDragging) {
2825
- event.preventDefault();
2826
- return false;
2827
- }
2828
- // we have to store that dragging started
2829
- if (isDraggable && isEditable && !isDragging && isClickEvent) {
2830
- const dragHandle = target.closest('[data-drag-handle]');
2831
- const isValidDragHandle = dragHandle
2832
- && (this.dom === dragHandle || (this.dom.contains(dragHandle)));
2833
- if (isValidDragHandle) {
2834
- this.isDragging = true;
2835
- document.addEventListener('dragend', () => {
2836
- this.isDragging = false;
2837
- }, { once: true });
2838
- document.addEventListener('mouseup', () => {
2839
- this.isDragging = false;
2840
- }, { once: true });
2841
- }
2842
- }
2843
- // these events are handled by prosemirror
2844
- if (isDragging
2845
- || isCopyEvent
2846
- || isPasteEvent
2847
- || isCutEvent
2848
- || (isClickEvent && isSelectable)) {
2849
- return false;
2850
- }
2851
- return true;
2852
- }
2853
- ignoreMutation(mutation) {
2854
- if (mutation.type === 'selection') {
2855
- if (this.node.isLeaf) {
2856
- return true;
2857
- }
2858
- return false;
2859
- }
2860
- if (!this.contentDOM) {
2861
- return true;
2862
- }
2863
- const contentDOMHasChanged = !this.contentDOM.contains(mutation.target)
2864
- || this.contentDOM === mutation.target;
2865
- return contentDOMHasChanged;
2866
- }
2867
- updateAttributes(attributes) {
2868
- if (!this.editor.view.editable) {
2869
- return;
2870
- }
2871
- const { state } = this.editor.view;
2872
- const pos = this.getPos();
2873
- const transaction = state.tr.setNodeMarkup(pos, undefined, {
2874
- ...this.node.attrs,
2875
- ...attributes,
2876
- });
2877
- this.editor.view.dispatch(transaction);
2878
- }
2879
- }
2880
-
2881
- function nodeInputRule (regexp, type, getAttributes) {
2882
- return new prosemirrorInputrules.InputRule(regexp, (state, match, start, end) => {
2883
- const attributes = getAttributes instanceof Function
2884
- ? getAttributes(match)
2885
- : getAttributes;
2886
- const { tr } = state;
2887
- if (match[0]) {
2888
- tr.replaceWith(start - 1, end, type.create(attributes));
2889
- }
2890
- return tr;
2891
- });
2892
- }
2893
-
2894
- function getMarksBetween(from, to, state) {
2895
- let marks = [];
2896
- state.doc.nodesBetween(from, to, (node, pos) => {
2897
- marks = [...marks, ...node.marks.map(mark => ({
2898
- from: pos,
2899
- to: pos + node.nodeSize,
2900
- mark,
2901
- }))];
2902
- });
2903
- return marks;
2904
- }
2905
-
2906
- function markInputRule (regexp, markType, getAttributes) {
2907
- return new prosemirrorInputrules.InputRule(regexp, (state, match, start, end) => {
2908
- const attributes = getAttributes instanceof Function
2909
- ? getAttributes(match)
2910
- : getAttributes;
2911
- const { tr } = state;
2912
- const captureGroup = match[match.length - 1];
2913
- const fullMatch = match[0];
2914
- let markEnd = end;
2915
- if (captureGroup) {
2916
- const startSpaces = fullMatch.search(/\S/);
2917
- const textStart = start + fullMatch.indexOf(captureGroup);
2918
- const textEnd = textStart + captureGroup.length;
2919
- const excludedMarks = getMarksBetween(start, end, state)
2920
- .filter(item => {
2921
- // TODO: PR to add excluded to MarkType
2922
- // @ts-ignore
2923
- const { excluded } = item.mark.type;
2924
- return excluded.find((type) => type.name === markType.name);
2925
- })
2926
- .filter(item => item.to > textStart);
2927
- if (excludedMarks.length) {
2928
- return null;
2929
- }
2930
- if (textEnd < end) {
2931
- tr.delete(textEnd, end);
2932
- }
2933
- if (textStart > start) {
2934
- tr.delete(start + startSpaces, textStart);
2935
- }
2936
- markEnd = start + startSpaces + captureGroup.length;
2937
- tr.addMark(start + startSpaces, markEnd, markType.create(attributes));
2938
- tr.removeStoredMark(markType);
2939
- }
2940
- return tr;
2941
- });
2942
- }
2943
-
2944
- function markPasteRule (regexp, type, getAttrs) {
2945
- const handler = (fragment, parent) => {
2946
- const nodes = [];
2947
- fragment.forEach(child => {
2948
- if (child.isText && child.text) {
2949
- const { text } = child;
2950
- let pos = 0;
2951
- let match;
2952
- // eslint-disable-next-line
2953
- while ((match = regexp.exec(text)) !== null) {
2954
- const outerMatch = Math.max(match.length - 2, 0);
2955
- const innerMatch = Math.max(match.length - 1, 0);
2956
- if (parent === null || parent === void 0 ? void 0 : parent.type.allowsMarkType(type)) {
2957
- const start = match.index;
2958
- const matchStart = start + match[0].indexOf(match[outerMatch]);
2959
- const matchEnd = matchStart + match[outerMatch].length;
2960
- const textStart = matchStart + match[outerMatch].lastIndexOf(match[innerMatch]);
2961
- const textEnd = textStart + match[innerMatch].length;
2962
- const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs;
2963
- // adding text before markdown to nodes
2964
- if (matchStart > 0) {
2965
- nodes.push(child.cut(pos, matchStart));
2966
- }
2967
- // adding the markdown part to nodes
2968
- nodes.push(child
2969
- .cut(textStart, textEnd)
2970
- .mark(type.create(attrs).addToSet(child.marks)));
2971
- pos = matchEnd;
2972
- }
2973
- }
2974
- // adding rest of text to nodes
2975
- if (pos < text.length) {
2976
- nodes.push(child.cut(pos));
2977
- }
2978
- }
2979
- else {
2980
- nodes.push(child.copy(handler(child.content, child)));
2981
- }
2982
- });
2983
- return prosemirrorModel.Fragment.fromArray(nodes);
2984
- };
2985
- return new prosemirrorState.Plugin({
2986
- key: new prosemirrorState.PluginKey('markPasteRule'),
2987
- props: {
2988
- transformPasted: slice => {
2989
- return new prosemirrorModel.Slice(handler(slice.content), slice.openStart, slice.openEnd);
2990
- },
2991
- },
2992
- });
2993
- }
2994
-
2995
- function generateHTML(doc, extensions) {
2996
- const schema = getSchema(extensions);
2997
- const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
2998
- return getHTMLFromFragment(contentNode, schema);
2999
- }
3000
-
3001
- function isNodeSelection(value) {
3002
- return isObject(value) && value instanceof prosemirrorState.NodeSelection;
3003
- }
3004
-
3005
- exports.Editor = Editor;
3006
- exports.Extension = Extension;
3007
- exports.Mark = Mark;
3008
- exports.Node = Node;
3009
- exports.NodeView = NodeView;
3010
- exports.callOrReturn = callOrReturn;
3011
- exports.extensions = extensions;
3012
- exports.findParentNodeClosestToPos = findParentNodeClosestToPos;
3013
- exports.generateHTML = generateHTML;
3014
- exports.getHTMLFromFragment = getHTMLFromFragment;
3015
- exports.getMarkAttributes = getMarkAttributes;
3016
- exports.getSchema = getSchema;
3017
- exports.isActive = isActive;
3018
- exports.isMarkActive = isMarkActive;
3019
- exports.isNodeActive = isNodeActive;
3020
- exports.isNodeEmpty = isNodeEmpty;
3021
- exports.isNodeSelection = isNodeSelection;
3022
- exports.isTextSelection = isTextSelection;
3023
- exports.markInputRule = markInputRule;
3024
- exports.markPasteRule = markPasteRule;
3025
- exports.mergeAttributes = mergeAttributes;
3026
- exports.nodeInputRule = nodeInputRule;
3027
- //# sourceMappingURL=tiptap-core.cjs.js.map