@tiptap/core 2.0.0-beta.21 → 2.0.0-beta.210

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 (289) hide show
  1. package/README.md +2 -2
  2. package/dist/index.cjs +4311 -0
  3. package/dist/index.d.ts +2330 -0
  4. package/dist/index.js +4311 -0
  5. package/package.json +39 -25
  6. package/src/CommandManager.ts +76 -86
  7. package/src/Editor.ts +120 -81
  8. package/src/EventEmitter.ts +14 -4
  9. package/src/Extension.ts +280 -108
  10. package/src/ExtensionManager.ts +254 -108
  11. package/src/InputRule.ts +260 -0
  12. package/src/Mark.ts +398 -147
  13. package/src/Node.ts +437 -171
  14. package/src/NodeView.ts +132 -63
  15. package/src/PasteRule.ts +240 -0
  16. package/src/Tracker.ts +38 -0
  17. package/src/commands/blur.ts +12 -6
  18. package/src/commands/clearContent.ts +3 -3
  19. package/src/commands/clearNodes.ts +31 -19
  20. package/src/commands/command.ts +2 -2
  21. package/src/commands/createParagraphNear.ts +5 -4
  22. package/src/commands/deleteCurrentNode.ts +41 -0
  23. package/src/commands/deleteNode.ts +37 -0
  24. package/src/commands/deleteRange.ts +3 -3
  25. package/src/commands/deleteSelection.ts +5 -4
  26. package/src/commands/enter.ts +3 -3
  27. package/src/commands/exitCode.ts +5 -4
  28. package/src/commands/extendMarkRange.ts +16 -12
  29. package/src/commands/first.ts +3 -3
  30. package/src/commands/focus.ts +47 -44
  31. package/src/commands/forEach.ts +24 -0
  32. package/src/commands/index.ts +50 -0
  33. package/src/commands/insertContent.ts +17 -24
  34. package/src/commands/insertContentAt.ts +94 -0
  35. package/src/commands/join.ts +53 -0
  36. package/src/commands/keyboardShortcut.ts +6 -6
  37. package/src/commands/lift.ts +8 -7
  38. package/src/commands/liftEmptyBlock.ts +5 -4
  39. package/src/commands/liftListItem.ts +7 -6
  40. package/src/commands/newlineInCode.ts +5 -4
  41. package/src/commands/resetAttributes.ts +18 -12
  42. package/src/commands/scrollIntoView.ts +3 -3
  43. package/src/commands/selectAll.ts +8 -6
  44. package/src/commands/selectNodeBackward.ts +5 -4
  45. package/src/commands/selectNodeForward.ts +5 -4
  46. package/src/commands/selectParentNode.ts +5 -4
  47. package/src/commands/selectTextblockEnd.ts +20 -0
  48. package/src/commands/selectTextblockStart.ts +20 -0
  49. package/src/commands/setContent.ts +9 -16
  50. package/src/commands/setMark.ts +90 -14
  51. package/src/commands/setMeta.ts +18 -0
  52. package/src/commands/setNode.ts +32 -8
  53. package/src/commands/setNodeSelection.ts +27 -0
  54. package/src/commands/setTextSelection.ts +31 -0
  55. package/src/commands/sinkListItem.ts +7 -6
  56. package/src/commands/splitBlock.ts +31 -41
  57. package/src/commands/splitListItem.ts +46 -29
  58. package/src/commands/toggleList.ts +88 -20
  59. package/src/commands/toggleMark.ts +19 -8
  60. package/src/commands/toggleNode.ts +11 -6
  61. package/src/commands/toggleWrap.ts +10 -10
  62. package/src/commands/undoInputRule.ts +34 -5
  63. package/src/commands/unsetAllMarks.ts +7 -11
  64. package/src/commands/unsetMark.ts +36 -23
  65. package/src/commands/updateAttributes.ts +27 -15
  66. package/src/commands/wrapIn.ts +7 -12
  67. package/src/commands/wrapInList.ts +7 -6
  68. package/src/extensions/clipboardTextSerializer.ts +15 -36
  69. package/src/extensions/commands.ts +3 -147
  70. package/src/extensions/editable.ts +2 -1
  71. package/src/extensions/focusEvents.ts +4 -6
  72. package/src/extensions/index.ts +1 -0
  73. package/src/extensions/keymap.ts +106 -13
  74. package/src/extensions/tabindex.ts +18 -0
  75. package/src/helpers/combineTransactionSteps.ts +21 -0
  76. package/src/helpers/createChainableState.ts +38 -0
  77. package/src/helpers/createDocument.ts +5 -6
  78. package/src/helpers/createNodeFromContent.ts +20 -28
  79. package/src/helpers/defaultBlockAt.ts +13 -0
  80. package/src/helpers/findChildren.ts +18 -0
  81. package/src/helpers/findChildrenInRange.ts +36 -0
  82. package/src/helpers/findParentNode.ts +4 -3
  83. package/src/helpers/findParentNodeClosestToPos.ts +13 -7
  84. package/src/helpers/generateHTML.ts +7 -6
  85. package/src/helpers/generateJSON.ts +12 -0
  86. package/src/helpers/generateText.ts +27 -0
  87. package/src/helpers/getAttributes.ts +26 -0
  88. package/src/helpers/getAttributesFromExtensions.ts +42 -14
  89. package/src/helpers/getChangedRanges.ts +83 -0
  90. package/src/helpers/getDebugJSON.ts +54 -0
  91. package/src/helpers/getExtensionField.ts +25 -0
  92. package/src/helpers/getHTMLFromFragment.ts +5 -6
  93. package/src/helpers/getMarkAttributes.ts +18 -11
  94. package/src/helpers/getMarkRange.ts +41 -8
  95. package/src/helpers/getMarkType.ts +8 -2
  96. package/src/helpers/getMarksBetween.ts +34 -10
  97. package/src/helpers/getNodeAttributes.ts +14 -13
  98. package/src/helpers/getNodeType.ts +8 -2
  99. package/src/helpers/getRenderedAttributes.ts +9 -7
  100. package/src/helpers/getSchema.ts +7 -133
  101. package/src/helpers/getSchemaByResolvedExtensions.ts +192 -0
  102. package/src/helpers/getSchemaTypeByName.ts +3 -11
  103. package/src/helpers/getSchemaTypeNameByName.ts +2 -2
  104. package/src/helpers/getSplittedAttributes.ts +4 -4
  105. package/src/helpers/getText.ts +19 -0
  106. package/src/helpers/getTextBetween.ts +46 -0
  107. package/src/helpers/getTextContentFromNodes.ts +26 -0
  108. package/src/helpers/getTextSerializersFromSchema.ts +11 -0
  109. package/src/helpers/index.ts +33 -0
  110. package/src/helpers/injectExtensionAttributesToParseRule.ts +22 -23
  111. package/src/helpers/isActive.ts +10 -6
  112. package/src/helpers/isExtensionRulesEnabled.ts +15 -0
  113. package/src/helpers/isList.ts +14 -7
  114. package/src/helpers/isMarkActive.ts +49 -27
  115. package/src/helpers/isNodeActive.ts +29 -39
  116. package/src/helpers/isNodeEmpty.ts +2 -2
  117. package/src/helpers/isNodeSelection.ts +3 -4
  118. package/src/helpers/isTextSelection.ts +3 -4
  119. package/src/helpers/posToDOMRect.ts +35 -0
  120. package/src/helpers/resolveFocusPosition.ts +42 -0
  121. package/src/helpers/selectionToInsertionEnd.ts +3 -3
  122. package/src/helpers/splitExtensions.ts +3 -3
  123. package/src/index.ts +15 -24
  124. package/src/inputRules/index.ts +5 -0
  125. package/src/inputRules/markInputRule.ts +59 -40
  126. package/src/inputRules/nodeInputRule.ts +45 -12
  127. package/src/inputRules/textInputRule.ts +35 -0
  128. package/src/inputRules/textblockTypeInputRule.ts +37 -0
  129. package/src/inputRules/wrappingInputRule.ts +59 -0
  130. package/src/pasteRules/index.ts +3 -0
  131. package/src/pasteRules/markPasteRule.ts +61 -53
  132. package/src/pasteRules/nodePasteRule.ts +37 -0
  133. package/src/pasteRules/textPasteRule.ts +35 -0
  134. package/src/style.ts +16 -3
  135. package/src/types.ts +170 -97
  136. package/src/utilities/callOrReturn.ts +6 -3
  137. package/src/utilities/createStyleTag.ts +12 -1
  138. package/src/utilities/deleteProps.ts +2 -4
  139. package/src/utilities/elementFromString.ts +4 -5
  140. package/src/utilities/escapeForRegEx.ts +4 -0
  141. package/src/utilities/findDuplicates.ts +5 -0
  142. package/src/utilities/fromString.ts +2 -2
  143. package/src/utilities/index.ts +20 -0
  144. package/src/utilities/isEmptyObject.ts +2 -2
  145. package/src/utilities/isFunction.ts +3 -0
  146. package/src/utilities/isMacOS.ts +5 -0
  147. package/src/utilities/isNumber.ts +3 -0
  148. package/src/utilities/isPlainObject.ts +8 -5
  149. package/src/utilities/isRegExp.ts +3 -0
  150. package/src/utilities/isString.ts +3 -0
  151. package/src/utilities/isiOS.ts +12 -0
  152. package/src/utilities/mergeAttributes.ts +2 -3
  153. package/src/utilities/mergeDeep.ts +2 -3
  154. package/src/utilities/minMax.ts +1 -1
  155. package/src/utilities/objectIncludes.ts +17 -5
  156. package/src/utilities/removeDuplicates.ts +15 -0
  157. package/CHANGELOG.md +0 -365
  158. package/LICENSE.md +0 -21
  159. package/dist/packages/core/src/CommandManager.d.ts +0 -13
  160. package/dist/packages/core/src/Editor.d.ts +0 -142
  161. package/dist/packages/core/src/EventEmitter.d.ts +0 -7
  162. package/dist/packages/core/src/Extension.d.ts +0 -148
  163. package/dist/packages/core/src/ExtensionManager.d.ts +0 -24
  164. package/dist/packages/core/src/Mark.d.ts +0 -211
  165. package/dist/packages/core/src/Node.d.ts +0 -265
  166. package/dist/packages/core/src/NodeView.d.ts +0 -31
  167. package/dist/packages/core/src/commands/blur.d.ts +0 -12
  168. package/dist/packages/core/src/commands/clearContent.d.ts +0 -12
  169. package/dist/packages/core/src/commands/clearNodes.d.ts +0 -12
  170. package/dist/packages/core/src/commands/command.d.ts +0 -12
  171. package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -12
  172. package/dist/packages/core/src/commands/deleteRange.d.ts +0 -12
  173. package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -12
  174. package/dist/packages/core/src/commands/enter.d.ts +0 -12
  175. package/dist/packages/core/src/commands/exitCode.d.ts +0 -12
  176. package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -13
  177. package/dist/packages/core/src/commands/first.d.ts +0 -12
  178. package/dist/packages/core/src/commands/focus.d.ts +0 -12
  179. package/dist/packages/core/src/commands/insertContent.d.ts +0 -12
  180. package/dist/packages/core/src/commands/insertHTML.d.ts +0 -12
  181. package/dist/packages/core/src/commands/insertNode.d.ts +0 -13
  182. package/dist/packages/core/src/commands/insertText.d.ts +0 -12
  183. package/dist/packages/core/src/commands/joinBackward.d.ts +0 -12
  184. package/dist/packages/core/src/commands/joinForward.d.ts +0 -12
  185. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -12
  186. package/dist/packages/core/src/commands/lift.d.ts +0 -13
  187. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -12
  188. package/dist/packages/core/src/commands/liftListItem.d.ts +0 -13
  189. package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -12
  190. package/dist/packages/core/src/commands/replace.d.ts +0 -13
  191. package/dist/packages/core/src/commands/replaceRange.d.ts +0 -13
  192. package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -13
  193. package/dist/packages/core/src/commands/resetNodeAttributes.d.ts +0 -13
  194. package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -12
  195. package/dist/packages/core/src/commands/selectAll.d.ts +0 -12
  196. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -12
  197. package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -12
  198. package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -12
  199. package/dist/packages/core/src/commands/setContent.d.ts +0 -12
  200. package/dist/packages/core/src/commands/setMark.d.ts +0 -13
  201. package/dist/packages/core/src/commands/setNode.d.ts +0 -13
  202. package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -13
  203. package/dist/packages/core/src/commands/splitBlock.d.ts +0 -14
  204. package/dist/packages/core/src/commands/splitListItem.d.ts +0 -13
  205. package/dist/packages/core/src/commands/toggleList.d.ts +0 -13
  206. package/dist/packages/core/src/commands/toggleMark.d.ts +0 -13
  207. package/dist/packages/core/src/commands/toggleNode.d.ts +0 -13
  208. package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -13
  209. package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -12
  210. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -12
  211. package/dist/packages/core/src/commands/unsetMark.d.ts +0 -13
  212. package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -13
  213. package/dist/packages/core/src/commands/updateNodeAttributes.d.ts +0 -13
  214. package/dist/packages/core/src/commands/wrapIn.d.ts +0 -13
  215. package/dist/packages/core/src/commands/wrapInList.d.ts +0 -13
  216. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -2
  217. package/dist/packages/core/src/extensions/commands.d.ts +0 -100
  218. package/dist/packages/core/src/extensions/editable.d.ts +0 -2
  219. package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
  220. package/dist/packages/core/src/extensions/index.d.ts +0 -5
  221. package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
  222. package/dist/packages/core/src/helpers/createDocument.d.ts +0 -4
  223. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -8
  224. package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -9
  225. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -8
  226. package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -2
  227. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
  228. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
  229. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -4
  230. package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
  231. package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
  232. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
  233. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -4
  234. package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
  235. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
  236. package/dist/packages/core/src/helpers/getSchema.d.ts +0 -3
  237. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -2
  238. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -2
  239. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -2
  240. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
  241. package/dist/packages/core/src/helpers/isActive.d.ts +0 -3
  242. package/dist/packages/core/src/helpers/isList.d.ts +0 -2
  243. package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -4
  244. package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -4
  245. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -2
  246. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
  247. package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
  248. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
  249. package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
  250. package/dist/packages/core/src/index.d.ts +0 -30
  251. package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -3
  252. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -3
  253. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -3
  254. package/dist/packages/core/src/style.d.ts +0 -2
  255. package/dist/packages/core/src/types.d.ts +0 -154
  256. package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -8
  257. package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
  258. package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -7
  259. package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
  260. package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
  261. package/dist/packages/core/src/utilities/isClass.d.ts +0 -1
  262. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
  263. package/dist/packages/core/src/utilities/isObject.d.ts +0 -1
  264. package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
  265. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -2
  266. package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -2
  267. package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
  268. package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -7
  269. package/dist/packages/core/src/utilities/removeElement.d.ts +0 -1
  270. package/dist/tiptap-core.bundle.umd.min.js +0 -17
  271. package/dist/tiptap-core.bundle.umd.min.js.map +0 -1
  272. package/dist/tiptap-core.cjs.js +0 -3027
  273. package/dist/tiptap-core.cjs.js.map +0 -1
  274. package/dist/tiptap-core.esm.js +0 -3002
  275. package/dist/tiptap-core.esm.js.map +0 -1
  276. package/dist/tiptap-core.umd.js +0 -3024
  277. package/dist/tiptap-core.umd.js.map +0 -1
  278. package/src/commands/insertHTML.ts +0 -30
  279. package/src/commands/insertNode.ts +0 -33
  280. package/src/commands/insertText.ts +0 -22
  281. package/src/commands/joinBackward.ts +0 -17
  282. package/src/commands/joinForward.ts +0 -17
  283. package/src/commands/replace.ts +0 -20
  284. package/src/commands/replaceRange.ts +0 -36
  285. package/src/commands/resetNodeAttributes.ts +0 -33
  286. package/src/commands/updateNodeAttributes.ts +0 -35
  287. package/src/utilities/isClass.ts +0 -7
  288. package/src/utilities/isObject.ts +0 -10
  289. package/src/utilities/removeElement.ts +0 -5
