@tiptap/core 3.0.0-next.3 → 3.0.0-next.5

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 (146) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +5 -1
  3. package/dist/index.cjs +2627 -2651
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +2423 -2688
  6. package/dist/index.d.ts +2423 -2688
  7. package/dist/index.js +2639 -2684
  8. package/dist/index.js.map +1 -1
  9. package/dist/jsx-runtime/jsx-runtime.cjs +56 -0
  10. package/dist/jsx-runtime/jsx-runtime.cjs.map +1 -0
  11. package/dist/jsx-runtime/jsx-runtime.d.cts +22 -0
  12. package/dist/jsx-runtime/jsx-runtime.d.ts +22 -0
  13. package/dist/jsx-runtime/jsx-runtime.js +26 -0
  14. package/dist/jsx-runtime/jsx-runtime.js.map +1 -0
  15. package/jsx-runtime/index.cjs +1 -0
  16. package/jsx-runtime/index.d.cts +1 -0
  17. package/jsx-runtime/index.d.ts +1 -0
  18. package/jsx-runtime/index.js +1 -0
  19. package/package.json +27 -6
  20. package/src/CommandManager.ts +2 -9
  21. package/src/Editor.ts +191 -94
  22. package/src/EventEmitter.ts +7 -10
  23. package/src/Extendable.ts +483 -0
  24. package/src/Extension.ts +5 -496
  25. package/src/ExtensionManager.ts +81 -135
  26. package/src/InputRule.ts +35 -48
  27. package/src/Mark.ts +135 -623
  28. package/src/MarkView.ts +66 -0
  29. package/src/Node.ts +325 -829
  30. package/src/NodePos.ts +1 -3
  31. package/src/NodeView.ts +10 -20
  32. package/src/PasteRule.ts +43 -55
  33. package/src/Tracker.ts +7 -9
  34. package/src/commands/blur.ts +14 -12
  35. package/src/commands/clearContent.ts +12 -5
  36. package/src/commands/clearNodes.ts +32 -30
  37. package/src/commands/command.ts +1 -1
  38. package/src/commands/createParagraphNear.ts +5 -3
  39. package/src/commands/cut.ts +12 -10
  40. package/src/commands/deleteCurrentNode.ts +23 -21
  41. package/src/commands/deleteNode.ts +18 -16
  42. package/src/commands/deleteRange.ts +10 -8
  43. package/src/commands/deleteSelection.ts +5 -3
  44. package/src/commands/enter.ts +6 -4
  45. package/src/commands/exitCode.ts +5 -3
  46. package/src/commands/extendMarkRange.ts +14 -12
  47. package/src/commands/first.ts +2 -4
  48. package/src/commands/focus.ts +51 -48
  49. package/src/commands/forEach.ts +2 -2
  50. package/src/commands/insertContent.ts +12 -14
  51. package/src/commands/insertContentAt.ts +105 -98
  52. package/src/commands/join.ts +20 -12
  53. package/src/commands/joinItemBackward.ts +16 -18
  54. package/src/commands/joinItemForward.ts +16 -18
  55. package/src/commands/joinTextblockBackward.ts +5 -3
  56. package/src/commands/joinTextblockForward.ts +5 -3
  57. package/src/commands/keyboardShortcut.ts +29 -34
  58. package/src/commands/lift.ts +10 -8
  59. package/src/commands/liftEmptyBlock.ts +6 -4
  60. package/src/commands/liftListItem.ts +6 -4
  61. package/src/commands/newlineInCode.ts +5 -3
  62. package/src/commands/resetAttributes.ts +36 -41
  63. package/src/commands/scrollIntoView.ts +9 -7
  64. package/src/commands/selectAll.ts +10 -8
  65. package/src/commands/selectNodeBackward.ts +5 -3
  66. package/src/commands/selectNodeForward.ts +5 -3
  67. package/src/commands/selectParentNode.ts +5 -3
  68. package/src/commands/selectTextblockEnd.ts +5 -3
  69. package/src/commands/selectTextblockStart.ts +5 -3
  70. package/src/commands/setContent.ts +37 -36
  71. package/src/commands/setMark.ts +55 -57
  72. package/src/commands/setMeta.ts +7 -5
  73. package/src/commands/setNode.ts +32 -30
  74. package/src/commands/setNodeSelection.ts +11 -9
  75. package/src/commands/setTextSelection.ts +15 -13
  76. package/src/commands/sinkListItem.ts +6 -4
  77. package/src/commands/splitBlock.ts +67 -76
  78. package/src/commands/splitListItem.ts +93 -106
  79. package/src/commands/toggleList.ts +73 -71
  80. package/src/commands/toggleMark.ts +11 -9
  81. package/src/commands/toggleNode.ts +18 -16
  82. package/src/commands/toggleWrap.ts +10 -8
  83. package/src/commands/undoInputRule.ts +31 -29
  84. package/src/commands/unsetAllMarks.ts +16 -14
  85. package/src/commands/unsetMark.ts +27 -25
  86. package/src/commands/updateAttributes.ts +92 -100
  87. package/src/commands/wrapIn.ts +6 -4
  88. package/src/commands/wrapInList.ts +6 -4
  89. package/src/extensions/clipboardTextSerializer.ts +2 -4
  90. package/src/extensions/delete.ts +89 -0
  91. package/src/extensions/focusEvents.ts +2 -6
  92. package/src/extensions/index.ts +1 -0
  93. package/src/extensions/keymap.ts +58 -50
  94. package/src/extensions/paste.ts +0 -1
  95. package/src/extensions/tabindex.ts +1 -1
  96. package/src/helpers/combineTransactionSteps.ts +1 -4
  97. package/src/helpers/createChainableState.ts +1 -4
  98. package/src/helpers/createDocument.ts +1 -3
  99. package/src/helpers/createNodeFromContent.ts +4 -10
  100. package/src/helpers/findChildrenInRange.ts +1 -5
  101. package/src/helpers/findParentNode.ts +3 -1
  102. package/src/helpers/flattenExtensions.ts +30 -0
  103. package/src/helpers/getAttributes.ts +1 -4
  104. package/src/helpers/getAttributesFromExtensions.ts +28 -37
  105. package/src/helpers/getChangedRanges.ts +13 -11
  106. package/src/helpers/getExtensionField.ts +11 -11
  107. package/src/helpers/getMarkAttributes.ts +1 -4
  108. package/src/helpers/getMarkRange.ts +5 -15
  109. package/src/helpers/getMarkType.ts +1 -3
  110. package/src/helpers/getNodeAttributes.ts +1 -4
  111. package/src/helpers/getNodeType.ts +1 -3
  112. package/src/helpers/getRenderedAttributes.ts +1 -3
  113. package/src/helpers/getSchema.ts +2 -2
  114. package/src/helpers/getSchemaByResolvedExtensions.ts +45 -77
  115. package/src/helpers/getSplittedAttributes.ts +4 -4
  116. package/src/helpers/getTextContentFromNodes.ts +8 -11
  117. package/src/helpers/index.ts +4 -0
  118. package/src/helpers/injectExtensionAttributesToParseRule.ts +1 -1
  119. package/src/helpers/isActive.ts +1 -5
  120. package/src/helpers/isExtensionRulesEnabled.ts +1 -3
  121. package/src/helpers/isNodeEmpty.ts +2 -2
  122. package/src/helpers/resolveExtensions.ts +25 -0
  123. package/src/helpers/resolveFocusPosition.ts +3 -14
  124. package/src/helpers/rewriteUnknownContent.ts +149 -0
  125. package/src/helpers/sortExtensions.ts +26 -0
  126. package/src/index.ts +3 -7
  127. package/src/inputRules/markInputRule.ts +1 -5
  128. package/src/inputRules/nodeInputRule.ts +2 -9
  129. package/src/inputRules/textInputRule.ts +1 -4
  130. package/src/inputRules/textblockTypeInputRule.ts +2 -8
  131. package/src/inputRules/wrappingInputRule.ts +13 -19
  132. package/src/jsx-runtime.ts +64 -0
  133. package/src/pasteRules/markPasteRule.ts +1 -3
  134. package/src/pasteRules/nodePasteRule.ts +2 -8
  135. package/src/pasteRules/textPasteRule.ts +1 -4
  136. package/src/types.ts +529 -174
  137. package/src/utilities/createStyleTag.ts +3 -1
  138. package/src/utilities/deleteProps.ts +7 -11
  139. package/src/utilities/elementFromString.ts +3 -0
  140. package/src/utilities/findDuplicates.ts +4 -1
  141. package/src/utilities/index.ts +1 -0
  142. package/src/utilities/isFunction.ts +1 -0
  143. package/src/utilities/isMacOS.ts +1 -3
  144. package/src/utilities/isiOS.ts +5 -10
  145. package/src/utilities/mergeAttributes.ts +17 -7
  146. package/src/utilities/removeDuplicates.ts +1 -3
