@tiptap/core 2.0.0-beta.14 → 2.0.0-beta.143

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 (274) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -2
  3. package/dist/packages/core/src/CommandManager.d.ts +12 -5
  4. package/dist/packages/core/src/Editor.d.ts +19 -20
  5. package/dist/packages/core/src/EventEmitter.d.ts +8 -4
  6. package/dist/packages/core/src/Extension.d.ts +102 -26
  7. package/dist/packages/core/src/ExtensionManager.d.ts +6 -11
  8. package/dist/packages/core/src/InputRule.d.ts +42 -0
  9. package/dist/packages/core/src/Mark.d.ts +134 -30
  10. package/dist/packages/core/src/Node.d.ts +151 -32
  11. package/dist/packages/core/src/NodeView.d.ts +5 -9
  12. package/dist/packages/core/src/PasteRule.d.ts +42 -0
  13. package/dist/packages/core/src/Tracker.d.ts +11 -0
  14. package/dist/packages/core/src/commands/blur.d.ts +3 -3
  15. package/dist/packages/core/src/commands/clearContent.d.ts +3 -3
  16. package/dist/packages/core/src/commands/clearNodes.d.ts +3 -3
  17. package/dist/packages/core/src/commands/command.d.ts +2 -2
  18. package/dist/packages/core/src/commands/createParagraphNear.d.ts +3 -3
  19. package/dist/packages/core/src/commands/deleteNode.d.ts +13 -0
  20. package/dist/packages/core/src/commands/deleteRange.d.ts +3 -3
  21. package/dist/packages/core/src/commands/deleteSelection.d.ts +3 -3
  22. package/dist/packages/core/src/commands/enter.d.ts +3 -3
  23. package/dist/packages/core/src/commands/exitCode.d.ts +3 -3
  24. package/dist/packages/core/src/commands/extendMarkRange.d.ts +3 -3
  25. package/dist/packages/core/src/commands/first.d.ts +3 -3
  26. package/dist/packages/core/src/commands/focus.d.ts +5 -3
  27. package/dist/packages/core/src/commands/forEach.d.ts +14 -0
  28. package/dist/packages/core/src/commands/insertContent.d.ts +16 -0
  29. package/dist/packages/core/src/commands/insertContentAt.d.ts +16 -0
  30. package/dist/packages/core/src/commands/joinBackward.d.ts +3 -3
  31. package/dist/packages/core/src/commands/joinForward.d.ts +3 -3
  32. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +3 -3
  33. package/dist/packages/core/src/commands/lift.d.ts +3 -3
  34. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +3 -3
  35. package/dist/packages/core/src/commands/liftListItem.d.ts +3 -3
  36. package/dist/packages/core/src/commands/newlineInCode.d.ts +3 -3
  37. package/dist/packages/core/src/commands/resetAttributes.d.ts +13 -0
  38. package/dist/packages/core/src/commands/scrollIntoView.d.ts +3 -3
  39. package/dist/packages/core/src/commands/selectAll.d.ts +3 -3
  40. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +3 -3
  41. package/dist/packages/core/src/commands/selectNodeForward.d.ts +3 -3
  42. package/dist/packages/core/src/commands/selectParentNode.d.ts +3 -3
  43. package/dist/packages/core/src/commands/setContent.d.ts +4 -3
  44. package/dist/packages/core/src/commands/setMark.d.ts +3 -3
  45. package/dist/packages/core/src/commands/setMeta.d.ts +12 -0
  46. package/dist/packages/core/src/commands/setNode.d.ts +3 -3
  47. package/dist/packages/core/src/commands/setNodeSelection.d.ts +12 -0
  48. package/dist/packages/core/src/commands/setTextSelection.d.ts +12 -0
  49. package/dist/packages/core/src/commands/sinkListItem.d.ts +3 -3
  50. package/dist/packages/core/src/commands/splitBlock.d.ts +3 -3
  51. package/dist/packages/core/src/commands/splitListItem.d.ts +3 -3
  52. package/dist/packages/core/src/commands/toggleList.d.ts +3 -3
  53. package/dist/packages/core/src/commands/toggleMark.d.ts +8 -3
  54. package/dist/packages/core/src/commands/toggleNode.d.ts +3 -3
  55. package/dist/packages/core/src/commands/toggleWrap.d.ts +3 -3
  56. package/dist/packages/core/src/commands/undoInputRule.d.ts +3 -3
  57. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +3 -3
  58. package/dist/packages/core/src/commands/unsetMark.d.ts +8 -3
  59. package/dist/packages/core/src/commands/updateAttributes.d.ts +13 -0
  60. package/dist/packages/core/src/commands/wrapIn.d.ts +3 -3
  61. package/dist/packages/core/src/commands/wrapInList.d.ts +3 -3
  62. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +1 -1
  63. package/dist/packages/core/src/extensions/commands.d.ts +19 -15
  64. package/dist/packages/core/src/extensions/editable.d.ts +1 -1
  65. package/dist/packages/core/src/extensions/focusEvents.d.ts +1 -1
  66. package/dist/packages/core/src/extensions/index.d.ts +1 -0
  67. package/dist/packages/core/src/extensions/keymap.d.ts +1 -1
  68. package/dist/packages/core/src/extensions/tabindex.d.ts +2 -0
  69. package/dist/packages/core/src/helpers/createChainableState.d.ts +5 -0
  70. package/dist/packages/core/src/helpers/createDocument.d.ts +3 -0
  71. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +7 -0
  72. package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +2 -0
  73. package/dist/packages/core/src/helpers/findChildren.d.ts +3 -0
  74. package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +6 -0
  75. package/dist/packages/core/src/helpers/generateHTML.d.ts +2 -2
  76. package/dist/packages/core/src/helpers/generateJSON.d.ts +2 -0
  77. package/dist/packages/core/src/helpers/generateText.d.ts +5 -0
  78. package/dist/packages/core/src/helpers/getAttributes.d.ts +3 -0
  79. package/dist/packages/core/src/helpers/getDebugJSON.d.ts +8 -0
  80. package/dist/packages/core/src/helpers/getExtensionField.d.ts +2 -0
  81. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +2 -2
  82. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +1 -2
  83. package/dist/packages/core/src/helpers/getMarkRange.d.ts +1 -1
  84. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +1 -2
  85. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +2 -2
  86. package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +3 -0
  87. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +2 -2
  88. package/dist/packages/core/src/helpers/getText.d.ts +6 -0
  89. package/dist/packages/core/src/helpers/getTextBetween.d.ts +6 -0
  90. package/dist/packages/core/src/helpers/getTextSeralizersFromSchema.d.ts +3 -0
  91. package/dist/packages/core/src/helpers/isActive.d.ts +1 -2
  92. package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +2 -0
  93. package/dist/packages/core/src/helpers/isMarkActive.d.ts +1 -2
  94. package/dist/packages/core/src/helpers/isNodeActive.d.ts +1 -2
  95. package/dist/packages/core/src/helpers/posToDOMRect.d.ts +2 -0
  96. package/dist/packages/core/src/helpers/splitExtensions.d.ts +3 -3
  97. package/dist/packages/core/src/index.d.ts +33 -5
  98. package/dist/packages/core/src/inputRules/markInputRule.d.ts +11 -2
  99. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +11 -2
  100. package/dist/packages/core/src/inputRules/textInputRule.d.ts +9 -0
  101. package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +14 -0
  102. package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +23 -0
  103. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +11 -2
  104. package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +9 -0
  105. package/dist/packages/core/src/style.d.ts +1 -1
  106. package/dist/packages/core/src/types.d.ts +105 -52
  107. package/dist/packages/core/src/utilities/callOrReturn.d.ts +2 -1
  108. package/dist/packages/core/src/utilities/deleteProps.d.ts +1 -2
  109. package/dist/packages/core/src/utilities/findDuplicates.d.ts +1 -0
  110. package/dist/packages/core/src/utilities/isClass.d.ts +1 -1
  111. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +1 -1
  112. package/dist/packages/core/src/utilities/isFunction.d.ts +1 -0
  113. package/dist/packages/core/src/utilities/isNumber.d.ts +1 -0
  114. package/dist/packages/core/src/utilities/isObject.d.ts +1 -1
  115. package/dist/packages/core/src/utilities/isPlainObject.d.ts +1 -1
  116. package/dist/packages/core/src/utilities/isRegExp.d.ts +1 -0
  117. package/dist/packages/core/src/utilities/isiOS.d.ts +1 -0
  118. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +1 -2
  119. package/dist/packages/core/src/utilities/mergeDeep.d.ts +1 -2
  120. package/dist/packages/core/src/utilities/objectIncludes.d.ts +3 -2
  121. package/dist/tiptap-core.cjs.js +2723 -1455
  122. package/dist/tiptap-core.cjs.js.map +1 -1
  123. package/dist/tiptap-core.esm.js +3172 -1931
  124. package/dist/tiptap-core.esm.js.map +1 -1
  125. package/dist/tiptap-core.umd.js +3292 -2023
  126. package/dist/tiptap-core.umd.js.map +1 -1
  127. package/package.json +19 -16
  128. package/src/CommandManager.ts +59 -61
  129. package/src/Editor.ts +91 -95
  130. package/src/EventEmitter.ts +14 -4
  131. package/src/Extension.ts +202 -40
  132. package/src/ExtensionManager.ts +234 -70
  133. package/src/InputRule.ts +268 -0
  134. package/src/Mark.ts +248 -46
  135. package/src/Node.ts +275 -51
  136. package/src/NodeView.ts +105 -42
  137. package/src/PasteRule.ts +215 -0
  138. package/src/Tracker.ts +42 -0
  139. package/src/commands/blur.ts +9 -7
  140. package/src/commands/clearContent.ts +3 -3
  141. package/src/commands/clearNodes.ts +25 -19
  142. package/src/commands/command.ts +2 -2
  143. package/src/commands/createParagraphNear.ts +3 -3
  144. package/src/commands/deleteNode.ts +36 -0
  145. package/src/commands/deleteRange.ts +3 -3
  146. package/src/commands/deleteSelection.ts +3 -3
  147. package/src/commands/enter.ts +3 -3
  148. package/src/commands/exitCode.ts +3 -3
  149. package/src/commands/extendMarkRange.ts +8 -8
  150. package/src/commands/first.ts +3 -3
  151. package/src/commands/focus.ts +54 -12
  152. package/src/commands/forEach.ts +24 -0
  153. package/src/commands/insertContent.ts +23 -0
  154. package/src/commands/insertContentAt.ts +93 -0
  155. package/src/commands/joinBackward.ts +3 -3
  156. package/src/commands/joinForward.ts +3 -3
  157. package/src/commands/keyboardShortcut.ts +3 -3
  158. package/src/commands/lift.ts +3 -3
  159. package/src/commands/liftEmptyBlock.ts +3 -3
  160. package/src/commands/liftListItem.ts +3 -3
  161. package/src/commands/newlineInCode.ts +3 -3
  162. package/src/commands/resetAttributes.ts +61 -0
  163. package/src/commands/scrollIntoView.ts +3 -3
  164. package/src/commands/selectAll.ts +8 -6
  165. package/src/commands/selectNodeBackward.ts +3 -3
  166. package/src/commands/selectNodeForward.ts +3 -3
  167. package/src/commands/selectParentNode.ts +3 -3
  168. package/src/commands/setContent.ts +10 -5
  169. package/src/commands/setMark.ts +33 -10
  170. package/src/commands/setMeta.ts +18 -0
  171. package/src/commands/setNode.ts +3 -3
  172. package/src/commands/setNodeSelection.ts +28 -0
  173. package/src/commands/setTextSelection.ts +32 -0
  174. package/src/commands/sinkListItem.ts +3 -3
  175. package/src/commands/splitBlock.ts +13 -8
  176. package/src/commands/splitListItem.ts +39 -13
  177. package/src/commands/toggleList.ts +4 -4
  178. package/src/commands/toggleMark.ts +15 -5
  179. package/src/commands/toggleNode.ts +3 -3
  180. package/src/commands/toggleWrap.ts +3 -3
  181. package/src/commands/undoInputRule.ts +33 -5
  182. package/src/commands/unsetAllMarks.ts +3 -3
  183. package/src/commands/unsetMark.ts +32 -20
  184. package/src/commands/updateAttributes.ts +72 -0
  185. package/src/commands/wrapIn.ts +3 -3
  186. package/src/commands/wrapInList.ts +3 -3
  187. package/src/extensions/clipboardTextSerializer.ts +11 -35
  188. package/src/extensions/commands.ts +27 -21
  189. package/src/extensions/focusEvents.ts +0 -3
  190. package/src/extensions/index.ts +1 -0
  191. package/src/extensions/keymap.ts +6 -6
  192. package/src/extensions/tabindex.ts +19 -0
  193. package/src/helpers/createChainableState.ts +37 -0
  194. package/src/helpers/createDocument.ts +11 -0
  195. package/src/helpers/createNodeFromContent.ts +56 -0
  196. package/src/helpers/defaultBlockAt.ts +13 -0
  197. package/src/helpers/findChildren.ts +17 -0
  198. package/src/helpers/findChildrenInRange.ts +31 -0
  199. package/src/helpers/generateHTML.ts +3 -3
  200. package/src/helpers/generateJSON.ts +13 -0
  201. package/src/helpers/generateText.ts +29 -0
  202. package/src/helpers/getAttributes.ts +27 -0
  203. package/src/helpers/getAttributesFromExtensions.ts +26 -5
  204. package/src/helpers/getDebugJSON.ts +46 -0
  205. package/src/helpers/getExtensionField.ts +25 -0
  206. package/src/helpers/getHTMLFromFragment.ts +5 -5
  207. package/src/helpers/getMarkAttributes.ts +11 -8
  208. package/src/helpers/getMarkRange.ts +34 -6
  209. package/src/helpers/getMarkType.ts +4 -0
  210. package/src/helpers/getMarksBetween.ts +3 -3
  211. package/src/helpers/getNodeAttributes.ts +6 -7
  212. package/src/helpers/getNodeType.ts +4 -0
  213. package/src/helpers/getRenderedAttributes.ts +3 -5
  214. package/src/helpers/getSchema.ts +5 -132
  215. package/src/helpers/getSchemaByResolvedExtensions.ts +147 -0
  216. package/src/helpers/getSchemaTypeByName.ts +1 -9
  217. package/src/helpers/getSplittedAttributes.ts +3 -3
  218. package/src/helpers/getText.ts +18 -0
  219. package/src/helpers/getTextBetween.ts +45 -0
  220. package/src/helpers/getTextSeralizersFromSchema.ts +9 -0
  221. package/src/helpers/injectExtensionAttributesToParseRule.ts +12 -8
  222. package/src/helpers/isActive.ts +1 -2
  223. package/src/helpers/isExtensionRulesEnabled.ts +15 -0
  224. package/src/helpers/isList.ts +11 -4
  225. package/src/helpers/isMarkActive.ts +40 -16
  226. package/src/helpers/isNodeActive.ts +23 -31
  227. package/src/helpers/posToDOMRect.ts +34 -0
  228. package/src/index.ts +35 -5
  229. package/src/inputRules/markInputRule.ts +57 -38
  230. package/src/inputRules/nodeInputRule.ts +43 -11
  231. package/src/inputRules/textInputRule.ts +35 -0
  232. package/src/inputRules/textblockTypeInputRule.ts +37 -0
  233. package/src/inputRules/wrappingInputRule.ts +59 -0
  234. package/src/pasteRules/markPasteRule.ts +61 -53
  235. package/src/pasteRules/textPasteRule.ts +35 -0
  236. package/src/style.ts +15 -0
  237. package/src/types.ts +119 -37
  238. package/src/utilities/callOrReturn.ts +6 -3
  239. package/src/utilities/createStyleTag.ts +7 -0
  240. package/src/utilities/deleteProps.ts +2 -4
  241. package/src/utilities/elementFromString.ts +3 -4
  242. package/src/utilities/findDuplicates.ts +5 -0
  243. package/src/utilities/fromString.ts +1 -1
  244. package/src/utilities/isClass.ts +2 -2
  245. package/src/utilities/isEmptyObject.ts +2 -2
  246. package/src/utilities/isFunction.ts +3 -0
  247. package/src/utilities/isNumber.ts +3 -0
  248. package/src/utilities/isObject.ts +5 -5
  249. package/src/utilities/isPlainObject.ts +5 -5
  250. package/src/utilities/isRegExp.ts +3 -0
  251. package/src/utilities/isString.ts +3 -0
  252. package/src/utilities/isiOS.ts +12 -0
  253. package/src/utilities/mergeAttributes.ts +2 -3
  254. package/src/utilities/mergeDeep.ts +1 -2
  255. package/src/utilities/objectIncludes.ts +17 -5
  256. package/CHANGELOG.md +0 -294
  257. package/dist/packages/core/src/commands/insertHTML.d.ts +0 -12
  258. package/dist/packages/core/src/commands/insertNode.d.ts +0 -13
  259. package/dist/packages/core/src/commands/insertText.d.ts +0 -12
  260. package/dist/packages/core/src/commands/replace.d.ts +0 -13
  261. package/dist/packages/core/src/commands/replaceRange.d.ts +0 -13
  262. package/dist/packages/core/src/commands/resetNodeAttributes.d.ts +0 -13
  263. package/dist/packages/core/src/commands/updateNodeAttributes.d.ts +0 -13
  264. package/dist/packages/core/src/utilities/removeElement.d.ts +0 -1
  265. package/dist/tiptap-core.bundle.umd.min.js +0 -17
  266. package/dist/tiptap-core.bundle.umd.min.js.map +0 -1
  267. package/src/commands/insertHTML.ts +0 -28
  268. package/src/commands/insertNode.ts +0 -31
  269. package/src/commands/insertText.ts +0 -20
  270. package/src/commands/replace.ts +0 -20
  271. package/src/commands/replaceRange.ts +0 -36
  272. package/src/commands/resetNodeAttributes.ts +0 -31
  273. package/src/commands/updateNodeAttributes.ts +0 -33
  274. package/src/utilities/removeElement.ts +0 -5
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@tiptap/core",
3
3
  "description": "headless rich text editor",
