@tiptap/core 2.0.0-beta.135 → 2.0.0-beta.136

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 (143) hide show
  1. package/dist/packages/core/src/CommandManager.d.ts +20 -0
  2. package/dist/packages/core/src/Editor.d.ts +145 -0
  3. package/dist/packages/core/src/EventEmitter.d.ts +11 -0
  4. package/dist/packages/core/src/Extension.d.ts +227 -0
  5. package/dist/packages/core/src/ExtensionManager.d.ts +18 -0
  6. package/dist/packages/core/src/InputRule.d.ts +42 -0
  7. package/dist/packages/core/src/Mark.d.ts +319 -0
  8. package/dist/packages/core/src/Node.d.ts +388 -0
  9. package/dist/packages/core/src/NodeView.d.ts +27 -0
  10. package/dist/packages/core/src/PasteRule.d.ts +42 -0
  11. package/dist/packages/core/src/Tracker.d.ts +11 -0
  12. package/dist/packages/core/src/commands/blur.d.ts +12 -0
  13. package/dist/packages/core/src/commands/clearContent.d.ts +12 -0
  14. package/dist/packages/core/src/commands/clearNodes.d.ts +12 -0
  15. package/dist/packages/core/src/commands/command.d.ts +12 -0
  16. package/dist/packages/core/src/commands/createParagraphNear.d.ts +12 -0
  17. package/dist/packages/core/src/commands/deleteNode.d.ts +13 -0
  18. package/dist/packages/core/src/commands/deleteRange.d.ts +12 -0
  19. package/dist/packages/core/src/commands/deleteSelection.d.ts +12 -0
  20. package/dist/packages/core/src/commands/enter.d.ts +12 -0
  21. package/dist/packages/core/src/commands/exitCode.d.ts +12 -0
  22. package/dist/packages/core/src/commands/extendMarkRange.d.ts +13 -0
  23. package/dist/packages/core/src/commands/first.d.ts +12 -0
  24. package/dist/packages/core/src/commands/focus.d.ts +12 -0
  25. package/dist/packages/core/src/commands/forEach.d.ts +14 -0
  26. package/dist/packages/core/src/commands/insertContent.d.ts +16 -0
  27. package/dist/packages/core/src/commands/insertContentAt.d.ts +16 -0
  28. package/dist/packages/core/src/commands/joinBackward.d.ts +12 -0
  29. package/dist/packages/core/src/commands/joinForward.d.ts +12 -0
  30. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +12 -0
  31. package/dist/packages/core/src/commands/lift.d.ts +13 -0
  32. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +12 -0
  33. package/dist/packages/core/src/commands/liftListItem.d.ts +13 -0
  34. package/dist/packages/core/src/commands/newlineInCode.d.ts +12 -0
  35. package/dist/packages/core/src/commands/resetAttributes.d.ts +13 -0
  36. package/dist/packages/core/src/commands/scrollIntoView.d.ts +12 -0
  37. package/dist/packages/core/src/commands/selectAll.d.ts +12 -0
  38. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +12 -0
  39. package/dist/packages/core/src/commands/selectNodeForward.d.ts +12 -0
  40. package/dist/packages/core/src/commands/selectParentNode.d.ts +12 -0
  41. package/dist/packages/core/src/commands/setContent.d.ts +13 -0
  42. package/dist/packages/core/src/commands/setMark.d.ts +13 -0
  43. package/dist/packages/core/src/commands/setMeta.d.ts +12 -0
  44. package/dist/packages/core/src/commands/setNode.d.ts +13 -0
  45. package/dist/packages/core/src/commands/setNodeSelection.d.ts +12 -0
  46. package/dist/packages/core/src/commands/setTextSelection.d.ts +12 -0
  47. package/dist/packages/core/src/commands/sinkListItem.d.ts +13 -0
  48. package/dist/packages/core/src/commands/splitBlock.d.ts +14 -0
  49. package/dist/packages/core/src/commands/splitListItem.d.ts +13 -0
  50. package/dist/packages/core/src/commands/toggleList.d.ts +13 -0
  51. package/dist/packages/core/src/commands/toggleMark.d.ts +18 -0
  52. package/dist/packages/core/src/commands/toggleNode.d.ts +13 -0
  53. package/dist/packages/core/src/commands/toggleWrap.d.ts +13 -0
  54. package/dist/packages/core/src/commands/undoInputRule.d.ts +12 -0
  55. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +12 -0
  56. package/dist/packages/core/src/commands/unsetMark.d.ts +18 -0
  57. package/dist/packages/core/src/commands/updateAttributes.d.ts +13 -0
  58. package/dist/packages/core/src/commands/wrapIn.d.ts +13 -0
  59. package/dist/packages/core/src/commands/wrapInList.d.ts +13 -0
  60. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +2 -0
  61. package/dist/packages/core/src/extensions/commands.d.ts +98 -0
  62. package/dist/packages/core/src/extensions/editable.d.ts +2 -0
  63. package/dist/packages/core/src/extensions/focusEvents.d.ts +2 -0
  64. package/dist/packages/core/src/extensions/index.d.ts +6 -0
  65. package/dist/packages/core/src/extensions/keymap.d.ts +2 -0
  66. package/dist/packages/core/src/extensions/tabindex.d.ts +2 -0
  67. package/dist/packages/core/src/helpers/createChainableState.d.ts +5 -0
  68. package/dist/packages/core/src/helpers/createDocument.d.ts +3 -0
  69. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +7 -0
  70. package/dist/packages/core/src/helpers/findChildren.d.ts +3 -0
  71. package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +6 -0
  72. package/dist/packages/core/src/helpers/findParentNode.d.ts +9 -0
  73. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +8 -0
  74. package/dist/packages/core/src/helpers/generateHTML.d.ts +2 -0
  75. package/dist/packages/core/src/helpers/generateJSON.d.ts +2 -0
  76. package/dist/packages/core/src/helpers/generateText.d.ts +5 -0
  77. package/dist/packages/core/src/helpers/getAttributes.d.ts +3 -0
  78. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +6 -0
  79. package/dist/packages/core/src/helpers/getDebugJSON.d.ts +8 -0
  80. package/dist/packages/core/src/helpers/getExtensionField.d.ts +2 -0
  81. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +2 -0
  82. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +3 -0
  83. package/dist/packages/core/src/helpers/getMarkRange.d.ts +3 -0
  84. package/dist/packages/core/src/helpers/getMarkType.d.ts +2 -0
  85. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +3 -0
  86. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +3 -0
  87. package/dist/packages/core/src/helpers/getNodeType.d.ts +2 -0
  88. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +3 -0
  89. package/dist/packages/core/src/helpers/getSchema.d.ts +3 -0
  90. package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +3 -0
  91. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +2 -0
  92. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +2 -0
  93. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +2 -0
  94. package/dist/packages/core/src/helpers/getText.d.ts +6 -0
  95. package/dist/packages/core/src/helpers/getTextBetween.d.ts +6 -0
  96. package/dist/packages/core/src/helpers/getTextSeralizersFromSchema.d.ts +3 -0
  97. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +9 -0
  98. package/dist/packages/core/src/helpers/isActive.d.ts +2 -0
  99. package/dist/packages/core/src/helpers/isList.d.ts +2 -0
  100. package/dist/packages/core/src/helpers/isMarkActive.d.ts +3 -0
  101. package/dist/packages/core/src/helpers/isNodeActive.d.ts +3 -0
  102. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +2 -0
  103. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +2 -0
  104. package/dist/packages/core/src/helpers/isTextSelection.d.ts +2 -0
  105. package/dist/packages/core/src/helpers/posToDOMRect.d.ts +2 -0
  106. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +2 -0
  107. package/dist/packages/core/src/helpers/splitExtensions.d.ts +9 -0
  108. package/dist/packages/core/src/index.d.ts +57 -0
  109. package/dist/packages/core/src/inputRules/markInputRule.d.ts +12 -0
  110. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +12 -0
  111. package/dist/packages/core/src/inputRules/textInputRule.d.ts +9 -0
  112. package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +14 -0
  113. package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +23 -0
  114. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +12 -0
  115. package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +9 -0
  116. package/dist/packages/core/src/style.d.ts +2 -0
  117. package/dist/packages/core/src/types.d.ts +209 -0
  118. package/dist/packages/core/src/utilities/callOrReturn.d.ts +9 -0
  119. package/dist/packages/core/src/utilities/createStyleTag.d.ts +1 -0
  120. package/dist/packages/core/src/utilities/deleteProps.d.ts +6 -0
  121. package/dist/packages/core/src/utilities/elementFromString.d.ts +1 -0
  122. package/dist/packages/core/src/utilities/findDuplicates.d.ts +1 -0
  123. package/dist/packages/core/src/utilities/fromString.d.ts +1 -0
  124. package/dist/packages/core/src/utilities/isClass.d.ts +1 -0
  125. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +1 -0
  126. package/dist/packages/core/src/utilities/isFunction.d.ts +1 -0
  127. package/dist/packages/core/src/utilities/isNumber.d.ts +1 -0
  128. package/dist/packages/core/src/utilities/isObject.d.ts +1 -0
  129. package/dist/packages/core/src/utilities/isPlainObject.d.ts +1 -0
  130. package/dist/packages/core/src/utilities/isRegExp.d.ts +1 -0
  131. package/dist/packages/core/src/utilities/isiOS.d.ts +1 -0
  132. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +1 -0
  133. package/dist/packages/core/src/utilities/mergeDeep.d.ts +1 -0
  134. package/dist/packages/core/src/utilities/minMax.d.ts +1 -0
  135. package/dist/packages/core/src/utilities/objectIncludes.d.ts +8 -0
  136. package/dist/packages/core/src/utilities/removeElement.d.ts +1 -0
  137. package/dist/tiptap-core.cjs.js +4133 -0
  138. package/dist/tiptap-core.cjs.js.map +1 -0
  139. package/dist/tiptap-core.esm.js +4082 -0
  140. package/dist/tiptap-core.esm.js.map +1 -0
  141. package/dist/tiptap-core.umd.js +4131 -0
  142. package/dist/tiptap-core.umd.js.map +1 -0
  143. package/package.json +2 -2
