@tiptap/core 2.0.0-beta.98 → 2.0.0-rc.1

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