package/src/Extension.ts CHANGED
@@ -1,508 +1,17 @@
1
- import { Plugin, Transaction } from '@tiptap/pm/state'
1
+ import { type ExtendableConfig, Extendable } from './Extendable.js'
2
2
 
3
- import { Editor } from './Editor.js'
4
- import { getExtensionField } from './helpers/getExtensionField.js'
5
- import { ExtensionConfig } from './index.js'
6
- import { InputRule } from './InputRule.js'
7
- import { Mark } from './Mark.js'
8
- import { Node } from './Node.js'
9
- import { PasteRule } from './PasteRule.js'
10
- import {
11
- AnyConfig,
12
- Extensions,
13
- GlobalAttributes,
14
- KeyboardShortcutCommand,
15
- ParentConfig,
16
- RawCommands,
17
- } from './types.js'
18
- import { callOrReturn } from './utilities/callOrReturn.js'
19
- import { mergeDeep } from './utilities/mergeDeep.js'
20
-
21
- declare module '@tiptap/core' {
22
- interface ExtensionConfig<Options = any, Storage = any> {
23
- // @ts-ignore - this is a dynamic key
24
- [key: string]: any
25
-
26
- /**
27
- * The extension name - this must be unique.
28
- * It will be used to identify the extension.
29
- *
30
- * @example 'myExtension'
31
- */
32
- name: string
33
-
34
- /**
35
- * The priority of your extension. The higher, the earlier it will be called
36
- * and will take precedence over other extensions with a lower priority.
37
- * @default 100
38
- * @example 101
39
- */
40
- priority?: number
41
-
42
- /**
43
- * The default options for this extension.
44
- * @example
45
- * defaultOptions: {
46
- * myOption: 'foo',
47
- * myOtherOption: 10,
48
- * }
49
- */
50
- defaultOptions?: Options
51
-
52
- /**
53
- * This method will add options to this extension
54
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#settings
55
- * @example
56
- * addOptions() {
57
- * return {
58
- * myOption: 'foo',
59
- * myOtherOption: 10,
60
- * }
61
- */
62
- addOptions?: (this: {
63
- name: string
64
- parent: Exclude<ParentConfig<ExtensionConfig<Options, Storage>>['addOptions'], undefined>
65
- }) => Options
66
-
67
- /**
68
- * The default storage this extension can save data to.
69
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#storage
70
- * @example
71
- * defaultStorage: {
72
- * prefetchedUsers: [],
73
- * loading: false,
74
- * }
75
- */
76
- addStorage?: (this: {
77
- name: string
78
- options: Options
79
- parent: Exclude<ParentConfig<ExtensionConfig<Options, Storage>>['addStorage'], undefined>
80
- }) => Storage
81
-
82
- /**
83
- * This function adds globalAttributes to specific nodes.
84
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#global-attributes
85
- * @example
86
- * addGlobalAttributes() {
87
- * return [
88
- * {
89
- // Extend the following extensions
90
- * types: [
91
- * 'heading',
92
- * 'paragraph',
93
- * ],
94
- * // … with those attributes
95
- * attributes: {
96
- * textAlign: {
97
- * default: 'left',
98
- * renderHTML: attributes => ({
99
- * style: `text-align: ${attributes.textAlign}`,
100
- * }),
101
- * parseHTML: element => element.style.textAlign || 'left',
102
- * },
103
- * },
104
- * },
105
- * ]
106
- * }
107
- */
108
- addGlobalAttributes?: (this: {
109
- name: string
110
- options: Options
111
- storage: Storage
112
- extensions: (Node | Mark)[]
113
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addGlobalAttributes']
114
- }) => GlobalAttributes
115
-
116
- /**
117
- * This function adds commands to the editor
118
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#commands
119
- * @example
120
- * addCommands() {
121
- * return {
122
- * myCommand: () => ({ chain }) => chain().setMark('type', 'foo').run(),
123
- * }
124
- * }
125
- */
126
- addCommands?: (this: {
127
- name: string
128
- options: Options
129
- storage: Storage
130
- editor: Editor
131
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addCommands']
132
- }) => Partial<RawCommands>
133
-
134
- /**
135
- * This function registers keyboard shortcuts.
136
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#keyboard-shortcuts
137
- * @example
138
- * addKeyboardShortcuts() {
139
- * return {
140
- * 'Mod-l': () => this.editor.commands.toggleBulletList(),
141
- * }
142
- * },
143
- */
144
- addKeyboardShortcuts?: (this: {
145
- name: string
146
- options: Options
147
- storage: Storage
148
- editor: Editor
149
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addKeyboardShortcuts']
150
- }) => {
151
- [key: string]: KeyboardShortcutCommand
152
- }
153
-
154
- /**
155
- * This function adds input rules to the editor.
156
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#input-rules
157
- * @example
158
- * addInputRules() {
159
- * return [
160
- * markInputRule({
161
- * find: inputRegex,
162
- * type: this.type,
163
- * }),
164
- * ]
165
- * },
166
- */
167
- addInputRules?: (this: {
168
- name: string
169
- options: Options
170
- storage: Storage
171
- editor: Editor
172
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addInputRules']
173
- }) => InputRule[]
174
-
175
- /**
176
- * This function adds paste rules to the editor.
177
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#paste-rules
178
- * @example
179
- * addPasteRules() {
180
- * return [
181
- * markPasteRule({
182
- * find: pasteRegex,
183
- * type: this.type,
184
- * }),
185
- * ]
186
- * },
187
- */
188
- addPasteRules?: (this: {
189
- name: string
190
- options: Options
191
- storage: Storage
192
- editor: Editor
193
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addPasteRules']
194
- }) => PasteRule[]
195
-
196
- /**
197
- * This function adds Prosemirror plugins to the editor
198
- * @see https://tiptap.dev/docs/editor/guide/custom-extensions#prosemirror-plugins
199
- * @example
200
- * addProseMirrorPlugins() {
201
- * return [
202
- * customPlugin(),
203
- * ]
204
- * }
205
- */
206
- addProseMirrorPlugins?: (this: {
207
- name: string
208
- options: Options
209
- storage: Storage
210
- editor: Editor
211
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addProseMirrorPlugins']
212
- }) => Plugin[]
213
-
214
- /**
215
- * This function adds additional extensions to the editor. This is useful for
216
- * building extension kits.
217
- * @example
218
- * addExtensions() {
219
- * return [
220
- * BulletList,
221
- * OrderedList,
222
- * ListItem
223
- * ]
224
- * }
225
- */
226
- addExtensions?: (this: {
227
- name: string
228
- options: Options
229
- storage: Storage
230
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['addExtensions']
231
- }) => Extensions
232
-
233
- /**
234
- * This function extends the schema of the node.
235
- * @example
236
- * extendNodeSchema() {
237
- * return {
238
- * group: 'inline',
239
- * selectable: false,
240
- * }
241
- * }
242
- */
243
- extendNodeSchema?:
244
- | ((
245
- this: {
246
- name: string
247
- options: Options
248
- storage: Storage
249
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['extendNodeSchema']
250
- },
251
- extension: Node,
252
- ) => Record<string, any>)
253
- | null
254
-
255
- /**
256
- * This function extends the schema of the mark.
257
- * @example
258
- * extendMarkSchema() {
259
- * return {
260
- * group: 'inline',
261
- * selectable: false,
262
- * }
263
- * }
264
- */
265
- extendMarkSchema?:
266
- | ((
267
- this: {
268
- name: string
269
- options: Options
270
- storage: Storage
271
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['extendMarkSchema']
272
- },
273
- extension: Mark,
274
- ) => Record<string, any>)
275
- | null
276
-
277
- /**
278
- * The editor is not ready yet.
279
- */
280
- onBeforeCreate?:
281
- | ((this: {
282
- name: string
283
- options: Options
284
- storage: Storage
285
- editor: Editor
286
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onBeforeCreate']
287
- }) => void)
288
- | null
289
-
290
- /**
291
- * The editor is ready.
292
- */
293
- onCreate?:
294
- | ((this: {
295
- name: string
296
- options: Options
297
- storage: Storage
298
- editor: Editor
299
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onCreate']
300
- }) => void)
301
- | null
302
-
303
- /**
304
- * The content has changed.
305
- */
306
- onUpdate?:
307
- | ((this: {
308
- name: string
309
- options: Options
310
- storage: Storage
311
- editor: Editor
312
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onUpdate']
313
- }) => void)
314
- | null
315
-
316
- /**
317
- * The selection has changed.
318
- */
319
- onSelectionUpdate?:
320
- | ((this: {
321
- name: string
322
- options: Options
323
- storage: Storage
324
- editor: Editor
325
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onSelectionUpdate']
326
- }) => void)
327
- | null
328
-
329
- /**
330
- * The editor state has changed.
331
- */
332
- onTransaction?:
333
- | ((
334
- this: {
335
- name: string
336
- options: Options
337
- storage: Storage
338
- editor: Editor
339
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onTransaction']
340
- },
341
- props: {
342
- editor: Editor
343
- transaction: Transaction
344
- },
345
- ) => void)
346
- | null
347
-
348
- /**
349
- * The editor is focused.
350
- */
351
- onFocus?:
352
- | ((
353
- this: {
354
- name: string
355
- options: Options
356
- storage: Storage
357
- editor: Editor
358
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onFocus']
359
- },
360
- props: {
361
- event: FocusEvent
362
- },
363
- ) => void)
364
- | null
365
-
366
- /**
367
- * The editor isn’t focused anymore.
368
- */
369
- onBlur?:
370
- | ((
371
- this: {
372
- name: string
373
- options: Options
374
- storage: Storage
375
- editor: Editor
376
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onBlur']
377
- },
378
- props: {
379
- event: FocusEvent
380
- },
381
- ) => void)
382
- | null
383
-
384
- /**
385
- * The editor is destroyed.
386
- */
387
- onDestroy?:
388
- | ((this: {
389
- name: string
390
- options: Options
391
- storage: Storage
392
- editor: Editor
393
- parent: ParentConfig<ExtensionConfig<Options, Storage>>['onDestroy']
394
- }) => void)
395
- | null
396
- }
397
- }
3
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
4
+ export interface ExtensionConfig<Options = any, Storage = any>
5
+ extends ExtendableConfig<Options, Storage, ExtensionConfig<Options, Storage>, null> {}
398
6
 
