@blocknote/core 0.47.2 → 0.48.0

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 (124) 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 -4216
  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/api/parsers/html/parseHTML.ts +2 -0
  56. package/src/api/parsers/html/util/normalizeWhitespace.ts +87 -0
  57. package/src/blocks/File/helpers/render/createResizableFileBlockWrapper.ts +17 -23
  58. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +346 -0
  59. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +143 -71
  60. package/src/editor/Block.css +5 -8
  61. package/src/editor/BlockNoteEditor.ts +38 -1
  62. package/src/editor/editor.css +0 -20
  63. package/src/editor/managers/ExtensionManager/extensions.ts +12 -1
  64. package/src/editor/performance.test.ts +134 -0
  65. package/src/extensions/Collaboration/ForkYDoc.test.ts +115 -103
  66. package/src/extensions/Placeholder/Placeholder.ts +1 -1
  67. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +86 -0
  68. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +33 -49
  69. package/src/extensions/SideMenu/SideMenu.ts +14 -8
  70. package/src/extensions/TableHandles/TableHandles.ts +14 -0
  71. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +57 -17
  72. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +9 -8
  73. package/src/index.ts +1 -1
  74. package/src/schema/blocks/createSpec.ts +6 -2
  75. package/src/schema/inlineContent/types.ts +0 -1
  76. package/src/vite-env.d.ts +6 -0
  77. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.d.ts +14 -1
  78. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.test.d.ts +1 -0
  79. package/types/src/api/parsers/html/util/normalizeWhitespace.d.ts +6 -0
  80. package/types/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.d.ts +1 -0
  81. package/types/src/editor/BlockNoteEditor.d.ts +13 -0
  82. package/types/src/editor/performance.test.d.ts +1 -0
  83. package/types/src/extensions/PreviousBlockType/PreviousBlockType.test.d.ts +1 -0
  84. package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -0
  85. package/types/src/extensions/TableHandles/TableHandles.d.ts +6 -0
  86. package/dist/BlockNoteExtension-BWw0r8Gy.cjs +0 -2
  87. package/dist/BlockNoteExtension-BWw0r8Gy.cjs.map +0 -1
  88. package/dist/BlockNoteExtension-C2X7LW-V.js +0 -25
  89. package/dist/BlockNoteExtension-C2X7LW-V.js.map +0 -1
  90. package/dist/BlockNoteSchema-CCs_V3lo.cjs +0 -2
  91. package/dist/BlockNoteSchema-CCs_V3lo.cjs.map +0 -1
  92. package/dist/BlockNoteSchema-ooiKsd5B.js +0 -270
  93. package/dist/BlockNoteSchema-ooiKsd5B.js.map +0 -1
  94. package/dist/EventEmitter-CLwfmbqG.cjs +0 -2
  95. package/dist/EventEmitter-CLwfmbqG.cjs.map +0 -1
  96. package/dist/EventEmitter-CjSwpTbz.js +0 -27
  97. package/dist/EventEmitter-CjSwpTbz.js.map +0 -1
  98. package/dist/ShowSelection-B0ch3unP.js +0 -51
  99. package/dist/ShowSelection-B0ch3unP.js.map +0 -1
  100. package/dist/ShowSelection-BxnbRvy4.cjs +0 -2
  101. package/dist/ShowSelection-BxnbRvy4.cjs.map +0 -1
  102. package/dist/TrailingNode-GzE59m_7.js +0 -2288
  103. package/dist/TrailingNode-GzE59m_7.js.map +0 -1
  104. package/dist/TrailingNode-n0WdMPUl.cjs +0 -2
  105. package/dist/TrailingNode-n0WdMPUl.cjs.map +0 -1
  106. package/dist/blockToNode-BNoNIXU7.js +0 -1140
  107. package/dist/blockToNode-BNoNIXU7.js.map +0 -1
  108. package/dist/blockToNode-CumVjgem.cjs +0 -7
  109. package/dist/blockToNode-CumVjgem.cjs.map +0 -1
  110. package/dist/blocknote.cjs.map +0 -1
  111. package/dist/blocknote.js.map +0 -1
  112. package/dist/blocks.cjs.map +0 -1
  113. package/dist/blocks.js.map +0 -1
  114. package/dist/defaultBlocks-Dg9kQWXm.cjs +0 -6
  115. package/dist/defaultBlocks-Dg9kQWXm.cjs.map +0 -1
  116. package/dist/defaultBlocks-ZzGbYgQn.js +0 -3284
  117. package/dist/defaultBlocks-ZzGbYgQn.js.map +0 -1
  118. package/dist/en-Cl87Uuyf.cjs +0 -2
  119. package/dist/en-Cl87Uuyf.cjs.map +0 -1
  120. package/dist/en-njEqD7AG.js +0 -408
  121. package/dist/en-njEqD7AG.js.map +0 -1
  122. package/dist/extensions.cjs.map +0 -1
  123. package/dist/extensions.js.map +0 -1
  124. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -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