@@ -1,16 +1,20 @@
1
- import { NodeType, MarkType } from 'prosemirror-model'
2
- import getNodeType from '../helpers/getNodeType'
3
- import getMarkType from '../helpers/getMarkType'
4
- import getSchemaTypeNameByName from '../helpers/getSchemaTypeNameByName'
5
- import { AnyObject, Command, RawCommands } from '../types'
1
+ import { MarkType, NodeType } from '@tiptap/pm/model'
2
+
3
+ import { getMarkType } from '../helpers/getMarkType'
4
+ import { getNodeType } from '../helpers/getNodeType'
5
+ import { getSchemaTypeNameByName } from '../helpers/getSchemaTypeNameByName'
6
+ import { RawCommands } from '../types'
6
7
 
7
8
  declare module '@tiptap/core' {
8
- interface Commands {
9
+ interface Commands<ReturnType> {
9
10
  updateAttributes: {
10
11
  /**
11
12
  * Update attributes of a node or mark.
12
13
  */
13
- updateAttributes: (typeOrName: string | NodeType | MarkType, attributes: AnyObject) => Command,
14
+ updateAttributes: (
15
+ typeOrName: string | NodeType | MarkType,
16
+ attributes: Record<string, any>,
17
+ ) => ReturnType
14
18
  }
15
19
  }
16
20
  }
@@ -20,9 +24,7 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
20
24
  let markType: MarkType | null = null
21
25
 
22
26
  const schemaType = getSchemaTypeNameByName(
23
- typeof typeOrName === 'string'
24
- ? typeOrName
25
- : typeOrName.name,
27
+ typeof typeOrName === 'string' ? typeOrName : typeOrName.name,
26
28
  state.schema,
27
29
  )
28
30
 
@@ -40,7 +42,10 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
40
42
 
41
43
  if (dispatch) {
42
44
  tr.selection.ranges.forEach(range => {
43
- state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
45
+ const from = range.$from.pos
46
+ const to = range.$to.pos
47
+
48
+ state.doc.nodesBetween(from, to, (node, pos) => {
44
49
  if (nodeType && nodeType === node.type) {
45
50
  tr.setNodeMarkup(pos, undefined, {
46
51
  ...node.attrs,
@@ -51,10 +56,17 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
51
56
  if (markType && node.marks.length) {
52
57
  node.marks.forEach(mark => {
53
58
  if (markType === mark.type) {
54
- tr.addMark(pos, pos + node.nodeSize, markType.create({
55
- ...mark.attrs,
56
- ...attributes,
57
- }))
59
+ const trimmedFrom = Math.max(pos, from)
60
+ const trimmedTo = Math.min(pos + node.nodeSize, to)
61
+
62
+ tr.addMark(
63
+ trimmedFrom,
64
+ trimmedTo,
65
+ markType.create({
66
+ ...mark.attrs,
67
+ ...attributes,
68
+ }),
69
+ )
58
70
  }
59
71
  })
60
72
  }
@@ -1,27 +1,22 @@
1
- import { wrapIn as originalWrapIn } from 'prosemirror-commands'
2
- import { NodeType } from 'prosemirror-model'
3
- import { AnyObject, Command, RawCommands } from '../types'
4
- import isNodeActive from '../helpers/isNodeActive'
5
- import getNodeType from '../helpers/getNodeType'
1
+ import { wrapIn as originalWrapIn } from '@tiptap/pm/commands'
2
+ import { NodeType } from '@tiptap/pm/model'
3
+
4
+ import { getNodeType } from '../helpers/getNodeType'
5
+ import { RawCommands } from '../types'
6
6
 
7
7
  declare module '@tiptap/core' {
8
- interface Commands {
8
+ interface Commands<ReturnType> {
9
9
  wrapIn: {
10
10
  /**
11
11
  * Wraps nodes in another node.
12
12
  */
13
- wrapIn: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
13
+ wrapIn: (typeOrName: string | NodeType, attributes?: Record<string, any>) => ReturnType
14
14
  }
15
15
  }
16
16
  }
17
17
 
18
18
  export const wrapIn: RawCommands['wrapIn'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
19
19
  const type = getNodeType(typeOrName, state.schema)
20
- const isActive = isNodeActive(state, type, attributes)
21
-
22
- if (isActive) {
23
- return false
24
- }
25
20
 
26
21
  return originalWrapIn(type, attributes)(state, dispatch)
27
22
  }
@@ -1,15 +1,16 @@
1
- import { wrapInList as originalWrapInList } from 'prosemirror-schema-list'
2
- import { NodeType } from 'prosemirror-model'
3
- import { AnyObject, Command, RawCommands } from '../types'
4
- import getNodeType from '../helpers/getNodeType'
1
+ import { NodeType } from '@tiptap/pm/model'
2
+ import { wrapInList as originalWrapInList } from '@tiptap/pm/schema-list'
3
+
4
+ import { getNodeType } from '../helpers/getNodeType'
5
+ import { RawCommands } from '../types'
5
6
 
6
7
  declare module '@tiptap/core' {
7
- interface Commands {
8
+ interface Commands<ReturnType> {
8
9
  wrapInList: {
9
10
  /**
10
11
  * Wrap a node in a list.
11
12
  */
12
- wrapInList: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
13
+ wrapInList: (typeOrName: string | NodeType, attributes?: Record<string, any>) => ReturnType
13
14
  }
14
15
  }
15
16
  }
@@ -1,40 +1,11 @@
1
- import { Editor } from '@tiptap/core'
2
- import { Plugin, PluginKey } from 'prosemirror-state'
3
- import { Extension } from '../Extension'
4
-
5
- const textBetween = (
6
- editor: Editor,
7
- from: number,
8
- to: number,
9
- blockSeparator?: string,
10
- leafText?: string,
11
- ): string => {
12
- let text = ''
13
- let separated = true
14
-
15
- editor.state.doc.nodesBetween(from, to, (node, pos) => {
16
- const textSerializer = editor.extensionManager.textSerializers[node.type.name]
1
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
17
2
 
18
- if (textSerializer) {
19
- text += textSerializer({ node })
20
- separated = !blockSeparator
21
- } else if (node.isText) {
22
- text += node?.text?.slice(Math.max(from, pos) - pos, to - pos)
23
- separated = !blockSeparator
24
- } else if (node.isLeaf && leafText) {
25
- text += leafText
26
- separated = !blockSeparator
27
- } else if (!separated && node.isBlock) {
28
- text += blockSeparator
29
- separated = true
30
- }
31
- }, 0)
32
-
33
- return text
34
- }
3
+ import { Extension } from '../Extension'
4
+ import { getTextBetween } from '../helpers/getTextBetween'
5
+ import { getTextSerializersFromSchema } from '../helpers/getTextSerializersFromSchema'
35
6
 
36
7
  export const ClipboardTextSerializer = Extension.create({
37
- name: 'editable',
8
+ name: 'clipboardTextSerializer',
38
9
 
39
10
  addProseMirrorPlugins() {
40
11
  return [
@@ -43,9 +14,17 @@ export const ClipboardTextSerializer = Extension.create({
43
14
  props: {
44
15
  clipboardTextSerializer: () => {
45
16
  const { editor } = this
46
- const { from, to } = editor.state.selection
17
+ const { state, schema } = editor
18
+ const { doc, selection } = state
19
+ const { ranges } = selection
20
+ const from = Math.min(...ranges.map(range => range.$from.pos))
21
+ const to = Math.max(...ranges.map(range => range.$to.pos))
22
+ const textSerializers = getTextSerializersFromSchema(schema)
23
+ const range = { from, to }
47
24
 
48
- return textBetween(editor, from, to, '\n')
25
+ return getTextBetween(doc, range, {
26
+ textSerializers,
27
+ })
49
28
  },
50
29
  },
51
30
  }),
@@ -1,158 +1,14 @@
1
+ import * as commands from '../commands'
1
2
  import { Extension } from '../Extension'
2
- import * as blur from '../commands/blur'
3
- import * as clearContent from '../commands/clearContent'
4
- import * as clearNodes from '../commands/clearNodes'
5
- import * as command from '../commands/command'
6
- import * as createParagraphNear from '../commands/createParagraphNear'
7
- import * as deleteRange from '../commands/deleteRange'
8
- import * as deleteSelection from '../commands/deleteSelection'
9
- import * as enter from '../commands/enter'
10
- import * as exitCode from '../commands/exitCode'
11
- import * as extendMarkRange from '../commands/extendMarkRange'
12
- import * as first from '../commands/first'
13
- import * as focus from '../commands/focus'
14
- import * as insertContent from '../commands/insertContent'
15
- import * as insertHTML from '../commands/insertHTML'
16
- import * as insertNode from '../commands/insertNode'
17
- import * as insertText from '../commands/insertText'
18
- import * as joinBackward from '../commands/joinBackward'
19
- import * as joinForward from '../commands/joinForward'
20
- import * as keyboardShortcut from '../commands/keyboardShortcut'
21
- import * as lift from '../commands/lift'
22
- import * as liftEmptyBlock from '../commands/liftEmptyBlock'
23
- import * as liftListItem from '../commands/liftListItem'
24
- import * as newlineInCode from '../commands/newlineInCode'
25
- import * as replace from '../commands/replace'
26
- import * as replaceRange from '../commands/replaceRange'
27
- import * as resetAttributes from '../commands/resetAttributes'
28
- import * as resetNodeAttributes from '../commands/resetNodeAttributes'
29
- import * as scrollIntoView from '../commands/scrollIntoView'
30
- import * as selectAll from '../commands/selectAll'
31
- import * as selectNodeBackward from '../commands/selectNodeBackward'
32
- import * as selectNodeForward from '../commands/selectNodeForward'
33
- import * as selectParentNode from '../commands/selectParentNode'
34
- import * as setContent from '../commands/setContent'
35
- import * as setMark from '../commands/setMark'
36
- import * as setNode from '../commands/setNode'
37
- import * as sinkListItem from '../commands/sinkListItem'
38
- import * as splitBlock from '../commands/splitBlock'
39
- import * as splitListItem from '../commands/splitListItem'
40
- import * as toggleList from '../commands/toggleList'
41
- import * as toggleMark from '../commands/toggleMark'
42
- import * as toggleNode from '../commands/toggleNode'
43
- import * as toggleWrap from '../commands/toggleWrap'
44
- import * as undoInputRule from '../commands/undoInputRule'
45
- import * as unsetAllMarks from '../commands/unsetAllMarks'
46
- import * as unsetMark from '../commands/unsetMark'
47
- import * as updateAttributes from '../commands/updateAttributes'
48
- import * as updateNodeAttributes from '../commands/updateNodeAttributes'
49
- import * as wrapIn from '../commands/wrapIn'
50
- import * as wrapInList from '../commands/wrapInList'
51
3
 
52
- export { blur }
53
- export { clearContent }
54
- export { clearNodes }
55
- export { command }
56
- export { createParagraphNear }
57
- export { deleteRange }
58
- export { deleteSelection }
59
- export { enter }
60
- export { exitCode }
61
- export { extendMarkRange }
62
- export { first }
63
- export { focus }
64
- export { insertContent }
65
- export { insertHTML }
66
- export { insertNode }
67
- export { insertText }
68
- export { joinBackward }
69
- export { joinForward }
70
- export { keyboardShortcut }
71
- export { lift }
72
- export { liftEmptyBlock }
73
- export { liftListItem }
74
- export { newlineInCode }
75
- export { replace }
76
- export { replaceRange }
77
- export { resetAttributes }
78
- export { resetNodeAttributes }
79
- export { scrollIntoView }
80
- export { selectAll }
81
- export { selectNodeBackward }
82
- export { selectNodeForward }
83
- export { selectParentNode }
84
- export { setContent }
85
- export { setMark }
86
- export { setNode }
87
- export { sinkListItem }
88
- export { splitBlock }
89
- export { splitListItem }
90
- export { toggleList }
91
- export { toggleMark }
92
- export { toggleNode }
93
- export { toggleWrap }
94
- export { undoInputRule }
95
- export { unsetAllMarks }
96
- export { unsetMark }
97
- export { updateAttributes }
98
- export { updateNodeAttributes }
99
- export { wrapIn }
100
- export { wrapInList }
4
+ export * from '../commands'
101
5
 
102
6
  export const Commands = Extension.create({
103
7
  name: 'commands',
104
8
 
105
9
  addCommands() {
106
10
  return {
107
- ...blur,
108
- ...clearContent,
109
- ...clearNodes,
110
- ...command,
111
- ...createParagraphNear,
112
- ...deleteRange,
113
- ...deleteSelection,
114
- ...enter,
115
- ...exitCode,
116
- ...extendMarkRange,
117
- ...first,
118
- ...focus,
119
- ...insertContent,
120
- ...insertHTML,
121
- ...insertNode,
122
- ...insertText,
123
- ...joinBackward,
124
- ...joinForward,
125
- ...keyboardShortcut,
126
- ...lift,
127
- ...liftEmptyBlock,
128
- ...liftListItem,
129
- ...newlineInCode,
130
- ...replace,
131
- ...replaceRange,
132
- ...resetAttributes,
133
- ...resetNodeAttributes,
134
- ...scrollIntoView,
135
- ...selectAll,
136
- ...selectNodeBackward,
137
- ...selectNodeForward,
138
- ...selectParentNode,
139
- ...setContent,
140
- ...setMark,
141
- ...setNode,
142
- ...sinkListItem,
143
- ...splitBlock,
144
- ...splitListItem,
145
- ...toggleList,
146
- ...toggleMark,
147
- ...toggleNode,
148
- ...toggleWrap,
149
- ...undoInputRule,
150
- ...unsetAllMarks,
151
- ...unsetMark,
152
- ...updateAttributes,
153
- ...updateNodeAttributes,
154
- ...wrapIn,
155
- ...wrapInList,
11
+ ...commands,
156
12
  }
157
13
  },
158
14
  })
@@ -1,4 +1,5 @@
1
- import { Plugin, PluginKey } from 'prosemirror-state'
1
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
2
+
2
3
  import { Extension } from '../Extension'
3
4
 
4
5
  export const Editable = Extension.create({
@@ -1,4 +1,5 @@
1
- import { Plugin, PluginKey } from 'prosemirror-state'
1
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
2
+
2
3
  import { Extension } from '../Extension'
3
4
 
4
5
  export const FocusEvents = Extension.create({
@@ -11,11 +12,8 @@ export const FocusEvents = Extension.create({
11
12
  new Plugin({
12
13
  key: new PluginKey('focusEvents'),
13
14
  props: {
14
- attributes: {
15
- tabindex: '0',
16
- },
17
15
  handleDOMEvents: {
18
- focus: (view, event) => {
16
+ focus: (view, event: Event) => {
19
17
  editor.isFocused = true
20
18
 
21
19
  const transaction = editor.state.tr
@@ -26,7 +24,7 @@ export const FocusEvents = Extension.create({
26
24
 
27
25
  return false
28
26
  },
29
- blur: (view, event) => {
27
+ blur: (view, event: Event) => {
30
28
  editor.isFocused = false
31
29
 
32
30
  const transaction = editor.state.tr
@@ -3,3 +3,4 @@ export { Commands } from './commands'
3
3
  export { Editable } from './editable'
4
4
  export { FocusEvents } from './focusEvents'
5
5
  export { Keymap } from './keymap'
6
+ export { Tabindex } from './tabindex'
@@ -1,4 +1,10 @@
1
+ import { Plugin, PluginKey, Selection } from '@tiptap/pm/state'
2
+
3
+ import { CommandManager } from '../CommandManager'
1
4
  import { Extension } from '../Extension'
5
+ import { createChainableState } from '../helpers/createChainableState'
6
+ import { isiOS } from '../utilities/isiOS'
7
+ import { isMacOS } from '../utilities/isMacOS'
2
8
 
3
9
  export const Keymap = Extension.create({
4
10
  name: 'keymap',
@@ -6,6 +12,19 @@ export const Keymap = Extension.create({
6
12
  addKeyboardShortcuts() {
7
13
  const handleBackspace = () => this.editor.commands.first(({ commands }) => [
8
14
  () => commands.undoInputRule(),
15
+ // maybe convert first text block node to default node
16
+ () => commands.command(({ tr }) => {
17
+ const { selection, doc } = tr
18
+ const { empty, $anchor } = selection
19
+ const { pos, parent } = $anchor
20
+ const isAtStart = Selection.atStart(doc).from === pos
21
+
22
+ if (!empty || !isAtStart || !parent.type.isTextblock || parent.textContent.length) {
23
+ return false
24
+ }
25
+
26
+ return commands.clearNodes()
27
+ }),
9
28
  () => commands.deleteSelection(),
10
29
  () => commands.joinBackward(),
11
30
  () => commands.selectNodeBackward(),
@@ -13,24 +32,98 @@ export const Keymap = Extension.create({
13
32
 
14
33
  const handleDelete = () => this.editor.commands.first(({ commands }) => [
15
34
  () => commands.deleteSelection(),
35
+ () => commands.deleteCurrentNode(),
16
36
  () => commands.joinForward(),
17
37
  () => commands.selectNodeForward(),
18
38
  ])
19
39
 
20
- return {
21
- Enter: () => this.editor.commands.first(({ commands }) => [
22
- () => commands.newlineInCode(),
23
- () => commands.createParagraphNear(),
24
- () => commands.liftEmptyBlock(),
25
- () => commands.splitBlock(),
26
- ]),
40
+ const handleEnter = () => this.editor.commands.first(({ commands }) => [
41
+ () => commands.newlineInCode(),
42
+ () => commands.createParagraphNear(),
43
+ () => commands.liftEmptyBlock(),
44
+ () => commands.splitBlock(),
45
+ ])
46
+
47
+ const baseKeymap = {
48
+ Enter: handleEnter,
27
49
  'Mod-Enter': () => this.editor.commands.exitCode(),
28
- Backspace: () => handleBackspace(),
29
- 'Mod-Backspace': () => handleBackspace(),
30
- Delete: () => handleDelete(),
31
- 'Mod-Delete': () => handleDelete(),
32
- // we don’t need a custom `selectAll` for now
33
- // 'Mod-a': () => this.editor.commands.selectAll(),
50
+ Backspace: handleBackspace,
51
+ 'Mod-Backspace': handleBackspace,
52
+ 'Shift-Backspace': handleBackspace,
53
+ Delete: handleDelete,
54
+ 'Mod-Delete': handleDelete,
55
+ 'Mod-a': () => this.editor.commands.selectAll(),
56
+ }
57
+
58
+ const pcKeymap = {
59
+ ...baseKeymap,
60
+ }
61
+
62
+ const macKeymap = {
63
+ ...baseKeymap,
64
+ 'Ctrl-h': handleBackspace,
65
+ 'Alt-Backspace': handleBackspace,
66
+ 'Ctrl-d': handleDelete,
67
+ 'Ctrl-Alt-Backspace': handleDelete,
68
+ 'Alt-Delete': handleDelete,
69
+ 'Alt-d': handleDelete,
70
+ 'Ctrl-a': () => this.editor.commands.selectTextblockStart(),
71
+ 'Ctrl-e': () => this.editor.commands.selectTextblockEnd(),
72
+ }
73
+
74
+ if (isiOS() || isMacOS()) {
75
+ return macKeymap
34
76
  }
77
+
78
+ return pcKeymap
79
+ },
80
+
81
+ addProseMirrorPlugins() {
82
+ return [
83
+ // With this plugin we check if the whole document was selected and deleted.
84
+ // In this case we will additionally call `clearNodes()` to convert e.g. a heading
85
+ // to a paragraph if necessary.
86
+ // This is an alternative to ProseMirror's `AllSelection`, which doesn’t work well
87
+ // with many other commands.
88
+ new Plugin({
89
+ key: new PluginKey('clearDocument'),
90
+ appendTransaction: (transactions, oldState, newState) => {
91
+ const docChanges = transactions.some(transaction => transaction.docChanged)
92
+ && !oldState.doc.eq(newState.doc)
93
+
94
+ if (!docChanges) {
95
+ return
96
+ }
97
+
98
+ const { empty, from, to } = oldState.selection
99
+ const allFrom = Selection.atStart(oldState.doc).from
100
+ const allEnd = Selection.atEnd(oldState.doc).to
101
+ const allWasSelected = from === allFrom && to === allEnd
102
+ const isEmpty = newState.doc.textBetween(0, newState.doc.content.size, ' ', ' ').length === 0
103
+
104
+ if (empty || !allWasSelected || !isEmpty) {
105
+ return
106
+ }
107
+
108
+ const tr = newState.tr
109
+ const state = createChainableState({
110
+ state: newState,
111
+ transaction: tr,
112
+ })
113
+ const { commands } = new CommandManager({
114
+ editor: this.editor,
115
+ state,
116
+ })
117
+
118
+ commands.clearNodes()
119
+
120
+ if (!tr.steps.length) {
121
+ return
122
+ }
123
+
124
+ return tr
125
+ },
126
+ }),
127
+ ]
35
128
  },
36
129
  })
@@ -0,0 +1,18 @@
1
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
2
+
3
+ import { Extension } from '../Extension'
4
+
5
+ export const Tabindex = Extension.create({
6
+ name: 'tabindex',
7
+
8
+ addProseMirrorPlugins() {
9
+ return [
10
+ new Plugin({
11
+ key: new PluginKey('tabindex'),
12
+ props: {
13
+ attributes: this.editor.isEditable ? { tabindex: '0' } : {},
14
+ },
15
+ }),
16
+ ]
17
+ },
18
+ })
@@ -0,0 +1,21 @@
1
+ import { Node as ProseMirrorNode } from '@tiptap/pm/model'
2
+ import { Transaction } from '@tiptap/pm/state'
3
+ import { Transform } from '@tiptap/pm/transform'
4
+
5
+ /**
6
+ * Returns a new `Transform` based on all steps of the passed transactions.
7
+ */
8
+ export function combineTransactionSteps(
9
+ oldDoc: ProseMirrorNode,
10
+ transactions: Transaction[],
11
+ ): Transform {
12
+ const transform = new Transform(oldDoc)
13
+
14
+ transactions.forEach(transaction => {
15
+ transaction.steps.forEach(step => {
16
+ transform.step(step)
17
+ })
18
+ })
19
+
20
+ return transform
21
+ }
@@ -0,0 +1,38 @@
1
+ import { EditorState, Transaction } from '@tiptap/pm/state'
2
+
3
+ export function createChainableState(config: {
4
+ transaction: Transaction
5
+ state: EditorState
6
+ }): EditorState {
7
+ const { state, transaction } = config
8
+ let { selection } = transaction
9
+ let { doc } = transaction
10
+ let { storedMarks } = transaction
11
+
12
+ return {
13
+ ...state,
14
+ apply: state.apply.bind(state),
15
+ applyTransaction: state.applyTransaction.bind(state),
16
+ filterTransaction: state.filterTransaction,
17
+ plugins: state.plugins,
18
+ schema: state.schema,
19
+ reconfigure: state.reconfigure.bind(state),
20
+ toJSON: state.toJSON.bind(state),
21
+ get storedMarks() {
22
+ return storedMarks
23
+ },
24
+ get selection() {
25
+ return selection
26
+ },
27
+ get doc() {
28
+ return doc
29
+ },
30
+ get tr() {
31
+ selection = transaction.selection
32
+ doc = transaction.doc
33
+ storedMarks = transaction.storedMarks
34
+
35
+ return transaction
36
+ },
37
+ }
38
+ }
@@ -1,13 +1,12 @@
1
- import { Schema, Node as ProseMirrorNode } from 'prosemirror-model'
2
- import { AnyObject } from '../types'
3
- import createNodeFromContent from './createNodeFromContent'
1
+ import { Node as ProseMirrorNode, ParseOptions, Schema } from '@tiptap/pm/model'
4
2
 
5
- export type Content = string | JSON | null
3
+ import { Content } from '../types'
4
+ import { createNodeFromContent } from './createNodeFromContent'
6
5
 
7
- export default function createDocument(
6
+ export function createDocument(
8
7
  content: Content,
9
8
  schema: Schema,
10
- parseOptions: AnyObject = {},
9
+ parseOptions: ParseOptions = {},
11
10
  ): ProseMirrorNode {
12
11
  return createNodeFromContent(content, schema, { slice: false, parseOptions }) as ProseMirrorNode
13
12
  }