399
7
  /**
400
8
  * The Extension class is the base class for all extensions.
401
9
  * @see https://tiptap.dev/api/extensions#create-a-new-extension
402
10
  */
403
- export class Extension<Options = any, Storage = any> {
11
+ export class Extension<Options = any, Storage = any> extends Extendable<Options, Storage> {
404
12
  type = 'extension'
405
13
 
406
- name = 'extension'
407
-
408
- parent: Extension | null = null
409
-
410
- child: Extension | null = null
411
-
412
- options: Options
413
-
414
- storage: Storage
415
-
416
- config: ExtensionConfig = {
417
- name: this.name,
418
- defaultOptions: {},
419
- }
420
-
421
- constructor(config: Partial<ExtensionConfig<Options, Storage>> = {}) {
422
- this.config = {
423
- ...this.config,
424
- ...config,
425
- }
426
-
427
- this.name = this.config.name
428
-
429
- if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
430
- console.warn(
431
- `[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`,
432
- )
433
- }
434
-
435
- // TODO: remove `addOptions` fallback
436
- this.options = this.config.defaultOptions
437
-
438
- if (this.config.addOptions) {
439
- this.options = callOrReturn(
440
- getExtensionField<AnyConfig['addOptions']>(this, 'addOptions', {
441
- name: this.name,
442
- }),
443
- )
444
- }
445
-
446
- this.storage = callOrReturn(
447
- getExtensionField<AnyConfig['addStorage']>(this, 'addStorage', {
448
- name: this.name,
449
- options: this.options,
450
- }),
451
- ) || {}
452
- }
453
-
454
14
  static create<O = any, S = any>(config: Partial<ExtensionConfig<O, S>> = {}) {
455
15
  return new Extension<O, S>(config)
456
16
  }
457
-
458
- configure(options: Partial<Options> = {}) {
459
- // return a new instance so we can use the same extension
460
- // with different calls of `configure`
461
- const extension = this.extend<Options, Storage>({
462
- ...this.config,
463
- addOptions: () => {
464
- return mergeDeep(this.options as Record<string, any>, options) as Options
465
- },
466
- })
467
-
468
- // Always preserve the current name
469
- extension.name = this.name
470
- // Set the parent to be our parent
471
- extension.parent = this.parent
472
-
473
- return extension
474
- }
475
-
476
- extend<ExtendedOptions = Options, ExtendedStorage = Storage>(
477
- extendedConfig: Partial<ExtensionConfig<ExtendedOptions, ExtendedStorage>> = {},
478
- ) {
479
- const extension = new Extension<ExtendedOptions, ExtendedStorage>({ ...this.config, ...extendedConfig })
480
-
481
- extension.parent = this
482
-
483
- this.child = extension
484
-
485
- extension.name = extendedConfig.name ? extendedConfig.name : extension.parent.name
486
-
487
- if (extendedConfig.defaultOptions && Object.keys(extendedConfig.defaultOptions).length > 0) {
488
- console.warn(
489
- `[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`,
490
- )
491
- }
492
-
493
- extension.options = callOrReturn(
494
- getExtensionField<AnyConfig['addOptions']>(extension, 'addOptions', {
495
- name: extension.name,
496
- }),
497
- )
498
-
499
- extension.storage = callOrReturn(
500
- getExtensionField<AnyConfig['addStorage']>(extension, 'addStorage', {
501
- name: extension.name,
502
- options: extension.options,
503
- }),
504
- )
505
-
506
- return extension
507
- }
508
17
  }