@tiptap/core 2.0.0-beta.22 → 2.0.0-beta.220

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