+ });
@@ -20,38 +20,42 @@ it("can fork a document", async () => {
20
20
  },
21
21
  });
22
22
 
23
- const div = document.createElement("div");
24
- editor.mount(div);
25
-
26
- editor.replaceBlocks(editor.document, [
27
- {
28
- type: "paragraph",
29
- content: [{ text: "Hello", styles: {}, type: "text" }],
30
- },
31
- ]);
23
+ try {
24
+ const div = document.createElement("div");
25
+ editor.mount(div);
26
+
27
+ editor.replaceBlocks(editor.document, [
28
+ {
29
+ type: "paragraph",
30
+ content: [{ text: "Hello", styles: {}, type: "text" }],
31
+ },
32
+ ]);
32
33
 
33
- await expect(fragment.toJSON()).toMatchFileSnapshot(
34
- "__snapshots__/fork-yjs-snap.html",
35
- );
36
- await expect(editor.document).toMatchFileSnapshot(
37
- "__snapshots__/fork-yjs-snap-editor.json",
38
- );
34
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
35
+ "__snapshots__/fork-yjs-snap.html",
36
+ );
37
+ await expect(editor.document).toMatchFileSnapshot(
38
+ "__snapshots__/fork-yjs-snap-editor.json",
39
+ );
39
40
 
40
- editor.getExtension(ForkYDocExtension)!.fork();
41
+ editor.getExtension(ForkYDocExtension)!.fork();
41
42
 
42
- editor.replaceBlocks(editor.document, [
43
- {
44
- type: "paragraph",
45
- content: [{ text: "Hello World", styles: {}, type: "text" }],
46
- },
47
- ]);
48
-
49
- await expect(fragment.toJSON()).toMatchFileSnapshot(
50
- "__snapshots__/fork-yjs-snap.html",
51
- );
52
- await expect(editor.document).toMatchFileSnapshot(
53
- "__snapshots__/fork-yjs-snap-editor-forked.json",
54
- );
43
+ editor.replaceBlocks(editor.document, [
44
+ {
45
+ type: "paragraph",
46
+ content: [{ text: "Hello World", styles: {}, type: "text" }],
47
+ },
48
+ ]);
49
+
50
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
51
+ "__snapshots__/fork-yjs-snap.html",
52
+ );
53
+ await expect(editor.document).toMatchFileSnapshot(
54
+ "__snapshots__/fork-yjs-snap-editor-forked.json",
55
+ );
56
+ } finally {
57
+ editor.unmount();
58
+ }
55
59
  });
56
60
 
57
61
  it("can merge a document", async () => {
@@ -67,47 +71,51 @@ it("can merge a document", async () => {
67
71
  },
68
72
  });
69
73
 
70
- const div = document.createElement("div");
71
- editor.mount(div);
74
+ try {
75
+ const div = document.createElement("div");
76
+ editor.mount(div);
72
77
 
73
- editor.replaceBlocks(editor.document, [
74
- {
75
- type: "paragraph",
76
- content: [{ text: "Hello", styles: {}, type: "text" }],
77
- },
78
- ]);
78
+ editor.replaceBlocks(editor.document, [
79
+ {
80
+ type: "paragraph",
81
+ content: [{ text: "Hello", styles: {}, type: "text" }],
82
+ },
83
+ ]);
79
84
 
80
- await expect(fragment.toJSON()).toMatchFileSnapshot(
81
- "__snapshots__/fork-yjs-snap.html",
82
- );
83
- await expect(editor.document).toMatchFileSnapshot(
84
- "__snapshots__/fork-yjs-snap-editor.json",
85
- );
85
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
86
+ "__snapshots__/fork-yjs-snap.html",
87
+ );
88
+ await expect(editor.document).toMatchFileSnapshot(
89
+ "__snapshots__/fork-yjs-snap-editor.json",
90
+ );
86
91
 
87
- editor.getExtension(ForkYDocExtension)!.fork();
92
+ editor.getExtension(ForkYDocExtension)!.fork();
88
93
 
89
- editor.replaceBlocks(editor.document, [
90
- {
91
- type: "paragraph",
92
- content: [{ text: "Hello World", styles: {}, type: "text" }],
93
- },
94
- ]);
95
-
96
- await expect(fragment.toJSON()).toMatchFileSnapshot(
97
- "__snapshots__/fork-yjs-snap.html",
98
- );
99
- await expect(editor.document).toMatchFileSnapshot(
100
- "__snapshots__/fork-yjs-snap-editor-forked.json",
101
- );
102
-
103
- editor.getExtension(ForkYDocExtension)!.merge({ keepChanges: false });
104
-
105
- await expect(fragment.toJSON()).toMatchFileSnapshot(
106
- "__snapshots__/fork-yjs-snap.html",
107
- );
108
- await expect(editor.document).toMatchFileSnapshot(
109
- "__snapshots__/fork-yjs-snap-editor.json",
110
- );
94
+ editor.replaceBlocks(editor.document, [
95
+ {
96
+ type: "paragraph",
97
+ content: [{ text: "Hello World", styles: {}, type: "text" }],
98
+ },
99
+ ]);
100
+
101
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
102
+ "__snapshots__/fork-yjs-snap.html",
103
+ );
104
+ await expect(editor.document).toMatchFileSnapshot(
105
+ "__snapshots__/fork-yjs-snap-editor-forked.json",
106
+ );
107
+
108
+ editor.getExtension(ForkYDocExtension)!.merge({ keepChanges: false });
109
+
110
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
111
+ "__snapshots__/fork-yjs-snap.html",
112
+ );
113
+ await expect(editor.document).toMatchFileSnapshot(
114
+ "__snapshots__/fork-yjs-snap-editor.json",
115
+ );
116
+ } finally {
117
+ editor.unmount();
118
+ }
111
119
  });