4
- "version": "2.0.0-beta.14",
4
+ "version": "2.0.0-beta.143",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
8
8
  "headless",
9
9
  "wysiwyg",
10
- "text editor"
10
+ "text editor",
11
+ "prosemirror"
11
12
  ],
12
13
  "license": "MIT",
13
14
  "funding": {
@@ -17,7 +18,6 @@
17
18
  "main": "dist/tiptap-core.cjs.js",
18
19
  "umd": "dist/tiptap-core.umd.js",
19
20
  "module": "dist/tiptap-core.esm.js",
20
- "unpkg": "dist/tiptap-core.bundle.umd.min.js",
21
21
  "types": "dist/packages/core/src/index.d.ts",
22
22
  "files": [
23
23
  "src",
@@ -25,21 +25,24 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "@types/prosemirror-commands": "^1.0.4",
28
- "@types/prosemirror-inputrules": "^1.0.4",
29
28
  "@types/prosemirror-keymap": "^1.0.4",
30
- "@types/prosemirror-model": "^1.13.0",
29
+ "@types/prosemirror-model": "^1.13.2",
31
30
  "@types/prosemirror-schema-list": "^1.0.3",
32
- "@types/prosemirror-state": "^1.2.6",
33
- "@types/prosemirror-transform": "^1.1.2",
34
- "@types/prosemirror-view": "^1.17.1",
35
- "prosemirror-commands": "^1.1.7",
36
- "prosemirror-inputrules": "^1.1.3",
37
- "prosemirror-keymap": "^1.1.3",
38
- "prosemirror-model": "^1.13.3",
39
- "prosemirror-schema-list": "^1.1.4",
31
+ "@types/prosemirror-state": "^1.2.8",
32
+ "@types/prosemirror-transform": "^1.1.4",
33
+ "@types/prosemirror-view": "^1.19.1",
34
+ "prosemirror-commands": "^1.1.12",
35
+ "prosemirror-keymap": "^1.1.5",
36
+ "prosemirror-model": "^1.15.0",
37
+ "prosemirror-schema-list": "^1.1.6",
40
38
  "prosemirror-state": "^1.3.4",
41
- "prosemirror-transform": "^1.2.12",
42
- "prosemirror-view": "^1.18.2"
39
+ "prosemirror-transform": "^1.3.3",
40
+ "prosemirror-view": "^1.23.1"
43
41
  },
44
- "gitHead": "5faf1ab10f1f11b28a6738d0373a224b4bab4b91"
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/ueberdosis/tiptap",
45
+ "directory": "packages/core"
46
+ },
47
+ "gitHead": "7a04a32f1a11de0bc2e39d9c6d15626e158a4e39"
45
48
  }
