@tiptap/core 2.0.0-beta.19 → 2.0.0-beta.193

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 (283) hide show
  1. package/README.md +2 -2
  2. package/package.json +16 -20
  3. package/src/CommandManager.ts +64 -65
  4. package/src/Editor.ts +116 -78
  5. package/src/EventEmitter.ts +14 -4
  6. package/src/Extension.ts +193 -43
  7. package/src/ExtensionManager.ts +242 -84
  8. package/src/InputRule.ts +265 -0
  9. package/src/Mark.ts +277 -48
  10. package/src/Node.ts +279 -57
  11. package/src/NodeView.ts +115 -47
  12. package/src/PasteRule.ts +247 -0
  13. package/src/Tracker.ts +42 -0
  14. package/src/commands/blur.ts +12 -6
  15. package/src/commands/clearContent.ts +3 -3
  16. package/src/commands/clearNodes.ts +30 -18
  17. package/src/commands/command.ts +2 -2
  18. package/src/commands/createParagraphNear.ts +4 -3
  19. package/src/commands/deleteNode.ts +37 -0
  20. package/src/commands/deleteRange.ts +3 -3
  21. package/src/commands/deleteSelection.ts +4 -3
  22. package/src/commands/enter.ts +3 -3
  23. package/src/commands/exitCode.ts +4 -3
  24. package/src/commands/extendMarkRange.ts +12 -11
  25. package/src/commands/first.ts +3 -3
  26. package/src/commands/focus.ts +47 -44
  27. package/src/commands/forEach.ts +24 -0
  28. package/src/commands/index.ts +50 -0
  29. package/src/commands/insertContent.ts +13 -24
  30. package/src/commands/insertContentAt.ts +108 -0
  31. package/src/commands/joinBackward.ts +4 -3
  32. package/src/commands/joinForward.ts +4 -3
  33. package/src/commands/keyboardShortcut.ts +6 -6
  34. package/src/commands/lift.ts +6 -5
  35. package/src/commands/liftEmptyBlock.ts +4 -3
  36. package/src/commands/liftListItem.ts +6 -5
  37. package/src/commands/newlineInCode.ts +4 -3
  38. package/src/commands/resetAttributes.ts +62 -0
  39. package/src/commands/scrollIntoView.ts +3 -3
  40. package/src/commands/selectAll.ts +8 -6
  41. package/src/commands/selectNodeBackward.ts +4 -3
  42. package/src/commands/selectNodeForward.ts +4 -3
  43. package/src/commands/selectParentNode.ts +4 -3
  44. package/src/commands/selectTextblockEnd.ts +20 -0
  45. package/src/commands/selectTextblockStart.ts +20 -0
  46. package/src/commands/setContent.ts +9 -15
  47. package/src/commands/setMark.ts +36 -12
  48. package/src/commands/setMeta.ts +18 -0
  49. package/src/commands/setNode.ts +29 -7
  50. package/src/commands/setNodeSelection.ts +27 -0
  51. package/src/commands/setTextSelection.ts +33 -0
  52. package/src/commands/sinkListItem.ts +6 -5
  53. package/src/commands/splitBlock.ts +14 -19
  54. package/src/commands/splitListItem.ts +45 -18
  55. package/src/commands/toggleList.ts +83 -18
  56. package/src/commands/toggleMark.ts +18 -7
  57. package/src/commands/toggleNode.ts +6 -5
  58. package/src/commands/toggleWrap.ts +9 -9
  59. package/src/commands/undoInputRule.ts +34 -5
  60. package/src/commands/unsetAllMarks.ts +7 -11
  61. package/src/commands/unsetMark.ts +35 -22
  62. package/src/commands/updateAttributes.ts +16 -9
  63. package/src/commands/wrapIn.ts +5 -10
  64. package/src/commands/wrapInList.ts +6 -5
  65. package/src/extensions/clipboardTextSerializer.ts +14 -35
  66. package/src/extensions/commands.ts +3 -144
  67. package/src/extensions/editable.ts +1 -0
  68. package/src/extensions/focusEvents.ts +3 -5
  69. package/src/extensions/index.ts +1 -0
  70. package/src/extensions/keymap.ts +110 -13
  71. package/src/extensions/tabindex.ts +18 -0
  72. package/src/helpers/combineTransactionSteps.ts +18 -0
  73. package/src/helpers/createChainableState.ts +38 -0
  74. package/src/helpers/createDocument.ts +5 -6
  75. package/src/helpers/createNodeFromContent.ts +17 -19
  76. package/src/helpers/defaultBlockAt.ts +13 -0
  77. package/src/helpers/findChildren.ts +18 -0
  78. package/src/helpers/findChildrenInRange.ts +32 -0
  79. package/src/helpers/findParentNode.ts +3 -2
  80. package/src/helpers/findParentNodeClosestToPos.ts +3 -2
  81. package/src/helpers/generateHTML.ts +6 -5
  82. package/src/helpers/generateJSON.ts +14 -0
  83. package/src/helpers/generateText.ts +30 -0
  84. package/src/helpers/getAttributes.ts +28 -0
  85. package/src/helpers/getAttributesFromExtensions.ts +42 -14
  86. package/src/helpers/getChangedRanges.ts +83 -0
  87. package/src/helpers/getDebugJSON.ts +54 -0
  88. package/src/helpers/getExtensionField.ts +25 -0
  89. package/src/helpers/getHTMLFromFragment.ts +6 -5
  90. package/src/helpers/getMarkAttributes.ts +14 -10
  91. package/src/helpers/getMarkRange.ts +41 -8
  92. package/src/helpers/getMarkType.ts +5 -1
  93. package/src/helpers/getMarksBetween.ts +32 -10
  94. package/src/helpers/getNodeAttributes.ts +9 -9
  95. package/src/helpers/getNodeType.ts +5 -1
  96. package/src/helpers/getRenderedAttributes.ts +6 -7
  97. package/src/helpers/getSchema.ts +7 -133
  98. package/src/helpers/getSchemaByResolvedExtensions.ts +148 -0
  99. package/src/helpers/getSchemaTypeByName.ts +2 -10
  100. package/src/helpers/getSchemaTypeNameByName.ts +1 -1
  101. package/src/helpers/getSplittedAttributes.ts +4 -4
  102. package/src/helpers/getText.ts +19 -0
  103. package/src/helpers/getTextBetween.ts +49 -0
  104. package/src/helpers/getTextContentFromNodes.ts +17 -0
  105. package/src/helpers/getTextSerializersFromSchema.ts +10 -0
  106. package/src/helpers/index.ts +33 -0
  107. package/src/helpers/injectExtensionAttributesToParseRule.ts +17 -19
  108. package/src/helpers/isActive.ts +5 -5
  109. package/src/helpers/isExtensionRulesEnabled.ts +15 -0
  110. package/src/helpers/isList.ts +14 -7
  111. package/src/helpers/isMarkActive.ts +45 -20
  112. package/src/helpers/isNodeActive.ts +28 -35
  113. package/src/helpers/isNodeEmpty.ts +1 -1
  114. package/src/helpers/isNodeSelection.ts +2 -3
  115. package/src/helpers/isTextSelection.ts +2 -3
  116. package/src/helpers/posToDOMRect.ts +35 -0
  117. package/src/helpers/resolveFocusPosition.ts +43 -0
  118. package/src/helpers/selectionToInsertionEnd.ts +2 -2
  119. package/src/helpers/splitExtensions.ts +3 -3
  120. package/src/index.ts +15 -24
  121. package/src/inputRules/index.ts +5 -0
  122. package/src/inputRules/markInputRule.ts +59 -39
  123. package/src/inputRules/nodeInputRule.ts +45 -11
  124. package/src/inputRules/textInputRule.ts +35 -0
  125. package/src/inputRules/textblockTypeInputRule.ts +38 -0
  126. package/src/inputRules/wrappingInputRule.ts +60 -0
  127. package/src/pasteRules/index.ts +3 -0
  128. package/src/pasteRules/markPasteRule.ts +62 -53
  129. package/src/pasteRules/nodePasteRule.ts +39 -0
  130. package/src/pasteRules/textPasteRule.ts +35 -0
  131. package/src/style.ts +16 -3
  132. package/src/types.ts +129 -43
  133. package/src/utilities/callOrReturn.ts +6 -3
  134. package/src/utilities/createStyleTag.ts +12 -1
  135. package/src/utilities/deleteProps.ts +2 -4
  136. package/src/utilities/elementFromString.ts +4 -5
  137. package/src/utilities/escapeForRegEx.ts +4 -0
  138. package/src/utilities/findDuplicates.ts +5 -0
  139. package/src/utilities/fromString.ts +2 -2
  140. package/src/utilities/index.ts +3 -0
  141. package/src/utilities/isEmptyObject.ts +2 -2
  142. package/src/utilities/isFunction.ts +3 -0
  143. package/src/utilities/isMacOS.ts +5 -0
  144. package/src/utilities/isNumber.ts +3 -0
  145. package/src/utilities/isPlainObject.ts +8 -5
  146. package/src/utilities/isRegExp.ts +3 -0
  147. package/src/utilities/isString.ts +3 -0
  148. package/src/utilities/isiOS.ts +12 -0
  149. package/src/utilities/mergeAttributes.ts +2 -3
  150. package/src/utilities/mergeDeep.ts +2 -3
  151. package/src/utilities/minMax.ts +1 -1
  152. package/src/utilities/objectIncludes.ts +17 -5
  153. package/src/utilities/removeDuplicates.ts +15 -0
  154. package/CHANGELOG.md +0 -346
  155. package/LICENSE.md +0 -21
  156. package/dist/packages/core/src/CommandManager.d.ts +0 -13
  157. package/dist/packages/core/src/Editor.d.ts +0 -142
  158. package/dist/packages/core/src/EventEmitter.d.ts +0 -7
  159. package/dist/packages/core/src/Extension.d.ts +0 -155
  160. package/dist/packages/core/src/ExtensionManager.d.ts +0 -24
  161. package/dist/packages/core/src/Mark.d.ts +0 -219
  162. package/dist/packages/core/src/Node.d.ts +0 -273
  163. package/dist/packages/core/src/NodeView.d.ts +0 -31
  164. package/dist/packages/core/src/commands/blur.d.ts +0 -12
  165. package/dist/packages/core/src/commands/clearContent.d.ts +0 -12
  166. package/dist/packages/core/src/commands/clearNodes.d.ts +0 -12
  167. package/dist/packages/core/src/commands/command.d.ts +0 -12
  168. package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -12
  169. package/dist/packages/core/src/commands/deleteRange.d.ts +0 -12
  170. package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -12
  171. package/dist/packages/core/src/commands/enter.d.ts +0 -12
  172. package/dist/packages/core/src/commands/exitCode.d.ts +0 -12
  173. package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -13
  174. package/dist/packages/core/src/commands/first.d.ts +0 -12
  175. package/dist/packages/core/src/commands/focus.d.ts +0 -12
  176. package/dist/packages/core/src/commands/insertContent.d.ts +0 -12
  177. package/dist/packages/core/src/commands/insertHTML.d.ts +0 -12
  178. package/dist/packages/core/src/commands/insertNode.d.ts +0 -13
  179. package/dist/packages/core/src/commands/insertText.d.ts +0 -12
  180. package/dist/packages/core/src/commands/joinBackward.d.ts +0 -12
  181. package/dist/packages/core/src/commands/joinForward.d.ts +0 -12
  182. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -12
  183. package/dist/packages/core/src/commands/lift.d.ts +0 -13
  184. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -12
  185. package/dist/packages/core/src/commands/liftListItem.d.ts +0 -13
  186. package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -12
  187. package/dist/packages/core/src/commands/replace.d.ts +0 -13
  188. package/dist/packages/core/src/commands/replaceRange.d.ts +0 -13
  189. package/dist/packages/core/src/commands/resetNodeAttributes.d.ts +0 -13
  190. package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -12
  191. package/dist/packages/core/src/commands/selectAll.d.ts +0 -12
  192. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -12
  193. package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -12
  194. package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -12
  195. package/dist/packages/core/src/commands/setContent.d.ts +0 -12
  196. package/dist/packages/core/src/commands/setMark.d.ts +0 -13
  197. package/dist/packages/core/src/commands/setNode.d.ts +0 -13
  198. package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -13
  199. package/dist/packages/core/src/commands/splitBlock.d.ts +0 -14
  200. package/dist/packages/core/src/commands/splitListItem.d.ts +0 -13
  201. package/dist/packages/core/src/commands/toggleList.d.ts +0 -13
  202. package/dist/packages/core/src/commands/toggleMark.d.ts +0 -13
  203. package/dist/packages/core/src/commands/toggleNode.d.ts +0 -13
  204. package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -13
  205. package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -12
  206. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -12
  207. package/dist/packages/core/src/commands/unsetMark.d.ts +0 -13
  208. package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -13
  209. package/dist/packages/core/src/commands/updateNodeAttributes.d.ts +0 -13
  210. package/dist/packages/core/src/commands/wrapIn.d.ts +0 -13
  211. package/dist/packages/core/src/commands/wrapInList.d.ts +0 -13
  212. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -2
  213. package/dist/packages/core/src/extensions/commands.d.ts +0 -98
  214. package/dist/packages/core/src/extensions/editable.d.ts +0 -2
  215. package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
  216. package/dist/packages/core/src/extensions/index.d.ts +0 -5
  217. package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
  218. package/dist/packages/core/src/helpers/createDocument.d.ts +0 -4
  219. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -8
  220. package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -9
  221. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -8
  222. package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -2
  223. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
  224. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
  225. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -4
  226. package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
  227. package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
  228. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
  229. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -4
  230. package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
  231. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
  232. package/dist/packages/core/src/helpers/getSchema.d.ts +0 -3
  233. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -2
  234. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -2
  235. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -2
  236. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
  237. package/dist/packages/core/src/helpers/isActive.d.ts +0 -3
  238. package/dist/packages/core/src/helpers/isList.d.ts +0 -2
  239. package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -4
  240. package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -4
  241. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -2
  242. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
  243. package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
  244. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
  245. package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
  246. package/dist/packages/core/src/index.d.ts +0 -30
  247. package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -3
  248. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -3
  249. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -3
  250. package/dist/packages/core/src/style.d.ts +0 -2
  251. package/dist/packages/core/src/types.d.ts +0 -157
  252. package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -8
  253. package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
  254. package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -7
  255. package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
  256. package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
  257. package/dist/packages/core/src/utilities/isClass.d.ts +0 -1
  258. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
  259. package/dist/packages/core/src/utilities/isObject.d.ts +0 -1
  260. package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
  261. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -2
  262. package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -2
  263. package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
  264. package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -7
  265. package/dist/packages/core/src/utilities/removeElement.d.ts +0 -1
  266. package/dist/tiptap-core.bundle.umd.min.js +0 -17
  267. package/dist/tiptap-core.bundle.umd.min.js.map +0 -1
  268. package/dist/tiptap-core.cjs.js +0 -3000
  269. package/dist/tiptap-core.cjs.js.map +0 -1
  270. package/dist/tiptap-core.esm.js +0 -2975
  271. package/dist/tiptap-core.esm.js.map +0 -1
  272. package/dist/tiptap-core.umd.js +0 -2997
  273. package/dist/tiptap-core.umd.js.map +0 -1
  274. package/src/commands/insertHTML.ts +0 -30
  275. package/src/commands/insertNode.ts +0 -33
  276. package/src/commands/insertText.ts +0 -22
  277. package/src/commands/replace.ts +0 -20
  278. package/src/commands/replaceRange.ts +0 -36
  279. package/src/commands/resetNodeAttributes.ts +0 -31
  280. package/src/commands/updateNodeAttributes.ts +0 -35
  281. package/src/utilities/isClass.ts +0 -7
  282. package/src/utilities/isObject.ts +0 -10
  283. package/src/utilities/removeElement.ts +0 -5