112
120
 
113
121
  it("can fork an keep the changes to the original document", async () => {
@@ -123,45 +131,49 @@ it("can fork an keep the changes to the original document", async () => {
123
131
  },
124
132
  });
125
133
 
126
- const div = document.createElement("div");
127
- editor.mount(div);
134
+ try {
135
+ const div = document.createElement("div");
136
+ editor.mount(div);
128
137
 
129
- editor.replaceBlocks(editor.document, [
130
- {
131
- type: "paragraph",
132
- content: [{ text: "Hello", styles: {}, type: "text" }],
133
- },
134
- ]);
138
+ editor.replaceBlocks(editor.document, [
139
+ {
140
+ type: "paragraph",
141
+ content: [{ text: "Hello", styles: {}, type: "text" }],
142
+ },
143
+ ]);
135
144
 
136
- await expect(fragment.toJSON()).toMatchFileSnapshot(
137
- "__snapshots__/fork-yjs-snap.html",
138
- );
139
- await expect(editor.document).toMatchFileSnapshot(
140
- "__snapshots__/fork-yjs-snap-editor.json",
141
- );
145
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
146
+ "__snapshots__/fork-yjs-snap.html",
147
+ );
148
+ await expect(editor.document).toMatchFileSnapshot(
149
+ "__snapshots__/fork-yjs-snap-editor.json",
150
+ );
142
151
 
143
- editor.getExtension(ForkYDocExtension)!.fork();
152
+ editor.getExtension(ForkYDocExtension)!.fork();
144
153
 
145
- editor.replaceBlocks(editor.document, [
146
- {
147
- type: "paragraph",
148
- content: [{ text: "Hello World", styles: {}, type: "text" }],
149
- },
150
- ]);
151
-
152
- await expect(fragment.toJSON()).toMatchFileSnapshot(
153
- "__snapshots__/fork-yjs-snap.html",
154
- );
155
- await expect(editor.document).toMatchFileSnapshot(
156
- "__snapshots__/fork-yjs-snap-editor-forked.json",
157
- );
158
-
159
- editor.getExtension(ForkYDocExtension)!.merge({ keepChanges: true });
160
-
161
- await expect(fragment.toJSON()).toMatchFileSnapshot(
162
- "__snapshots__/fork-yjs-snap-forked.html",
163
- );
164
- await expect(editor.document).toMatchFileSnapshot(
165
- "__snapshots__/fork-yjs-snap-editor-forked.json",
166
- );
154
+ editor.replaceBlocks(editor.document, [
155
+ {
156
+ type: "paragraph",
157
+ content: [{ text: "Hello World", styles: {}, type: "text" }],
158
+ },
159
+ ]);
160
+
161
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
162
+ "__snapshots__/fork-yjs-snap.html",
163
+ );
164
+ await expect(editor.document).toMatchFileSnapshot(
165
+ "__snapshots__/fork-yjs-snap-editor-forked.json",
166
+ );
167
+
168
+ editor.getExtension(ForkYDocExtension)!.merge({ keepChanges: true });
169
+
170
+ await expect(fragment.toJSON()).toMatchFileSnapshot(
171
+ "__snapshots__/fork-yjs-snap-forked.html",
172
+ );
173
+ await expect(editor.document).toMatchFileSnapshot(
174
+ "__snapshots__/fork-yjs-snap-editor-forked.json",
175
+ );
176
+ } finally {
177
+ editor.unmount();
178
+ }
167
179
  });
