@tiptap/core 2.0.0-beta.19 → 2.0.0-beta.193

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