@blocknote/core 0.47.3 → 0.48.1

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 (121) hide show
  1. package/dist/BlockNoteExtension-B6RhuQ3V.js +27 -0
  2. package/dist/BlockNoteExtension-B6RhuQ3V.js.map +1 -0
  3. package/dist/BlockNoteExtension-ab0REuob.cjs +2 -0
  4. package/dist/BlockNoteExtension-ab0REuob.cjs.map +1 -0
  5. package/dist/EventEmitter-CuPnGDYt.js +22 -0
  6. package/dist/EventEmitter-CuPnGDYt.js.map +1 -0
  7. package/dist/EventEmitter-DHdSnYCD.cjs +2 -0
  8. package/dist/EventEmitter-DHdSnYCD.cjs.map +1 -0
  9. package/dist/ShowSelection-BsHFfdMi.js +29 -0
  10. package/dist/ShowSelection-BsHFfdMi.js.map +1 -0
  11. package/dist/ShowSelection-DhXglbt8.cjs +2 -0
  12. package/dist/ShowSelection-DhXglbt8.cjs.map +1 -0
  13. package/dist/blocknote.cjs +1 -5
  14. package/dist/blocknote.js +7 -4256
  15. package/dist/blocks-BEvIOzln.cjs +11 -0
  16. package/dist/blocks-BEvIOzln.cjs.map +1 -0
  17. package/dist/blocks-DYl7UTS4.js +3313 -0
  18. package/dist/blocks-DYl7UTS4.js.map +1 -0
  19. package/dist/blocks.cjs +1 -2
  20. package/dist/blocks.js +2 -74
  21. package/dist/comments.cjs +2 -2
  22. package/dist/comments.cjs.map +1 -1
  23. package/dist/comments.js +604 -900
  24. package/dist/comments.js.map +1 -1
  25. package/dist/en-C4ykW581.cjs +2 -0
  26. package/dist/en-C4ykW581.cjs.map +1 -0
  27. package/dist/en-QsSI16s5.js +453 -0
  28. package/dist/en-QsSI16s5.js.map +1 -0
  29. package/dist/extensions-3nf9PPhA.js +1495 -0
  30. package/dist/extensions-3nf9PPhA.js.map +1 -0
  31. package/dist/extensions-D9y6evn6.cjs +2 -0
  32. package/dist/extensions-D9y6evn6.cjs.map +1 -0
  33. package/dist/extensions.cjs +1 -2
  34. package/dist/extensions.js +4 -36
  35. package/dist/locales.cjs +2 -2
  36. package/dist/locales.cjs.map +1 -1
  37. package/dist/locales.js +10049 -9111
  38. package/dist/locales.js.map +1 -1
  39. package/dist/src-BKJH7Jla.cjs +5 -0
  40. package/dist/src-BKJH7Jla.cjs.map +1 -0
  41. package/dist/src-Hjv1f5lc.js +2386 -0
  42. package/dist/src-Hjv1f5lc.js.map +1 -0
  43. package/dist/style.css +2 -1
  44. package/dist/webpack-stats.json +1 -1
  45. package/dist/yjs.cjs +2 -2
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +20 -38
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +8 -8
  50. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts +15 -1
  51. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts +5 -0
  52. package/src/api/blockManipulation/commands/nestBlock/__snapshots__/nestBlock.test.ts.snap +1095 -0
  53. package/src/api/blockManipulation/commands/nestBlock/nestBlock.test.ts +661 -0
  54. package/src/api/blockManipulation/commands/nestBlock/nestBlock.ts +128 -11
  55. package/src/blocks/File/helpers/render/createResizableFileBlockWrapper.ts +17 -23
  56. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +346 -0
  57. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +143 -71
  58. package/src/editor/Block.css +5 -8
  59. package/src/editor/BlockNoteEditor.ts +38 -1
  60. package/src/editor/editor.css +0 -20
  61. package/src/editor/managers/ExtensionManager/extensions.ts +12 -1
  62. package/src/editor/performance.test.ts +134 -0
  63. package/src/extensions/Collaboration/ForkYDoc.test.ts +115 -103
  64. package/src/extensions/Placeholder/Placeholder.ts +1 -1
  65. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +86 -0
  66. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +33 -49
  67. package/src/extensions/SideMenu/SideMenu.ts +14 -8
  68. package/src/extensions/TableHandles/TableHandles.ts +14 -0
  69. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +57 -17
  70. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +9 -8
  71. package/src/index.ts +1 -1
  72. package/src/schema/blocks/createSpec.ts +6 -2
  73. package/src/schema/inlineContent/types.ts +0 -1
  74. package/src/vite-env.d.ts +6 -0
  75. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.d.ts +14 -1
  76. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.test.d.ts +1 -0
  77. package/types/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.d.ts +1 -0
  78. package/types/src/editor/BlockNoteEditor.d.ts +13 -0
  79. package/types/src/editor/performance.test.d.ts +1 -0
  80. package/types/src/extensions/PreviousBlockType/PreviousBlockType.test.d.ts +1 -0
  81. package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -0
  82. package/types/src/extensions/TableHandles/TableHandles.d.ts +6 -0
  83. package/dist/BlockNoteExtension-BWw0r8Gy.cjs +0 -2
  84. package/dist/BlockNoteExtension-BWw0r8Gy.cjs.map +0 -1
  85. package/dist/BlockNoteExtension-C2X7LW-V.js +0 -25
  86. package/dist/BlockNoteExtension-C2X7LW-V.js.map +0 -1
  87. package/dist/BlockNoteSchema-CCs_V3lo.cjs +0 -2
  88. package/dist/BlockNoteSchema-CCs_V3lo.cjs.map +0 -1
  89. package/dist/BlockNoteSchema-ooiKsd5B.js +0 -270
  90. package/dist/BlockNoteSchema-ooiKsd5B.js.map +0 -1
  91. package/dist/EventEmitter-CLwfmbqG.cjs +0 -2
  92. package/dist/EventEmitter-CLwfmbqG.cjs.map +0 -1
  93. package/dist/EventEmitter-CjSwpTbz.js +0 -27
  94. package/dist/EventEmitter-CjSwpTbz.js.map +0 -1
  95. package/dist/ShowSelection-B0ch3unP.js +0 -51
  96. package/dist/ShowSelection-B0ch3unP.js.map +0 -1
  97. package/dist/ShowSelection-BxnbRvy4.cjs +0 -2
  98. package/dist/ShowSelection-BxnbRvy4.cjs.map +0 -1
  99. package/dist/TrailingNode-GzE59m_7.js +0 -2288
  100. package/dist/TrailingNode-GzE59m_7.js.map +0 -1
  101. package/dist/TrailingNode-n0WdMPUl.cjs +0 -2
  102. package/dist/TrailingNode-n0WdMPUl.cjs.map +0 -1
  103. package/dist/blockToNode-BNoNIXU7.js +0 -1140
  104. package/dist/blockToNode-BNoNIXU7.js.map +0 -1
  105. package/dist/blockToNode-CumVjgem.cjs +0 -7
  106. package/dist/blockToNode-CumVjgem.cjs.map +0 -1
  107. package/dist/blocknote.cjs.map +0 -1
  108. package/dist/blocknote.js.map +0 -1
  109. package/dist/blocks.cjs.map +0 -1
  110. package/dist/blocks.js.map +0 -1
  111. package/dist/defaultBlocks-Dg9kQWXm.cjs +0 -6
  112. package/dist/defaultBlocks-Dg9kQWXm.cjs.map +0 -1
  113. package/dist/defaultBlocks-ZzGbYgQn.js +0 -3284
  114. package/dist/defaultBlocks-ZzGbYgQn.js.map +0 -1
  115. package/dist/en-Cl87Uuyf.cjs +0 -2
  116. package/dist/en-Cl87Uuyf.cjs.map +0 -1
  117. package/dist/en-njEqD7AG.js +0 -408
  118. package/dist/en-njEqD7AG.js.map +0 -1
  119. package/dist/extensions.cjs.map +0 -1
  120. package/dist/extensions.js.map +0 -1
  121. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -17,7 +17,10 @@ type DecoSpec = {
17
17
  type Deco = Omit<Decoration, "spec"> & { spec: DecoSpec };
18
18
 
19
19
  /**
20
- * Calculate the index for a numbered list item based on its position and previous siblings
20
+ * Calculate the index for a numbered list item based on its position and previous siblings.
21
+ * Iteratively walks backwards to find the start of the contiguous list (or a cached entry),
22
+ * then walks forward to populate the cache. This avoids deep recursion that would overflow
23
+ * the stack on large documents.
21
24
  */
22
25
  function calculateListItemIndex(
23
26
  node: Node,
@@ -25,52 +28,93 @@ function calculateListItemIndex(
25
28
  tr: Transaction,
26
29
  map: Map<Node, number>,
27
30
  ): { index: number; isFirst: boolean; hasStart: boolean } {
28
- let index: number = node.firstChild!.attrs["start"] || 1;
29
- let isFirst = true;
30
31
  const hasStart = !!node.firstChild!.attrs["start"];
31
32
 
32
- const blockInfo = getBlockInfo({
33
- posBeforeNode: pos,
34
- node,
35
- });
36
-
33
+ // Fast path: previous sibling already in cache
34
+ const blockInfo = getBlockInfo({ posBeforeNode: pos, node });
37
35
  if (!blockInfo.isBlockContainer) {
38
36
  throw new Error("impossible");
39
37
  }
40
-
41
- // Check if this block is the start of a new ordered list
42
38
  const prevBlock = tr.doc.resolve(blockInfo.bnBlock.beforePos).nodeBefore;
43
39
  const prevBlockIndex = prevBlock ? map.get(prevBlock) : undefined;
44
-
45
40
  if (prevBlockIndex !== undefined) {
46
- index = prevBlockIndex + 1;
47
- isFirst = false;
48
- } else if (prevBlock) {
49
- // Because we only check the affected ranges, we may need to walk backwards to find the previous block's index
50
- // We can't just rely on the map, because the map is reset every `apply` call
51
- const prevBlockInfo = getBlockInfo({
52
- posBeforeNode: blockInfo.bnBlock.beforePos - prevBlock.nodeSize,
53
- node: prevBlock,
41
+ const index = prevBlockIndex + 1;
42
+ map.set(node, index);
43
+ return { index, isFirst: false, hasStart };
44
+ }
45
+
46
+ // Walk backwards iteratively to collect the chain of consecutive
47
+ // numbered list items until we hit a cached entry, a non-list block,
48
+ // or the start of the parent.
49
+ const chain: { node: Node; pos: number }[] = [{ node, pos }];
50
+ let curNode = prevBlock;
51
+ let curBeforePos = blockInfo.bnBlock.beforePos;
52
+
53
+ while (curNode) {
54
+ const cachedIndex = map.get(curNode);
55
+ if (cachedIndex !== undefined) {
56
+ // Found a cached predecessor — start counting from here
57
+ break;
58
+ }
59
+ const curInfo = getBlockInfo({
60
+ posBeforeNode: curBeforePos - curNode.nodeSize,
61
+ node: curNode,
54
62
  });
63
+ if (curInfo.blockNoteType !== "numberedListItem") {
64
+ break;
65
+ }
66
+ chain.push({ node: curNode, pos: curBeforePos - curNode.nodeSize });
67
+ const nextPrev = tr.doc.resolve(curInfo.bnBlock.beforePos).nodeBefore;
68
+ curBeforePos = curInfo.bnBlock.beforePos;
69
+ curNode = nextPrev;
70
+ }
71
+
72
+ // Walk forward (reverse of the collected chain) to assign indices
73
+ // The last element in chain is the furthest predecessor
74
+ let index: number;
75
+ let isFirst: boolean;
76
+
77
+ // Determine starting index from the block just before the chain
78
+ const lastInChain = chain[chain.length - 1];
79
+ const lastInfo = getBlockInfo({
80
+ posBeforeNode: lastInChain.pos,
81
+ node: lastInChain.node,
82
+ });
83
+ if (!lastInfo.isBlockContainer) {
84
+ throw new Error("impossible");
85
+ }
86
+ const predecessorNode = tr.doc.resolve(lastInfo.bnBlock.beforePos).nodeBefore;
87
+ const predecessorIndex = predecessorNode
88
+ ? map.get(predecessorNode)
89
+ : undefined;
90
+
91
+ if (predecessorIndex !== undefined) {
92
+ index = predecessorIndex;
93
+ isFirst = false;
94
+ } else {
95
+ // Start of a new list
96
+ index = (lastInChain.node.firstChild!.attrs["start"] || 1) - 1;
97
+ isFirst = true;
98
+ }
55
99
 
56
- const isPrevBlockOrderedListItem =
57
- prevBlockInfo.blockNoteType === "numberedListItem";
58
- if (isPrevBlockOrderedListItem) {
59
- // recurse to get the index of the previous block
60
- const itemIndex = calculateListItemIndex(
61
- prevBlock,
62
- blockInfo.bnBlock.beforePos - prevBlock.nodeSize,
63
- tr,
64
- map,
65
- );
66
- index = itemIndex.index + 1;
100
+ // Assign indices from the end of the chain (furthest back) to the front (original node)
101
+ for (let i = chain.length - 1; i >= 0; i--) {
102
+ const entry = chain[i];
103
+ if (isFirst && i < chain.length - 1) {
104
+ // Only the very first item in the list gets isFirst
67
105
  isFirst = false;
68
106
  }
107
+ index++;
108
+ map.set(entry.node, index);
69
109
  }
70
- // Note: we set the map late, so that when we recurse, we can rely on the map to get the previous block's index in one lookup
71
- map.set(node, index);
72
110
 
73
- return { index, isFirst, hasStart };
111
+ // isFirst is true only for the very first item in a new list:
112
+ // chain.length > 1 means we found predecessor list items, so not first.
113
+ return {
114
+ index,
115
+ isFirst: chain.length === 1 ? isFirst || predecessorIndex === undefined : false,
116
+ hasStart,
117
+ };
74
118
  }
75
119
 
76
120
  /**
@@ -87,42 +131,74 @@ function getDecorations(
87
131
  tr.mapping,
88
132
  tr.doc,
89
133
  );
134
+
135
+ // Find the start of the first change to limit traversal scope.
136
+ // We only need to check from the change point forward, since earlier
137
+ // blocks are unaffected and their mapped decorations remain correct.
138
+ const range = tr.changedRange();
139
+ if (!range) {
140
+ return { decorations: nextDecorationSet };
141
+ }
90
142
  const decorationsToAdd = [] as Deco[];
91
143
 
92
- tr.doc.nodesBetween(0, tr.doc.nodeSize - 2, (node, pos) => {
93
- if (
94
- node.type.name === "blockContainer" &&
95
- node.firstChild!.type.name === "numberedListItem"
96
- ) {
97
- const { index, isFirst, hasStart } = calculateListItemIndex(
98
- node,
99
- pos,
100
- tr,
101
- map,
102
- );
103
-
104
- // Check if decoration already exists with the same properties (for perf reasons)
105
- const existingDecorations = nextDecorationSet.find(
106
- pos,
107
- pos + node.nodeSize,
108
- (deco: DecoSpec) =>
109
- deco.index === index &&
110
- deco.isFirst === isFirst &&
111
- deco.hasStart === hasStart,
112
- );
113
-
114
- if (existingDecorations.length === 0) {
115
- const blockNode = tr.doc.nodeAt(pos + 1);
116
- // Create a widget decoration to display the index
117
- decorationsToAdd.push(
118
- // move in by 1 to account for the block container
119
- Decoration.node(pos + 1, pos + 1 + blockNode!.nodeSize, {
120
- "data-index": index.toString(),
121
- }),
144
+ // Track blockGroups where we've verified a decoration match past the
145
+ // changed range. Within a single blockGroup, indices are sequential —
146
+ // if one matches, all subsequent siblings must too. But sibling items
147
+ // in *other* blockGroups (e.g. nested lists) are independent.
148
+ const completedGroups = new Set<Node>();
149
+
150
+ tr.doc.nodesBetween(
151
+ range.from,
152
+ tr.doc.nodeSize - 2,
153
+ (node, pos, parent) => {
154
+ if (parent && completedGroups.has(parent)) {
155
+ return false;
156
+ }
157
+
158
+ if (
159
+ node.type.name === "blockContainer" &&
160
+ node.firstChild!.type.name === "numberedListItem"
161
+ ) {
162
+ const { index, isFirst, hasStart } = calculateListItemIndex(
163
+ node,
164
+ pos,
165
+ tr,
166
+ map,
122
167
  );
168
+
169
+ // Search only the numberedListItem node range, not the full
170
+ // blockContainer (which includes nested blockGroups whose
171
+ // decorations could falsely match).
172
+ const blockNode = tr.doc.nodeAt(pos + 1)!;
173
+ const existingDecorations = nextDecorationSet.find(
174
+ pos + 1,
175
+ pos + 1 + blockNode.nodeSize,
176
+ (deco: DecoSpec) =>
177
+ deco.index === index &&
178
+ deco.isFirst === isFirst &&
179
+ deco.hasStart === hasStart,
180
+ );
181
+
182
+ if (existingDecorations.length === 0) {
183
+ decorationsToAdd.push(
184
+ Decoration.node(
185
+ pos + 1,
186
+ pos + 1 + blockNode.nodeSize,
187
+ { "data-index": index.toString() },
188
+ { index, isFirst, hasStart },
189
+ ) as Deco,
190
+ );
191
+ } else if (pos >= range.to && parent) {
192
+ // Past the changed range and decoration matches in this blockGroup:
193
+ // all subsequent siblings must also match. Mark group as done and
194
+ // skip this node's children (nested lists are unaffected too).
195
+ completedGroups.add(parent);
196
+ return false;
197
+ }
123
198
  }
124
- }
125
- });
199
+ return undefined;
200
+ },
201
+ );
126
202
 
127
203
  // Remove any decorations that exist at the same position, they will be replaced by the new decorations
128
204
  const decorationsToRemove = decorationsToAdd.flatMap((deco) =>
@@ -153,12 +229,8 @@ export const NumberedListIndexingDecorationPlugin = () => {
153
229
  });
154
230
  },
155
231
  apply(tr, previousPluginState) {
156
- if (
157
- !tr.docChanged &&
158
- !tr.selectionSet &&
159
- previousPluginState.decorations
160
- ) {
161
- // Just reuse the existing decorations, since nothing should have changed
232
+ if (!tr.docChanged && previousPluginState.decorations) {
233
+ // Selection-only changes don't affect list indices, just reuse existing decorations
162
234
  return previousPluginState;
163
235
  }
164
236
  return getDecorations(tr, previousPluginState);
@@ -41,7 +41,6 @@ BASIC STYLES
41
41
  }
42
42
 
43
43
  .bn-inline-content {
44
- width: 100%;
45
44
  /* Ensure pre-wrap even when a parent node view wrapper (e.g. tiptap's
46
45
  NodeViewWrapper) resets white-space to "normal". Without this, browsers
47
46
  normalize trailing spaces to NBSP on input, which causes ProseMirror to
@@ -552,15 +551,13 @@ NESTED BLOCKS
552
551
  }
553
552
 
554
553
  /* PLACEHOLDERS*/
555
- .bn-inline-content:has(> .ProseMirror-trailingBreak:only-child):before {
556
- /*float: left; */
557
- pointer-events: none;
558
- height: 0;
559
- /* width: 0; */
560
- position: absolute;
554
+ .bn-block-content:has(.ProseMirror-trailingBreak:only-child):after {
561
555
  font-style: italic;
556
+ /* Removes text cursor offset. */
557
+ margin-inline: -2px;
558
+ pointer-events: none;
559
+ max-width: 100%;
562
560
  }
563
- /* TODO: should this be here? */
564
561
 
565
562
  /* TEXT COLORS */
566
563
  [data-style-type="textColor"][data-value="gray"],
@@ -423,7 +423,6 @@ export class BlockNoteEditor<
423
423
  },
424
424
  };
425
425
 
426
- // @ts-ignore
427
426
  this.schema = newOptions.schema;
428
427
  this.blockImplementations = newOptions.schema.blockSpecs;
429
428
  this.inlineContentImplementations = newOptions.schema.inlineContentSpecs;
@@ -680,6 +679,12 @@ export class BlockNoteEditor<
680
679
  * @warning Not needed to call manually when using React, use BlockNoteView to take care of mounting
681
680
  */
682
681
  public mount = (element: HTMLElement) => {
682
+ const root = element.getRootNode();
683
+ if (typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot) {
684
+ root.appendChild(this.portalElement);
685
+ } else {
686
+ document.body.appendChild(this.portalElement);
687
+ }
683
688
  this._tiptapEditor.mount({ mount: element });
684
689
  };
685
690
 
@@ -687,6 +692,7 @@ export class BlockNoteEditor<
687
692
  * Unmount the editor from the DOM element it is bound to
688
693
  */
689
694
  public unmount = () => {
695
+ this.portalElement?.remove();
690
696
  this._tiptapEditor.unmount();
691
697
  };
692
698
 
@@ -714,6 +720,37 @@ export class BlockNoteEditor<
714
720
  return this.prosemirrorView?.dom as HTMLDivElement | undefined;
715
721
  }
716
722
 
723
+ private _portalElement: HTMLElement | undefined;
724
+
725
+ /**
726
+ * The portal container element at `document.body` used by floating UI
727
+ * elements (menus, toolbars) to escape overflow:hidden ancestors.
728
+ * Set by BlockNoteView; undefined in headless mode.
729
+ */
730
+ public get portalElement() {
731
+ if (typeof document === "undefined") {
732
+ throw new Error(
733
+ "Portal element accessed, but not available in headless mode",
734
+ );
735
+ }
736
+ if (!this._portalElement) {
737
+ this._portalElement = document.createElement("div");
738
+ }
739
+ return this._portalElement;
740
+ }
741
+
742
+ /**
743
+ * Checks whether a DOM element belongs to this editor — either inside the
744
+ * editor's DOM tree or inside its portal container (used for floating UI
745
+ * elements like menus and toolbars).
746
+ */
747
+ public isWithinEditor = (element: Element): boolean => {
748
+ return !!(
749
+ this.domElement?.parentElement?.contains(element) ||
750
+ this.portalElement?.contains(element)
751
+ );
752
+ };
753
+
717
754
  public isFocused() {
718
755
  if (this.headless) {
719
756
  return false;
@@ -20,26 +20,6 @@
20
20
  padding: 0;
21
21
  }
22
22
 
23
- /*
24
- bn-root should be applied to all top-level elements
25
-
26
- This includes the Prosemirror editor, but also <div> element such as
27
- Tippy popups that are appended to document.body directly
28
- */
29
- .bn-root {
30
- -webkit-box-sizing: border-box;
31
- -moz-box-sizing: border-box;
32
- box-sizing: border-box;
33
- }
34
-
35
- .bn-root *,
36
- .bn-root *::before,
37
- .bn-root *::after {
38
- -webkit-box-sizing: inherit;
39
- -moz-box-sizing: inherit;
40
- box-sizing: inherit;
41
- }
42
-
43
23
  /* reset styles, they will be set on blockContent */
44
24
  .bn-default-styles p,
45
25
  .bn-default-styles h1,
@@ -71,6 +71,7 @@ export function getDefaultTiptapExtensions(
71
71
  // everything from bnBlock group (nodes that represent a BlockNote block should have an id)
72
72
  types: ["blockContainer", "columnList", "column"],
73
73
  setIdAttribute: options.setIdAttribute,
74
+ isWithinEditor: editor.isWithinEditor,
74
75
  }),
75
76
  HardBreak,
76
77
  Text,
@@ -81,7 +82,17 @@ export function getDefaultTiptapExtensions(
81
82
  SuggestionModificationMark,
82
83
  Link.extend({
83
84
  inclusive: false,
84
- }).configure({
85
+ })
86
+ .extend({
87
+ // Remove the title attribute added in newer versions of @tiptap/extension-link
88
+ // to avoid unnecessary null attributes in serialized output
89
+ addAttributes() {
90
+ const attrs = this.parent?.() || {};
91
+ delete (attrs as Record<string, unknown>).title;
92
+ return attrs;
93
+ },
94
+ })
95
+ .configure({
85
96
  defaultProtocol: DEFAULT_LINK_PROTOCOL,
86
97
  // only call this once if we have multiple editors installed. Or fix https://github.com/ueberdosis/tiptap/issues/5450
87
98
  protocols: LINKIFY_INITIALIZED ? [] : VALID_LINK_PROTOCOLS,
@@ -0,0 +1,134 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { BlockNoteEditor } from "./BlockNoteEditor.js";
4
+
5
+ /**
6
+ * @vitest-environment jsdom
7
+ */
8
+
9
+ /**
10
+ * Performance regression tests for issue #2595:
11
+ * Typing/echo lag with many blocks (~50k chars total).
12
+ *
13
+ * The root cause was several plugins performing O(n) full-document traversals
14
+ * on every ProseMirror transaction, where n = total number of blocks.
15
+ * The fix limits traversals to only the changed ranges.
16
+ *
17
+ * These tests verify the optimization by:
18
+ * 1. Checking plugin state reflects scoped (not full) traversal
19
+ * 2. Measuring that transaction time scales sub-linearly with doc size
20
+ */
21
+
22
+ function createEditorWithBlocks(
23
+ blockCount: number,
24
+ blockType: "heading" | "paragraph" | "numberedListItem" = "heading",
25
+ ) {
26
+ const editor = BlockNoteEditor.create();
27
+ editor.mount(document.createElement("div"));
28
+ const blocks = [];
29
+ for (let i = 0; i < blockCount; i++) {
30
+ blocks.push({
31
+ type: blockType as any,
32
+ content: `Block number ${i} with some text to simulate a real document`,
33
+ ...(blockType === "heading" ? { props: { level: 1 } } : {}),
34
+ });
35
+ }
36
+ editor.replaceBlocks(editor.document, blocks as any);
37
+ return editor;
38
+ }
39
+
40
+ describe("Performance: transaction processing scales sub-linearly (#2595)", () => {
41
+ // Compare timing between a small and large document.
42
+ // At 10k blocks the ratio is dominated by ProseMirror's DecorationSet.map()
43
+ // which is inherently O(n). The thresholds verify BlockNote plugins don't
44
+ // add significant overhead on top of that.
45
+ const SMALL = 100;
46
+ const LARGE = 5000;
47
+ const ITERATIONS = 5;
48
+
49
+ function measureAvgInsertTime(
50
+ editor: BlockNoteEditor<any, any, any>,
51
+ pos: number,
52
+ ) {
53
+ const view = editor._tiptapEditor.view;
54
+ // Warm up
55
+ view.dispatch(view.state.tr.insertText("w", pos));
56
+
57
+ const start = performance.now();
58
+ for (let i = 0; i < ITERATIONS; i++) {
59
+ view.dispatch(view.state.tr.insertText("x", pos));
60
+ }
61
+ return (performance.now() - start) / ITERATIONS;
62
+ }
63
+
64
+ it("heading blocks: typing at end", { timeout: 30_000 }, () => {
65
+ const smallEditor = createEditorWithBlocks(SMALL, "heading");
66
+ const largeEditor = createEditorWithBlocks(LARGE, "heading");
67
+
68
+ const smallAvg = measureAvgInsertTime(
69
+ smallEditor,
70
+ smallEditor._tiptapEditor.view.state.doc.content.size - 4,
71
+ );
72
+ const largeAvg = measureAvgInsertTime(
73
+ largeEditor,
74
+ largeEditor._tiptapEditor.view.state.doc.content.size - 4,
75
+ );
76
+ const ratio = largeAvg / smallAvg;
77
+
78
+ // eslint-disable-next-line no-console
79
+ console.log(
80
+ `Heading blocks (end): ${SMALL}=${smallAvg.toFixed(3)}ms, ${LARGE}=${largeAvg.toFixed(3)}ms, ratio=${ratio.toFixed(2)}x`,
81
+ );
82
+
83
+ // Ratio scales roughly linearly with block count due to ProseMirror
84
+ // DOM update costs. At 10k/100 = 100x blocks, expect <50x ratio.
85
+ expect(ratio).toBeLessThan(50);
86
+ });
87
+
88
+ it("numbered list items: typing at beginning", { timeout: 30_000 }, () => {
89
+ const smallEditor = createEditorWithBlocks(SMALL, "numberedListItem");
90
+ const largeEditor = createEditorWithBlocks(LARGE, "numberedListItem");
91
+
92
+ // Position 4 = inside the first block's content
93
+ const smallAvg = measureAvgInsertTime(smallEditor, 4);
94
+ const largeAvg = measureAvgInsertTime(largeEditor, 4);
95
+ const ratio = largeAvg / smallAvg;
96
+
97
+ // eslint-disable-next-line no-console
98
+ console.log(
99
+ `Numbered list (begin): ${SMALL}=${smallAvg.toFixed(3)}ms, ${LARGE}=${largeAvg.toFixed(3)}ms, ratio=${ratio.toFixed(2)}x`,
100
+ );
101
+
102
+ // Typing doesn't change indices, so the plugin exits early once it
103
+ // verifies the first decoration past the changed range still matches.
104
+ // Remaining cost is DecorationSet.map() which is O(n decorations).
105
+ // Ratio appears high (~200x) because the 100-block baseline is very
106
+ // fast for begin-of-doc edits. Absolute time (~32ms) is comparable
107
+ // to end-of-doc (~40ms) — both dominated by DecorationSet.map().
108
+ expect(ratio).toBeLessThan(250);
109
+ });
110
+
111
+ it("numbered list items: typing at end", { timeout: 30_000 }, () => {
112
+ const smallEditor = createEditorWithBlocks(SMALL, "numberedListItem");
113
+ const largeEditor = createEditorWithBlocks(LARGE, "numberedListItem");
114
+
115
+ const smallAvg = measureAvgInsertTime(
116
+ smallEditor,
117
+ smallEditor._tiptapEditor.view.state.doc.content.size - 4,
118
+ );
119
+ const largeAvg = measureAvgInsertTime(
120
+ largeEditor,
121
+ largeEditor._tiptapEditor.view.state.doc.content.size - 4,
122
+ );
123
+ const ratio = largeAvg / smallAvg;
124
+
125
+ // eslint-disable-next-line no-console
126
+ console.log(
127
+ `Numbered list (end): ${SMALL}=${smallAvg.toFixed(3)}ms, ${LARGE}=${largeAvg.toFixed(3)}ms, ratio=${ratio.toFixed(2)}x`,
128
+ );
129
+
130
+ // DecorationSet.map() on 10k decorations is O(n) in ProseMirror.
131
+ // Absolute time (~40ms) is comparable to begin (~32ms).
132
+ expect(ratio).toBeLessThan(250);
133
+ });
134
+ });