@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,50 +1,69 @@
1
- import { InputRule } from 'prosemirror-inputrules'
2
- import { MarkType } from 'prosemirror-model'
3
- import getMarksBetween from '../helpers/getMarksBetween'
4
-
5
- export default function (regexp: RegExp, markType: MarkType, getAttributes?: Function): InputRule {
6
- return new InputRule(regexp, (state, match, start, end) => {
7
- const attributes = getAttributes instanceof Function
8
- ? getAttributes(match)
9
- : getAttributes
10
- const { tr } = state
11
- const captureGroup = match[match.length - 1]
12
- const fullMatch = match[0]
13
- let markEnd = end
14
-
15
- if (captureGroup) {
16
- const startSpaces = fullMatch.search(/\S/)
17
- const textStart = start + fullMatch.indexOf(captureGroup)
18
- const textEnd = textStart + captureGroup.length
19
-
20
- const excludedMarks = getMarksBetween(start, end, state)
21
- .filter(item => {
22
- // TODO: PR to add excluded to MarkType
23
- // @ts-ignore
24
- const { excluded } = item.mark.type
25
- return excluded.find((type: MarkType) => type.name === markType.name)
26
- })
27
- .filter(item => item.to > textStart)
28
-
29
- if (excludedMarks.length) {
1
+ import { MarkType } from '@tiptap/pm/model'
2
+
3
+ import { getMarksBetween } from '../helpers/getMarksBetween'
4
+ import { InputRule, InputRuleFinder } from '../InputRule'
5
+ import { ExtendedRegExpMatchArray } from '../types'
6
+ import { callOrReturn } from '../utilities/callOrReturn'
7
+
8
+ /**
9
+ * Build an input rule that adds a mark when the
10
+ * matched text is typed into it.
11
+ */
12
+ export function markInputRule(config: {
13
+ find: InputRuleFinder
14
+ type: MarkType
15
+ getAttributes?:
16
+ | Record<string, any>
17
+ | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
18
+ | false
19
+ | null
20
+ }) {
21
+ return new InputRule({
22
+ find: config.find,
23
+ handler: ({ state, range, match }) => {
24
+ const attributes = callOrReturn(config.getAttributes, undefined, match)
25
+
26
+ if (attributes === false || attributes === null) {
30
27
  return null
31
28
  }
32
29
 
33
- if (textEnd < end) {
34
- tr.delete(textEnd, end)
35
- }
30
+ const { tr } = state
31
+ const captureGroup = match[match.length - 1]
32
+ const fullMatch = match[0]
33
+ let markEnd = range.to
36
34
 
37
- if (textStart > start) {
38
- tr.delete(start + startSpaces, textStart)
39
- }
35
+ if (captureGroup) {
36
+ const startSpaces = fullMatch.search(/\S/)
37
+ const textStart = range.from + fullMatch.indexOf(captureGroup)
38
+ const textEnd = textStart + captureGroup.length
40
39
 
41
- markEnd = start + startSpaces + captureGroup.length
40
+ const excludedMarks = getMarksBetween(range.from, range.to, state.doc)
41
+ .filter(item => {
42
+ // @ts-ignore
43
+ const excluded = item.mark.type.excluded as MarkType[]
42
44
 
43
- tr.addMark(start + startSpaces, markEnd, markType.create(attributes))
45
+ return excluded.find(type => type === config.type && type !== item.mark.type)
46
+ })
47
+ .filter(item => item.to > textStart)
44
48
 
45
- tr.removeStoredMark(markType)
46
- }
49
+ if (excludedMarks.length) {
50
+ return null
51
+ }
47
52
 
48
- return tr
53
+ if (textEnd < range.to) {
54
+ tr.delete(textEnd, range.to)
55
+ }
56
+
57
+ if (textStart > range.from) {
58
+ tr.delete(range.from + startSpaces, textStart)
59
+ }
60
+
61
+ markEnd = range.from + startSpaces + captureGroup.length
62
+
63
+ tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}))
64
+
65
+ tr.removeStoredMark(config.type)
66
+ }
67
+ },
49
68
  })
50
69
  }
@@ -1,17 +1,50 @@
1
- import { InputRule } from 'prosemirror-inputrules'
2
- import { NodeType } from 'prosemirror-model'
1
+ import { NodeType } from '@tiptap/pm/model'
3
2
 
4
- export default function (regexp: RegExp, type: NodeType, getAttributes?: (match: any) => any): InputRule {
5
- return new InputRule(regexp, (state, match, start, end) => {
6
- const attributes = getAttributes instanceof Function
7
- ? getAttributes(match)
8
- : getAttributes
9
- const { tr } = state
3
+ import { InputRule, InputRuleFinder } from '../InputRule'
4
+ import { ExtendedRegExpMatchArray } from '../types'
5
+ import { callOrReturn } from '../utilities/callOrReturn'
10
6
 
11
- if (match[0]) {
12
- tr.replaceWith(start - 1, end, type.create(attributes))
13
- }
7
+ /**
8
+ * Build an input rule that adds a node when the
9
+ * matched text is typed into it.
10
+ */
11
+ export function nodeInputRule(config: {
12
+ find: InputRuleFinder
13
+ type: NodeType
14
+ getAttributes?:
15
+ | Record<string, any>
16
+ | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
17
+ | false
18
+ | null
19
+ }) {
20
+ return new InputRule({
21
+ find: config.find,
22
+ handler: ({ state, range, match }) => {
23
+ const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
24
+ const { tr } = state
25
+ const start = range.from
26
+ let end = range.to
14
27
 
15
- return tr
28
+ if (match[1]) {
29
+ const offset = match[0].lastIndexOf(match[1])
30
+ let matchStart = start + offset
31
+
32
+ if (matchStart > end) {
33
+ matchStart = end
34
+ } else {
35
+ end = matchStart + match[1].length
36
+ }
37
+
38
+ // insert last typed character
39
+ const lastChar = match[0][match[0].length - 1]
40
+
41
+ tr.insertText(lastChar, start + match[0].length - 1)
42
+
43
+ // insert node from input rule
44
+ tr.replaceWith(matchStart, end, config.type.create(attributes))
45
+ } else if (match[0]) {
46
+ tr.replaceWith(start, end, config.type.create(attributes))
47
+ }
48
+ },
16
49
  })
17
50
  }
@@ -0,0 +1,35 @@
1
+ import { InputRule, InputRuleFinder } from '../InputRule'
2
+
3
+ /**
4
+ * Build an input rule that replaces text when the
5
+ * matched text is typed into it.
6
+ */
7
+ export function textInputRule(config: {
8
+ find: InputRuleFinder,
9
+ replace: string,
10
+ }) {
11
+ return new InputRule({
12
+ find: config.find,
13
+ handler: ({ state, range, match }) => {
14
+ let insert = config.replace
15
+ let start = range.from
16
+ const end = range.to
17
+
18
+ if (match[1]) {
19
+ const offset = match[0].lastIndexOf(match[1])
20
+
21
+ insert += match[0].slice(offset + match[1].length)
22
+ start += offset
23
+
24
+ const cutOff = start - end
25
+
26
+ if (cutOff > 0) {
27
+ insert = match[0].slice(offset - cutOff, offset) + insert
28
+ start = end
29
+ }
30
+ }
31
+
32
+ state.tr.insertText(insert, start, end)
33
+ },
34
+ })
35
+ }
@@ -0,0 +1,37 @@
1
+ import { NodeType } from '@tiptap/pm/model'
2
+
3
+ import { InputRule, InputRuleFinder } from '../InputRule'
4
+ import { ExtendedRegExpMatchArray } from '../types'
5
+ import { callOrReturn } from '../utilities/callOrReturn'
6
+
7
+ /**
8
+ * Build an input rule that changes the type of a textblock when the
9
+ * matched text is typed into it. When using a regular expresion you’ll
10
+ * probably want the regexp to start with `^`, so that the pattern can
11
+ * only occur at the start of a textblock.
12
+ */
13
+ export function textblockTypeInputRule(config: {
14
+ find: InputRuleFinder
15
+ type: NodeType
16
+ getAttributes?:
17
+ | Record<string, any>
18
+ | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
19
+ | false
20
+ | null
21
+ }) {
22
+ return new InputRule({
23
+ find: config.find,
24
+ handler: ({ state, range, match }) => {
25
+ const $start = state.doc.resolve(range.from)
26
+ const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
27
+
28
+ if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), config.type)) {
29
+ return null
30
+ }
31
+
32
+ state.tr
33
+ .delete(range.from, range.to)
34
+ .setBlockType(range.from, range.from, config.type, attributes)
35
+ },
36
+ })
37
+ }
@@ -0,0 +1,59 @@
1
+ import { Node as ProseMirrorNode, NodeType } from '@tiptap/pm/model'
2
+ import { canJoin, findWrapping } from '@tiptap/pm/transform'
3
+
4
+ import { InputRule, InputRuleFinder } from '../InputRule'
5
+ import { ExtendedRegExpMatchArray } from '../types'
6
+ import { callOrReturn } from '../utilities/callOrReturn'
7
+
8
+ /**
9
+ * Build an input rule for automatically wrapping a textblock when a
10
+ * given string is typed. When using a regular expresion you’ll
11
+ * probably want the regexp to start with `^`, so that the pattern can
12
+ * only occur at the start of a textblock.
13
+ *
14
+ * `type` is the type of node to wrap in.
15
+ *
16
+ * By default, if there’s a node with the same type above the newly
17
+ * wrapped node, the rule will try to join those
18
+ * two nodes. You can pass a join predicate, which takes a regular
19
+ * expression match and the node before the wrapped node, and can
20
+ * return a boolean to indicate whether a join should happen.
21
+ */
22
+ export function wrappingInputRule(config: {
23
+ find: InputRuleFinder
24
+ type: NodeType
25
+ getAttributes?:
26
+ | Record<string, any>
27
+ | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
28
+ | false
29
+ | null
30
+ joinPredicate?: (match: ExtendedRegExpMatchArray, node: ProseMirrorNode) => boolean
31
+ }) {
32
+ return new InputRule({
33
+ find: config.find,
34
+ handler: ({ state, range, match }) => {
35
+ const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
36
+ const tr = state.tr.delete(range.from, range.to)
37
+ const $start = tr.doc.resolve(range.from)
38
+ const blockRange = $start.blockRange()
39
+ const wrapping = blockRange && findWrapping(blockRange, config.type, attributes)
40
+
41
+ if (!wrapping) {
42
+ return null
43
+ }
44
+
45
+ tr.wrap(blockRange, wrapping)
46
+
47
+ const before = tr.doc.resolve(range.from - 1).nodeBefore
48
+
49
+ if (
50
+ before
51
+ && before.type === config.type
52
+ && canJoin(tr.doc, range.from - 1)
53
+ && (!config.joinPredicate || config.joinPredicate(match, before))
54
+ ) {
55
+ tr.join(range.from - 1)
56
+ }
57
+ },
58
+ })
59
+ }
@@ -0,0 +1,3 @@
1
+ export * from './markPasteRule'
2
+ export * from './nodePasteRule'
3
+ export * from './textPasteRule'
@@ -1,61 +1,69 @@
1
- import { Plugin, PluginKey } from 'prosemirror-state'
2
- import { Slice, Fragment, MarkType } from 'prosemirror-model'
3
-
4
- export default function (regexp: RegExp, type: MarkType, getAttrs?: (match: any) => any): Plugin {
5
- const handler = (fragment: Fragment, parent?: any) => {
6
- const nodes: any[] = []
7
-
8
- fragment.forEach(child => {
9
- if (child.isText && child.text) {
10
- const { text } = child
11
- let pos = 0
12
- let match
13
-
14
- // eslint-disable-next-line
15
- while ((match = regexp.exec(text)) !== null) {
16
- const outerMatch = Math.max(match.length - 2, 0)
17
- const innerMatch = Math.max(match.length - 1, 0)
18
-
19
- if (parent?.type.allowsMarkType(type)) {
20
- const start = match.index
21
- const matchStart = start + match[0].indexOf(match[outerMatch])
22
- const matchEnd = matchStart + match[outerMatch].length
23
- const textStart = matchStart + match[outerMatch].lastIndexOf(match[innerMatch])
24
- const textEnd = textStart + match[innerMatch].length
25
- const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
26
-
27
- // adding text before markdown to nodes
28
- if (matchStart > 0) {
29
- nodes.push(child.cut(pos, matchStart))
30
- }
31
-
32
- // adding the markdown part to nodes
33
- nodes.push(child
34
- .cut(textStart, textEnd)
35
- .mark(type.create(attrs).addToSet(child.marks)))
36
-
37
- pos = matchEnd
38
- }
1
+ import { MarkType } from '@tiptap/pm/model'
2
+
3
+ import { getMarksBetween } from '../helpers/getMarksBetween'
4
+ import { PasteRule, PasteRuleFinder } from '../PasteRule'
5
+ import { ExtendedRegExpMatchArray } from '../types'
6
+ import { callOrReturn } from '../utilities/callOrReturn'
7
+
8
+ /**
9
+ * Build an paste rule that adds a mark when the
10
+ * matched text is pasted into it.
11
+ */
12
+ export function markPasteRule(config: {
13
+ find: PasteRuleFinder
14
+ type: MarkType
15
+ getAttributes?:
16
+ | Record<string, any>
17
+ | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
18
+ | false
19
+ | null
20
+ }) {
21
+ return new PasteRule({
22
+ find: config.find,
23
+ handler: ({ state, range, match }) => {
24
+ const attributes = callOrReturn(config.getAttributes, undefined, match)
25
+
26
+ if (attributes === false || attributes === null) {
27
+ return null
28
+ }
29
+
30
+ const { tr } = state
31
+ const captureGroup = match[match.length - 1]
32
+ const fullMatch = match[0]
33
+ let markEnd = range.to
34
+
35
+ if (captureGroup) {
36
+ const startSpaces = fullMatch.search(/\S/)
37
+ const textStart = range.from + fullMatch.indexOf(captureGroup)
38
+ const textEnd = textStart + captureGroup.length
39
+
40
+ const excludedMarks = getMarksBetween(range.from, range.to, state.doc)
41
+ .filter(item => {
42
+ // @ts-ignore
43
+ const excluded = item.mark.type.excluded as MarkType[]
44
+
45
+ return excluded.find(type => type === config.type && type !== item.mark.type)
46
+ })
47
+ .filter(item => item.to > textStart)
48
+
49
+ if (excludedMarks.length) {
50
+ return null
39
51
  }
40
52
 
41
- // adding rest of text to nodes
42
- if (pos < text.length) {
43
- nodes.push(child.cut(pos))
53
+ if (textEnd < range.to) {
54
+ tr.delete(textEnd, range.to)
44
55
  }
45
- } else {
46
- nodes.push(child.copy(handler(child.content, child)))
47
- }
48
- })
49
56
 
50
- return Fragment.fromArray(nodes)
51
- }
57
+ if (textStart > range.from) {
58
+ tr.delete(range.from + startSpaces, textStart)
59
+ }
52
60
 
53
- return new Plugin({
54
- key: new PluginKey('markPasteRule'),
55
- props: {
56
- transformPasted: slice => {
57
- return new Slice(handler(slice.content), slice.openStart, slice.openEnd)
58
- },
61
+ markEnd = range.from + startSpaces + captureGroup.length
62
+
63
+ tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}))
64
+
65
+ tr.removeStoredMark(config.type)
66
+ }
59
67
  },
60
68
  })
61
69
  }
@@ -0,0 +1,37 @@
1
+ import { NodeType } from '@tiptap/pm/model'
2
+
3
+ import { PasteRule } from '../PasteRule'
4
+ import { ExtendedRegExpMatchArray } from '../types'
5
+ import { callOrReturn } from '../utilities'
6
+
7
+ /**
8
+ * Build an paste rule that adds a node when the
9
+ * matched text is pasted into it.
10
+ */
11
+ export function nodePasteRule(config: {
12
+ find: RegExp
13
+ type: NodeType
14
+ getAttributes?:
15
+ | Record<string, any>
16
+ | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
17
+ | false
18
+ | null
19
+ }) {
20
+ return new PasteRule({
21
+ find: config.find,
22
+ handler({ match, chain, range }) {
23
+ const attributes = callOrReturn(config.getAttributes, undefined, match)
24
+
25
+ if (attributes === false || attributes === null) {
26
+ return null
27
+ }
28
+
29
+ if (match.input) {
30
+ chain().deleteRange(range).insertContentAt(range.from, {
31
+ type: config.type.name,
32
+ attrs: attributes,
33
+ })
34
+ }
35
+ },
36
+ })
37
+ }
@@ -0,0 +1,35 @@
1
+ import { PasteRule, PasteRuleFinder } from '../PasteRule'
2
+
3
+ /**
4
+ * Build an paste rule that replaces text when the
5
+ * matched text is pasted into it.
6
+ */
7
+ export function textPasteRule(config: {
8
+ find: PasteRuleFinder,
9
+ replace: string,
10
+ }) {
11
+ return new PasteRule({
12
+ find: config.find,
13
+ handler: ({ state, range, match }) => {
14
+ let insert = config.replace
15
+ let start = range.from
16
+ const end = range.to
17
+
18
+ if (match[1]) {
19
+ const offset = match[0].lastIndexOf(match[1])
20
+
21
+ insert += match[0].slice(offset + match[1].length)
22
+ start += offset
23
+
24
+ const cutOff = start - end
25
+
26
+ if (cutOff > 0) {
27
+ insert = match[0].slice(offset - cutOff, offset) + insert
28
+ start = end
29
+ }
30
+ }
31
+
32
+ state.tr.insertText(insert, start, end)
33
+ },
34
+ })
35
+ }
package/src/style.ts CHANGED
@@ -1,12 +1,14 @@
1
- const style = `.ProseMirror {
1
+ export const style = `.ProseMirror {
2
2
  position: relative;
3
3
  }
4
4
 
5
5
  .ProseMirror {
6
6
  word-wrap: break-word;
7
7
  white-space: pre-wrap;
8
+ white-space: break-spaces;
8
9
  -webkit-font-variant-ligatures: none;
9
10
  font-variant-ligatures: none;
11
+ font-feature-settings: "liga" 0; /* the above doesn't seem to work in Edge */
10
12
  }
11
13
 
12
14
  .ProseMirror [contenteditable="false"] {
@@ -21,10 +23,19 @@ const style = `.ProseMirror {
21
23
  white-space: pre-wrap;
22
24
  }
23
25
 
26
+ img.ProseMirror-separator {
27
+ display: inline !important;
28
+ border: none !important;
29
+ margin: 0 !important;
30
+ width: 1px !important;
31
+ height: 1px !important;
32
+ }
33
+
24
34
  .ProseMirror-gapcursor {
25
35
  display: none;
26
36
  pointer-events: none;
27
37
  position: absolute;
38
+ margin: 0;
28
39
  }
29
40
 
30
41
  .ProseMirror-gapcursor:after {
@@ -57,6 +68,8 @@ const style = `.ProseMirror {
57
68
 
58
69
  .ProseMirror-focused .ProseMirror-gapcursor {
59
70
  display: block;
60
- }`
71
+ }
61
72
 
62
- export default style
73
+ .tippy-box[data-animation=fade][data-state=hidden] {
74
+ opacity: 0
75
+ }`