@@ -41,7 +41,7 @@ export const PlaceholderExtension = createExtension(
41
41
  const styleSheet = styleEl.sheet!;
42
42
 
43
43
  const getSelector = (additionalSelectors = "") =>
44
- `.${uniqueEditorSelector} .bn-block-content${additionalSelectors} .bn-inline-content:has(> .ProseMirror-trailingBreak:only-child):before`;
44
+ `.${uniqueEditorSelector} .bn-block-content${additionalSelectors}:has(.ProseMirror-trailingBreak:only-child):after`;
45
45
 
46
46
  try {
47
47
  // FIXME: the names "default" and "emptyDocument" are hardcoded
@@ -0,0 +1,86 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
4
+
5
+ /**
6
+ * @vitest-environment jsdom
7
+ */
8
+
9
+ function createEditorWithBlocks(
10
+ blockCount: number,
11
+ blockType: "heading" | "paragraph" = "heading",
12
+ ) {
13
+ const editor = BlockNoteEditor.create();
14
+ editor.mount(document.createElement("div"));
15
+ const blocks = [];
16
+ for (let i = 0; i < blockCount; i++) {
17
+ blocks.push({
18
+ type: blockType as any,
19
+ content: `Block number ${i} with some text to simulate a real document`,
20
+ ...(blockType === "heading" ? { props: { level: 1 } } : {}),
21
+ });
22
+ }
23
+ editor.replaceBlocks(editor.document, blocks as any);
24
+ return editor;
25
+ }
26
+
27
+ function getPreviousBlockTypePluginState(
28
+ editor: BlockNoteEditor<any, any, any>,
29
+ ) {
30
+ const view = editor._tiptapEditor.view;
31
+ const plugin = view.state.plugins.find((p) => {
32
+ const key = (p.spec as any)?.key;
33
+ return key && typeof key === "object" && key.key === "previous-blocks$";
34
+ });
35
+ if (!plugin) {
36
+ throw new Error("PreviousBlockType plugin not found");
37
+ }
38
+ return plugin.getState(view.state) as {
39
+ updatedBlocks: Set<string>;
40
+ currentTransactionOldBlockAttrs: Record<string, any>;
41
+ prevTransactionOldBlockAttrs: Record<string, any>;
42
+ };
43
+ }
44
+
45
+ describe("PreviousBlockType: scoped traversal", () => {
46
+ it("only tracks blocks in the changed range, not the entire document", () => {
47
+ const editor = createEditorWithBlocks(500, "heading");
48
+ const view = editor._tiptapEditor.view;
49
+
50
+ // Insert a character — block attributes (type, level, depth) don't change
51
+ const pos = view.state.doc.content.size - 4;
52
+ view.dispatch(view.state.tr.insertText("x", pos));
53
+
54
+ const state = getPreviousBlockTypePluginState(editor);
55
+
56
+ // No blocks should be marked as updated since typing doesn't change attrs
57
+ expect(state.updatedBlocks.size).toBe(0);
58
+
59
+ // The plugin should only have tracked the block(s) in the changed range,
60
+ // not all 500 blocks. Before the fix this was 500; after, typically 1-3.
61
+ const trackedBlockCount = Object.keys(
62
+ state.currentTransactionOldBlockAttrs,
63
+ ).length;
64
+ expect(trackedBlockCount).toBeLessThan(10);
65
+ });
66
+
67
+ it("correctly detects attribute changes only for affected blocks", () => {
68
+ const editor = createEditorWithBlocks(100, "heading");
69
+
70
+ // Change the first block's type from heading to paragraph
71
+ const firstBlock = editor.document[0];
72
+ editor.updateBlock(firstBlock, { type: "paragraph" });
73
+
74
+ const state = getPreviousBlockTypePluginState(editor);
75
+
76
+ // Only the first block should be marked as updated
77
+ expect(state.updatedBlocks.size).toBe(1);
78
+ expect(state.updatedBlocks.has(firstBlock.id)).toBe(true);
79
+
80
+ // Only a small number of blocks tracked (the changed range)
81
+ const trackedBlockCount = Object.keys(
82
+ state.currentTransactionOldBlockAttrs,
83
+ ).length;
84
+ expect(trackedBlockCount).toBeLessThan(10);
85
+ });
86
+ });