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