@@ -0,0 +1,4131 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('prosemirror-state'), require('prosemirror-transform'), require('prosemirror-commands'), require('prosemirror-model'), require('prosemirror-schema-list'), require('prosemirror-view'), require('prosemirror-keymap')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'prosemirror-state', 'prosemirror-transform', 'prosemirror-commands', 'prosemirror-model', 'prosemirror-schema-list', 'prosemirror-view', 'prosemirror-keymap'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@tiptap/core"] = {}, global.prosemirrorState, global.prosemirrorTransform, global.prosemirrorCommands, global.prosemirrorModel, global.prosemirrorSchemaList, global.prosemirrorView, global.prosemirrorKeymap));
5
+ })(this, (function (exports, prosemirrorState, prosemirrorTransform, prosemirrorCommands, prosemirrorModel, prosemirrorSchemaList, prosemirrorView, prosemirrorKeymap) { 'use strict';
6
+
7
+ // see: https://github.com/mesqueeb/is-what/blob/88d6e4ca92fb2baab6003c54e02eedf4e729e5ab/src/index.ts
8
+ function getType(value) {
9
+ return Object.prototype.toString.call(value).slice(8, -1);
10
+ }
11
+ function isPlainObject(value) {
12
+ if (getType(value) !== 'Object')
13
+ return false;
14
+ return value.constructor === Object && Object.getPrototypeOf(value) === Object.prototype;
15
+ }
16
+
17
+ function mergeDeep(target, source) {
18
+ const output = { ...target };
19
+ if (isPlainObject(target) && isPlainObject(source)) {
20
+ Object.keys(source).forEach(key => {
21
+ if (isPlainObject(source[key])) {
22
+ if (!(key in target)) {
23
+ Object.assign(output, { [key]: source[key] });
24
+ }
25
+ else {
26
+ output[key] = mergeDeep(target[key], source[key]);
27
+ }
28
+ }
29
+ else {
30
+ Object.assign(output, { [key]: source[key] });
31
+ }
32
+ });
33
+ }
34
+ return output;
35
+ }
36
+
37
+ function isObject$1(value) {
38
+ return typeof value === 'function';
39
+ }
40
+
41
+ /**
42
+ * Optionally calls `value` as a function.
43
+ * Otherwise it is returned directly.
44
+ * @param value Function or any value.
45
+ * @param context Optional context to bind to function.
46
+ * @param props Optional props to pass to function.
47
+ */
48
+ function callOrReturn(value, context = undefined, ...props) {
49
+ if (isObject$1(value)) {
50
+ if (context) {
51
+ return value.bind(context)(...props);
52
+ }
53
+ return value(...props);
54
+ }
55
+ return value;
56
+ }
57
+
58
+ function getExtensionField(extension, field, context) {
59
+ if (extension.config[field] === undefined && extension.parent) {
60
+ return getExtensionField(extension.parent, field, context);
61
+ }
62
+ if (typeof extension.config[field] === 'function') {
63
+ const value = extension.config[field].bind({
64
+ ...context,
65
+ parent: extension.parent
66
+ ? getExtensionField(extension.parent, field, context)
67
+ : null,
68
+ });
69
+ return value;
70
+ }
71
+ return extension.config[field];
72
+ }
73
+
74
+ class Extension {
75
+ constructor(config = {}) {
76
+ this.type = 'extension';
77
+ this.name = 'extension';
78
+ this.parent = null;
79
+ this.child = null;
80
+ this.config = {
81
+ name: this.name,
82
+ defaultOptions: {},
83
+ };
84
+ this.config = {
85
+ ...this.config,
86
+ ...config,
87
+ };
88
+ this.name = this.config.name;
89
+ if (config.defaultOptions) {
90
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
91
+ }
92
+ // TODO: remove `addOptions` fallback
93
+ this.options = this.config.defaultOptions;
94
+ if (this.config.addOptions) {
95
+ this.options = callOrReturn(getExtensionField(this, 'addOptions', {
96
+ name: this.name,
97
+ }));
98
+ }
99
+ this.storage = callOrReturn(getExtensionField(this, 'addStorage', {
100
+ name: this.name,
101
+ options: this.options,
102
+ })) || {};
103
+ }
104
+ static create(config = {}) {
105
+ return new Extension(config);
106
+ }
107
+ configure(options = {}) {
108
+ // return a new instance so we can use the same extension
109
+ // with different calls of `configure`
110
+ const extension = this.extend();
111
+ extension.options = mergeDeep(this.options, options);
112
+ extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
113
+ name: extension.name,
114
+ options: extension.options,
115
+ }));
116
+ return extension;
117
+ }
118
+ extend(extendedConfig = {}) {
119
+ const extension = new Extension(extendedConfig);
120
+ extension.parent = this;
121
+ this.child = extension;
122
+ extension.name = extendedConfig.name
123
+ ? extendedConfig.name
124
+ : extension.parent.name;
125
+ if (extendedConfig.defaultOptions) {
126
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`);
127
+ }
128
+ // TODO: remove `addOptions` fallback
129
+ extension.options = extendedConfig.defaultOptions
130
+ ? extendedConfig.defaultOptions
131
+ : extension.parent.options;
132
+ if (extendedConfig.addOptions) {
133
+ extension.options = callOrReturn(getExtensionField(extension, 'addOptions', {
134
+ name: extension.name,
135
+ }));
136
+ }
137
+ extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
138
+ name: extension.name,
139
+ options: extension.options,
140
+ }));
141
+ return extension;
142
+ }
143
+ }
144
+
145
+ function getTextBetween(startNode, range, options) {
146
+ const { from, to } = range;
147
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
148
+ let text = '';
149
+ let separated = true;
150
+ startNode.nodesBetween(from, to, (node, pos, parent, index) => {
151
+ var _a;
152
+ const textSerializer = textSerializers === null || textSerializers === void 0 ? void 0 : textSerializers[node.type.name];
153
+ if (textSerializer) {
154
+ if (node.isBlock && !separated) {
155
+ text += blockSeparator;
156
+ separated = true;
157
+ }
158
+ text += textSerializer({
159
+ node,
160
+ pos,
161
+ parent,
162
+ index,
163
+ });
164
+ }
165
+ else if (node.isText) {
166
+ text += (_a = node === null || node === void 0 ? void 0 : node.text) === null || _a === void 0 ? void 0 : _a.slice(Math.max(from, pos) - pos, to - pos);
167
+ separated = false;
168
+ }
169
+ else if (node.isBlock && !separated) {
170
+ text += blockSeparator;
171
+ separated = true;
172
+ }
173
+ });
174
+ return text;
175
+ }
176
+
177
+ function getTextSeralizersFromSchema(schema) {
178
+ return Object.fromEntries(Object
179
+ .entries(schema.nodes)
180
+ .filter(([, node]) => node.spec.toText)
181
+ .map(([name, node]) => [name, node.spec.toText]));
182
+ }
183
+
184
+ const ClipboardTextSerializer = Extension.create({
185
+ name: 'clipboardTextSerializer',
186
+ addProseMirrorPlugins() {
187
+ return [
188
+ new prosemirrorState.Plugin({
189
+ key: new prosemirrorState.PluginKey('clipboardTextSerializer'),
190
+ props: {
191
+ clipboardTextSerializer: () => {
192
+ const { editor } = this;
193
+ const { state, schema } = editor;
194
+ const { doc, selection } = state;
195
+ const { from, to } = selection;
196
+ const textSerializers = getTextSeralizersFromSchema(schema);
197
+ const range = { from, to };
198
+ return getTextBetween(doc, range, {
199
+ textSerializers,
200
+ });
201
+ },
202
+ },
203
+ }),
204
+ ];
205
+ },
206
+ });
207
+
208
+ const blur = () => ({ editor, view }) => {
209
+ requestAnimationFrame(() => {
210
+ if (!editor.isDestroyed) {
211
+ view.dom.blur();
212
+ }
213
+ });
214
+ return true;
215
+ };
216
+
217
+ var blur$1 = /*#__PURE__*/Object.freeze({
218
+ __proto__: null,
219
+ blur: blur
220
+ });
221
+
222
+ const clearContent = (emitUpdate = false) => ({ commands }) => {
223
+ return commands.setContent('', emitUpdate);
224
+ };
225
+
226
+ var clearContent$1 = /*#__PURE__*/Object.freeze({
227
+ __proto__: null,
228
+ clearContent: clearContent
229
+ });
230
+
231
+ const clearNodes = () => ({ state, tr, dispatch }) => {
232
+ const { selection } = tr;
233
+ const { ranges } = selection;
234
+ ranges.forEach(range => {
235
+ state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
236
+ if (node.type.isText) {
237
+ return;
238
+ }
239
+ const $fromPos = tr.doc.resolve(tr.mapping.map(pos));
240
+ const $toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize));
241
+ const nodeRange = $fromPos.blockRange($toPos);
242
+ if (!nodeRange) {
243
+ return;
244
+ }
245
+ const targetLiftDepth = prosemirrorTransform.liftTarget(nodeRange);
246
+ if (node.type.isTextblock && dispatch) {
247
+ const { defaultType } = $fromPos.parent.contentMatchAt($fromPos.index());
248
+ tr.setNodeMarkup(nodeRange.start, defaultType);
249
+ }
250
+ if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
251
+ tr.lift(nodeRange, targetLiftDepth);
252
+ }
253
+ });
254
+ });
255
+ return true;
256
+ };
257
+
258
+ var clearNodes$1 = /*#__PURE__*/Object.freeze({
259
+ __proto__: null,
260
+ clearNodes: clearNodes
261
+ });
262
+
263
+ const command = fn => props => {
264
+ return fn(props);
265
+ };
266
+
267
+ var command$1 = /*#__PURE__*/Object.freeze({
268
+ __proto__: null,
269
+ command: command
270
+ });
271
+
272
+ const createParagraphNear = () => ({ state, dispatch }) => {
273
+ return prosemirrorCommands.createParagraphNear(state, dispatch);
274
+ };
275
+
276
+ var createParagraphNear$1 = /*#__PURE__*/Object.freeze({
277
+ __proto__: null,
278
+ createParagraphNear: createParagraphNear
279
+ });
280
+
281
+ function getNodeType(nameOrType, schema) {
282
+ if (typeof nameOrType === 'string') {
283
+ if (!schema.nodes[nameOrType]) {
284
+ throw Error(`There is no node type named '${nameOrType}'. Maybe you forgot to add the extension?`);
285
+ }
286
+ return schema.nodes[nameOrType];
287
+ }
288
+ return nameOrType;
289
+ }
290
+
291
+ const deleteNode = typeOrName => ({ tr, state, dispatch }) => {
292
+ const type = getNodeType(typeOrName, state.schema);
293
+ const $pos = tr.selection.$anchor;
294
+ for (let depth = $pos.depth; depth > 0; depth -= 1) {
295
+ const node = $pos.node(depth);
296
+ if (node.type === type) {
297
+ if (dispatch) {
298
+ const from = $pos.before(depth);
299
+ const to = $pos.after(depth);
300
+ tr.delete(from, to).scrollIntoView();
301
+ }
302
+ return true;
303
+ }
304
+ }
305
+ return false;
306
+ };
307
+
308
+ var deleteNode$1 = /*#__PURE__*/Object.freeze({
309
+ __proto__: null,
310
+ deleteNode: deleteNode
311
+ });
312
+
313
+ const deleteRange = range => ({ tr, dispatch }) => {
314
+ const { from, to } = range;
315
+ if (dispatch) {
316
+ tr.delete(from, to);
317
+ }
318
+ return true;
319
+ };
320
+
321
+ var deleteRange$1 = /*#__PURE__*/Object.freeze({
322
+ __proto__: null,
323
+ deleteRange: deleteRange
324
+ });
325
+
326
+ const deleteSelection = () => ({ state, dispatch }) => {
327
+ return prosemirrorCommands.deleteSelection(state, dispatch);
328
+ };
329
+
330
+ var deleteSelection$1 = /*#__PURE__*/Object.freeze({
331
+ __proto__: null,
332
+ deleteSelection: deleteSelection
333
+ });
334
+
335
+ const enter = () => ({ commands }) => {
336
+ return commands.keyboardShortcut('Enter');
337
+ };
338
+
339
+ var enter$1 = /*#__PURE__*/Object.freeze({
340
+ __proto__: null,
341
+ enter: enter
342
+ });
343
+
344
+ const exitCode = () => ({ state, dispatch }) => {
345
+ return prosemirrorCommands.exitCode(state, dispatch);
346
+ };
347
+
348
+ var exitCode$1 = /*#__PURE__*/Object.freeze({
349
+ __proto__: null,
350
+ exitCode: exitCode
351
+ });
352
+
353
+ function getMarkType(nameOrType, schema) {
354
+ if (typeof nameOrType === 'string') {
355
+ if (!schema.marks[nameOrType]) {
356
+ throw Error(`There is no mark type named '${nameOrType}'. Maybe you forgot to add the extension?`);
357
+ }
358
+ return schema.marks[nameOrType];
359
+ }
360
+ return nameOrType;
361
+ }
362
+
363
+ function isRegExp(value) {
364
+ return Object.prototype.toString.call(value) === '[object RegExp]';
365
+ }
366
+
367
+ /**
368
+ * Check if object1 includes object2
369
+ * @param object1 Object
370
+ * @param object2 Object
371
+ */
372
+ function objectIncludes(object1, object2, options = { strict: true }) {
373
+ const keys = Object.keys(object2);
374
+ if (!keys.length) {
375
+ return true;
376
+ }
377
+ return keys.every(key => {
378
+ if (options.strict) {
379
+ return object2[key] === object1[key];
380
+ }
381
+ if (isRegExp(object2[key])) {
382
+ return object2[key].test(object1[key]);
383
+ }
384
+ return object2[key] === object1[key];
385
+ });
386
+ }
387
+
388
+ function findMarkInSet(marks, type, attributes = {}) {
389
+ return marks.find(item => {
390
+ return item.type === type && objectIncludes(item.attrs, attributes);
391
+ });
392
+ }
393
+ function isMarkInSet(marks, type, attributes = {}) {
394
+ return !!findMarkInSet(marks, type, attributes);
395
+ }
396
+ function getMarkRange($pos, type, attributes = {}) {
397
+ if (!$pos || !type) {
398
+ return;
399
+ }
400
+ const start = $pos.parent.childAfter($pos.parentOffset);
401
+ if (!start.node) {
402
+ return;
403
+ }
404
+ const mark = findMarkInSet(start.node.marks, type, attributes);
405
+ if (!mark) {
406
+ return;
407
+ }
408
+ let startIndex = $pos.index();
409
+ let startPos = $pos.start() + start.offset;
410
+ let endIndex = startIndex + 1;
411
+ let endPos = startPos + start.node.nodeSize;
412
+ findMarkInSet(start.node.marks, type, attributes);
413
+ while (startIndex > 0 && mark.isInSet($pos.parent.child(startIndex - 1).marks)) {
414
+ startIndex -= 1;
415
+ startPos -= $pos.parent.child(startIndex).nodeSize;
416
+ }
417
+ while (endIndex < $pos.parent.childCount
418
+ && isMarkInSet($pos.parent.child(endIndex).marks, type, attributes)) {
419
+ endPos += $pos.parent.child(endIndex).nodeSize;
420
+ endIndex += 1;
421
+ }
422
+ return {
423
+ from: startPos,
424
+ to: endPos,
425
+ };
426
+ }
427
+
428
+ const extendMarkRange = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
429
+ const type = getMarkType(typeOrName, state.schema);
430
+ const { doc, selection } = tr;
431
+ const { $from, from, to } = selection;
432
+ if (dispatch) {
433
+ const range = getMarkRange($from, type, attributes);
434
+ if (range && range.from <= from && range.to >= to) {
435
+ const newSelection = prosemirrorState.TextSelection.create(doc, range.from, range.to);
436
+ tr.setSelection(newSelection);
437
+ }
438
+ }
439
+ return true;
440
+ };
441
+
442
+ var extendMarkRange$1 = /*#__PURE__*/Object.freeze({
443
+ __proto__: null,
444
+ extendMarkRange: extendMarkRange
445
+ });
446
+
447
+ const first = commands => props => {
448
+ const items = typeof commands === 'function'
449
+ ? commands(props)
450
+ : commands;
451
+ for (let i = 0; i < items.length; i += 1) {
452
+ if (items[i](props)) {
453
+ return true;
454
+ }
455
+ }
456
+ return false;
457
+ };
458
+
459
+ var first$1 = /*#__PURE__*/Object.freeze({
460
+ __proto__: null,
461
+ first: first
462
+ });
463
+
464
+ function minMax(value = 0, min = 0, max = 0) {
465
+ return Math.min(Math.max(value, min), max);
466
+ }
467
+
468
+ function isClass(value) {
469
+ var _a;
470
+ if (((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.toString().substring(0, 5)) !== 'class') {
471
+ return false;
472
+ }
473
+ return true;
474
+ }
475
+
476
+ function isObject(value) {
477
+ return (value
478
+ && typeof value === 'object'
479
+ && !Array.isArray(value)
480
+ && !isClass(value));
481
+ }
482
+
483
+ function isTextSelection(value) {
484
+ return isObject(value) && value instanceof prosemirrorState.TextSelection;
485
+ }
486
+
487
+ function isiOS() {
488
+ return [
489
+ 'iPad Simulator',
490
+ 'iPhone Simulator',
491
+ 'iPod Simulator',
492
+ 'iPad',
493
+ 'iPhone',
494
+ 'iPod',
495
+ ].includes(navigator.platform)
496
+ // iPad on iOS 13 detection
497
+ || (navigator.userAgent.includes('Mac') && 'ontouchend' in document);
498
+ }
499
+
500
+ function resolveSelection(state, position = null) {
501
+ if (!position) {
502
+ return null;
503
+ }
504
+ if (position === 'start' || position === true) {
505
+ return {
506
+ from: 0,
507
+ to: 0,
508
+ };
509
+ }
510
+ if (position === 'end') {
511
+ const { size } = state.doc.content;
512
+ return {
513
+ from: size,
514
+ to: size,
515
+ };
516
+ }
517
+ return {
518
+ from: position,
519
+ to: position,
520
+ };
521
+ }
522
+ const focus = (position = null) => ({ editor, view, tr, dispatch, }) => {
523
+ const delayedFocus = () => {
524
+ // focus within `requestAnimationFrame` breaks focus on iOS
525
+ // so we have to call this
526
+ if (isiOS()) {
527
+ view.dom.focus();
528
+ }
529
+ // For React we have to focus asynchronously. Otherwise wild things happen.
530
+ // see: https://github.com/ueberdosis/tiptap/issues/1520
531
+ requestAnimationFrame(() => {
532
+ if (!editor.isDestroyed) {
533
+ view.focus();
534
+ editor.commands.scrollIntoView();
535
+ }
536
+ });
537
+ };
538
+ if ((view.hasFocus() && position === null) || position === false) {
539
+ return true;
540
+ }
541
+ // we don’t try to resolve a NodeSelection or CellSelection
542
+ if (dispatch && position === null && !isTextSelection(editor.state.selection)) {
543
+ delayedFocus();
544
+ return true;
545
+ }
546
+ const { from, to } = resolveSelection(editor.state, position) || editor.state.selection;
547
+ const { doc, storedMarks } = tr;
548
+ const minPos = prosemirrorState.Selection.atStart(doc).from;
549
+ const maxPos = prosemirrorState.Selection.atEnd(doc).to;
550
+ const resolvedFrom = minMax(from, minPos, maxPos);
551
+ const resolvedEnd = minMax(to, minPos, maxPos);
552
+ const selection = prosemirrorState.TextSelection.create(doc, resolvedFrom, resolvedEnd);
553
+ const isSameSelection = editor.state.selection.eq(selection);
554
+ if (dispatch) {
555
+ if (!isSameSelection) {
556
+ tr.setSelection(selection);
557
+ }
558
+ // `tr.setSelection` resets the stored marks
559
+ // so we’ll restore them if the selection is the same as before
560
+ if (isSameSelection && storedMarks) {
561
+ tr.setStoredMarks(storedMarks);
562
+ }
563
+ delayedFocus();
564
+ }
565
+ return true;
566
+ };
567
+
568
+ var focus$1 = /*#__PURE__*/Object.freeze({
569
+ __proto__: null,
570
+ focus: focus
571
+ });
572
+
573
+ const forEach = (items, fn) => props => {
574
+ return items.every((item, index) => fn(item, { ...props, index }));
575
+ };
576
+
577
+ var forEach$1 = /*#__PURE__*/Object.freeze({
578
+ __proto__: null,
579
+ forEach: forEach
580
+ });
581
+
582
+ const insertContent = (value, options) => ({ tr, commands }) => {
583
+ return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
584
+ };
585
+
586
+ var insertContent$1 = /*#__PURE__*/Object.freeze({
587
+ __proto__: null,
588
+ insertContent: insertContent
589
+ });
590
+
591
+ function elementFromString(value) {
592
+ // add a wrapper to preserve leading and trailing whitespace
593
+ const wrappedValue = `<body>${value}</body>`;
594
+ return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
595
+ }
596
+
597
+ function createNodeFromContent(content, schema, options) {
598
+ options = {
599
+ slice: true,
600
+ parseOptions: {},
601
+ ...options,
602
+ };
603
+ if (typeof content === 'object' && content !== null) {
604
+ try {
605
+ if (Array.isArray(content)) {
606
+ return prosemirrorModel.Fragment.fromArray(content.map(item => schema.nodeFromJSON(item)));
607
+ }
608
+ return schema.nodeFromJSON(content);
609
+ }
610
+ catch (error) {
611
+ console.warn('[tiptap warn]: Invalid content.', 'Passed value:', content, 'Error:', error);
612
+ return createNodeFromContent('', schema, options);
613
+ }
614
+ }
615
+ if (typeof content === 'string') {
616
+ const parser = prosemirrorModel.DOMParser.fromSchema(schema);
617
+ return options.slice
618
+ ? parser.parseSlice(elementFromString(content), options.parseOptions).content
619
+ : parser.parse(elementFromString(content), options.parseOptions);
620
+ }
621
+ return createNodeFromContent('', schema, options);
622
+ }
623
+
624
+ // source: https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
625
+ function selectionToInsertionEnd(tr, startLen, bias) {
626
+ const last = tr.steps.length - 1;
627
+ if (last < startLen) {
628
+ return;
629
+ }
630
+ const step = tr.steps[last];
631
+ if (!(step instanceof prosemirrorTransform.ReplaceStep || step instanceof prosemirrorTransform.ReplaceAroundStep)) {
632
+ return;
633
+ }
634
+ const map = tr.mapping.maps[last];
635
+ let end = 0;
636
+ map.forEach((_from, _to, _newFrom, newTo) => {
637
+ if (end === 0) {
638
+ end = newTo;
639
+ }
640
+ });
641
+ tr.setSelection(prosemirrorState.Selection.near(tr.doc.resolve(end), bias));
642
+ }
643
+
644
+ const insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) => {
645
+ if (dispatch) {
646
+ options = {
647
+ parseOptions: {},
648
+ updateSelection: true,
649
+ ...options,
650
+ };
651
+ const content = createNodeFromContent(value, editor.schema, {
652
+ parseOptions: {
653
+ preserveWhitespace: 'full',
654
+ ...options.parseOptions,
655
+ },
656
+ });
657
+ // don’t dispatch an empty fragment because this can lead to strange errors
658
+ if (content.toString() === '<>') {
659
+ return true;
660
+ }
661
+ let { from, to } = typeof position === 'number'
662
+ ? { from: position, to: position }
663
+ : position;
664
+ let isOnlyBlockContent = true;
665
+ content.forEach(node => {
666
+ isOnlyBlockContent = isOnlyBlockContent
667
+ ? node.isBlock
668
+ : false;
669
+ });
670
+ // check if we can replace the wrapping node by
671
+ // the newly inserted content
672
+ // example:
673
+ // replace an empty paragraph by an inserted image
674
+ // instead of inserting the image below the paragraph
675
+ if (from === to && isOnlyBlockContent) {
676
+ const $from = tr.doc.resolve(from);
677
+ const isEmptyTextBlock = $from.parent.isTextblock
678
+ && !$from.parent.type.spec.code
679
+ && !$from.parent.textContent;
680
+ if (isEmptyTextBlock) {
681
+ from -= 1;
682
+ to += 1;
683
+ }
684
+ }
685
+ tr.replaceWith(from, to, content);
686
+ // set cursor at end of inserted content
687
+ if (options.updateSelection) {
688
+ selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
689
+ }
690
+ }
691
+ return true;
692
+ };
693
+
694
+ var insertContentAt$1 = /*#__PURE__*/Object.freeze({
695
+ __proto__: null,
696
+ insertContentAt: insertContentAt
697
+ });
698
+
699
+ const joinBackward = () => ({ state, dispatch }) => {
700
+ return prosemirrorCommands.joinBackward(state, dispatch);
701
+ };
702
+
703
+ var joinBackward$1 = /*#__PURE__*/Object.freeze({
704
+ __proto__: null,
705
+ joinBackward: joinBackward
706
+ });
707
+
708
+ const joinForward = () => ({ state, dispatch }) => {
709
+ return prosemirrorCommands.joinForward(state, dispatch);
710
+ };
711
+
712
+ var joinForward$1 = /*#__PURE__*/Object.freeze({
713
+ __proto__: null,
714
+ joinForward: joinForward
715
+ });
716
+
717
+ const mac = typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false;
718
+ function normalizeKeyName(name) {
719
+ const parts = name.split(/-(?!$)/);
720
+ let result = parts[parts.length - 1];
721
+ if (result === 'Space') {
722
+ result = ' ';
723
+ }
724
+ let alt;
725
+ let ctrl;
726
+ let shift;
727
+ let meta;
728
+ for (let i = 0; i < parts.length - 1; i += 1) {
729
+ const mod = parts[i];
730
+ if (/^(cmd|meta|m)$/i.test(mod)) {
731
+ meta = true;
732
+ }
733
+ else if (/^a(lt)?$/i.test(mod)) {
734
+ alt = true;
735
+ }
736
+ else if (/^(c|ctrl|control)$/i.test(mod)) {
737
+ ctrl = true;
738
+ }
739
+ else if (/^s(hift)?$/i.test(mod)) {
740
+ shift = true;
741
+ }
742
+ else if (/^mod$/i.test(mod)) {
743
+ if (mac) {
744
+ meta = true;
745
+ }
746
+ else {
747
+ ctrl = true;
748
+ }
749
+ }
750
+ else {
751
+ throw new Error(`Unrecognized modifier name: ${mod}`);
752
+ }
753
+ }
754
+ if (alt) {
755
+ result = `Alt-${result}`;
756
+ }
757
+ if (ctrl) {
758
+ result = `Ctrl-${result}`;
759
+ }
760
+ if (meta) {
761
+ result = `Meta-${result}`;
762
+ }
763
+ if (shift) {
764
+ result = `Shift-${result}`;
765
+ }
766
+ return result;
767
+ }
768
+ const keyboardShortcut = name => ({ editor, view, tr, dispatch, }) => {
769
+ const keys = normalizeKeyName(name).split(/-(?!$)/);
770
+ const key = keys.find(item => !['Alt', 'Ctrl', 'Meta', 'Shift'].includes(item));
771
+ const event = new KeyboardEvent('keydown', {
772
+ key: key === 'Space'
773
+ ? ' '
774
+ : key,
775
+ altKey: keys.includes('Alt'),
776
+ ctrlKey: keys.includes('Ctrl'),
777
+ metaKey: keys.includes('Meta'),
778
+ shiftKey: keys.includes('Shift'),
779
+ bubbles: true,
780
+ cancelable: true,
781
+ });
782
+ const capturedTransaction = editor.captureTransaction(() => {
783
+ view.someProp('handleKeyDown', f => f(view, event));
784
+ });
785
+ capturedTransaction === null || capturedTransaction === void 0 ? void 0 : capturedTransaction.steps.forEach(step => {
786
+ const newStep = step.map(tr.mapping);
787
+ if (newStep && dispatch) {
788
+ tr.maybeStep(newStep);
789
+ }
790
+ });
791
+ return true;
792
+ };
793
+
794
+ var keyboardShortcut$1 = /*#__PURE__*/Object.freeze({
795
+ __proto__: null,
796
+ keyboardShortcut: keyboardShortcut
797
+ });
798
+
799
+ function isNodeActive(state, typeOrName, attributes = {}) {
800
+ const { from, to, empty } = state.selection;
801
+ const type = typeOrName
802
+ ? getNodeType(typeOrName, state.schema)
803
+ : null;
804
+ const nodeRanges = [];
805
+ state.doc.nodesBetween(from, to, (node, pos) => {
806
+ if (node.isText) {
807
+ return;
808
+ }
809
+ const relativeFrom = Math.max(from, pos);
810
+ const relativeTo = Math.min(to, pos + node.nodeSize);
811
+ nodeRanges.push({
812
+ node,
813
+ from: relativeFrom,
814
+ to: relativeTo,
815
+ });
816
+ });
817
+ const selectionRange = to - from;
818
+ const matchedNodeRanges = nodeRanges
819
+ .filter(nodeRange => {
820
+ if (!type) {
821
+ return true;
822
+ }
823
+ return type.name === nodeRange.node.type.name;
824
+ })
825
+ .filter(nodeRange => objectIncludes(nodeRange.node.attrs, attributes, { strict: false }));
826
+ if (empty) {
827
+ return !!matchedNodeRanges.length;
828
+ }
829
+ const range = matchedNodeRanges
830
+ .reduce((sum, nodeRange) => sum + nodeRange.to - nodeRange.from, 0);
831
+ return range >= selectionRange;
832
+ }
833
+
834
+ const lift = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
835
+ const type = getNodeType(typeOrName, state.schema);
836
+ const isActive = isNodeActive(state, type, attributes);
837
+ if (!isActive) {
838
+ return false;
839
+ }
840
+ return prosemirrorCommands.lift(state, dispatch);
841
+ };
842
+
843
+ var lift$1 = /*#__PURE__*/Object.freeze({
844
+ __proto__: null,
845
+ lift: lift
846
+ });
847
+
848
+ const liftEmptyBlock = () => ({ state, dispatch }) => {
849
+ return prosemirrorCommands.liftEmptyBlock(state, dispatch);
850
+ };
851
+
852
+ var liftEmptyBlock$1 = /*#__PURE__*/Object.freeze({
853
+ __proto__: null,
854
+ liftEmptyBlock: liftEmptyBlock
855
+ });
856
+
857
+ const liftListItem = typeOrName => ({ state, dispatch }) => {
858
+ const type = getNodeType(typeOrName, state.schema);
859
+ return prosemirrorSchemaList.liftListItem(type)(state, dispatch);
860
+ };
861
+
862
+ var liftListItem$1 = /*#__PURE__*/Object.freeze({
863
+ __proto__: null,
864
+ liftListItem: liftListItem
865
+ });
866
+
867
+ const newlineInCode = () => ({ state, dispatch }) => {
868
+ return prosemirrorCommands.newlineInCode(state, dispatch);
869
+ };
870
+
871
+ var newlineInCode$1 = /*#__PURE__*/Object.freeze({
872
+ __proto__: null,
873
+ newlineInCode: newlineInCode
874
+ });
875
+
876
+ function getSchemaTypeNameByName(name, schema) {
877
+ if (schema.nodes[name]) {
878
+ return 'node';
879
+ }
880
+ if (schema.marks[name]) {
881
+ return 'mark';
882
+ }
883
+ return null;
884
+ }
885
+
886
+ /**
887
+ * Remove a property or an array of properties from an object
888
+ * @param obj Object
889
+ * @param key Key to remove
890
+ */
891
+ function deleteProps(obj, propOrProps) {
892
+ const props = typeof propOrProps === 'string'
893
+ ? [propOrProps]
894
+ : propOrProps;
895
+ return Object
896
+ .keys(obj)
897
+ .reduce((newObj, prop) => {
898
+ if (!props.includes(prop)) {
899
+ newObj[prop] = obj[prop];
900
+ }
901
+ return newObj;
902
+ }, {});
903
+ }
904
+
905
+ const resetAttributes = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
906
+ let nodeType = null;
907
+ let markType = null;
908
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
909
+ ? typeOrName
910
+ : typeOrName.name, state.schema);
911
+ if (!schemaType) {
912
+ return false;
913
+ }
914
+ if (schemaType === 'node') {
915
+ nodeType = getNodeType(typeOrName, state.schema);
916
+ }
917
+ if (schemaType === 'mark') {
918
+ markType = getMarkType(typeOrName, state.schema);
919
+ }
920
+ if (dispatch) {
921
+ tr.selection.ranges.forEach(range => {
922
+ state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
923
+ if (nodeType && nodeType === node.type) {
924
+ tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes));
925
+ }
926
+ if (markType && node.marks.length) {
927
+ node.marks.forEach(mark => {
928
+ if (markType === mark.type) {
929
+ tr.addMark(pos, pos + node.nodeSize, markType.create(deleteProps(mark.attrs, attributes)));
930
+ }
931
+ });
932
+ }
933
+ });
934
+ });
935
+ }
936
+ return true;
937
+ };
938
+
939
+ var resetAttributes$1 = /*#__PURE__*/Object.freeze({
940
+ __proto__: null,
941
+ resetAttributes: resetAttributes
942
+ });
943
+
944
+ const scrollIntoView = () => ({ tr, dispatch }) => {
945
+ if (dispatch) {
946
+ tr.scrollIntoView();
947
+ }
948
+ return true;
949
+ };
950
+
951
+ var scrollIntoView$1 = /*#__PURE__*/Object.freeze({
952
+ __proto__: null,
953
+ scrollIntoView: scrollIntoView
954
+ });
955
+
956
+ const selectAll = () => ({ tr, commands }) => {
957
+ return commands.setTextSelection({
958
+ from: 0,
959
+ to: tr.doc.content.size,
960
+ });
961
+ };
962
+
963
+ var selectAll$1 = /*#__PURE__*/Object.freeze({
964
+ __proto__: null,
965
+ selectAll: selectAll
966
+ });
967
+
968
+ const selectNodeBackward = () => ({ state, dispatch }) => {
969
+ return prosemirrorCommands.selectNodeBackward(state, dispatch);
970
+ };
971
+
972
+ var selectNodeBackward$1 = /*#__PURE__*/Object.freeze({
973
+ __proto__: null,
974
+ selectNodeBackward: selectNodeBackward
975
+ });
976
+
977
+ const selectNodeForward = () => ({ state, dispatch }) => {
978
+ return prosemirrorCommands.selectNodeForward(state, dispatch);
979
+ };
980
+
981
+ var selectNodeForward$1 = /*#__PURE__*/Object.freeze({
982
+ __proto__: null,
983
+ selectNodeForward: selectNodeForward
984
+ });
985
+
986
+ const selectParentNode = () => ({ state, dispatch }) => {
987
+ return prosemirrorCommands.selectParentNode(state, dispatch);
988
+ };
989
+
990
+ var selectParentNode$1 = /*#__PURE__*/Object.freeze({
991
+ __proto__: null,
992
+ selectParentNode: selectParentNode
993
+ });
994
+
995
+ function createDocument(content, schema, parseOptions = {}) {
996
+ return createNodeFromContent(content, schema, { slice: false, parseOptions });
997
+ }
998
+
999
+ const setContent = (content, emitUpdate = false, parseOptions = {}) => ({ tr, editor, dispatch }) => {
1000
+ const { doc } = tr;
1001
+ const document = createDocument(content, editor.schema, parseOptions);
1002
+ const selection = prosemirrorState.TextSelection.create(doc, 0, doc.content.size);
1003
+ if (dispatch) {
1004
+ tr.setSelection(selection)
1005
+ .replaceSelectionWith(document, false)
1006
+ .setMeta('preventUpdate', !emitUpdate);
1007
+ }
1008
+ return true;
1009
+ };
1010
+
1011
+ var setContent$1 = /*#__PURE__*/Object.freeze({
1012
+ __proto__: null,
1013
+ setContent: setContent
1014
+ });
1015
+
1016
+ function getMarkAttributes(state, typeOrName) {
1017
+ const type = getMarkType(typeOrName, state.schema);
1018
+ const { from, to, empty } = state.selection;
1019
+ const marks = [];
1020
+ if (empty) {
1021
+ if (state.storedMarks) {
1022
+ marks.push(...state.storedMarks);
1023
+ }
1024
+ marks.push(...state.selection.$head.marks());
1025
+ }
1026
+ else {
1027
+ state.doc.nodesBetween(from, to, node => {
1028
+ marks.push(...node.marks);
1029
+ });
1030
+ }
1031
+ const mark = marks.find(markItem => markItem.type.name === type.name);
1032
+ if (!mark) {
1033
+ return {};
1034
+ }
1035
+ return { ...mark.attrs };
1036
+ }
1037
+
1038
+ const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1039
+ const { selection } = tr;
1040
+ const { empty, ranges } = selection;
1041
+ const type = getMarkType(typeOrName, state.schema);
1042
+ if (dispatch) {
1043
+ if (empty) {
1044
+ const oldAttributes = getMarkAttributes(state, type);
1045
+ tr.addStoredMark(type.create({
1046
+ ...oldAttributes,
1047
+ ...attributes,
1048
+ }));
1049
+ }
1050
+ else {
1051
+ ranges.forEach(range => {
1052
+ const from = range.$from.pos;
1053
+ const to = range.$to.pos;
1054
+ state.doc.nodesBetween(from, to, (node, pos) => {
1055
+ const trimmedFrom = Math.max(pos, from);
1056
+ const trimmedTo = Math.min(pos + node.nodeSize, to);
1057
+ const someHasMark = node.marks.find(mark => mark.type === type);
1058
+ // if there is already a mark of this type
1059
+ // we know that we have to merge its attributes
1060
+ // otherwise we add a fresh new mark
1061
+ if (someHasMark) {
1062
+ node.marks.forEach(mark => {
1063
+ if (type === mark.type) {
1064
+ tr.addMark(trimmedFrom, trimmedTo, type.create({
1065
+ ...mark.attrs,
1066
+ ...attributes,
1067
+ }));
1068
+ }
1069
+ });
1070
+ }
1071
+ else {
1072
+ tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
1073
+ }
1074
+ });
1075
+ });
1076
+ }
1077
+ }
1078
+ return true;
1079
+ };
1080
+
1081
+ var setMark$1 = /*#__PURE__*/Object.freeze({
1082
+ __proto__: null,
1083
+ setMark: setMark
1084
+ });
1085
+
1086
+ const setMeta = (key, value) => ({ tr }) => {
1087
+ tr.setMeta(key, value);
1088
+ return true;
1089
+ };
1090
+
1091
+ var setMeta$1 = /*#__PURE__*/Object.freeze({
1092
+ __proto__: null,
1093
+ setMeta: setMeta
1094
+ });
1095
+
1096
+ const setNode = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1097
+ const type = getNodeType(typeOrName, state.schema);
1098
+ return prosemirrorCommands.setBlockType(type, attributes)(state, dispatch);
1099
+ };
1100
+
1101
+ var setNode$1 = /*#__PURE__*/Object.freeze({
1102
+ __proto__: null,
1103
+ setNode: setNode
1104
+ });
1105
+
1106
+ const setNodeSelection = position => ({ tr, dispatch }) => {
1107
+ if (dispatch) {
1108
+ const { doc } = tr;
1109
+ const minPos = prosemirrorState.Selection.atStart(doc).from;
1110
+ const maxPos = prosemirrorState.Selection.atEnd(doc).to;
1111
+ const resolvedPos = minMax(position, minPos, maxPos);
1112
+ const selection = prosemirrorState.NodeSelection.create(doc, resolvedPos);
1113
+ tr.setSelection(selection);
1114
+ }
1115
+ return true;
1116
+ };
1117
+
1118
+ var setNodeSelection$1 = /*#__PURE__*/Object.freeze({
1119
+ __proto__: null,
1120
+ setNodeSelection: setNodeSelection
1121
+ });
1122
+
1123
+ const setTextSelection = position => ({ tr, dispatch }) => {
1124
+ if (dispatch) {
1125
+ const { doc } = tr;
1126
+ const { from, to } = typeof position === 'number'
1127
+ ? { from: position, to: position }
1128
+ : position;
1129
+ const minPos = prosemirrorState.Selection.atStart(doc).from;
1130
+ const maxPos = prosemirrorState.Selection.atEnd(doc).to;
1131
+ const resolvedFrom = minMax(from, minPos, maxPos);
1132
+ const resolvedEnd = minMax(to, minPos, maxPos);
1133
+ const selection = prosemirrorState.TextSelection.create(doc, resolvedFrom, resolvedEnd);
1134
+ tr.setSelection(selection);
1135
+ }
1136
+ return true;
1137
+ };
1138
+
1139
+ var setTextSelection$1 = /*#__PURE__*/Object.freeze({
1140
+ __proto__: null,
1141
+ setTextSelection: setTextSelection
1142
+ });
1143
+
1144
+ const sinkListItem = typeOrName => ({ state, dispatch }) => {
1145
+ const type = getNodeType(typeOrName, state.schema);
1146
+ return prosemirrorSchemaList.sinkListItem(type)(state, dispatch);
1147
+ };
1148
+
1149
+ var sinkListItem$1 = /*#__PURE__*/Object.freeze({
1150
+ __proto__: null,
1151
+ sinkListItem: sinkListItem
1152
+ });
1153
+
1154
+ function getSplittedAttributes(extensionAttributes, typeName, attributes) {
1155
+ return Object.fromEntries(Object
1156
+ .entries(attributes)
1157
+ .filter(([name]) => {
1158
+ const extensionAttribute = extensionAttributes.find(item => {
1159
+ return item.type === typeName && item.name === name;
1160
+ });
1161
+ if (!extensionAttribute) {
1162
+ return false;
1163
+ }
1164
+ return extensionAttribute.attribute.keepOnSplit;
1165
+ }));
1166
+ }
1167
+
1168
+ function defaultBlockAt(match) {
1169
+ for (let i = 0; i < match.edgeCount; i += 1) {
1170
+ const { type } = match.edge(i);
1171
+ if (type.isTextblock && !type.hasRequiredAttrs()) {
1172
+ return type;
1173
+ }
1174
+ }
1175
+ return null;
1176
+ }
1177
+ function ensureMarks(state, splittableMarks) {
1178
+ const marks = state.storedMarks
1179
+ || (state.selection.$to.parentOffset && state.selection.$from.marks());
1180
+ if (marks) {
1181
+ const filteredMarks = marks.filter(mark => splittableMarks === null || splittableMarks === void 0 ? void 0 : splittableMarks.includes(mark.type.name));
1182
+ state.tr.ensureMarks(filteredMarks);
1183
+ }
1184
+ }
1185
+ const splitBlock = ({ keepMarks = true } = {}) => ({ tr, state, dispatch, editor, }) => {
1186
+ const { selection, doc } = tr;
1187
+ const { $from, $to } = selection;
1188
+ const extensionAttributes = editor.extensionManager.attributes;
1189
+ const newAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
1190
+ if (selection instanceof prosemirrorState.NodeSelection && selection.node.isBlock) {
1191
+ if (!$from.parentOffset || !prosemirrorTransform.canSplit(doc, $from.pos)) {
1192
+ return false;
1193
+ }
1194
+ if (dispatch) {
1195
+ if (keepMarks) {
1196
+ ensureMarks(state, editor.extensionManager.splittableMarks);
1197
+ }
1198
+ tr.split($from.pos).scrollIntoView();
1199
+ }
1200
+ return true;
1201
+ }
1202
+ if (!$from.parent.isBlock) {
1203
+ return false;
1204
+ }
1205
+ if (dispatch) {
1206
+ const atEnd = $to.parentOffset === $to.parent.content.size;
1207
+ if (selection instanceof prosemirrorState.TextSelection) {
1208
+ tr.deleteSelection();
1209
+ }
1210
+ const deflt = $from.depth === 0
1211
+ ? undefined
1212
+ : defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)));
1213
+ let types = atEnd && deflt
1214
+ ? [{
1215
+ type: deflt,
1216
+ attrs: newAttributes,
1217
+ }]
1218
+ : undefined;
1219
+ let can = prosemirrorTransform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, types);
1220
+ if (!types
1221
+ && !can
1222
+ && prosemirrorTransform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : undefined)) {
1223
+ can = true;
1224
+ types = deflt
1225
+ ? [{
1226
+ type: deflt,
1227
+ attrs: newAttributes,
1228
+ }]
1229
+ : undefined;
1230
+ }
1231
+ if (can) {
1232
+ tr.split(tr.mapping.map($from.pos), 1, types);
1233
+ if (deflt
1234
+ && !atEnd
1235
+ && !$from.parentOffset
1236
+ && $from.parent.type !== deflt) {
1237
+ const first = tr.mapping.map($from.before());
1238
+ const $first = tr.doc.resolve(first);
1239
+ if ($from.node(-1).canReplaceWith($first.index(), $first.index() + 1, deflt)) {
1240
+ tr.setNodeMarkup(tr.mapping.map($from.before()), deflt);
1241
+ }
1242
+ }
1243
+ }
1244
+ if (keepMarks) {
1245
+ ensureMarks(state, editor.extensionManager.splittableMarks);
1246
+ }
1247
+ tr.scrollIntoView();
1248
+ }
1249
+ return true;
1250
+ };
1251
+
1252
+ var splitBlock$1 = /*#__PURE__*/Object.freeze({
1253
+ __proto__: null,
1254
+ splitBlock: splitBlock
1255
+ });
1256
+
1257
+ const splitListItem = typeOrName => ({ tr, state, dispatch, editor, }) => {
1258
+ var _a;
1259
+ const type = getNodeType(typeOrName, state.schema);
1260
+ const { $from, $to } = state.selection;
1261
+ // @ts-ignore
1262
+ // eslint-disable-next-line
1263
+ const node = state.selection.node;
1264
+ if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) {
1265
+ return false;
1266
+ }
1267
+ const grandParent = $from.node(-1);
1268
+ if (grandParent.type !== type) {
1269
+ return false;
1270
+ }
1271
+ const extensionAttributes = editor.extensionManager.attributes;
1272
+ if ($from.parent.content.size === 0 && $from.node(-1).childCount === $from.indexAfter(-1)) {
1273
+ // In an empty block. If this is a nested list, the wrapping
1274
+ // list item should be split. Otherwise, bail out and let next
1275
+ // command handle lifting.
1276
+ if ($from.depth === 2
1277
+ || $from.node(-3).type !== type
1278
+ || $from.index(-2) !== $from.node(-2).childCount - 1) {
1279
+ return false;
1280
+ }
1281
+ if (dispatch) {
1282
+ let wrap = prosemirrorModel.Fragment.empty;
1283
+ // eslint-disable-next-line
1284
+ const depthBefore = $from.index(-1)
1285
+ ? 1
1286
+ : $from.index(-2)
1287
+ ? 2
1288
+ : 3;
1289
+ // Build a fragment containing empty versions of the structure
1290
+ // from the outer list item to the parent node of the cursor
1291
+ for (let d = $from.depth - depthBefore; d >= $from.depth - 3; d -= 1) {
1292
+ wrap = prosemirrorModel.Fragment.from($from.node(d).copy(wrap));
1293
+ }
1294
+ // eslint-disable-next-line
1295
+ const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount
1296
+ ? 1
1297
+ : $from.indexAfter(-2) < $from.node(-3).childCount
1298
+ ? 2
1299
+ : 3;
1300
+ // Add a second list item with an empty default start node
1301
+ const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
1302
+ const nextType = ((_a = type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.createAndFill(newNextTypeAttributes)) || undefined;
1303
+ wrap = wrap.append(prosemirrorModel.Fragment.from(type.createAndFill(null, nextType) || undefined));
1304
+ const start = $from.before($from.depth - (depthBefore - 1));
1305
+ tr.replace(start, $from.after(-depthAfter), new prosemirrorModel.Slice(wrap, 4 - depthBefore, 0));
1306
+ let sel = -1;
1307
+ tr.doc.nodesBetween(start, tr.doc.content.size, (n, pos) => {
1308
+ if (sel > -1) {
1309
+ return false;
1310
+ }
1311
+ if (n.isTextblock && n.content.size === 0) {
1312
+ sel = pos + 1;
1313
+ }
1314
+ });
1315
+ if (sel > -1) {
1316
+ tr.setSelection(prosemirrorState.TextSelection.near(tr.doc.resolve(sel)));
1317
+ }
1318
+ tr.scrollIntoView();
1319
+ }
1320
+ return true;
1321
+ }
1322
+ const nextType = $to.pos === $from.end()
1323
+ ? grandParent.contentMatchAt(0).defaultType
1324
+ : null;
1325
+ const newTypeAttributes = getSplittedAttributes(extensionAttributes, grandParent.type.name, grandParent.attrs);
1326
+ const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
1327
+ tr.delete($from.pos, $to.pos);
1328
+ const types = nextType
1329
+ ? [{ type, attrs: newTypeAttributes }, { type: nextType, attrs: newNextTypeAttributes }]
1330
+ : [{ type, attrs: newTypeAttributes }];
1331
+ if (!prosemirrorTransform.canSplit(tr.doc, $from.pos, 2)) {
1332
+ return false;
1333
+ }
1334
+ if (dispatch) {
1335
+ tr.split($from.pos, 2, types).scrollIntoView();
1336
+ }
1337
+ return true;
1338
+ };
1339
+
1340
+ var splitListItem$1 = /*#__PURE__*/Object.freeze({
1341
+ __proto__: null,
1342
+ splitListItem: splitListItem
1343
+ });
1344
+
1345
+ function findParentNodeClosestToPos($pos, predicate) {
1346
+ for (let i = $pos.depth; i > 0; i -= 1) {
1347
+ const node = $pos.node(i);
1348
+ if (predicate(node)) {
1349
+ return {
1350
+ pos: i > 0 ? $pos.before(i) : 0,
1351
+ start: $pos.start(i),
1352
+ depth: i,
1353
+ node,
1354
+ };
1355
+ }
1356
+ }
1357
+ }
1358
+
1359
+ function findParentNode(predicate) {
1360
+ return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
1361
+ }
1362
+
1363
+ function splitExtensions(extensions) {
1364
+ const baseExtensions = extensions.filter(extension => extension.type === 'extension');
1365
+ const nodeExtensions = extensions.filter(extension => extension.type === 'node');
1366
+ const markExtensions = extensions.filter(extension => extension.type === 'mark');
1367
+ return {
1368
+ baseExtensions,
1369
+ nodeExtensions,
1370
+ markExtensions,
1371
+ };
1372
+ }
1373
+
1374
+ function isList(name, extensions) {
1375
+ const { nodeExtensions } = splitExtensions(extensions);
1376
+ const extension = nodeExtensions.find(item => item.name === name);
1377
+ if (!extension) {
1378
+ return false;
1379
+ }
1380
+ const context = {
1381
+ name: extension.name,
1382
+ options: extension.options,
1383
+ storage: extension.storage,
1384
+ };
1385
+ const group = callOrReturn(getExtensionField(extension, 'group', context));
1386
+ if (typeof group !== 'string') {
1387
+ return false;
1388
+ }
1389
+ return group.split(' ').includes('list');
1390
+ }
1391
+
1392
+ const toggleList = (listTypeOrName, itemTypeOrName) => ({ editor, tr, state, dispatch, chain, commands, can, }) => {
1393
+ const { extensions } = editor.extensionManager;
1394
+ const listType = getNodeType(listTypeOrName, state.schema);
1395
+ const itemType = getNodeType(itemTypeOrName, state.schema);
1396
+ const { selection } = state;
1397
+ const { $from, $to } = selection;
1398
+ const range = $from.blockRange($to);
1399
+ if (!range) {
1400
+ return false;
1401
+ }
1402
+ const parentList = findParentNode(node => isList(node.type.name, extensions))(selection);
1403
+ if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
1404
+ // remove list
1405
+ if (parentList.node.type === listType) {
1406
+ return commands.liftListItem(itemType);
1407
+ }
1408
+ // change list type
1409
+ if (isList(parentList.node.type.name, extensions)
1410
+ && listType.validContent(parentList.node.content)
1411
+ && dispatch) {
1412
+ tr.setNodeMarkup(parentList.pos, listType);
1413
+ return true;
1414
+ }
1415
+ }
1416
+ const canWrapInList = can().wrapInList(listType);
1417
+ // try to convert node to paragraph if needed
1418
+ if (!canWrapInList) {
1419
+ return chain()
1420
+ .clearNodes()
1421
+ .wrapInList(listType)
1422
+ .run();
1423
+ }
1424
+ return commands.wrapInList(listType);
1425
+ };
1426
+
1427
+ var toggleList$1 = /*#__PURE__*/Object.freeze({
1428
+ __proto__: null,
1429
+ toggleList: toggleList
1430
+ });
1431
+
1432
+ function isMarkActive(state, typeOrName, attributes = {}) {
1433
+ const { empty, ranges } = state.selection;
1434
+ const type = typeOrName
1435
+ ? getMarkType(typeOrName, state.schema)
1436
+ : null;
1437
+ if (empty) {
1438
+ return !!(state.storedMarks || state.selection.$from.marks())
1439
+ .filter(mark => {
1440
+ if (!type) {
1441
+ return true;
1442
+ }
1443
+ return type.name === mark.type.name;
1444
+ })
1445
+ .find(mark => objectIncludes(mark.attrs, attributes, { strict: false }));
1446
+ }
1447
+ let selectionRange = 0;
1448
+ const markRanges = [];
1449
+ ranges.forEach(({ $from, $to }) => {
1450
+ const from = $from.pos;
1451
+ const to = $to.pos;
1452
+ state.doc.nodesBetween(from, to, (node, pos) => {
1453
+ if (!node.isText && !node.marks.length) {
1454
+ return;
1455
+ }
1456
+ const relativeFrom = Math.max(from, pos);
1457
+ const relativeTo = Math.min(to, pos + node.nodeSize);
1458
+ const range = relativeTo - relativeFrom;
1459
+ selectionRange += range;
1460
+ markRanges.push(...node.marks.map(mark => ({
1461
+ mark,
1462
+ from: relativeFrom,
1463
+ to: relativeTo,
1464
+ })));
1465
+ });
1466
+ });
1467
+ if (selectionRange === 0) {
1468
+ return false;
1469
+ }
1470
+ // calculate range of matched mark
1471
+ const matchedRange = markRanges
1472
+ .filter(markRange => {
1473
+ if (!type) {
1474
+ return true;
1475
+ }
1476
+ return type.name === markRange.mark.type.name;
1477
+ })
1478
+ .filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
1479
+ .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
1480
+ // calculate range of marks that excludes the searched mark
1481
+ // for example `code` doesn’t allow any other marks
1482
+ const excludedRange = markRanges
1483
+ .filter(markRange => {
1484
+ if (!type) {
1485
+ return true;
1486
+ }
1487
+ return markRange.mark.type !== type
1488
+ && markRange.mark.type.excludes(type);
1489
+ })
1490
+ .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
1491
+ // we only include the result of `excludedRange`
1492
+ // if there is a match at all
1493
+ const range = matchedRange > 0
1494
+ ? matchedRange + excludedRange
1495
+ : matchedRange;
1496
+ return range >= selectionRange;
1497
+ }
1498
+
1499
+ const toggleMark = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
1500
+ const { extendEmptyMarkRange = false } = options;
1501
+ const type = getMarkType(typeOrName, state.schema);
1502
+ const isActive = isMarkActive(state, type, attributes);
1503
+ if (isActive) {
1504
+ return commands.unsetMark(type, { extendEmptyMarkRange });
1505
+ }
1506
+ return commands.setMark(type, attributes);
1507
+ };
1508
+
1509
+ var toggleMark$1 = /*#__PURE__*/Object.freeze({
1510
+ __proto__: null,
1511
+ toggleMark: toggleMark
1512
+ });
1513
+
1514
+ const toggleNode = (typeOrName, toggleTypeOrName, attributes = {}) => ({ state, commands }) => {
1515
+ const type = getNodeType(typeOrName, state.schema);
1516
+ const toggleType = getNodeType(toggleTypeOrName, state.schema);
1517
+ const isActive = isNodeActive(state, type, attributes);
1518
+ if (isActive) {
1519
+ return commands.setNode(toggleType);
1520
+ }
1521
+ return commands.setNode(type, attributes);
1522
+ };
1523
+
1524
+ var toggleNode$1 = /*#__PURE__*/Object.freeze({
1525
+ __proto__: null,
1526
+ toggleNode: toggleNode
1527
+ });
1528
+
1529
+ const toggleWrap = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1530
+ const type = getNodeType(typeOrName, state.schema);
1531
+ const isActive = isNodeActive(state, type, attributes);
1532
+ if (isActive) {
1533
+ return prosemirrorCommands.lift(state, dispatch);
1534
+ }
1535
+ return prosemirrorCommands.wrapIn(type, attributes)(state, dispatch);
1536
+ };
1537
+
1538
+ var toggleWrap$1 = /*#__PURE__*/Object.freeze({
1539
+ __proto__: null,
1540
+ toggleWrap: toggleWrap
1541
+ });
1542
+
1543
+ const undoInputRule = () => ({ state, dispatch }) => {
1544
+ const plugins = state.plugins;
1545
+ for (let i = 0; i < plugins.length; i += 1) {
1546
+ const plugin = plugins[i];
1547
+ let undoable;
1548
+ // @ts-ignore
1549
+ // eslint-disable-next-line
1550
+ if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
1551
+ if (dispatch) {
1552
+ const tr = state.tr;
1553
+ const toUndo = undoable.transform;
1554
+ for (let j = toUndo.steps.length - 1; j >= 0; j -= 1) {
1555
+ tr.step(toUndo.steps[j].invert(toUndo.docs[j]));
1556
+ }
1557
+ if (undoable.text) {
1558
+ const marks = tr.doc.resolve(undoable.from).marks();
1559
+ tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks));
1560
+ }
1561
+ else {
1562
+ tr.delete(undoable.from, undoable.to);
1563
+ }
1564
+ }
1565
+ return true;
1566
+ }
1567
+ }
1568
+ return false;
1569
+ };
1570
+
1571
+ var undoInputRule$1 = /*#__PURE__*/Object.freeze({
1572
+ __proto__: null,
1573
+ undoInputRule: undoInputRule
1574
+ });
1575
+
1576
+ const unsetAllMarks = () => ({ tr, state, dispatch }) => {
1577
+ const { selection } = tr;
1578
+ const { empty, ranges } = selection;
1579
+ if (empty) {
1580
+ return true;
1581
+ }
1582
+ if (dispatch) {
1583
+ Object
1584
+ .entries(state.schema.marks)
1585
+ .forEach(([, mark]) => {
1586
+ ranges.forEach(range => {
1587
+ tr.removeMark(range.$from.pos, range.$to.pos, mark);
1588
+ });
1589
+ });
1590
+ }
1591
+ return true;
1592
+ };
1593
+
1594
+ var unsetAllMarks$1 = /*#__PURE__*/Object.freeze({
1595
+ __proto__: null,
1596
+ unsetAllMarks: unsetAllMarks
1597
+ });
1598
+
1599
+ const unsetMark = (typeOrName, options = {}) => ({ tr, state, dispatch }) => {
1600
+ var _a;
1601
+ const { extendEmptyMarkRange = false } = options;
1602
+ const { selection } = tr;
1603
+ const type = getMarkType(typeOrName, state.schema);
1604
+ const { $from, empty, ranges } = selection;
1605
+ if (!dispatch) {
1606
+ return true;
1607
+ }
1608
+ if (empty && extendEmptyMarkRange) {
1609
+ let { from, to } = selection;
1610
+ const attrs = (_a = $from.marks().find(mark => mark.type === type)) === null || _a === void 0 ? void 0 : _a.attrs;
1611
+ const range = getMarkRange($from, type, attrs);
1612
+ if (range) {
1613
+ from = range.from;
1614
+ to = range.to;
1615
+ }
1616
+ tr.removeMark(from, to, type);
1617
+ }
1618
+ else {
1619
+ ranges.forEach(range => {
1620
+ tr.removeMark(range.$from.pos, range.$to.pos, type);
1621
+ });
1622
+ }
1623
+ tr.removeStoredMark(type);
1624
+ return true;
1625
+ };
1626
+
1627
+ var unsetMark$1 = /*#__PURE__*/Object.freeze({
1628
+ __proto__: null,
1629
+ unsetMark: unsetMark
1630
+ });
1631
+
1632
+ const updateAttributes = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1633
+ let nodeType = null;
1634
+ let markType = null;
1635
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
1636
+ ? typeOrName
1637
+ : typeOrName.name, state.schema);
1638
+ if (!schemaType) {
1639
+ return false;
1640
+ }
1641
+ if (schemaType === 'node') {
1642
+ nodeType = getNodeType(typeOrName, state.schema);
1643
+ }
1644
+ if (schemaType === 'mark') {
1645
+ markType = getMarkType(typeOrName, state.schema);
1646
+ }
1647
+ if (dispatch) {
1648
+ tr.selection.ranges.forEach(range => {
1649
+ const from = range.$from.pos;
1650
+ const to = range.$to.pos;
1651
+ state.doc.nodesBetween(from, to, (node, pos) => {
1652
+ if (nodeType && nodeType === node.type) {
1653
+ tr.setNodeMarkup(pos, undefined, {
1654
+ ...node.attrs,
1655
+ ...attributes,
1656
+ });
1657
+ }
1658
+ if (markType && node.marks.length) {
1659
+ node.marks.forEach(mark => {
1660
+ if (markType === mark.type) {
1661
+ const trimmedFrom = Math.max(pos, from);
1662
+ const trimmedTo = Math.min(pos + node.nodeSize, to);
1663
+ tr.addMark(trimmedFrom, trimmedTo, markType.create({
1664
+ ...mark.attrs,
1665
+ ...attributes,
1666
+ }));
1667
+ }
1668
+ });
1669
+ }
1670
+ });
1671
+ });
1672
+ }
1673
+ return true;
1674
+ };
1675
+
1676
+ var updateAttributes$1 = /*#__PURE__*/Object.freeze({
1677
+ __proto__: null,
1678
+ updateAttributes: updateAttributes
1679
+ });
1680
+
1681
+ const wrapIn = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1682
+ const type = getNodeType(typeOrName, state.schema);
1683
+ const isActive = isNodeActive(state, type, attributes);
1684
+ if (isActive) {
1685
+ return false;
1686
+ }
1687
+ return prosemirrorCommands.wrapIn(type, attributes)(state, dispatch);
1688
+ };
1689
+
1690
+ var wrapIn$1 = /*#__PURE__*/Object.freeze({
1691
+ __proto__: null,
1692
+ wrapIn: wrapIn
1693
+ });
1694
+
1695
+ const wrapInList = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
1696
+ const type = getNodeType(typeOrName, state.schema);
1697
+ return prosemirrorSchemaList.wrapInList(type, attributes)(state, dispatch);
1698
+ };
1699
+
1700
+ var wrapInList$1 = /*#__PURE__*/Object.freeze({
1701
+ __proto__: null,
1702
+ wrapInList: wrapInList
1703
+ });
1704
+
1705
+ const Commands = Extension.create({
1706
+ name: 'commands',
1707
+ addCommands() {
1708
+ return {
1709
+ ...blur$1,
1710
+ ...clearContent$1,
1711
+ ...clearNodes$1,
1712
+ ...command$1,
1713
+ ...createParagraphNear$1,
1714
+ ...deleteNode$1,
1715
+ ...deleteRange$1,
1716
+ ...deleteSelection$1,
1717
+ ...enter$1,
1718
+ ...exitCode$1,
1719
+ ...extendMarkRange$1,
1720
+ ...first$1,
1721
+ ...focus$1,
1722
+ ...forEach$1,
1723
+ ...insertContent$1,
1724
+ ...insertContentAt$1,
1725
+ ...joinBackward$1,
1726
+ ...joinForward$1,
1727
+ ...keyboardShortcut$1,
1728
+ ...lift$1,
1729
+ ...liftEmptyBlock$1,
1730
+ ...liftListItem$1,
1731
+ ...newlineInCode$1,
1732
+ ...resetAttributes$1,
1733
+ ...scrollIntoView$1,
1734
+ ...selectAll$1,
1735
+ ...selectNodeBackward$1,
1736
+ ...selectNodeForward$1,
1737
+ ...selectParentNode$1,
1738
+ ...setContent$1,
1739
+ ...setMark$1,
1740
+ ...setMeta$1,
1741
+ ...setNode$1,
1742
+ ...setNodeSelection$1,
1743
+ ...setTextSelection$1,
1744
+ ...sinkListItem$1,
1745
+ ...splitBlock$1,
1746
+ ...splitListItem$1,
1747
+ ...toggleList$1,
1748
+ ...toggleMark$1,
1749
+ ...toggleNode$1,
1750
+ ...toggleWrap$1,
1751
+ ...undoInputRule$1,
1752
+ ...unsetAllMarks$1,
1753
+ ...unsetMark$1,
1754
+ ...updateAttributes$1,
1755
+ ...wrapIn$1,
1756
+ ...wrapInList$1,
1757
+ };
1758
+ },
1759
+ });
1760
+
1761
+ const Editable = Extension.create({
1762
+ name: 'editable',
1763
+ addProseMirrorPlugins() {
1764
+ return [
1765
+ new prosemirrorState.Plugin({
1766
+ key: new prosemirrorState.PluginKey('editable'),
1767
+ props: {
1768
+ editable: () => this.editor.options.editable,
1769
+ },
1770
+ }),
1771
+ ];
1772
+ },
1773
+ });
1774
+
1775
+ const FocusEvents = Extension.create({
1776
+ name: 'focusEvents',
1777
+ addProseMirrorPlugins() {
1778
+ const { editor } = this;
1779
+ return [
1780
+ new prosemirrorState.Plugin({
1781
+ key: new prosemirrorState.PluginKey('focusEvents'),
1782
+ props: {
1783
+ handleDOMEvents: {
1784
+ focus: (view, event) => {
1785
+ editor.isFocused = true;
1786
+ const transaction = editor.state.tr
1787
+ .setMeta('focus', { event })
1788
+ .setMeta('addToHistory', false);
1789
+ view.dispatch(transaction);
1790
+ return false;
1791
+ },
1792
+ blur: (view, event) => {
1793
+ editor.isFocused = false;
1794
+ const transaction = editor.state.tr
1795
+ .setMeta('blur', { event })
1796
+ .setMeta('addToHistory', false);
1797
+ view.dispatch(transaction);
1798
+ return false;
1799
+ },
1800
+ },
1801
+ },
1802
+ }),
1803
+ ];
1804
+ },
1805
+ });
1806
+
1807
+ const Keymap = Extension.create({
1808
+ name: 'keymap',
1809
+ addKeyboardShortcuts() {
1810
+ const handleBackspace = () => this.editor.commands.first(({ commands }) => [
1811
+ () => commands.undoInputRule(),
1812
+ () => commands.deleteSelection(),
1813
+ () => commands.joinBackward(),
1814
+ () => commands.selectNodeBackward(),
1815
+ ]);
1816
+ const handleDelete = () => this.editor.commands.first(({ commands }) => [
1817
+ () => commands.deleteSelection(),
1818
+ () => commands.joinForward(),
1819
+ () => commands.selectNodeForward(),
1820
+ ]);
1821
+ return {
1822
+ Enter: () => this.editor.commands.first(({ commands }) => [
1823
+ () => commands.newlineInCode(),
1824
+ () => commands.createParagraphNear(),
1825
+ () => commands.liftEmptyBlock(),
1826
+ () => commands.splitBlock(),
1827
+ ]),
1828
+ 'Mod-Enter': () => this.editor.commands.exitCode(),
1829
+ Backspace: handleBackspace,
1830
+ 'Mod-Backspace': handleBackspace,
1831
+ 'Shift-Backspace': handleBackspace,
1832
+ Delete: handleDelete,
1833
+ 'Mod-Delete': handleDelete,
1834
+ 'Mod-a': () => this.editor.commands.selectAll(),
1835
+ };
1836
+ },
1837
+ });
1838
+
1839
+ const Tabindex = Extension.create({
1840
+ name: 'tabindex',
1841
+ addProseMirrorPlugins() {
1842
+ return [
1843
+ new prosemirrorState.Plugin({
1844
+ key: new prosemirrorState.PluginKey('tabindex'),
1845
+ props: {
1846
+ attributes: {
1847
+ tabindex: '0',
1848
+ },
1849
+ },
1850
+ }),
1851
+ ];
1852
+ },
1853
+ });
1854
+
1855
+ var extensions = /*#__PURE__*/Object.freeze({
1856
+ __proto__: null,
1857
+ ClipboardTextSerializer: ClipboardTextSerializer,
1858
+ Commands: Commands,
1859
+ Editable: Editable,
1860
+ FocusEvents: FocusEvents,
1861
+ Keymap: Keymap,
1862
+ Tabindex: Tabindex
1863
+ });
1864
+
1865
+ function getNodeAttributes(state, typeOrName) {
1866
+ const type = getNodeType(typeOrName, state.schema);
1867
+ const { from, to } = state.selection;
1868
+ const nodes = [];
1869
+ state.doc.nodesBetween(from, to, node => {
1870
+ nodes.push(node);
1871
+ });
1872
+ const node = nodes
1873
+ .reverse()
1874
+ .find(nodeItem => nodeItem.type.name === type.name);
1875
+ if (!node) {
1876
+ return {};
1877
+ }
1878
+ return { ...node.attrs };
1879
+ }
1880
+
1881
+ function getAttributes(state, typeOrName) {
1882
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
1883
+ ? typeOrName
1884
+ : typeOrName.name, state.schema);
1885
+ if (schemaType === 'node') {
1886
+ return getNodeAttributes(state, typeOrName);
1887
+ }
1888
+ if (schemaType === 'mark') {
1889
+ return getMarkAttributes(state, typeOrName);
1890
+ }
1891
+ return {};
1892
+ }
1893
+
1894
+ function isActive(state, name, attributes = {}) {
1895
+ if (!name) {
1896
+ return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
1897
+ }
1898
+ const schemaType = getSchemaTypeNameByName(name, state.schema);
1899
+ if (schemaType === 'node') {
1900
+ return isNodeActive(state, name, attributes);
1901
+ }
1902
+ if (schemaType === 'mark') {
1903
+ return isMarkActive(state, name, attributes);
1904
+ }
1905
+ return false;
1906
+ }
1907
+
1908
+ function removeElement(element) {
1909
+ if (element === null || element === void 0 ? void 0 : element.parentNode) {
1910
+ element.parentNode.removeChild(element);
1911
+ }
1912
+ }
1913
+
1914
+ function getHTMLFromFragment(fragment, schema) {
1915
+ const documentFragment = prosemirrorModel.DOMSerializer
1916
+ .fromSchema(schema)
1917
+ .serializeFragment(fragment);
1918
+ const temporaryDocument = document.implementation.createHTMLDocument();
1919
+ const container = temporaryDocument.createElement('div');
1920
+ container.appendChild(documentFragment);
1921
+ return container.innerHTML;
1922
+ }
1923
+
1924
+ function getText(node, options) {
1925
+ const range = {
1926
+ from: 0,
1927
+ to: node.content.size,
1928
+ };
1929
+ return getTextBetween(node, range, options);
1930
+ }
1931
+
1932
+ function isNodeEmpty(node) {
1933
+ var _a;
1934
+ const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
1935
+ const content = node.toJSON();
1936
+ return JSON.stringify(defaultContent) === JSON.stringify(content);
1937
+ }
1938
+
1939
+ function createStyleTag(style) {
1940
+ const tipTapStyleTag = document.querySelector('style[data-tiptap-style]');
1941
+ if (tipTapStyleTag !== null) {
1942
+ return tipTapStyleTag;
1943
+ }
1944
+ const styleNode = document.createElement('style');
1945
+ styleNode.setAttribute('data-tiptap-style', '');
1946
+ styleNode.innerHTML = style;
1947
+ document.getElementsByTagName('head')[0].appendChild(styleNode);
1948
+ return styleNode;
1949
+ }
1950
+
1951
+ function createChainableState(config) {
1952
+ const { state, transaction } = config;
1953
+ let { selection } = transaction;
1954
+ let { doc } = transaction;
1955
+ let { storedMarks } = transaction;
1956
+ return {
1957
+ ...state,
1958
+ schema: state.schema,
1959
+ plugins: state.plugins,
1960
+ apply: state.apply.bind(state),
1961
+ applyTransaction: state.applyTransaction.bind(state),
1962
+ reconfigure: state.reconfigure.bind(state),
1963
+ toJSON: state.toJSON.bind(state),
1964
+ get storedMarks() {
1965
+ return storedMarks;
1966
+ },
1967
+ get selection() {
1968
+ return selection;
1969
+ },
1970
+ get doc() {
1971
+ return doc;
1972
+ },
1973
+ get tr() {
1974
+ selection = transaction.selection;
1975
+ doc = transaction.doc;
1976
+ storedMarks = transaction.storedMarks;
1977
+ return transaction;
1978
+ },
1979
+ };
1980
+ }
1981
+
1982
+ class CommandManager {
1983
+ constructor(props) {
1984
+ this.editor = props.editor;
1985
+ this.rawCommands = this.editor.extensionManager.commands;
1986
+ this.customState = props.state;
1987
+ }
1988
+ get hasCustomState() {
1989
+ return !!this.customState;
1990
+ }
1991
+ get state() {
1992
+ return this.customState || this.editor.state;
1993
+ }
1994
+ get commands() {
1995
+ const { rawCommands, editor, state } = this;
1996
+ const { view } = editor;
1997
+ const { tr } = state;
1998
+ const props = this.buildProps(tr);
1999
+ return Object.fromEntries(Object
2000
+ .entries(rawCommands)
2001
+ .map(([name, command]) => {
2002
+ const method = (...args) => {
2003
+ const callback = command(...args)(props);
2004
+ if (!tr.getMeta('preventDispatch') && !this.hasCustomState) {
2005
+ view.dispatch(tr);
2006
+ }
2007
+ return callback;
2008
+ };
2009
+ return [name, method];
2010
+ }));
2011
+ }
2012
+ get chain() {
2013
+ return () => this.createChain();
2014
+ }
2015
+ get can() {
2016
+ return () => this.createCan();
2017
+ }
2018
+ createChain(startTr, shouldDispatch = true) {
2019
+ const { rawCommands, editor, state } = this;
2020
+ const { view } = editor;
2021
+ const callbacks = [];
2022
+ const hasStartTransaction = !!startTr;
2023
+ const tr = startTr || state.tr;
2024
+ const run = () => {
2025
+ if (!hasStartTransaction
2026
+ && shouldDispatch
2027
+ && !tr.getMeta('preventDispatch')
2028
+ && !this.hasCustomState) {
2029
+ view.dispatch(tr);
2030
+ }
2031
+ return callbacks.every(callback => callback === true);
2032
+ };
2033
+ const chain = {
2034
+ ...Object.fromEntries(Object.entries(rawCommands).map(([name, command]) => {
2035
+ const chainedCommand = (...args) => {
2036
+ const props = this.buildProps(tr, shouldDispatch);
2037
+ const callback = command(...args)(props);
2038
+ callbacks.push(callback);
2039
+ return chain;
2040
+ };
2041
+ return [name, chainedCommand];
2042
+ })),
2043
+ run,
2044
+ };
2045
+ return chain;
2046
+ }
2047
+ createCan(startTr) {
2048
+ const { rawCommands, state } = this;
2049
+ const dispatch = undefined;
2050
+ const tr = startTr || state.tr;
2051
+ const props = this.buildProps(tr, dispatch);
2052
+ const formattedCommands = Object.fromEntries(Object
2053
+ .entries(rawCommands)
2054
+ .map(([name, command]) => {
2055
+ return [name, (...args) => command(...args)({ ...props, dispatch })];
2056
+ }));
2057
+ return {
2058
+ ...formattedCommands,
2059
+ chain: () => this.createChain(tr, dispatch),
2060
+ };
2061
+ }
2062
+ buildProps(tr, shouldDispatch = true) {
2063
+ const { rawCommands, editor, state } = this;
2064
+ const { view } = editor;
2065
+ if (state.storedMarks) {
2066
+ tr.setStoredMarks(state.storedMarks);
2067
+ }
2068
+ const props = {
2069
+ tr,
2070
+ editor,
2071
+ view,
2072
+ state: createChainableState({
2073
+ state,
2074
+ transaction: tr,
2075
+ }),
2076
+ dispatch: shouldDispatch
2077
+ ? () => undefined
2078
+ : undefined,
2079
+ chain: () => this.createChain(tr),
2080
+ can: () => this.createCan(tr),
2081
+ get commands() {
2082
+ return Object.fromEntries(Object
2083
+ .entries(rawCommands)
2084
+ .map(([name, command]) => {
2085
+ return [name, (...args) => command(...args)(props)];
2086
+ }));
2087
+ },
2088
+ };
2089
+ return props;
2090
+ }
2091
+ }
2092
+
2093
+ class InputRule {
2094
+ constructor(config) {
2095
+ this.find = config.find;
2096
+ this.handler = config.handler;
2097
+ }
2098
+ }
2099
+ const inputRuleMatcherHandler = (text, find) => {
2100
+ if (isRegExp(find)) {
2101
+ return find.exec(text);
2102
+ }
2103
+ const inputRuleMatch = find(text);
2104
+ if (!inputRuleMatch) {
2105
+ return null;
2106
+ }
2107
+ const result = [];
2108
+ result.push(inputRuleMatch.text);
2109
+ result.index = inputRuleMatch.index;
2110
+ result.input = text;
2111
+ result.data = inputRuleMatch.data;
2112
+ if (inputRuleMatch.replaceWith) {
2113
+ if (!inputRuleMatch.text.includes(inputRuleMatch.replaceWith)) {
2114
+ console.warn('[tiptap warn]: "inputRuleMatch.replaceWith" must be part of "inputRuleMatch.text".');
2115
+ }
2116
+ result.push(inputRuleMatch.replaceWith);
2117
+ }
2118
+ return result;
2119
+ };
2120
+ function run$1(config) {
2121
+ var _a;
2122
+ const { editor, from, to, text, rules, plugin, } = config;
2123
+ const { view } = editor;
2124
+ if (view.composing) {
2125
+ return false;
2126
+ }
2127
+ const $from = view.state.doc.resolve(from);
2128
+ if (
2129
+ // check for code node
2130
+ $from.parent.type.spec.code
2131
+ // check for code mark
2132
+ || !!((_a = ($from.nodeBefore || $from.nodeAfter)) === null || _a === void 0 ? void 0 : _a.marks.find(mark => mark.type.spec.code))) {
2133
+ return false;
2134
+ }
2135
+ let matched = false;
2136
+ const maxMatch = 500;
2137
+ const textBefore = $from.parent.textBetween(Math.max(0, $from.parentOffset - maxMatch), $from.parentOffset, undefined, '\ufffc') + text;
2138
+ rules.forEach(rule => {
2139
+ if (matched) {
2140
+ return;
2141
+ }
2142
+ const match = inputRuleMatcherHandler(textBefore, rule.find);
2143
+ if (!match) {
2144
+ return;
2145
+ }
2146
+ const tr = view.state.tr;
2147
+ const state = createChainableState({
2148
+ state: view.state,
2149
+ transaction: tr,
2150
+ });
2151
+ const range = {
2152
+ from: from - (match[0].length - text.length),
2153
+ to,
2154
+ };
2155
+ const { commands, chain, can } = new CommandManager({
2156
+ editor,
2157
+ state,
2158
+ });
2159
+ rule.handler({
2160
+ state,
2161
+ range,
2162
+ match,
2163
+ commands,
2164
+ chain,
2165
+ can,
2166
+ });
2167
+ // stop if there are no changes
2168
+ if (!tr.steps.length) {
2169
+ return;
2170
+ }
2171
+ // store transform as meta data
2172
+ // so we can undo input rules within the `undoInputRules` command
2173
+ tr.setMeta(plugin, {
2174
+ transform: tr,
2175
+ from,
2176
+ to,
2177
+ text,
2178
+ });
2179
+ view.dispatch(tr);
2180
+ matched = true;
2181
+ });
2182
+ return matched;
2183
+ }
2184
+ /**
2185
+ * Create an input rules plugin. When enabled, it will cause text
2186
+ * input that matches any of the given rules to trigger the rule’s
2187
+ * action.
2188
+ */
2189
+ function inputRulesPlugin(props) {
2190
+ const { editor, rules } = props;
2191
+ const plugin = new prosemirrorState.Plugin({
2192
+ state: {
2193
+ init() {
2194
+ return null;
2195
+ },
2196
+ apply(tr, prev) {
2197
+ const stored = tr.getMeta(this);
2198
+ if (stored) {
2199
+ return stored;
2200
+ }
2201
+ return tr.selectionSet || tr.docChanged
2202
+ ? null
2203
+ : prev;
2204
+ },
2205
+ },
2206
+ props: {
2207
+ handleTextInput(view, from, to, text) {
2208
+ return run$1({
2209
+ editor,
2210
+ from,
2211
+ to,
2212
+ text,
2213
+ rules,
2214
+ plugin,
2215
+ });
2216
+ },
2217
+ handleDOMEvents: {
2218
+ compositionend: view => {
2219
+ setTimeout(() => {
2220
+ const { $cursor } = view.state.selection;
2221
+ if ($cursor) {
2222
+ run$1({
2223
+ editor,
2224
+ from: $cursor.pos,
2225
+ to: $cursor.pos,
2226
+ text: '',
2227
+ rules,
2228
+ plugin,
2229
+ });
2230
+ }
2231
+ });
2232
+ return false;
2233
+ },
2234
+ },
2235
+ // add support for input rules to trigger on enter
2236
+ // this is useful for example for code blocks
2237
+ handleKeyDown(view, event) {
2238
+ if (event.key !== 'Enter') {
2239
+ return false;
2240
+ }
2241
+ const { $cursor } = view.state.selection;
2242
+ if ($cursor) {
2243
+ return run$1({
2244
+ editor,
2245
+ from: $cursor.pos,
2246
+ to: $cursor.pos,
2247
+ text: '\n',
2248
+ rules,
2249
+ plugin,
2250
+ });
2251
+ }
2252
+ return false;
2253
+ },
2254
+ },
2255
+ // @ts-ignore
2256
+ isInputRules: true,
2257
+ });
2258
+ return plugin;
2259
+ }
2260
+
2261
+ function isNumber(value) {
2262
+ return typeof value === 'number';
2263
+ }
2264
+
2265
+ class PasteRule {
2266
+ constructor(config) {
2267
+ this.find = config.find;
2268
+ this.handler = config.handler;
2269
+ }
2270
+ }
2271
+ const pasteRuleMatcherHandler = (text, find) => {
2272
+ if (isRegExp(find)) {
2273
+ return [...text.matchAll(find)];
2274
+ }
2275
+ const matches = find(text);
2276
+ if (!matches) {
2277
+ return [];
2278
+ }
2279
+ return matches.map(pasteRuleMatch => {
2280
+ const result = [];
2281
+ result.push(pasteRuleMatch.text);
2282
+ result.index = pasteRuleMatch.index;
2283
+ result.input = text;
2284
+ result.data = pasteRuleMatch.data;
2285
+ if (pasteRuleMatch.replaceWith) {
2286
+ if (!pasteRuleMatch.text.includes(pasteRuleMatch.replaceWith)) {
2287
+ console.warn('[tiptap warn]: "pasteRuleMatch.replaceWith" must be part of "pasteRuleMatch.text".');
2288
+ }
2289
+ result.push(pasteRuleMatch.replaceWith);
2290
+ }
2291
+ return result;
2292
+ });
2293
+ };
2294
+ function run(config) {
2295
+ const { editor, state, from, to, rules, } = config;
2296
+ const { commands, chain, can } = new CommandManager({
2297
+ editor,
2298
+ state,
2299
+ });
2300
+ state.doc.nodesBetween(from, to, (node, pos) => {
2301
+ if (!node.isTextblock || node.type.spec.code) {
2302
+ return;
2303
+ }
2304
+ const resolvedFrom = Math.max(from, pos);
2305
+ const resolvedTo = Math.min(to, pos + node.content.size);
2306
+ const textToMatch = node.textBetween(resolvedFrom - pos, resolvedTo - pos, undefined, '\ufffc');
2307
+ rules.forEach(rule => {
2308
+ const matches = pasteRuleMatcherHandler(textToMatch, rule.find);
2309
+ matches.forEach(match => {
2310
+ if (match.index === undefined) {
2311
+ return;
2312
+ }
2313
+ const start = resolvedFrom + match.index + 1;
2314
+ const end = start + match[0].length;
2315
+ const range = {
2316
+ from: state.tr.mapping.map(start),
2317
+ to: state.tr.mapping.map(end),
2318
+ };
2319
+ rule.handler({
2320
+ state,
2321
+ range,
2322
+ match,
2323
+ commands,
2324
+ chain,
2325
+ can,
2326
+ });
2327
+ });
2328
+ });
2329
+ });
2330
+ }
2331
+ /**
2332
+ * Create an paste rules plugin. When enabled, it will cause pasted
2333
+ * text that matches any of the given rules to trigger the rule’s
2334
+ * action.
2335
+ */
2336
+ function pasteRulesPlugin(props) {
2337
+ const { editor, rules } = props;
2338
+ let isProseMirrorHTML = false;
2339
+ const plugin = new prosemirrorState.Plugin({
2340
+ props: {
2341
+ handlePaste: (view, event) => {
2342
+ var _a;
2343
+ const html = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData('text/html');
2344
+ isProseMirrorHTML = !!(html === null || html === void 0 ? void 0 : html.includes('data-pm-slice'));
2345
+ return false;
2346
+ },
2347
+ },
2348
+ appendTransaction: (transactions, oldState, state) => {
2349
+ const transaction = transactions[0];
2350
+ // stop if there is not a paste event
2351
+ if (!transaction.getMeta('paste') || isProseMirrorHTML) {
2352
+ return;
2353
+ }
2354
+ // stop if there is no changed range
2355
+ const { doc, before } = transaction;
2356
+ const from = before.content.findDiffStart(doc.content);
2357
+ const to = before.content.findDiffEnd(doc.content);
2358
+ if (!isNumber(from) || !to || from === to.b) {
2359
+ return;
2360
+ }
2361
+ // build a chainable state
2362
+ // so we can use a single transaction for all paste rules
2363
+ const tr = state.tr;
2364
+ const chainableState = createChainableState({
2365
+ state,
2366
+ transaction: tr,
2367
+ });
2368
+ run({
2369
+ editor,
2370
+ state: chainableState,
2371
+ from: Math.max(from - 1, 0),
2372
+ to: to.b,
2373
+ rules,
2374
+ plugin,
2375
+ });
2376
+ // stop if there are no changes
2377
+ if (!tr.steps.length) {
2378
+ return;
2379
+ }
2380
+ return tr;
2381
+ },
2382
+ // @ts-ignore
2383
+ isPasteRules: true,
2384
+ });
2385
+ return plugin;
2386
+ }
2387
+
2388
+ /**
2389
+ * Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`.
2390
+ * @param extensions List of extensions
2391
+ */
2392
+ function getAttributesFromExtensions(extensions) {
2393
+ const extensionAttributes = [];
2394
+ const { nodeExtensions, markExtensions } = splitExtensions(extensions);
2395
+ const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions];
2396
+ const defaultAttribute = {
2397
+ default: null,
2398
+ rendered: true,
2399
+ renderHTML: null,
2400
+ parseHTML: null,
2401
+ keepOnSplit: true,
2402
+ };
2403
+ extensions.forEach(extension => {
2404
+ const context = {
2405
+ name: extension.name,
2406
+ options: extension.options,
2407
+ storage: extension.storage,
2408
+ };
2409
+ const addGlobalAttributes = getExtensionField(extension, 'addGlobalAttributes', context);
2410
+ if (!addGlobalAttributes) {
2411
+ return;
2412
+ }
2413
+ // TODO: remove `as GlobalAttributes`
2414
+ const globalAttributes = addGlobalAttributes();
2415
+ globalAttributes.forEach(globalAttribute => {
2416
+ globalAttribute.types.forEach(type => {
2417
+ Object
2418
+ .entries(globalAttribute.attributes)
2419
+ .forEach(([name, attribute]) => {
2420
+ extensionAttributes.push({
2421
+ type,
2422
+ name,
2423
+ attribute: {
2424
+ ...defaultAttribute,
2425
+ ...attribute,
2426
+ },
2427
+ });
2428
+ });
2429
+ });
2430
+ });
2431
+ });
2432
+ nodeAndMarkExtensions.forEach(extension => {
2433
+ const context = {
2434
+ name: extension.name,
2435
+ options: extension.options,
2436
+ storage: extension.storage,
2437
+ };
2438
+ const addAttributes = getExtensionField(extension, 'addAttributes', context);
2439
+ if (!addAttributes) {
2440
+ return;
2441
+ }
2442
+ // TODO: remove `as Attributes`
2443
+ const attributes = addAttributes();
2444
+ Object
2445
+ .entries(attributes)
2446
+ .forEach(([name, attribute]) => {
2447
+ extensionAttributes.push({
2448
+ type: extension.name,
2449
+ name,
2450
+ attribute: {
2451
+ ...defaultAttribute,
2452
+ ...attribute,
2453
+ },
2454
+ });
2455
+ });
2456
+ });
2457
+ return extensionAttributes;
2458
+ }
2459
+
2460
+ function mergeAttributes(...objects) {
2461
+ return objects
2462
+ .filter(item => !!item)
2463
+ .reduce((items, item) => {
2464
+ const mergedAttributes = { ...items };
2465
+ Object.entries(item).forEach(([key, value]) => {
2466
+ const exists = mergedAttributes[key];
2467
+ if (!exists) {
2468
+ mergedAttributes[key] = value;
2469
+ return;
2470
+ }
2471
+ if (key === 'class') {
2472
+ mergedAttributes[key] = [mergedAttributes[key], value].join(' ');
2473
+ }
2474
+ else if (key === 'style') {
2475
+ mergedAttributes[key] = [mergedAttributes[key], value].join('; ');
2476
+ }
2477
+ else {
2478
+ mergedAttributes[key] = value;
2479
+ }
2480
+ });
2481
+ return mergedAttributes;
2482
+ }, {});
2483
+ }
2484
+
2485
+ function getRenderedAttributes(nodeOrMark, extensionAttributes) {
2486
+ return extensionAttributes
2487
+ .filter(item => item.attribute.rendered)
2488
+ .map(item => {
2489
+ if (!item.attribute.renderHTML) {
2490
+ return {
2491
+ [item.name]: nodeOrMark.attrs[item.name],
2492
+ };
2493
+ }
2494
+ return item.attribute.renderHTML(nodeOrMark.attrs) || {};
2495
+ })
2496
+ .reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {});
2497
+ }
2498
+
2499
+ function isEmptyObject(value = {}) {
2500
+ return Object.keys(value).length === 0 && value.constructor === Object;
2501
+ }
2502
+
2503
+ function fromString(value) {
2504
+ if (typeof value !== 'string') {
2505
+ return value;
2506
+ }
2507
+ if (value.match(/^[+-]?(?:\d*\.)?\d+$/)) {
2508
+ return Number(value);
2509
+ }
2510
+ if (value === 'true') {
2511
+ return true;
2512
+ }
2513
+ if (value === 'false') {
2514
+ return false;
2515
+ }
2516
+ return value;
2517
+ }
2518
+
2519
+ /**
2520
+ * This function merges extension attributes into parserule attributes (`attrs` or `getAttrs`).
2521
+ * Cancels when `getAttrs` returned `false`.
2522
+ * @param parseRule ProseMirror ParseRule
2523
+ * @param extensionAttributes List of attributes to inject
2524
+ */
2525
+ function injectExtensionAttributesToParseRule(parseRule, extensionAttributes) {
2526
+ if (parseRule.style) {
2527
+ return parseRule;
2528
+ }
2529
+ return {
2530
+ ...parseRule,
2531
+ getAttrs: node => {
2532
+ const oldAttributes = parseRule.getAttrs
2533
+ ? parseRule.getAttrs(node)
2534
+ : parseRule.attrs;
2535
+ if (oldAttributes === false) {
2536
+ return false;
2537
+ }
2538
+ const newAttributes = extensionAttributes
2539
+ .filter(item => item.attribute.rendered)
2540
+ .reduce((items, item) => {
2541
+ const value = item.attribute.parseHTML
2542
+ ? item.attribute.parseHTML(node)
2543
+ : fromString(node.getAttribute(item.name));
2544
+ if (isObject(value)) {
2545
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "parseHTML" for your attribute "${item.name}" returns an object but should return the value itself. If this is expected you can ignore this message. This warning will be removed in one of the next releases. Further information: https://github.com/ueberdosis/tiptap/issues/1863`);
2546
+ }
2547
+ if (value === null || value === undefined) {
2548
+ return items;
2549
+ }
2550
+ return {
2551
+ ...items,
2552
+ [item.name]: value,
2553
+ };
2554
+ }, {});
2555
+ return { ...oldAttributes, ...newAttributes };
2556
+ },
2557
+ };
2558
+ }
2559
+
2560
+ function cleanUpSchemaItem(data) {
2561
+ return Object.fromEntries(Object.entries(data).filter(([key, value]) => {
2562
+ if (key === 'attrs' && isEmptyObject(value)) {
2563
+ return false;
2564
+ }
2565
+ return value !== null && value !== undefined;
2566
+ }));
2567
+ }
2568
+ function getSchemaByResolvedExtensions(extensions) {
2569
+ var _a;
2570
+ const allAttributes = getAttributesFromExtensions(extensions);
2571
+ const { nodeExtensions, markExtensions } = splitExtensions(extensions);
2572
+ const topNode = (_a = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))) === null || _a === void 0 ? void 0 : _a.name;
2573
+ const nodes = Object.fromEntries(nodeExtensions.map(extension => {
2574
+ const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name);
2575
+ const context = {
2576
+ name: extension.name,
2577
+ options: extension.options,
2578
+ storage: extension.storage,
2579
+ };
2580
+ const extraNodeFields = extensions.reduce((fields, e) => {
2581
+ const extendNodeSchema = getExtensionField(e, 'extendNodeSchema', context);
2582
+ return {
2583
+ ...fields,
2584
+ ...(extendNodeSchema ? extendNodeSchema(extension) : {}),
2585
+ };
2586
+ }, {});
2587
+ const schema = cleanUpSchemaItem({
2588
+ ...extraNodeFields,
2589
+ content: callOrReturn(getExtensionField(extension, 'content', context)),
2590
+ marks: callOrReturn(getExtensionField(extension, 'marks', context)),
2591
+ group: callOrReturn(getExtensionField(extension, 'group', context)),
2592
+ inline: callOrReturn(getExtensionField(extension, 'inline', context)),
2593
+ atom: callOrReturn(getExtensionField(extension, 'atom', context)),
2594
+ selectable: callOrReturn(getExtensionField(extension, 'selectable', context)),
2595
+ draggable: callOrReturn(getExtensionField(extension, 'draggable', context)),
2596
+ code: callOrReturn(getExtensionField(extension, 'code', context)),
2597
+ defining: callOrReturn(getExtensionField(extension, 'defining', context)),
2598
+ isolating: callOrReturn(getExtensionField(extension, 'isolating', context)),
2599
+ attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
2600
+ var _a;
2601
+ return [extensionAttribute.name, { default: (_a = extensionAttribute === null || extensionAttribute === void 0 ? void 0 : extensionAttribute.attribute) === null || _a === void 0 ? void 0 : _a.default }];
2602
+ })),
2603
+ });
2604
+ const parseHTML = callOrReturn(getExtensionField(extension, 'parseHTML', context));
2605
+ if (parseHTML) {
2606
+ schema.parseDOM = parseHTML
2607
+ .map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes));
2608
+ }
2609
+ const renderHTML = getExtensionField(extension, 'renderHTML', context);
2610
+ if (renderHTML) {
2611
+ schema.toDOM = node => renderHTML({
2612
+ node,
2613
+ HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
2614
+ });
2615
+ }
2616
+ const renderText = getExtensionField(extension, 'renderText', context);
2617
+ if (renderText) {
2618
+ schema.toText = renderText;
2619
+ }
2620
+ return [extension.name, schema];
2621
+ }));
2622
+ const marks = Object.fromEntries(markExtensions.map(extension => {
2623
+ const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name);
2624
+ const context = {
2625
+ name: extension.name,
2626
+ options: extension.options,
2627
+ storage: extension.storage,
2628
+ };
2629
+ const extraMarkFields = extensions.reduce((fields, e) => {
2630
+ const extendMarkSchema = getExtensionField(e, 'extendMarkSchema', context);
2631
+ return {
2632
+ ...fields,
2633
+ ...(extendMarkSchema ? extendMarkSchema(extension) : {}),
2634
+ };
2635
+ }, {});
2636
+ const schema = cleanUpSchemaItem({
2637
+ ...extraMarkFields,
2638
+ inclusive: callOrReturn(getExtensionField(extension, 'inclusive', context)),
2639
+ excludes: callOrReturn(getExtensionField(extension, 'excludes', context)),
2640
+ group: callOrReturn(getExtensionField(extension, 'group', context)),
2641
+ spanning: callOrReturn(getExtensionField(extension, 'spanning', context)),
2642
+ code: callOrReturn(getExtensionField(extension, 'code', context)),
2643
+ attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
2644
+ var _a;
2645
+ return [extensionAttribute.name, { default: (_a = extensionAttribute === null || extensionAttribute === void 0 ? void 0 : extensionAttribute.attribute) === null || _a === void 0 ? void 0 : _a.default }];
2646
+ })),
2647
+ });
2648
+ const parseHTML = callOrReturn(getExtensionField(extension, 'parseHTML', context));
2649
+ if (parseHTML) {
2650
+ schema.parseDOM = parseHTML
2651
+ .map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes));
2652
+ }
2653
+ const renderHTML = getExtensionField(extension, 'renderHTML', context);
2654
+ if (renderHTML) {
2655
+ schema.toDOM = mark => renderHTML({
2656
+ mark,
2657
+ HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
2658
+ });
2659
+ }
2660
+ return [extension.name, schema];
2661
+ }));
2662
+ return new prosemirrorModel.Schema({
2663
+ topNode,
2664
+ nodes,
2665
+ marks,
2666
+ });
2667
+ }
2668
+
2669
+ function getSchemaTypeByName(name, schema) {
2670
+ return schema.nodes[name] || schema.marks[name] || null;
2671
+ }
2672
+
2673
+ function findDuplicates(items) {
2674
+ const filtered = items.filter((el, index) => items.indexOf(el) !== index);
2675
+ return [...new Set(filtered)];
2676
+ }
2677
+
2678
+ class ExtensionManager {
2679
+ constructor(extensions, editor) {
2680
+ this.splittableMarks = [];
2681
+ this.editor = editor;
2682
+ this.extensions = ExtensionManager.resolve(extensions);
2683
+ this.schema = getSchemaByResolvedExtensions(this.extensions);
2684
+ this.extensions.forEach(extension => {
2685
+ var _a;
2686
+ // store extension storage in editor
2687
+ this.editor.extensionStorage[extension.name] = extension.storage;
2688
+ const context = {
2689
+ name: extension.name,
2690
+ options: extension.options,
2691
+ storage: extension.storage,
2692
+ editor: this.editor,
2693
+ type: getSchemaTypeByName(extension.name, this.schema),
2694
+ };
2695
+ if (extension.type === 'mark') {
2696
+ const keepOnSplit = (_a = callOrReturn(getExtensionField(extension, 'keepOnSplit', context))) !== null && _a !== void 0 ? _a : true;
2697
+ if (keepOnSplit) {
2698
+ this.splittableMarks.push(extension.name);
2699
+ }
2700
+ }
2701
+ const onBeforeCreate = getExtensionField(extension, 'onBeforeCreate', context);
2702
+ if (onBeforeCreate) {
2703
+ this.editor.on('beforeCreate', onBeforeCreate);
2704
+ }
2705
+ const onCreate = getExtensionField(extension, 'onCreate', context);
2706
+ if (onCreate) {
2707
+ this.editor.on('create', onCreate);
2708
+ }
2709
+ const onUpdate = getExtensionField(extension, 'onUpdate', context);
2710
+ if (onUpdate) {
2711
+ this.editor.on('update', onUpdate);
2712
+ }
2713
+ const onSelectionUpdate = getExtensionField(extension, 'onSelectionUpdate', context);
2714
+ if (onSelectionUpdate) {
2715
+ this.editor.on('selectionUpdate', onSelectionUpdate);
2716
+ }
2717
+ const onTransaction = getExtensionField(extension, 'onTransaction', context);
2718
+ if (onTransaction) {
2719
+ this.editor.on('transaction', onTransaction);
2720
+ }
2721
+ const onFocus = getExtensionField(extension, 'onFocus', context);
2722
+ if (onFocus) {
2723
+ this.editor.on('focus', onFocus);
2724
+ }
2725
+ const onBlur = getExtensionField(extension, 'onBlur', context);
2726
+ if (onBlur) {
2727
+ this.editor.on('blur', onBlur);
2728
+ }
2729
+ const onDestroy = getExtensionField(extension, 'onDestroy', context);
2730
+ if (onDestroy) {
2731
+ this.editor.on('destroy', onDestroy);
2732
+ }
2733
+ });
2734
+ }
2735
+ static resolve(extensions) {
2736
+ const resolvedExtensions = ExtensionManager.sort(ExtensionManager.flatten(extensions));
2737
+ const duplicatedNames = findDuplicates(resolvedExtensions.map(extension => extension.name));
2738
+ if (duplicatedNames.length) {
2739
+ console.warn(`[tiptap warn]: Duplicate extension names found: [${duplicatedNames.map(item => `'${item}'`).join(', ')}]. This can lead to issues.`);
2740
+ }
2741
+ return resolvedExtensions;
2742
+ }
2743
+ static flatten(extensions) {
2744
+ return extensions
2745
+ .map(extension => {
2746
+ const context = {
2747
+ name: extension.name,
2748
+ options: extension.options,
2749
+ storage: extension.storage,
2750
+ };
2751
+ const addExtensions = getExtensionField(extension, 'addExtensions', context);
2752
+ if (addExtensions) {
2753
+ return [
2754
+ extension,
2755
+ ...this.flatten(addExtensions()),
2756
+ ];
2757
+ }
2758
+ return extension;
2759
+ })
2760
+ // `Infinity` will break TypeScript so we set a number that is probably high enough
2761
+ .flat(10);
2762
+ }
2763
+ static sort(extensions) {
2764
+ const defaultPriority = 100;
2765
+ return extensions.sort((a, b) => {
2766
+ const priorityA = getExtensionField(a, 'priority') || defaultPriority;
2767
+ const priorityB = getExtensionField(b, 'priority') || defaultPriority;
2768
+ if (priorityA > priorityB) {
2769
+ return -1;
2770
+ }
2771
+ if (priorityA < priorityB) {
2772
+ return 1;
2773
+ }
2774
+ return 0;
2775
+ });
2776
+ }
2777
+ get commands() {
2778
+ return this.extensions.reduce((commands, extension) => {
2779
+ const context = {
2780
+ name: extension.name,
2781
+ options: extension.options,
2782
+ storage: extension.storage,
2783
+ editor: this.editor,
2784
+ type: getSchemaTypeByName(extension.name, this.schema),
2785
+ };
2786
+ const addCommands = getExtensionField(extension, 'addCommands', context);
2787
+ if (!addCommands) {
2788
+ return commands;
2789
+ }
2790
+ return {
2791
+ ...commands,
2792
+ ...addCommands(),
2793
+ };
2794
+ }, {});
2795
+ }
2796
+ get plugins() {
2797
+ const { editor } = this;
2798
+ // With ProseMirror, first plugins within an array are executed first.
2799
+ // In tiptap, we provide the ability to override plugins,
2800
+ // so it feels more natural to run plugins at the end of an array first.
2801
+ // That’s why we have to reverse the `extensions` array and sort again
2802
+ // based on the `priority` option.
2803
+ const extensions = ExtensionManager.sort([...this.extensions].reverse());
2804
+ const inputRules = [];
2805
+ const pasteRules = [];
2806
+ const allPlugins = extensions
2807
+ .map(extension => {
2808
+ const context = {
2809
+ name: extension.name,
2810
+ options: extension.options,
2811
+ storage: extension.storage,
2812
+ editor,
2813
+ type: getSchemaTypeByName(extension.name, this.schema),
2814
+ };
2815
+ const plugins = [];
2816
+ const addKeyboardShortcuts = getExtensionField(extension, 'addKeyboardShortcuts', context);
2817
+ if (addKeyboardShortcuts) {
2818
+ const bindings = Object.fromEntries(Object
2819
+ .entries(addKeyboardShortcuts())
2820
+ .map(([shortcut, method]) => {
2821
+ return [shortcut, () => method({ editor })];
2822
+ }));
2823
+ const keyMapPlugin = prosemirrorKeymap.keymap(bindings);
2824
+ plugins.push(keyMapPlugin);
2825
+ }
2826
+ const addInputRules = getExtensionField(extension, 'addInputRules', context);
2827
+ if (editor.options.enableInputRules && addInputRules) {
2828
+ inputRules.push(...addInputRules());
2829
+ }
2830
+ const addPasteRules = getExtensionField(extension, 'addPasteRules', context);
2831
+ if (editor.options.enablePasteRules && addPasteRules) {
2832
+ pasteRules.push(...addPasteRules());
2833
+ }
2834
+ const addProseMirrorPlugins = getExtensionField(extension, 'addProseMirrorPlugins', context);
2835
+ if (addProseMirrorPlugins) {
2836
+ const proseMirrorPlugins = addProseMirrorPlugins();
2837
+ plugins.push(...proseMirrorPlugins);
2838
+ }
2839
+ return plugins;
2840
+ })
2841
+ .flat();
2842
+ return [
2843
+ inputRulesPlugin({
2844
+ editor,
2845
+ rules: inputRules,
2846
+ }),
2847
+ pasteRulesPlugin({
2848
+ editor,
2849
+ rules: pasteRules,
2850
+ }),
2851
+ ...allPlugins,
2852
+ ];
2853
+ }
2854
+ get attributes() {
2855
+ return getAttributesFromExtensions(this.extensions);
2856
+ }
2857
+ get nodeViews() {
2858
+ const { editor } = this;
2859
+ const { nodeExtensions } = splitExtensions(this.extensions);
2860
+ return Object.fromEntries(nodeExtensions
2861
+ .filter(extension => !!getExtensionField(extension, 'addNodeView'))
2862
+ .map(extension => {
2863
+ const extensionAttributes = this.attributes.filter(attribute => attribute.type === extension.name);
2864
+ const context = {
2865
+ name: extension.name,
2866
+ options: extension.options,
2867
+ storage: extension.storage,
2868
+ editor,
2869
+ type: getNodeType(extension.name, this.schema),
2870
+ };
2871
+ const addNodeView = getExtensionField(extension, 'addNodeView', context);
2872
+ if (!addNodeView) {
2873
+ return [];
2874
+ }
2875
+ const nodeview = (node, view, getPos, decorations) => {
2876
+ const HTMLAttributes = getRenderedAttributes(node, extensionAttributes);
2877
+ return addNodeView()({
2878
+ editor,
2879
+ node,
2880
+ getPos,
2881
+ decorations,
2882
+ HTMLAttributes,
2883
+ extension,
2884
+ });
2885
+ };
2886
+ return [extension.name, nodeview];
2887
+ }));
2888
+ }
2889
+ }
2890
+
2891
+ class EventEmitter {
2892
+ constructor() {
2893
+ this.callbacks = {};
2894
+ }
2895
+ on(event, fn) {
2896
+ if (!this.callbacks[event]) {
2897
+ this.callbacks[event] = [];
2898
+ }
2899
+ this.callbacks[event].push(fn);
2900
+ return this;
2901
+ }
2902
+ emit(event, ...args) {
2903
+ const callbacks = this.callbacks[event];
2904
+ if (callbacks) {
2905
+ callbacks.forEach(callback => callback.apply(this, args));
2906
+ }
2907
+ return this;
2908
+ }
2909
+ off(event, fn) {
2910
+ const callbacks = this.callbacks[event];
2911
+ if (callbacks) {
2912
+ if (fn) {
2913
+ this.callbacks[event] = callbacks.filter(callback => callback !== fn);
2914
+ }
2915
+ else {
2916
+ delete this.callbacks[event];
2917
+ }
2918
+ }
2919
+ return this;
2920
+ }
2921
+ removeAllListeners() {
2922
+ this.callbacks = {};
2923
+ }
2924
+ }
2925
+
2926
+ const style = `.ProseMirror {
2927
+ position: relative;
2928
+ }
2929
+
2930
+ .ProseMirror {
2931
+ word-wrap: break-word;
2932
+ white-space: pre-wrap;
2933
+ white-space: break-spaces;
2934
+ -webkit-font-variant-ligatures: none;
2935
+ font-variant-ligatures: none;
2936
+ font-feature-settings: "liga" 0; /* the above doesn't seem to work in Edge */
2937
+ }
2938
+
2939
+ .ProseMirror [contenteditable="false"] {
2940
+ white-space: normal;
2941
+ }
2942
+
2943
+ .ProseMirror [contenteditable="false"] [contenteditable="true"] {
2944
+ white-space: pre-wrap;
2945
+ }
2946
+
2947
+ .ProseMirror pre {
2948
+ white-space: pre-wrap;
2949
+ }
2950
+
2951
+ img.ProseMirror-separator {
2952
+ display: inline !important;
2953
+ border: none !important;
2954
+ margin: 0 !important;
2955
+ width: 1px !important;
2956
+ height: 1px !important;
2957
+ }
2958
+
2959
+ .ProseMirror-gapcursor {
2960
+ display: none;
2961
+ pointer-events: none;
2962
+ position: absolute;
2963
+ }
2964
+
2965
+ .ProseMirror-gapcursor:after {
2966
+ content: "";
2967
+ display: block;
2968
+ position: absolute;
2969
+ top: -2px;
2970
+ width: 20px;
2971
+ border-top: 1px solid black;
2972
+ animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite;
2973
+ }
2974
+
2975
+ @keyframes ProseMirror-cursor-blink {
2976
+ to {
2977
+ visibility: hidden;
2978
+ }
2979
+ }
2980
+
2981
+ .ProseMirror-hideselection *::selection {
2982
+ background: transparent;
2983
+ }
2984
+
2985
+ .ProseMirror-hideselection *::-moz-selection {
2986
+ background: transparent;
2987
+ }
2988
+
2989
+ .ProseMirror-hideselection * {
2990
+ caret-color: transparent;
2991
+ }
2992
+
2993
+ .ProseMirror-focused .ProseMirror-gapcursor {
2994
+ display: block;
2995
+ }
2996
+
2997
+ .tippy-box[data-animation=fade][data-state=hidden] {
2998
+ opacity: 0
2999
+ }`;
3000
+
3001
+ class Editor extends EventEmitter {
3002
+ constructor(options = {}) {
3003
+ super();
3004
+ this.isFocused = false;
3005
+ this.extensionStorage = {};
3006
+ this.options = {
3007
+ element: document.createElement('div'),
3008
+ content: '',
3009
+ injectCSS: true,
3010
+ extensions: [],
3011
+ autofocus: false,
3012
+ editable: true,
3013
+ editorProps: {},
3014
+ parseOptions: {},
3015
+ enableInputRules: true,
3016
+ enablePasteRules: true,
3017
+ enableCoreExtensions: true,
3018
+ onBeforeCreate: () => null,
3019
+ onCreate: () => null,
3020
+ onUpdate: () => null,
3021
+ onSelectionUpdate: () => null,
3022
+ onTransaction: () => null,
3023
+ onFocus: () => null,
3024
+ onBlur: () => null,
3025
+ onDestroy: () => null,
3026
+ };
3027
+ this.isCapturingTransaction = false;
3028
+ this.capturedTransaction = null;
3029
+ this.setOptions(options);
3030
+ this.createExtensionManager();
3031
+ this.createCommandManager();
3032
+ this.createSchema();
3033
+ this.on('beforeCreate', this.options.onBeforeCreate);
3034
+ this.emit('beforeCreate', { editor: this });
3035
+ this.createView();
3036
+ this.injectCSS();
3037
+ this.on('create', this.options.onCreate);
3038
+ this.on('update', this.options.onUpdate);
3039
+ this.on('selectionUpdate', this.options.onSelectionUpdate);
3040
+ this.on('transaction', this.options.onTransaction);
3041
+ this.on('focus', this.options.onFocus);
3042
+ this.on('blur', this.options.onBlur);
3043
+ this.on('destroy', this.options.onDestroy);
3044
+ window.setTimeout(() => {
3045
+ if (this.isDestroyed) {
3046
+ return;
3047
+ }
3048
+ this.commands.focus(this.options.autofocus);
3049
+ this.emit('create', { editor: this });
3050
+ }, 0);
3051
+ }
3052
+ /**
3053
+ * Returns the editor storage.
3054
+ */
3055
+ get storage() {
3056
+ return this.extensionStorage;
3057
+ }
3058
+ /**
3059
+ * An object of all registered commands.
3060
+ */
3061
+ get commands() {
3062
+ return this.commandManager.commands;
3063
+ }
3064
+ /**
3065
+ * Create a command chain to call multiple commands at once.
3066
+ */
3067
+ chain() {
3068
+ return this.commandManager.chain();
3069
+ }
3070
+ /**
3071
+ * Check if a command or a command chain can be executed. Without executing it.
3072
+ */
3073
+ can() {
3074
+ return this.commandManager.can();
3075
+ }
3076
+ /**
3077
+ * Inject CSS styles.
3078
+ */
3079
+ injectCSS() {
3080
+ if (this.options.injectCSS && document) {
3081
+ this.css = createStyleTag(style);
3082
+ }
3083
+ }
3084
+ /**
3085
+ * Update editor options.
3086
+ *
3087
+ * @param options A list of options
3088
+ */
3089
+ setOptions(options = {}) {
3090
+ this.options = {
3091
+ ...this.options,
3092
+ ...options,
3093
+ };
3094
+ if (!this.view || !this.state || this.isDestroyed) {
3095
+ return;
3096
+ }
3097
+ if (this.options.editorProps) {
3098
+ this.view.setProps(this.options.editorProps);
3099
+ }
3100
+ this.view.updateState(this.state);
3101
+ }
3102
+ /**
3103
+ * Update editable state of the editor.
3104
+ */
3105
+ setEditable(editable) {
3106
+ this.setOptions({ editable });
3107
+ }
3108
+ /**
3109
+ * Returns whether the editor is editable.
3110
+ */
3111
+ get isEditable() {
3112
+ // since plugins are applied after creating the view
3113
+ // `editable` is always `true` for one tick.
3114
+ // that’s why we also have to check for `options.editable`
3115
+ return this.options.editable
3116
+ && this.view
3117
+ && this.view.editable;
3118
+ }
3119
+ /**
3120
+ * Returns the editor state.
3121
+ */
3122
+ get state() {
3123
+ return this.view.state;
3124
+ }
3125
+ /**
3126
+ * Register a ProseMirror plugin.
3127
+ *
3128
+ * @param plugin A ProseMirror plugin
3129
+ * @param handlePlugins Control how to merge the plugin into the existing plugins.
3130
+ */
3131
+ registerPlugin(plugin, handlePlugins) {
3132
+ const plugins = isObject$1(handlePlugins)
3133
+ ? handlePlugins(plugin, this.state.plugins)
3134
+ : [...this.state.plugins, plugin];
3135
+ const state = this.state.reconfigure({ plugins });
3136
+ this.view.updateState(state);
3137
+ }
3138
+ /**
3139
+ * Unregister a ProseMirror plugin.
3140
+ *
3141
+ * @param nameOrPluginKey The plugins name
3142
+ */
3143
+ unregisterPlugin(nameOrPluginKey) {
3144
+ if (this.isDestroyed) {
3145
+ return;
3146
+ }
3147
+ const name = typeof nameOrPluginKey === 'string'
3148
+ ? `${nameOrPluginKey}$`
3149
+ // @ts-ignore
3150
+ : nameOrPluginKey.key;
3151
+ const state = this.state.reconfigure({
3152
+ // @ts-ignore
3153
+ plugins: this.state.plugins.filter(plugin => !plugin.key.startsWith(name)),
3154
+ });
3155
+ this.view.updateState(state);
3156
+ }
3157
+ /**
3158
+ * Creates an extension manager.
3159
+ */
3160
+ createExtensionManager() {
3161
+ const coreExtensions = this.options.enableCoreExtensions
3162
+ ? Object.values(extensions)
3163
+ : [];
3164
+ const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
3165
+ return ['extension', 'node', 'mark'].includes(extension === null || extension === void 0 ? void 0 : extension.type);
3166
+ });
3167
+ this.extensionManager = new ExtensionManager(allExtensions, this);
3168
+ }
3169
+ /**
3170
+ * Creates an command manager.
3171
+ */
3172
+ createCommandManager() {
3173
+ this.commandManager = new CommandManager({
3174
+ editor: this,
3175
+ });
3176
+ }
3177
+ /**
3178
+ * Creates a ProseMirror schema.
3179
+ */
3180
+ createSchema() {
3181
+ this.schema = this.extensionManager.schema;
3182
+ }
3183
+ /**
3184
+ * Creates a ProseMirror view.
3185
+ */
3186
+ createView() {
3187
+ this.view = new prosemirrorView.EditorView(this.options.element, {
3188
+ ...this.options.editorProps,
3189
+ dispatchTransaction: this.dispatchTransaction.bind(this),
3190
+ state: prosemirrorState.EditorState.create({
3191
+ doc: createDocument(this.options.content, this.schema, this.options.parseOptions),
3192
+ }),
3193
+ });
3194
+ // `editor.view` is not yet available at this time.
3195
+ // Therefore we will add all plugins and node views directly afterwards.
3196
+ const newState = this.state.reconfigure({
3197
+ plugins: this.extensionManager.plugins,
3198
+ });
3199
+ this.view.updateState(newState);
3200
+ this.createNodeViews();
3201
+ // Let’s store the editor instance in the DOM element.
3202
+ // So we’ll have access to it for tests.
3203
+ const dom = this.view.dom;
3204
+ dom.editor = this;
3205
+ }
3206
+ /**
3207
+ * Creates all node views.
3208
+ */
3209
+ createNodeViews() {
3210
+ this.view.setProps({
3211
+ nodeViews: this.extensionManager.nodeViews,
3212
+ });
3213
+ }
3214
+ captureTransaction(fn) {
3215
+ this.isCapturingTransaction = true;
3216
+ fn();
3217
+ this.isCapturingTransaction = false;
3218
+ const tr = this.capturedTransaction;
3219
+ this.capturedTransaction = null;
3220
+ return tr;
3221
+ }
3222
+ /**
3223
+ * The callback over which to send transactions (state updates) produced by the view.
3224
+ *
3225
+ * @param transaction An editor state transaction
3226
+ */
3227
+ dispatchTransaction(transaction) {
3228
+ if (this.isCapturingTransaction) {
3229
+ if (!this.capturedTransaction) {
3230
+ this.capturedTransaction = transaction;
3231
+ return;
3232
+ }
3233
+ transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
3234
+ return;
3235
+ }
3236
+ const state = this.state.apply(transaction);
3237
+ const selectionHasChanged = !this.state.selection.eq(state.selection);
3238
+ this.view.updateState(state);
3239
+ this.emit('transaction', {
3240
+ editor: this,
3241
+ transaction,
3242
+ });
3243
+ if (selectionHasChanged) {
3244
+ this.emit('selectionUpdate', {
3245
+ editor: this,
3246
+ transaction,
3247
+ });
3248
+ }
3249
+ const focus = transaction.getMeta('focus');
3250
+ const blur = transaction.getMeta('blur');
3251
+ if (focus) {
3252
+ this.emit('focus', {
3253
+ editor: this,
3254
+ event: focus.event,
3255
+ transaction,
3256
+ });
3257
+ }
3258
+ if (blur) {
3259
+ this.emit('blur', {
3260
+ editor: this,
3261
+ event: blur.event,
3262
+ transaction,
3263
+ });
3264
+ }
3265
+ if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
3266
+ return;
3267
+ }
3268
+ this.emit('update', {
3269
+ editor: this,
3270
+ transaction,
3271
+ });
3272
+ }
3273
+ /**
3274
+ * Get attributes of the currently selected node or mark.
3275
+ */
3276
+ getAttributes(nameOrType) {
3277
+ return getAttributes(this.state, nameOrType);
3278
+ }
3279
+ isActive(nameOrAttributes, attributesOrUndefined) {
3280
+ const name = typeof nameOrAttributes === 'string'
3281
+ ? nameOrAttributes
3282
+ : null;
3283
+ const attributes = typeof nameOrAttributes === 'string'
3284
+ ? attributesOrUndefined
3285
+ : nameOrAttributes;
3286
+ return isActive(this.state, name, attributes);
3287
+ }
3288
+ /**
3289
+ * Get the document as JSON.
3290
+ */
3291
+ getJSON() {
3292
+ return this.state.doc.toJSON();
3293
+ }
3294
+ /**
3295
+ * Get the document as HTML.
3296
+ */
3297
+ getHTML() {
3298
+ return getHTMLFromFragment(this.state.doc.content, this.schema);
3299
+ }
3300
+ /**
3301
+ * Get the document as text.
3302
+ */
3303
+ getText(options) {
3304
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3305
+ return getText(this.state.doc, {
3306
+ blockSeparator,
3307
+ textSerializers: {
3308
+ ...textSerializers,
3309
+ ...getTextSeralizersFromSchema(this.schema),
3310
+ },
3311
+ });
3312
+ }
3313
+ /**
3314
+ * Check if there is no content.
3315
+ */
3316
+ get isEmpty() {
3317
+ return isNodeEmpty(this.state.doc);
3318
+ }
3319
+ /**
3320
+ * Get the number of characters for the current document.
3321
+ */
3322
+ getCharacterCount() {
3323
+ return this.state.doc.content.size - 2;
3324
+ }
3325
+ /**
3326
+ * Destroy the editor.
3327
+ */
3328
+ destroy() {
3329
+ this.emit('destroy');
3330
+ if (this.view) {
3331
+ this.view.destroy();
3332
+ }
3333
+ this.removeAllListeners();
3334
+ removeElement(this.css);
3335
+ }
3336
+ /**
3337
+ * Check if the editor is already destroyed.
3338
+ */
3339
+ get isDestroyed() {
3340
+ var _a;
3341
+ // @ts-ignore
3342
+ return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3343
+ }
3344
+ }
3345
+
3346
+ class Node {
3347
+ constructor(config = {}) {
3348
+ this.type = 'node';
3349
+ this.name = 'node';
3350
+ this.parent = null;
3351
+ this.child = null;
3352
+ this.config = {
3353
+ name: this.name,
3354
+ defaultOptions: {},
3355
+ };
3356
+ this.config = {
3357
+ ...this.config,
3358
+ ...config,
3359
+ };
3360
+ this.name = this.config.name;
3361
+ if (config.defaultOptions) {
3362
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
3363
+ }
3364
+ // TODO: remove `addOptions` fallback
3365
+ this.options = this.config.defaultOptions;
3366
+ if (this.config.addOptions) {
3367
+ this.options = callOrReturn(getExtensionField(this, 'addOptions', {
3368
+ name: this.name,
3369
+ }));
3370
+ }
3371
+ this.storage = callOrReturn(getExtensionField(this, 'addStorage', {
3372
+ name: this.name,
3373
+ options: this.options,
3374
+ })) || {};
3375
+ }
3376
+ static create(config = {}) {
3377
+ return new Node(config);
3378
+ }
3379
+ configure(options = {}) {
3380
+ // return a new instance so we can use the same extension
3381
+ // with different calls of `configure`
3382
+ const extension = this.extend();
3383
+ extension.options = mergeDeep(this.options, options);
3384
+ extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
3385
+ name: extension.name,
3386
+ options: extension.options,
3387
+ }));
3388
+ return extension;
3389
+ }
3390
+ extend(extendedConfig = {}) {
3391
+ const extension = new Node(extendedConfig);
3392
+ extension.parent = this;
3393
+ this.child = extension;
3394
+ extension.name = extendedConfig.name
3395
+ ? extendedConfig.name
3396
+ : extension.parent.name;
3397
+ if (extendedConfig.defaultOptions) {
3398
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`);
3399
+ }
3400
+ // TODO: remove `addOptions` fallback
3401
+ extension.options = extendedConfig.defaultOptions
3402
+ ? extendedConfig.defaultOptions
3403
+ : extension.parent.options;
3404
+ if (extendedConfig.addOptions) {
3405
+ extension.options = callOrReturn(getExtensionField(extension, 'addOptions', {
3406
+ name: extension.name,
3407
+ }));
3408
+ }
3409
+ extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
3410
+ name: extension.name,
3411
+ options: extension.options,
3412
+ }));
3413
+ return extension;
3414
+ }
3415
+ }
3416
+
3417
+ class Mark {
3418
+ constructor(config = {}) {
3419
+ this.type = 'mark';
3420
+ this.name = 'mark';
3421
+ this.parent = null;
3422
+ this.child = null;
3423
+ this.config = {
3424
+ name: this.name,
3425
+ defaultOptions: {},
3426
+ };
3427
+ this.config = {
3428
+ ...this.config,
3429
+ ...config,
3430
+ };
3431
+ this.name = this.config.name;
3432
+ if (config.defaultOptions) {
3433
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
3434
+ }
3435
+ // TODO: remove `addOptions` fallback
3436
+ this.options = this.config.defaultOptions;
3437
+ if (this.config.addOptions) {
3438
+ this.options = callOrReturn(getExtensionField(this, 'addOptions', {
3439
+ name: this.name,
3440
+ }));
3441
+ }
3442
+ this.storage = callOrReturn(getExtensionField(this, 'addStorage', {
3443
+ name: this.name,
3444
+ options: this.options,
3445
+ })) || {};
3446
+ }
3447
+ static create(config = {}) {
3448
+ return new Mark(config);
3449
+ }
3450
+ configure(options = {}) {
3451
+ // return a new instance so we can use the same extension
3452
+ // with different calls of `configure`
3453
+ const extension = this.extend();
3454
+ extension.options = mergeDeep(this.options, options);
3455
+ extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
3456
+ name: extension.name,
3457
+ options: extension.options,
3458
+ }));
3459
+ return extension;
3460
+ }
3461
+ extend(extendedConfig = {}) {
3462
+ const extension = new Mark(extendedConfig);
3463
+ extension.parent = this;
3464
+ this.child = extension;
3465
+ extension.name = extendedConfig.name
3466
+ ? extendedConfig.name
3467
+ : extension.parent.name;
3468
+ if (extendedConfig.defaultOptions) {
3469
+ console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`);
3470
+ }
3471
+ // TODO: remove `addOptions` fallback
3472
+ extension.options = extendedConfig.defaultOptions
3473
+ ? extendedConfig.defaultOptions
3474
+ : extension.parent.options;
3475
+ if (extendedConfig.addOptions) {
3476
+ extension.options = callOrReturn(getExtensionField(extension, 'addOptions', {
3477
+ name: extension.name,
3478
+ }));
3479
+ }
3480
+ extension.storage = callOrReturn(getExtensionField(extension, 'addStorage', {
3481
+ name: extension.name,
3482
+ options: extension.options,
3483
+ }));
3484
+ return extension;
3485
+ }
3486
+ }
3487
+
3488
+ class NodeView {
3489
+ constructor(component, props, options) {
3490
+ this.isDragging = false;
3491
+ this.component = component;
3492
+ this.editor = props.editor;
3493
+ this.options = {
3494
+ stopEvent: null,
3495
+ ignoreMutation: null,
3496
+ ...options,
3497
+ };
3498
+ this.extension = props.extension;
3499
+ this.node = props.node;
3500
+ this.decorations = props.decorations;
3501
+ this.getPos = props.getPos;
3502
+ this.mount();
3503
+ }
3504
+ mount() {
3505
+ // eslint-disable-next-line
3506
+ return;
3507
+ }
3508
+ get dom() {
3509
+ return null;
3510
+ }
3511
+ get contentDOM() {
3512
+ return null;
3513
+ }
3514
+ onDragStart(event) {
3515
+ var _a, _b, _c;
3516
+ const { view } = this.editor;
3517
+ const target = event.target;
3518
+ // get the drag handle element
3519
+ // `closest` is not available for text nodes so we may have to use its parent
3520
+ const dragHandle = target.nodeType === 3
3521
+ ? (_a = target.parentElement) === null || _a === void 0 ? void 0 : _a.closest('[data-drag-handle]')
3522
+ : target.closest('[data-drag-handle]');
3523
+ if (!this.dom
3524
+ || ((_b = this.contentDOM) === null || _b === void 0 ? void 0 : _b.contains(target))
3525
+ || !dragHandle) {
3526
+ return;
3527
+ }
3528
+ let x = 0;
3529
+ let y = 0;
3530
+ // calculate offset for drag element if we use a different drag handle element
3531
+ if (this.dom !== dragHandle) {
3532
+ const domBox = this.dom.getBoundingClientRect();
3533
+ const handleBox = dragHandle.getBoundingClientRect();
3534
+ x = handleBox.x - domBox.x + event.offsetX;
3535
+ y = handleBox.y - domBox.y + event.offsetY;
3536
+ }
3537
+ (_c = event.dataTransfer) === null || _c === void 0 ? void 0 : _c.setDragImage(this.dom, x, y);
3538
+ // we need to tell ProseMirror that we want to move the whole node
3539
+ // so we create a NodeSelection
3540
+ const selection = prosemirrorState.NodeSelection.create(view.state.doc, this.getPos());
3541
+ const transaction = view.state.tr.setSelection(selection);
3542
+ view.dispatch(transaction);
3543
+ }
3544
+ stopEvent(event) {
3545
+ var _a;
3546
+ if (!this.dom) {
3547
+ return false;
3548
+ }
3549
+ if (typeof this.options.stopEvent === 'function') {
3550
+ return this.options.stopEvent({ event });
3551
+ }
3552
+ const target = event.target;
3553
+ const isInElement = this.dom.contains(target) && !((_a = this.contentDOM) === null || _a === void 0 ? void 0 : _a.contains(target));
3554
+ // any event from child nodes should be handled by ProseMirror
3555
+ if (!isInElement) {
3556
+ return false;
3557
+ }
3558
+ const isDropEvent = event.type === 'drop';
3559
+ const isInput = ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA'].includes(target.tagName)
3560
+ || target.isContentEditable;
3561
+ // any input event within node views should be ignored by ProseMirror
3562
+ if (isInput && !isDropEvent) {
3563
+ return true;
3564
+ }
3565
+ const { isEditable } = this.editor;
3566
+ const { isDragging } = this;
3567
+ const isDraggable = !!this.node.type.spec.draggable;
3568
+ const isSelectable = prosemirrorState.NodeSelection.isSelectable(this.node);
3569
+ const isCopyEvent = event.type === 'copy';
3570
+ const isPasteEvent = event.type === 'paste';
3571
+ const isCutEvent = event.type === 'cut';
3572
+ const isClickEvent = event.type === 'mousedown';
3573
+ const isDragEvent = event.type.startsWith('drag');
3574
+ // ProseMirror tries to drag selectable nodes
3575
+ // even if `draggable` is set to `false`
3576
+ // this fix prevents that
3577
+ if (!isDraggable && isSelectable && isDragEvent) {
3578
+ event.preventDefault();
3579
+ }
3580
+ if (isDraggable && isDragEvent && !isDragging) {
3581
+ event.preventDefault();
3582
+ return false;
3583
+ }
3584
+ // we have to store that dragging started
3585
+ if (isDraggable && isEditable && !isDragging && isClickEvent) {
3586
+ const dragHandle = target.closest('[data-drag-handle]');
3587
+ const isValidDragHandle = dragHandle
3588
+ && (this.dom === dragHandle || (this.dom.contains(dragHandle)));
3589
+ if (isValidDragHandle) {
3590
+ this.isDragging = true;
3591
+ document.addEventListener('dragend', () => {
3592
+ this.isDragging = false;
3593
+ }, { once: true });
3594
+ document.addEventListener('mouseup', () => {
3595
+ this.isDragging = false;
3596
+ }, { once: true });
3597
+ }
3598
+ }
3599
+ // these events are handled by prosemirror
3600
+ if (isDragging
3601
+ || isDropEvent
3602
+ || isCopyEvent
3603
+ || isPasteEvent
3604
+ || isCutEvent
3605
+ || (isClickEvent && isSelectable)) {
3606
+ return false;
3607
+ }
3608
+ return true;
3609
+ }
3610
+ ignoreMutation(mutation) {
3611
+ if (!this.dom || !this.contentDOM) {
3612
+ return true;
3613
+ }
3614
+ if (typeof this.options.ignoreMutation === 'function') {
3615
+ return this.options.ignoreMutation({ mutation });
3616
+ }
3617
+ // a leaf/atom node is like a black box for ProseMirror
3618
+ // and should be fully handled by the node view
3619
+ if (this.node.isLeaf || this.node.isAtom) {
3620
+ return true;
3621
+ }
3622
+ // ProseMirror should handle any selections
3623
+ if (mutation.type === 'selection') {
3624
+ return false;
3625
+ }
3626
+ // try to prevent a bug on iOS that will break node views on enter
3627
+ // this is because ProseMirror can’t preventDispatch on enter
3628
+ // this will lead to a re-render of the node view on enter
3629
+ // see: https://github.com/ueberdosis/tiptap/issues/1214
3630
+ if (this.dom.contains(mutation.target) && mutation.type === 'childList' && isiOS()) {
3631
+ const changedNodes = [
3632
+ ...Array.from(mutation.addedNodes),
3633
+ ...Array.from(mutation.removedNodes),
3634
+ ];
3635
+ // we’ll check if every changed node is contentEditable
3636
+ // to make sure it’s probably mutated by ProseMirror
3637
+ if (changedNodes.every(node => node.isContentEditable)) {
3638
+ return false;
3639
+ }
3640
+ }
3641
+ // we will allow mutation contentDOM with attributes
3642
+ // so we can for example adding classes within our node view
3643
+ if (this.contentDOM === mutation.target && mutation.type === 'attributes') {
3644
+ return true;
3645
+ }
3646
+ // ProseMirror should handle any changes within contentDOM
3647
+ if (this.contentDOM.contains(mutation.target)) {
3648
+ return false;
3649
+ }
3650
+ return true;
3651
+ }
3652
+ updateAttributes(attributes) {
3653
+ this.editor.commands.command(({ tr }) => {
3654
+ const pos = this.getPos();
3655
+ tr.setNodeMarkup(pos, undefined, {
3656
+ ...this.node.attrs,
3657
+ ...attributes,
3658
+ });
3659
+ return true;
3660
+ });
3661
+ }
3662
+ deleteNode() {
3663
+ const from = this.getPos();
3664
+ const to = from + this.node.nodeSize;
3665
+ this.editor.commands.deleteRange({ from, to });
3666
+ }
3667
+ }
3668
+
3669
+ class Tracker {
3670
+ constructor(transaction) {
3671
+ this.transaction = transaction;
3672
+ this.currentStep = this.transaction.steps.length;
3673
+ }
3674
+ map(position) {
3675
+ let deleted = false;
3676
+ const mappedPosition = this.transaction.steps
3677
+ .slice(this.currentStep)
3678
+ .reduce((newPosition, step) => {
3679
+ const mapResult = step
3680
+ .getMap()
3681
+ .mapResult(newPosition);
3682
+ if (mapResult.deleted) {
3683
+ deleted = true;
3684
+ }
3685
+ return mapResult.pos;
3686
+ }, position);
3687
+ return {
3688
+ position: mappedPosition,
3689
+ deleted,
3690
+ };
3691
+ }
3692
+ }
3693
+
3694
+ /**
3695
+ * Build an input rule that adds a node when the
3696
+ * matched text is typed into it.
3697
+ */
3698
+ function nodeInputRule(config) {
3699
+ return new InputRule({
3700
+ find: config.find,
3701
+ handler: ({ state, range, match }) => {
3702
+ const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
3703
+ const { tr } = state;
3704
+ const start = range.from;
3705
+ let end = range.to;
3706
+ if (match[1]) {
3707
+ const offset = match[0].lastIndexOf(match[1]);
3708
+ let matchStart = start + offset;
3709
+ if (matchStart > end) {
3710
+ matchStart = end;
3711
+ }
3712
+ else {
3713
+ end = matchStart + match[1].length;
3714
+ }
3715
+ // insert last typed character
3716
+ const lastChar = match[0][match[0].length - 1];
3717
+ tr.insertText(lastChar, start + match[0].length - 1);
3718
+ // insert node from input rule
3719
+ tr.replaceWith(matchStart, end, config.type.create(attributes));
3720
+ }
3721
+ else if (match[0]) {
3722
+ tr.replaceWith(start, end, config.type.create(attributes));
3723
+ }
3724
+ },
3725
+ });
3726
+ }
3727
+
3728
+ function getMarksBetween(from, to, state) {
3729
+ const marks = [];
3730
+ state.doc.nodesBetween(from, to, (node, pos) => {
3731
+ marks.push(...node.marks.map(mark => ({
3732
+ from: pos,
3733
+ to: pos + node.nodeSize,
3734
+ mark,
3735
+ })));
3736
+ });
3737
+ return marks;
3738
+ }
3739
+
3740
+ /**
3741
+ * Build an input rule that adds a mark when the
3742
+ * matched text is typed into it.
3743
+ */
3744
+ function markInputRule(config) {
3745
+ return new InputRule({
3746
+ find: config.find,
3747
+ handler: ({ state, range, match }) => {
3748
+ const attributes = callOrReturn(config.getAttributes, undefined, match);
3749
+ if (attributes === false || attributes === null) {
3750
+ return;
3751
+ }
3752
+ const { tr } = state;
3753
+ const captureGroup = match[match.length - 1];
3754
+ const fullMatch = match[0];
3755
+ let markEnd = range.to;
3756
+ if (captureGroup) {
3757
+ const startSpaces = fullMatch.search(/\S/);
3758
+ const textStart = range.from + fullMatch.indexOf(captureGroup);
3759
+ const textEnd = textStart + captureGroup.length;
3760
+ const excludedMarks = getMarksBetween(range.from, range.to, state)
3761
+ .filter(item => {
3762
+ // @ts-ignore
3763
+ const excluded = item.mark.type.excluded;
3764
+ return excluded.find(type => type === config.type && type !== item.mark.type);
3765
+ })
3766
+ .filter(item => item.to > textStart);
3767
+ if (excludedMarks.length) {
3768
+ return null;
3769
+ }
3770
+ if (textEnd < range.to) {
3771
+ tr.delete(textEnd, range.to);
3772
+ }
3773
+ if (textStart > range.from) {
3774
+ tr.delete(range.from + startSpaces, textStart);
3775
+ }
3776
+ markEnd = range.from + startSpaces + captureGroup.length;
3777
+ tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}));
3778
+ tr.removeStoredMark(config.type);
3779
+ }
3780
+ },
3781
+ });
3782
+ }
3783
+
3784
+ /**
3785
+ * Build an input rule that changes the type of a textblock when the
3786
+ * matched text is typed into it. When using a regular expresion you’ll
3787
+ * probably want the regexp to start with `^`, so that the pattern can
3788
+ * only occur at the start of a textblock.
3789
+ */
3790
+ function textblockTypeInputRule(config) {
3791
+ return new InputRule({
3792
+ find: config.find,
3793
+ handler: ({ state, range, match }) => {
3794
+ const $start = state.doc.resolve(range.from);
3795
+ const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
3796
+ if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), config.type)) {
3797
+ return null;
3798
+ }
3799
+ state.tr
3800
+ .delete(range.from, range.to)
3801
+ .setBlockType(range.from, range.from, config.type, attributes);
3802
+ },
3803
+ });
3804
+ }
3805
+
3806
+ /**
3807
+ * Build an input rule that replaces text when the
3808
+ * matched text is typed into it.
3809
+ */
3810
+ function textInputRule(config) {
3811
+ return new InputRule({
3812
+ find: config.find,
3813
+ handler: ({ state, range, match }) => {
3814
+ let insert = config.replace;
3815
+ let start = range.from;
3816
+ const end = range.to;
3817
+ if (match[1]) {
3818
+ const offset = match[0].lastIndexOf(match[1]);
3819
+ insert += match[0].slice(offset + match[1].length);
3820
+ start += offset;
3821
+ const cutOff = start - end;
3822
+ if (cutOff > 0) {
3823
+ insert = match[0].slice(offset - cutOff, offset) + insert;
3824
+ start = end;
3825
+ }
3826
+ }
3827
+ state.tr.insertText(insert, start, end);
3828
+ },
3829
+ });
3830
+ }
3831
+
3832
+ /**
3833
+ * Build an input rule for automatically wrapping a textblock when a
3834
+ * given string is typed. When using a regular expresion you’ll
3835
+ * probably want the regexp to start with `^`, so that the pattern can
3836
+ * only occur at the start of a textblock.
3837
+ *
3838
+ * `type` is the type of node to wrap in.
3839
+ *
3840
+ * By default, if there’s a node with the same type above the newly
3841
+ * wrapped node, the rule will try to join those
3842
+ * two nodes. You can pass a join predicate, which takes a regular
3843
+ * expression match and the node before the wrapped node, and can
3844
+ * return a boolean to indicate whether a join should happen.
3845
+ */
3846
+ function wrappingInputRule(config) {
3847
+ return new InputRule({
3848
+ find: config.find,
3849
+ handler: ({ state, range, match }) => {
3850
+ const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
3851
+ const tr = state.tr.delete(range.from, range.to);
3852
+ const $start = tr.doc.resolve(range.from);
3853
+ const blockRange = $start.blockRange();
3854
+ const wrapping = blockRange && prosemirrorTransform.findWrapping(blockRange, config.type, attributes);
3855
+ if (!wrapping) {
3856
+ return null;
3857
+ }
3858
+ tr.wrap(blockRange, wrapping);
3859
+ const before = tr.doc.resolve(range.from - 1).nodeBefore;
3860
+ if (before
3861
+ && before.type === config.type
3862
+ && prosemirrorTransform.canJoin(tr.doc, range.from - 1)
3863
+ && (!config.joinPredicate || config.joinPredicate(match, before))) {
3864
+ tr.join(range.from - 1);
3865
+ }
3866
+ },
3867
+ });
3868
+ }
3869
+
3870
+ /**
3871
+ * Build an paste rule that adds a mark when the
3872
+ * matched text is pasted into it.
3873
+ */
3874
+ function markPasteRule(config) {
3875
+ return new PasteRule({
3876
+ find: config.find,
3877
+ handler: ({ state, range, match }) => {
3878
+ const attributes = callOrReturn(config.getAttributes, undefined, match);
3879
+ if (attributes === false || attributes === null) {
3880
+ return;
3881
+ }
3882
+ const { tr } = state;
3883
+ const captureGroup = match[match.length - 1];
3884
+ const fullMatch = match[0];
3885
+ let markEnd = range.to;
3886
+ if (captureGroup) {
3887
+ const startSpaces = fullMatch.search(/\S/);
3888
+ const textStart = range.from + fullMatch.indexOf(captureGroup);
3889
+ const textEnd = textStart + captureGroup.length;
3890
+ const excludedMarks = getMarksBetween(range.from, range.to, state)
3891
+ .filter(item => {
3892
+ // @ts-ignore
3893
+ const excluded = item.mark.type.excluded;
3894
+ return excluded.find(type => type === config.type && type !== item.mark.type);
3895
+ })
3896
+ .filter(item => item.to > textStart);
3897
+ if (excludedMarks.length) {
3898
+ return null;
3899
+ }
3900
+ if (textEnd < range.to) {
3901
+ tr.delete(textEnd, range.to);
3902
+ }
3903
+ if (textStart > range.from) {
3904
+ tr.delete(range.from + startSpaces, textStart);
3905
+ }
3906
+ markEnd = range.from + startSpaces + captureGroup.length;
3907
+ tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}));
3908
+ tr.removeStoredMark(config.type);
3909
+ }
3910
+ },
3911
+ });
3912
+ }
3913
+
3914
+ /**
3915
+ * Build an paste rule that replaces text when the
3916
+ * matched text is pasted into it.
3917
+ */
3918
+ function textPasteRule(config) {
3919
+ return new PasteRule({
3920
+ find: config.find,
3921
+ handler: ({ state, range, match }) => {
3922
+ let insert = config.replace;
3923
+ let start = range.from;
3924
+ const end = range.to;
3925
+ if (match[1]) {
3926
+ const offset = match[0].lastIndexOf(match[1]);
3927
+ insert += match[0].slice(offset + match[1].length);
3928
+ start += offset;
3929
+ const cutOff = start - end;
3930
+ if (cutOff > 0) {
3931
+ insert = match[0].slice(offset - cutOff, offset) + insert;
3932
+ start = end;
3933
+ }
3934
+ }
3935
+ state.tr.insertText(insert, start, end);
3936
+ },
3937
+ });
3938
+ }
3939
+
3940
+ function findChildren(node, predicate) {
3941
+ const nodesWithPos = [];
3942
+ node.descendants((child, pos) => {
3943
+ if (predicate(child)) {
3944
+ nodesWithPos.push({
3945
+ node: child,
3946
+ pos,
3947
+ });
3948
+ }
3949
+ });
3950
+ return nodesWithPos;
3951
+ }
3952
+
3953
+ /**
3954
+ * Same as `findChildren` but searches only within a `range`.
3955
+ */
3956
+ function findChildrenInRange(node, range, predicate) {
3957
+ const nodesWithPos = [];
3958
+ // if (range.from === range.to) {
3959
+ // const nodeAt = node.nodeAt(range.from)
3960
+ // if (nodeAt) {
3961
+ // nodesWithPos.push({
3962
+ // node: nodeAt,
3963
+ // pos: range.from,
3964
+ // })
3965
+ // }
3966
+ // }
3967
+ node.nodesBetween(range.from, range.to, (child, pos) => {
3968
+ if (predicate(child)) {
3969
+ nodesWithPos.push({
3970
+ node: child,
3971
+ pos,
3972
+ });
3973
+ }
3974
+ });
3975
+ return nodesWithPos;
3976
+ }
3977
+
3978
+ function getSchema(extensions) {
3979
+ const resolvedExtensions = ExtensionManager.resolve(extensions);
3980
+ return getSchemaByResolvedExtensions(resolvedExtensions);
3981
+ }
3982
+
3983
+ function generateHTML(doc, extensions) {
3984
+ const schema = getSchema(extensions);
3985
+ const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
3986
+ return getHTMLFromFragment(contentNode.content, schema);
3987
+ }
3988
+
3989
+ function generateJSON(html, extensions) {
3990
+ const schema = getSchema(extensions);
3991
+ const dom = elementFromString(html);
3992
+ return prosemirrorModel.DOMParser.fromSchema(schema)
3993
+ .parse(dom)
3994
+ .toJSON();
3995
+ }
3996
+
3997
+ function generateText(doc, extensions, options) {
3998
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3999
+ const schema = getSchema(extensions);
4000
+ const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
4001
+ return getText(contentNode, {
4002
+ blockSeparator,
4003
+ textSerializers: {
4004
+ ...textSerializers,
4005
+ ...getTextSeralizersFromSchema(schema),
4006
+ },
4007
+ });
4008
+ }
4009
+
4010
+ function getDebugJSON(node, startOffset = 0) {
4011
+ const isTopNode = node.type === node.type.schema.topNodeType;
4012
+ const increment = isTopNode ? 0 : 1;
4013
+ const from = startOffset; // + offset
4014
+ const to = from + node.nodeSize;
4015
+ const marks = node.marks.map(mark => ({
4016
+ type: mark.type.name,
4017
+ attrs: { ...mark.attrs },
4018
+ }));
4019
+ const attrs = { ...node.attrs };
4020
+ const output = {
4021
+ type: node.type.name,
4022
+ from,
4023
+ to,
4024
+ };
4025
+ if (Object.keys(attrs).length) {
4026
+ output.attrs = attrs;
4027
+ }
4028
+ if (marks.length) {
4029
+ output.marks = marks;
4030
+ }
4031
+ if (node.content.childCount) {
4032
+ output.content = [];
4033
+ node.forEach((child, offset) => {
4034
+ var _a;
4035
+ (_a = output.content) === null || _a === void 0 ? void 0 : _a.push(getDebugJSON(child, startOffset + offset + increment));
4036
+ });
4037
+ }
4038
+ if (node.text) {
4039
+ output.text = node.text;
4040
+ }
4041
+ return output;
4042
+ }
4043
+
4044
+ function isNodeSelection(value) {
4045
+ return isObject(value) && value instanceof prosemirrorState.NodeSelection;
4046
+ }
4047
+
4048
+ function posToDOMRect(view, from, to) {
4049
+ const minPos = 0;
4050
+ const maxPos = view.state.doc.content.size;
4051
+ const resolvedFrom = minMax(from, minPos, maxPos);
4052
+ const resolvedEnd = minMax(to, minPos, maxPos);
4053
+ const start = view.coordsAtPos(resolvedFrom);
4054
+ const end = view.coordsAtPos(resolvedEnd, -1);
4055
+ const top = Math.min(start.top, end.top);
4056
+ const bottom = Math.max(start.bottom, end.bottom);
4057
+ const left = Math.min(start.left, end.left);
4058
+ const right = Math.max(start.right, end.right);
4059
+ const width = right - left;
4060
+ const height = bottom - top;
4061
+ const x = left;
4062
+ const y = top;
4063
+ const data = {
4064
+ top,
4065
+ bottom,
4066
+ left,
4067
+ right,
4068
+ width,
4069
+ height,
4070
+ x,
4071
+ y,
4072
+ };
4073
+ return {
4074
+ ...data,
4075
+ toJSON: () => data,
4076
+ };
4077
+ }
4078
+
4079
+ exports.Editor = Editor;
4080
+ exports.Extension = Extension;
4081
+ exports.InputRule = InputRule;
4082
+ exports.Mark = Mark;
4083
+ exports.Node = Node;
4084
+ exports.NodeView = NodeView;
4085
+ exports.PasteRule = PasteRule;
4086
+ exports.Tracker = Tracker;
4087
+ exports.callOrReturn = callOrReturn;
4088
+ exports.extensions = extensions;
4089
+ exports.findChildren = findChildren;
4090
+ exports.findChildrenInRange = findChildrenInRange;
4091
+ exports.findParentNode = findParentNode;
4092
+ exports.findParentNodeClosestToPos = findParentNodeClosestToPos;
4093
+ exports.generateHTML = generateHTML;
4094
+ exports.generateJSON = generateJSON;
4095
+ exports.generateText = generateText;
4096
+ exports.getAttributes = getAttributes;
4097
+ exports.getDebugJSON = getDebugJSON;
4098
+ exports.getExtensionField = getExtensionField;
4099
+ exports.getHTMLFromFragment = getHTMLFromFragment;
4100
+ exports.getMarkAttributes = getMarkAttributes;
4101
+ exports.getMarkRange = getMarkRange;
4102
+ exports.getMarkType = getMarkType;
4103
+ exports.getMarksBetween = getMarksBetween;
4104
+ exports.getNodeAttributes = getNodeAttributes;
4105
+ exports.getNodeType = getNodeType;
4106
+ exports.getSchema = getSchema;
4107
+ exports.getText = getText;
4108
+ exports.getTextBetween = getTextBetween;
4109
+ exports.inputRulesPlugin = inputRulesPlugin;
4110
+ exports.isActive = isActive;
4111
+ exports.isList = isList;
4112
+ exports.isMarkActive = isMarkActive;
4113
+ exports.isNodeActive = isNodeActive;
4114
+ exports.isNodeEmpty = isNodeEmpty;
4115
+ exports.isNodeSelection = isNodeSelection;
4116
+ exports.isTextSelection = isTextSelection;
4117
+ exports.markInputRule = markInputRule;
4118
+ exports.markPasteRule = markPasteRule;
4119
+ exports.mergeAttributes = mergeAttributes;
4120
+ exports.nodeInputRule = nodeInputRule;
4121
+ exports.pasteRulesPlugin = pasteRulesPlugin;
4122
+ exports.posToDOMRect = posToDOMRect;
4123
+ exports.textInputRule = textInputRule;
4124
+ exports.textPasteRule = textPasteRule;
4125
+ exports.textblockTypeInputRule = textblockTypeInputRule;
4126
+ exports.wrappingInputRule = wrappingInputRule;
4127
+
4128
+ Object.defineProperty(exports, '__esModule', { value: true });
4129
+
4130
+ }));
4131
+ //# sourceMappingURL=tiptap-core.umd.js.map