@@ -1,10 +1,11 @@
1
1
  import { EditorState, Transaction } from 'prosemirror-state'
2
2
  import { Editor } from './Editor'
3
+ import createChainableState from './helpers/createChainableState'
3
4
  import {
4
5
  SingleCommands,
5
6
  ChainedCommands,
6
7
  CanCommands,
7
- RawCommands,
8
+ AnyCommands,
8
9
  CommandProps,
9
10
  } from './types'
10
11
 
@@ -12,26 +13,40 @@ export default class CommandManager {
12
13
 
13
14
  editor: Editor
14
15
 
15
- commands: RawCommands
16
+ rawCommands: AnyCommands
16
17
 
17
- constructor(editor: Editor, commands: RawCommands) {
18
- this.editor = editor
19
- this.commands = commands
18
+ customState?: EditorState
19
+
20
+ constructor(props: {
21
+ editor: Editor,
22
+ state?: EditorState,
23
+ }) {
24
+ this.editor = props.editor
25
+ this.rawCommands = this.editor.extensionManager.commands
26
+ this.customState = props.state
27
+ }
28
+
29
+ get hasCustomState(): boolean {
30
+ return !!this.customState
31
+ }
32
+
33
+ get state(): EditorState {
34
+ return this.customState || this.editor.state
20
35
  }
21
36
 
22
- public createCommands(): SingleCommands {
23
- const { commands, editor } = this
24
- const { state, view } = editor
37
+ get commands(): SingleCommands {
38
+ const { rawCommands, editor, state } = this
39
+ const { view } = editor
25
40
  const { tr } = state
26
41
  const props = this.buildProps(tr)
27
42
 
28
43
  return Object.fromEntries(Object
29
- .entries(commands)
44
+ .entries(rawCommands)
30
45
  .map(([name, command]) => {
31
- const method = (...args: any) => {
46
+ const method = (...args: any[]) => {
32
47
  const callback = command(...args)(props)
33
48
 
34
- if (!tr.getMeta('preventDispatch')) {
49
+ if (!tr.getMeta('preventDispatch') && !this.hasCustomState) {
35
50
  view.dispatch(tr)
36
51
  }
37
52
 
@@ -39,27 +54,40 @@ export default class CommandManager {
39
54
  }
40
55
 
41
56
  return [name, method]
42
- })) as SingleCommands
57
+ })) as unknown as SingleCommands
58
+ }
59
+
60
+ get chain(): () => ChainedCommands {
61
+ return () => this.createChain()
62
+ }
63
+
64
+ get can(): () => CanCommands {
65
+ return () => this.createCan()
43
66
  }
44
67
 
45
68
  public createChain(startTr?: Transaction, shouldDispatch = true): ChainedCommands {
46
- const { commands, editor } = this
47
- const { state, view } = editor
69
+ const { rawCommands, editor, state } = this
70
+ const { view } = editor
48
71
  const callbacks: boolean[] = []
49
72
  const hasStartTransaction = !!startTr
50
73
  const tr = startTr || state.tr
51
74
 
52
75
  const run = () => {
53
- if (!hasStartTransaction && shouldDispatch && !tr.getMeta('preventDispatch')) {
76
+ if (
77
+ !hasStartTransaction
78
+ && shouldDispatch
79
+ && !tr.getMeta('preventDispatch')
80
+ && !this.hasCustomState
81
+ ) {
54
82
  view.dispatch(tr)
55
83
  }
56
84
 
57
- return () => callbacks.every(callback => callback === true)
85
+ return callbacks.every(callback => callback === true)
58
86
  }
59
87
 
60
88
  const chain = {
61
- ...Object.fromEntries(Object.entries(commands).map(([name, command]) => {
62
- const chainedCommand = (...args: any[]) => {
89
+ ...Object.fromEntries(Object.entries(rawCommands).map(([name, command]) => {
90
+ const chainedCommand = (...args: never[]) => {
63
91
  const props = this.buildProps(tr, shouldDispatch)
64
92
  const callback = command(...args)(props)
65
93
 
@@ -77,16 +105,15 @@ export default class CommandManager {
77
105
  }
78
106
 
79
107
  public createCan(startTr?: Transaction): CanCommands {
80
- const { commands, editor } = this
81
- const { state } = editor
108
+ const { rawCommands, state } = this
82
109
  const dispatch = undefined
83
110
  const tr = startTr || state.tr
84
111
  const props = this.buildProps(tr, dispatch)
85
112
  const formattedCommands = Object.fromEntries(Object
86
- .entries(commands)
113
+ .entries(rawCommands)
87
114
  .map(([name, command]) => {
88
- return [name, (...args: any[]) => command(...args)({ ...props, dispatch })]
89
- })) as SingleCommands
115
+ return [name, (...args: never[]) => command(...args)({ ...props, dispatch })]
116
+ })) as unknown as SingleCommands
90
117
 
91
118
  return {
92
119
  ...formattedCommands,
@@ -95,8 +122,8 @@ export default class CommandManager {
95
122
  }
96
123
 
97
124
  public buildProps(tr: Transaction, shouldDispatch = true): CommandProps {
98
- const { editor, commands } = this
99
- const { state, view } = editor
125
+ const { rawCommands, editor, state } = this
126
+ const { view } = editor
100
127
 
101
128
  if (state.storedMarks) {
102
129
  tr.setStoredMarks(state.storedMarks)
@@ -106,7 +133,10 @@ export default class CommandManager {
106
133
  tr,
107
134
  editor,
108
135
  view,
109
- state: this.chainableState(tr, state),
136
+ state: createChainableState({
137
+ state,
138
+ transaction: tr,
139
+ }),
110
140
  dispatch: shouldDispatch
111
141
  ? () => undefined
112
142
  : undefined,
@@ -114,46 +144,14 @@ export default class CommandManager {
114
144
  can: () => this.createCan(tr),
115
145
  get commands() {
116
146
  return Object.fromEntries(Object
117
- .entries(commands)
147
+ .entries(rawCommands)
118
148
  .map(([name, command]) => {
119
- return [name, (...args: any[]) => command(...args)(props)]
120
- })) as SingleCommands
149
+ return [name, (...args: never[]) => command(...args)(props)]
150
+ })) as unknown as SingleCommands
121
151
  },
122
152
  }
123
153
 
124
154
  return props
125
155
  }
126
156
 
127
- public chainableState(tr: Transaction, state: EditorState): EditorState {
128
- let { selection } = tr
129
- let { doc } = tr
130
- let { storedMarks } = tr
131
-
132
- return {
133
- ...state,
134
- schema: state.schema,
135
- plugins: state.plugins,
136
- apply: state.apply.bind(state),
137
- applyTransaction: state.applyTransaction.bind(state),
138
- reconfigure: state.reconfigure.bind(state),
139
- toJSON: state.toJSON.bind(state),
140
- get storedMarks() {
141
- return storedMarks
142
- },
143
- get selection() {
144
- return selection
145
- },
146
- get doc() {
147
- return doc
148
- },
149
- get tr() {
150
- selection = tr.selection
151
- doc = tr.doc
152
- storedMarks = tr.storedMarks
153
-
154
- return tr
155
- },
156
- }
157
- }
158
-
159
157
  }
package/src/Editor.ts CHANGED
@@ -1,26 +1,31 @@
1
1
  import {
2
- EditorState, Plugin, PluginKey, Transaction,
2
+ EditorState,
3
+ Plugin,
4
+ PluginKey,
5
+ Transaction,
3
6
  } from 'prosemirror-state'
4
7
  import { EditorView } from 'prosemirror-view'
5
- import { Schema, DOMParser, Node } from 'prosemirror-model'
6
- import elementFromString from './utilities/elementFromString'
7
- import getNodeAttributes from './helpers/getNodeAttributes'
8
- import getMarkAttributes from './helpers/getMarkAttributes'
8
+ import { Schema, MarkType, NodeType } from 'prosemirror-model'
9
+ import getAttributes from './helpers/getAttributes'
9
10
  import isActive from './helpers/isActive'
10
- import removeElement from './utilities/removeElement'
11
+ import createDocument from './helpers/createDocument'
11
12
  import getHTMLFromFragment from './helpers/getHTMLFromFragment'
13
+ import getText from './helpers/getText'
12
14
  import isNodeEmpty from './helpers/isNodeEmpty'
15
+ import getTextSeralizersFromSchema from './helpers/getTextSeralizersFromSchema'
13
16
  import createStyleTag from './utilities/createStyleTag'
17
+ import isFunction from './utilities/isFunction'
14
18
  import CommandManager from './CommandManager'
15
19
  import ExtensionManager from './ExtensionManager'
16
20
  import EventEmitter from './EventEmitter'
17
21
  import {
18
22
  EditorOptions,
19
- Content,
20
23
  CanCommands,
21
24
  ChainedCommands,
25
+ JSONContent,
22
26
  SingleCommands,
23
- AnyObject,
27
+ TextSerializer,
28
+ EditorEvents,
24
29
  } from './types'
25
30
  import * as extensions from './extensions'
26
31
  import style from './style'
@@ -31,7 +36,7 @@ export interface HTMLElement {
31
36
  editor?: Editor
32
37
  }
33
38
 
34
- export class Editor extends EventEmitter {
39
+ export class Editor extends EventEmitter<EditorEvents> {
35
40
 
36
41
  private commandManager!: CommandManager
37
42
 
@@ -45,7 +50,7 @@ export class Editor extends EventEmitter {
45
50
 
46
51
  public isFocused = false
47
52
 
48
- private resizeObserver!: ResizeObserver
53
+ public extensionStorage: Record<string, any> = {}
49
54
 
50
55
  public options: EditorOptions = {
51
56
  element: document.createElement('div'),
@@ -58,14 +63,14 @@ export class Editor extends EventEmitter {
58
63
  parseOptions: {},
59
64
  enableInputRules: true,
60
65
  enablePasteRules: true,
66
+ enableCoreExtensions: true,
67
+ onBeforeCreate: () => null,
61
68
  onCreate: () => null,
62
69
  onUpdate: () => null,
63
70
  onSelectionUpdate: () => null,
64
- onViewUpdate: () => null,
65
71
  onTransaction: () => null,
66
72
  onFocus: () => null,
67
73
  onBlur: () => null,
68
- onResize: () => null,
69
74
  onDestroy: () => null,
70
75
  }
71
76
 
@@ -75,50 +80,54 @@ export class Editor extends EventEmitter {
75
80
  this.createExtensionManager()
76
81
  this.createCommandManager()
77
82
  this.createSchema()
83
+ this.on('beforeCreate', this.options.onBeforeCreate)
84
+ this.emit('beforeCreate', { editor: this })
78
85
  this.createView()
79
86
  this.injectCSS()
80
87
  this.on('create', this.options.onCreate)
81
88
  this.on('update', this.options.onUpdate)
82
89
  this.on('selectionUpdate', this.options.onSelectionUpdate)
83
- this.on('viewUpdate', this.options.onViewUpdate)
84
90
  this.on('transaction', this.options.onTransaction)
85
91
  this.on('focus', this.options.onFocus)
86
92
  this.on('blur', this.options.onBlur)
87
93
  this.on('destroy', this.options.onDestroy)
88
94
 
89
95
  window.setTimeout(() => {
96
+ if (this.isDestroyed) {
97
+ return
98
+ }
99
+
90
100
  this.commands.focus(this.options.autofocus)
91
101
  this.emit('create', { editor: this })
92
-
93
- if (window.ResizeObserver) {
94
- this.resizeObserver = new ResizeObserver(() => {
95
- this.emit('resize', { editor: this })
96
- })
97
- this.resizeObserver.observe(this.view.dom)
98
- }
99
102
  }, 0)
103
+ }
100
104
 
105
+ /**
106
+ * Returns the editor storage.
107
+ */
108
+ public get storage(): Record<string, any> {
109
+ return this.extensionStorage
101
110
  }
102
111
 
103
112
  /**
104
113
  * An object of all registered commands.
105
114
  */
106
115
  public get commands(): SingleCommands {
107
- return this.commandManager.createCommands()
116
+ return this.commandManager.commands
108
117
  }
109
118
 
110
119
  /**
111
120
  * Create a command chain to call multiple commands at once.
112
121
  */
113
122
  public chain(): ChainedCommands {
114
- return this.commandManager.createChain()
123
+ return this.commandManager.chain()
115
124
  }
116
125
 
117
126
  /**
118
127
  * Check if a command or a command chain can be executed. Without executing it.
119
128
  */
120
129
  public can(): CanCommands {
121
- return this.commandManager.createCan()
130
+ return this.commandManager.can()
122
131
  }
123
132
 
124
133
  /**
@@ -136,7 +145,20 @@ export class Editor extends EventEmitter {
136
145
  * @param options A list of options
137
146
  */
138
147
  public setOptions(options: Partial<EditorOptions> = {}): void {
139
- this.options = { ...this.options, ...options }
148
+ this.options = {
149
+ ...this.options,
150
+ ...options,
151
+ }
152
+
153
+ if (!this.view || !this.state || this.isDestroyed) {
154
+ return
155
+ }
156
+
157
+ if (this.options.editorProps) {
158
+ this.view.setProps(this.options.editorProps)
159
+ }
160
+
161
+ this.view.updateState(this.state)
140
162
  }
141
163
 
142
164
  /**
@@ -144,17 +166,18 @@ export class Editor extends EventEmitter {
144
166
  */
145
167
  public setEditable(editable: boolean): void {
146
168
  this.setOptions({ editable })
147
-
148
- if (this.view && this.state && !this.isDestroyed) {
149
- this.view.updateState(this.state)
150
- }
151
169
  }
152
170
 
153
171
  /**
154
172
  * Returns whether the editor is editable.
155
173
  */
156
174
  public get isEditable(): boolean {
157
- return this.view && this.view.editable
175
+ // since plugins are applied after creating the view
176
+ // `editable` is always `true` for one tick.
177
+ // that’s why we also have to check for `options.editable`
178
+ return this.options.editable
179
+ && this.view
180
+ && this.view.editable
158
181
  }
159
182
 
160
183
  /**
@@ -171,7 +194,7 @@ export class Editor extends EventEmitter {
171
194
  * @param handlePlugins Control how to merge the plugin into the existing plugins.
172
195
  */
173
196
  public registerPlugin(plugin: Plugin, handlePlugins?: (newPlugin: Plugin, plugins: Plugin[]) => Plugin[]): void {
174
- const plugins = typeof handlePlugins === 'function'
197
+ const plugins = isFunction(handlePlugins)
175
198
  ? handlePlugins(plugin, this.state.plugins)
176
199
  : [...this.state.plugins, plugin]
177
200
 
@@ -183,7 +206,7 @@ export class Editor extends EventEmitter {
183
206
  /**
184
207
  * Unregister a ProseMirror plugin.
185
208
  *
186
- * @param name The plugins name
209
+ * @param nameOrPluginKey The plugins name
187
210
  */
188
211
  public unregisterPlugin(nameOrPluginKey: string | PluginKey): void {
189
212
  if (this.isDestroyed) {
@@ -207,7 +230,9 @@ export class Editor extends EventEmitter {
207
230
  * Creates an extension manager.
208
231
  */
209
232
  private createExtensionManager(): void {
210
- const coreExtensions = Object.entries(extensions).map(([, extension]) => extension)
233
+ const coreExtensions = this.options.enableCoreExtensions
234
+ ? Object.values(extensions)
235
+ : []
211
236
  const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
212
237
  return ['extension', 'node', 'mark'].includes(extension?.type)
213
238
  })
@@ -219,7 +244,9 @@ export class Editor extends EventEmitter {
219
244
  * Creates an command manager.
220
245
  */
221
246
  private createCommandManager(): void {
222
- this.commandManager = new CommandManager(this, this.extensionManager.commands)
247
+ this.commandManager = new CommandManager({
248
+ editor: this,
249
+ })
223
250
  }
224
251
 
225
252
  /**
@@ -237,23 +264,14 @@ export class Editor extends EventEmitter {
237
264
  ...this.options.editorProps,
238
265
  dispatchTransaction: this.dispatchTransaction.bind(this),
239
266
  state: EditorState.create({
240
- doc: this.createDocument(this.options.content),
267
+ doc: createDocument(this.options.content, this.schema, this.options.parseOptions),
241
268
  }),
242
269
  })
243
270
 
244
271
  // `editor.view` is not yet available at this time.
245
272
  // Therefore we will add all plugins and node views directly afterwards.
246
273
  const newState = this.state.reconfigure({
247
- plugins: [
248
- new Plugin({
249
- view: () => ({
250
- update: () => this.emit('viewUpdate', {
251
- editor: this,
252
- }),
253
- }),
254
- }),
255
- ...this.extensionManager.plugins,
256
- ],
274
+ plugins: this.extensionManager.plugins,
257
275
  })
258
276
 
259
277
  this.view.updateState(newState)
@@ -275,34 +293,6 @@ export class Editor extends EventEmitter {
275
293
  })
276
294
  }
277
295
 
278
- /**
279
- * Creates a ProseMirror document.
280
- */
281
- public createDocument = (content: Content, parseOptions = this.options.parseOptions): Node => {
282
- if (content && typeof content === 'object') {
283
- try {
284
- return this.schema.nodeFromJSON(content)
285
- } catch (error) {
286
- console.warn(
287
- '[tiptap warn]: Invalid content.',
288
- 'Passed value:',
289
- content,
290
- 'Error:',
291
- error,
292
- )
293
- return this.createDocument('')
294
- }
295
- }
296
-
297
- if (typeof content === 'string') {
298
- return DOMParser
299
- .fromSchema(this.schema)
300
- .parse(elementFromString(content), parseOptions)
301
- }
302
-
303
- return this.createDocument('')
304
- }
305
-
306
296
  public isCapturingTransaction = false
307
297
 
308
298
  private capturedTransaction: Transaction | null = null
@@ -325,10 +315,6 @@ export class Editor extends EventEmitter {
325
315
  * @param transaction An editor state transaction
326
316
  */
327
317
  private dispatchTransaction(transaction: Transaction): void {
328
- if (transaction.docChanged && !this.isEditable) {
329
- return
330
- }
331
-
332
318
  if (this.isCapturingTransaction) {
333
319
  if (!this.capturedTransaction) {
334
320
  this.capturedTransaction = transaction
@@ -353,6 +339,7 @@ export class Editor extends EventEmitter {
353
339
  if (selectionHasChanged) {
354
340
  this.emit('selectionUpdate', {
355
341
  editor: this,
342
+ transaction,
356
343
  })
357
344
  }
358
345
 
@@ -363,6 +350,7 @@ export class Editor extends EventEmitter {
363
350
  this.emit('focus', {
364
351
  editor: this,
365
352
  event: focus.event,
353
+ transaction,
366
354
  })
367
355
  }
368
356
 
@@ -370,6 +358,7 @@ export class Editor extends EventEmitter {
370
358
  this.emit('blur', {
371
359
  editor: this,
372
360
  event: blur.event,
361
+ transaction,
373
362
  })
374
363
  }
375
364
 
@@ -384,21 +373,10 @@ export class Editor extends EventEmitter {
384
373
  }
385
374
 
386
375
  /**
387
- * Get attributes of the currently selected node.
388
- *
389
- * @param name Name of the node
376
+ * Get attributes of the currently selected node or mark.
390
377
  */
391
- public getNodeAttributes(name: string): AnyObject {
392
- return getNodeAttributes(this.state, name)
393
- }
394
-
395
- /**
396
- * Get attributes of the currently selected mark.
397
- *
398
- * @param name Name of the mark
399
- */
400
- public getMarkAttributes(name: string): AnyObject {
401
- return getMarkAttributes(this.state, name)
378
+ public getAttributes(nameOrType: string | NodeType | MarkType): Record<string, any> {
379
+ return getAttributes(this.state, nameOrType)
402
380
  }
403
381
 
404
382
  /**
@@ -424,7 +402,7 @@ export class Editor extends EventEmitter {
424
402
  /**
425
403
  * Get the document as JSON.
426
404
  */
427
- public getJSON(): AnyObject {
405
+ public getJSON(): JSONContent {
428
406
  return this.state.doc.toJSON()
429
407
  }
430
408
 
@@ -432,7 +410,28 @@ export class Editor extends EventEmitter {
432
410
  * Get the document as HTML.
433
411
  */
434
412
  public getHTML(): string {
435
- return getHTMLFromFragment(this.state.doc, this.schema)
413
+ return getHTMLFromFragment(this.state.doc.content, this.schema)
414
+ }
415
+
416
+ /**
417
+ * Get the document as text.
418
+ */
419
+ public getText(options?: {
420
+ blockSeparator?: string,
421
+ textSerializers?: Record<string, TextSerializer>,
422
+ }): string {
423
+ const {
424
+ blockSeparator = '\n\n',
425
+ textSerializers = {},
426
+ } = options || {}
427
+
428
+ return getText(this.state.doc, {
429
+ blockSeparator,
430
+ textSerializers: {
431
+ ...textSerializers,
432
+ ...getTextSeralizersFromSchema(this.schema),
433
+ },
434
+ })
436
435
  }
437
436
 
438
437
  /**
@@ -453,8 +452,6 @@ export class Editor extends EventEmitter {
453
452
  * Destroy the editor.
454
453
  */
455
454
  public destroy(): void {
456
- this.resizeObserver?.unobserve(this.view.dom)
457
-
458
455
  this.emit('destroy')
459
456
 
460
457
  if (this.view) {
@@ -462,7 +459,6 @@ export class Editor extends EventEmitter {
462
459
  }
463
460
 
464
461
  this.removeAllListeners()
465
- removeElement(this.css)
466
462
  }
467
463
 
468
464
  /**
@@ -1,8 +1,18 @@
1
- export default class EventEmitter {
1
+ type StringKeyOf<T> = Extract<keyof T, string>
2
+ type CallbackType<
3
+ T extends Record<string, any>,
4
+ EventName extends StringKeyOf<T>,
5
+ > = T[EventName] extends any[] ? T[EventName] : [T[EventName]]
6
+ type CallbackFunction<
7
+ T extends Record<string, any>,
8
+ EventName extends StringKeyOf<T>,
9
+ > = (...props: CallbackType<T, EventName>) => any
10
+
11
+ export default class EventEmitter<T extends Record<string, any>> {
2
12
 
3
13
  private callbacks: { [key: string]: Function[] } = {}
4
14
 
5
- public on(event: string, fn: Function): this {
15
+ public on<EventName extends StringKeyOf<T>>(event: EventName, fn: CallbackFunction<T, EventName>): this {
6
16
  if (!this.callbacks[event]) {
7
17
  this.callbacks[event] = []
8
18
  }
@@ -12,7 +22,7 @@ export default class EventEmitter {
12
22
  return this
13
23
  }
14
24
 
15
- protected emit(event: string, ...args: any): this {
25
+ protected emit<EventName extends StringKeyOf<T>>(event: EventName, ...args: CallbackType<T, EventName>): this {
16
26
  const callbacks = this.callbacks[event]
17
27
 
18
28
  if (callbacks) {
@@ -22,7 +32,7 @@ export default class EventEmitter {
22
32
  return this
23
33
  }
24
34
 
25
- public off(event: string, fn?: Function): this {
35
+ public off<EventName extends StringKeyOf<T>>(event: EventName, fn?: CallbackFunction<T, EventName>): this {
26
36
  const callbacks = this.callbacks[event]
27
37
 
28
38
  if (callbacks) {