@@ -0,0 +1,265 @@
1
+ import { EditorState, Plugin, TextSelection } from 'prosemirror-state'
2
+
3
+ import { CommandManager } from './CommandManager'
4
+ import { Editor } from './Editor'
5
+ import { createChainableState } from './helpers/createChainableState'
6
+ import { getTextContentFromNodes } from './helpers/getTextContentFromNodes'
7
+ import {
8
+ CanCommands,
9
+ ChainedCommands,
10
+ ExtendedRegExpMatchArray,
11
+ Range,
12
+ SingleCommands,
13
+ } from './types'
14
+ import { isRegExp } from './utilities/isRegExp'
15
+
16
+ export type InputRuleMatch = {
17
+ index: number,
18
+ text: string,
19
+ replaceWith?: string,
20
+ match?: RegExpMatchArray,
21
+ data?: Record<string, any>,
22
+ }
23
+
24
+ export type InputRuleFinder =
25
+ | RegExp
26
+ | ((text: string) => InputRuleMatch | null)
27
+
28
+ export class InputRule {
29
+ find: InputRuleFinder
30
+
31
+ handler: (props: {
32
+ state: EditorState,
33
+ range: Range,
34
+ match: ExtendedRegExpMatchArray,
35
+ commands: SingleCommands,
36
+ chain: () => ChainedCommands,
37
+ can: () => CanCommands,
38
+ }) => void | null
39
+
40
+ constructor(config: {
41
+ find: InputRuleFinder,
42
+ handler: (props: {
43
+ state: EditorState,
44
+ range: Range,
45
+ match: ExtendedRegExpMatchArray,
46
+ commands: SingleCommands,
47
+ chain: () => ChainedCommands,
48
+ can: () => CanCommands,
49
+ }) => void | null,
50
+ }) {
51
+ this.find = config.find
52
+ this.handler = config.handler
53
+ }
54
+ }
55
+
56
+ const inputRuleMatcherHandler = (text: string, find: InputRuleFinder): ExtendedRegExpMatchArray | null => {
57
+ if (isRegExp(find)) {
58
+ return find.exec(text)
59
+ }
60
+
61
+ const inputRuleMatch = find(text)
62
+
63
+ if (!inputRuleMatch) {
64
+ return null
65
+ }
66
+
67
+ const result: ExtendedRegExpMatchArray = []
68
+
69
+ result.push(inputRuleMatch.text)
70
+ result.index = inputRuleMatch.index
71
+ result.input = text
72
+ result.data = inputRuleMatch.data
73
+
74
+ if (inputRuleMatch.replaceWith) {
75
+ if (!inputRuleMatch.text.includes(inputRuleMatch.replaceWith)) {
76
+ console.warn('[tiptap warn]: "inputRuleMatch.replaceWith" must be part of "inputRuleMatch.text".')
77
+ }
78
+
79
+ result.push(inputRuleMatch.replaceWith)
80
+ }
81
+
82
+ return result
83
+ }
84
+
85
+ function run(config: {
86
+ editor: Editor,
87
+ from: number,
88
+ to: number,
89
+ text: string,
90
+ rules: InputRule[],
91
+ plugin: Plugin,
92
+ }): boolean {
93
+ const {
94
+ editor,
95
+ from,
96
+ to,
97
+ text,
98
+ rules,
99
+ plugin,
100
+ } = config
101
+ const { view } = editor
102
+
103
+ if (view.composing) {
104
+ return false
105
+ }
106
+
107
+ const $from = view.state.doc.resolve(from)
108
+
109
+ if (
110
+ // check for code node
111
+ $from.parent.type.spec.code
112
+ // check for code mark
113
+ || !!($from.nodeBefore || $from.nodeAfter)?.marks.find(mark => mark.type.spec.code)
114
+ ) {
115
+ return false
116
+ }
117
+
118
+ let matched = false
119
+
120
+ const textBefore = getTextContentFromNodes($from) + text
121
+
122
+ rules.forEach(rule => {
123
+ if (matched) {
124
+ return
125
+ }
126
+
127
+ const match = inputRuleMatcherHandler(textBefore, rule.find)
128
+
129
+ if (!match) {
130
+ return
131
+ }
132
+
133
+ const tr = view.state.tr
134
+ const state = createChainableState({
135
+ state: view.state,
136
+ transaction: tr,
137
+ })
138
+ const range = {
139
+ from: from - (match[0].length - text.length),
140
+ to,
141
+ }
142
+
143
+ const { commands, chain, can } = new CommandManager({
144
+ editor,
145
+ state,
146
+ })
147
+
148
+ const handler = rule.handler({
149
+ state,
150
+ range,
151
+ match,
152
+ commands,
153
+ chain,
154
+ can,
155
+ })
156
+
157
+ // stop if there are no changes
158
+ if (handler === null || !tr.steps.length) {
159
+ return
160
+ }
161
+
162
+ // store transform as meta data
163
+ // so we can undo input rules within the `undoInputRules` command
164
+ tr.setMeta(plugin, {
165
+ transform: tr,
166
+ from,
167
+ to,
168
+ text,
169
+ })
170
+
171
+ view.dispatch(tr)
172
+ matched = true
173
+ })
174
+
175
+ return matched
176
+ }
177
+
178
+ /**
179
+ * Create an input rules plugin. When enabled, it will cause text
180
+ * input that matches any of the given rules to trigger the rule’s
181
+ * action.
182
+ */
183
+ export function inputRulesPlugin(props: { editor: Editor, rules: InputRule[] }): Plugin {
184
+ const { editor, rules } = props
185
+ const plugin = new Plugin({
186
+ state: {
187
+ init() {
188
+ return null
189
+ },
190
+ apply(tr, prev) {
191
+ const stored = tr.getMeta(plugin)
192
+
193
+ if (stored) {
194
+ return stored
195
+ }
196
+
197
+ return tr.selectionSet || tr.docChanged
198
+ ? null
199
+ : prev
200
+ },
201
+ },
202
+
203
+ props: {
204
+ handleTextInput(view, from, to, text) {
205
+ return run({
206
+ editor,
207
+ from,
208
+ to,
209
+ text,
210
+ rules,
211
+ plugin,
212
+ })
213
+ },
214
+
215
+ handleDOMEvents: {
216
+ compositionend: view => {
217
+ setTimeout(() => {
218
+ const { $cursor } = view.state.selection as TextSelection
219
+
220
+ if ($cursor) {
221
+ run({
222
+ editor,
223
+ from: $cursor.pos,
224
+ to: $cursor.pos,
225
+ text: '',
226
+ rules,
227
+ plugin,
228
+ })
229
+ }
230
+ })
231
+
232
+ return false
233
+ },
234
+ },
235
+
236
+ // add support for input rules to trigger on enter
237
+ // this is useful for example for code blocks
238
+ handleKeyDown(view, event) {
239
+ if (event.key !== 'Enter') {
240
+ return false
241
+ }
242
+
243
+ const { $cursor } = view.state.selection as TextSelection
244
+
245
+ if ($cursor) {
246
+ return run({
247
+ editor,
248
+ from: $cursor.pos,
249
+ to: $cursor.pos,
250
+ text: '\n',
251
+ rules,
252
+ plugin,
253
+ })
254
+ }
255
+
256
+ return false
257
+ },
258
+ },
259
+
260
+ // @ts-ignore
261
+ isInputRules: true,
262
+ }) as Plugin
263
+
264
+ return plugin
265
+ }