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