Skip to content

Commit f69a59f

Browse files
authored
chore: upgrade tiptap 2.5.0 (#923)
* upgrade tiptap and y-prosemirror * fix build * fix: overwrite createView of tiptapeditor separately * fix test
1 parent d897307 commit f69a59f

File tree

20 files changed

+319
-243
lines changed

20 files changed

+319
-243
lines changed

package-lock.json

Lines changed: 152 additions & 145 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,27 @@
5454
"clean": "rimraf dist && rimraf types"
5555
},
5656
"dependencies": {
57+
"@tiptap/core": "^2.5.0",
58+
"@tiptap/extension-bold": "^2.5.0",
59+
"@tiptap/extension-code": "^2.5.0",
60+
"@tiptap/extension-collaboration": "^2.5.0",
61+
"@tiptap/extension-collaboration-cursor": "^2.5.0",
62+
"@tiptap/extension-dropcursor": "^2.5.0",
63+
"@tiptap/extension-gapcursor": "^2.5.0",
64+
"@tiptap/extension-hard-break": "^2.5.0",
65+
"@tiptap/extension-history": "^2.5.0",
66+
"@tiptap/extension-horizontal-rule": "^2.5.0",
67+
"@tiptap/extension-italic": "^2.5.0",
68+
"@tiptap/extension-link": "^2.5.0",
69+
"@tiptap/extension-paragraph": "^2.5.0",
70+
"@tiptap/extension-strike": "^2.5.0",
71+
"@tiptap/extension-table-cell": "^2.5.0",
72+
"@tiptap/extension-table-header": "^2.5.0",
73+
"@tiptap/extension-table-row": "^2.5.0",
74+
"@tiptap/extension-text": "^2.5.0",
75+
"@tiptap/extension-underline": "^2.5.0",
76+
"@tiptap/pm": "^2.5.0",
5777
"@emoji-mart/data": "^1.2.1",
58-
"@tiptap/core": "^2.4.0",
59-
"@tiptap/extension-bold": "^2.4.0",
60-
"@tiptap/extension-code": "^2.4.0",
61-
"@tiptap/extension-collaboration": "^2.4.0",
62-
"@tiptap/extension-collaboration-cursor": "^2.4.0",
63-
"@tiptap/extension-dropcursor": "^2.4.0",
64-
"@tiptap/extension-gapcursor": "^2.4.0",
65-
"@tiptap/extension-hard-break": "^2.4.0",
66-
"@tiptap/extension-history": "^2.4.0",
67-
"@tiptap/extension-horizontal-rule": "^2.4.0",
68-
"@tiptap/extension-italic": "^2.4.0",
69-
"@tiptap/extension-link": "^2.4.0",
70-
"@tiptap/extension-paragraph": "^2.4.0",
71-
"@tiptap/extension-strike": "^2.4.0",
72-
"@tiptap/extension-table-cell": "^2.4.0",
73-
"@tiptap/extension-table-header": "^2.4.0",
74-
"@tiptap/extension-table-row": "^2.4.0",
75-
"@tiptap/extension-text": "^2.4.0",
76-
"@tiptap/extension-underline": "^2.4.0",
77-
"@tiptap/pm": "^2.4.0",
7878
"emoji-mart": "^5.6.0",
7979
"hast-util-from-dom": "^4.2.0",
8080
"prosemirror-model": "^1.21.0",
@@ -92,7 +92,7 @@
9292
"remark-stringify": "^10.0.2",
9393
"unified": "^10.1.2",
9494
"uuid": "^8.3.2",
95-
"y-prosemirror": "1.2.5",
95+
"y-prosemirror": "1.2.9",
9696
"y-protocols": "^1.0.6",
9797
"yjs": "^13.6.15"
9898
},

packages/core/src/api/blockManipulation/blockManipulation.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,45 @@ export function insertBlocks<
2323
placement: "before" | "after" | "nested" = "before",
2424
editor: BlockNoteEditor<BSchema, I, S>
2525
): Block<BSchema, I, S>[] {
26-
const ttEditor = editor._tiptapEditor;
27-
2826
const id =
2927
typeof referenceBlock === "string" ? referenceBlock : referenceBlock.id;
3028

3129
const nodesToInsert: Node[] = [];
3230
for (const blockSpec of blocksToInsert) {
3331
nodesToInsert.push(
34-
blockToNode(blockSpec, ttEditor.schema, editor.schema.styleSchema)
32+
blockToNode(blockSpec, editor.pmSchema, editor.schema.styleSchema)
3533
);
3634
}
3735

38-
const { node, posBeforeNode } = getNodeById(id, ttEditor.state.doc);
36+
const { node, posBeforeNode } = getNodeById(
37+
id,
38+
editor._tiptapEditor.state.doc
39+
);
3940

4041
if (placement === "before") {
41-
ttEditor.view.dispatch(
42-
ttEditor.state.tr.insert(posBeforeNode, nodesToInsert)
42+
editor.dispatch(
43+
editor._tiptapEditor.state.tr.insert(posBeforeNode, nodesToInsert)
4344
);
4445
}
4546

4647
if (placement === "after") {
47-
ttEditor.view.dispatch(
48-
ttEditor.state.tr.insert(posBeforeNode + node.nodeSize, nodesToInsert)
48+
editor.dispatch(
49+
editor._tiptapEditor.state.tr.insert(
50+
posBeforeNode + node.nodeSize,
51+
nodesToInsert
52+
)
4953
);
5054
}
5155

5256
if (placement === "nested") {
5357
// Case if block doesn't already have children.
5458
if (node.childCount < 2) {
55-
const blockGroupNode = ttEditor.state.schema.nodes["blockGroup"].create(
56-
{},
57-
nodesToInsert
58-
);
59+
const blockGroupNode = editor._tiptapEditor.state.schema.nodes[
60+
"blockGroup"
61+
].create({}, nodesToInsert);
5962

60-
ttEditor.view.dispatch(
61-
ttEditor.state.tr.insert(
63+
editor.dispatch(
64+
editor._tiptapEditor.state.tr.insert(
6265
posBeforeNode + node.firstChild!.nodeSize + 1,
6366
blockGroupNode
6467
)
@@ -186,7 +189,7 @@ function removeBlocksWithCallback<
186189
);
187190
}
188191

189-
ttEditor.view.dispatch(tr);
192+
editor.dispatch(tr);
190193

191194
return removedBlocks;
192195
}
@@ -214,12 +217,10 @@ export function replaceBlocks<
214217
insertedBlocks: Block<BSchema, I, S>[];
215218
removedBlocks: Block<BSchema, I, S>[];
216219
} {
217-
const ttEditor = editor._tiptapEditor;
218-
219220
const nodesToInsert: Node[] = [];
220221
for (const block of blocksToInsert) {
221222
nodesToInsert.push(
222-
blockToNode(block, ttEditor.schema, editor.schema.styleSchema)
223+
blockToNode(block, editor.pmSchema, editor.schema.styleSchema)
223224
);
224225
}
225226

@@ -274,8 +275,7 @@ export function insertContentAt<
274275
updateSelection: boolean;
275276
} = { updateSelection: true }
276277
) {
277-
const ttEditor = editor._tiptapEditor;
278-
const tr = ttEditor.state.tr;
278+
const tr = editor._tiptapEditor.state.tr;
279279

280280
// don’t dispatch an empty fragment because this can lead to strange errors
281281
// if (content.toString() === "<>") {
@@ -344,7 +344,7 @@ export function insertContentAt<
344344
selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
345345
}
346346

347-
ttEditor.view.dispatch(tr);
347+
editor.dispatch(tr);
348348

349349
return true;
350350
}

packages/core/src/api/exporters/copyExtension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ export const createCopyToClipboardExtension = <
7474
(view.state.selection.node as Node).type.spec.group ===
7575
"blockContent"
7676
) {
77-
view.dispatch(
78-
view.state.tr.setSelection(
77+
editor.dispatch(
78+
editor._tiptapEditor.state.tr.setSelection(
7979
new NodeSelection(
8080
view.state.doc.resolve(view.state.selection.from - 1)
8181
)
@@ -113,8 +113,8 @@ export const createCopyToClipboardExtension = <
113113
}
114114

115115
// Expands the selection to the parent `blockContainer` node.
116-
view.dispatch(
117-
view.state.tr.setSelection(
116+
editor.dispatch(
117+
editor._tiptapEditor.state.tr.setSelection(
118118
new NodeSelection(
119119
view.state.doc.resolve(view.state.selection.from - 1)
120120
)

packages/core/src/api/exporters/html/htmlConversion.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe("Test ProseMirror fragment edge case conversion", () => {
177177

178178
it("Selection within a block's children", () => {
179179
// Selection starts and ends within the first block's children.
180-
editor.prosemirrorView.dispatch(
180+
editor.dispatch(
181181
editor._tiptapEditor.state.tr.setSelection(
182182
TextSelection.create(editor._tiptapEditor.state.doc, 18, 80)
183183
)
@@ -200,7 +200,7 @@ describe("Test ProseMirror fragment edge case conversion", () => {
200200
it("Selection leaves a block's children", () => {
201201
// Selection starts and ends within the first block's children and ends
202202
// outside, at a shallower nesting level in the second block.
203-
editor.prosemirrorView.dispatch(
203+
editor.dispatch(
204204
editor._tiptapEditor.state.tr.setSelection(
205205
TextSelection.create(editor._tiptapEditor.state.doc, 18, 97)
206206
)
@@ -223,7 +223,7 @@ describe("Test ProseMirror fragment edge case conversion", () => {
223223
it("Selection spans multiple blocks' children", () => {
224224
// Selection starts and ends within the first block's children and ends
225225
// within the second block's children.
226-
editor.prosemirrorView.dispatch(
226+
editor.dispatch(
227227
editor._tiptapEditor.state.tr.setSelection(
228228
TextSelection.create(editor._tiptapEditor.state.doc, 18, 163)
229229
)

packages/core/src/api/parsers/html/parseHTML.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ async function parseHTMLAndCompareSnapshots(
3333

3434
(window as any).__TEST_OPTIONS.mockID = 0; // reset id counter
3535
const htmlNode = nestedListsToBlockNoteStructure(html);
36-
const tt = editor._tiptapEditor;
3736

3837
const slice = view.__parseFromClipboard(
39-
tt.view,
38+
editor.prosemirrorView,
4039
"",
4140
htmlNode.innerHTML,
4241
false,
43-
tt.view.state.selection.$from
42+
editor._tiptapEditor.state.selection.$from
4443
);
45-
tt.view.dispatch(tt.view.state.tr.replaceSelection(slice));
44+
editor.dispatch(editor._tiptapEditor.state.tr.replaceSelection(slice));
4645

4746
// alternative paste simulation doesn't work in a non-browser vitest env
4847
// editor._tiptapEditor.view.pasteHTML(html, {

packages/core/src/blocks/FileBlockContent/fileBlockHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const createAddFileButton = (
7575
};
7676
// Opens the file toolbar.
7777
const addFileButtonClickHandler = () => {
78-
editor._tiptapEditor.view.dispatch(
78+
editor.dispatch(
7979
editor._tiptapEditor.state.tr.setMeta(editor.filePanel!.plugin, {
8080
block: block,
8181
})

packages/core/src/editor/BlockNoteEditor.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, it } from "vitest";
2-
import { BlockNoteEditor } from "./BlockNoteEditor";
32
import { getBlockInfoFromPos } from "../api/getBlockInfoFromPos";
3+
import { BlockNoteEditor } from "./BlockNoteEditor";
44

55
/**
66
* @vitest-environment jsdom
@@ -10,3 +10,50 @@ it("creates an editor", () => {
1010
const blockInfo = getBlockInfoFromPos(editor._tiptapEditor.state.doc, 2);
1111
expect(blockInfo?.contentNode.type.name).toEqual("paragraph");
1212
});
13+
14+
it("immediately replaces doc", async () => {
15+
const editor = BlockNoteEditor.create();
16+
const blocks = await editor.tryParseMarkdownToBlocks(
17+
"This is a normal text\n\n# And this is a large heading"
18+
);
19+
editor.replaceBlocks(editor.document, blocks);
20+
expect(editor.document).toMatchInlineSnapshot(`
21+
[
22+
{
23+
"children": [],
24+
"content": [
25+
{
26+
"styles": {},
27+
"text": "This is a normal text",
28+
"type": "text",
29+
},
30+
],
31+
"id": "1",
32+
"props": {
33+
"backgroundColor": "default",
34+
"textAlignment": "left",
35+
"textColor": "default",
36+
},
37+
"type": "paragraph",
38+
},
39+
{
40+
"children": [],
41+
"content": [
42+
{
43+
"styles": {},
44+
"text": "And this is a large heading",
45+
"type": "text",
46+
},
47+
],
48+
"id": "2",
49+
"props": {
50+
"backgroundColor": "default",
51+
"level": 1,
52+
"textAlignment": "left",
53+
"textColor": "default",
54+
},
55+
"type": "heading",
56+
},
57+
]
58+
`);
59+
});

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import { PlaceholderPlugin } from "../extensions/Placeholder/PlaceholderPlugin";
6565
import { Dictionary } from "../i18n/dictionary";
6666
import { en } from "../i18n/locales";
6767

68+
import { Transaction } from "@tiptap/pm/state";
6869
import { createInternalHTMLSerializer } from "../api/exporters/html/internalHTMLSerializer";
6970
import "../style.css";
7071

@@ -428,6 +429,10 @@ export class BlockNoteEditor<
428429
}
429430
}
430431

432+
dispatch(tr: Transaction) {
433+
this._tiptapEditor.dispatch(tr);
434+
}
435+
431436
/**
432437
* Mount the editor to a parent DOM element. Call mount(undefined) to clean up
433438
*
@@ -954,8 +959,8 @@ export class BlockNoteEditor<
954959

955960
const mark = this.pmSchema.mark("link", { href: url });
956961

957-
this._tiptapEditor.view.dispatch(
958-
this._tiptapEditor.view.state.tr
962+
this.dispatch(
963+
this._tiptapEditor.state.tr
959964
.insertText(text, from, to)
960965
.addMark(from, from + text.length, mark)
961966
);

packages/core/src/editor/BlockNoteTipTapEditor.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { EditorOptions, createDocument } from "@tiptap/core";
33
import { Editor as TiptapEditor } from "@tiptap/core";
44
import { Node } from "@tiptap/pm/model";
55
import { EditorView } from "@tiptap/pm/view";
6-
import { EditorState } from "prosemirror-state";
76

7+
import { EditorState, Transaction } from "@tiptap/pm/state";
88
import { blockToNode } from "../api/nodeConversions/nodeConversions";
99
import { PartialBlock } from "../blocks/defaultBlocks";
1010
import { StyleSchema } from "../schema";
@@ -115,10 +115,13 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
115115
return this._state;
116116
}
117117

118-
createView() {
119-
// no-op
120-
// Disable default call to `createView` in the Editor constructor.
121-
// We should call `createView` manually only when a DOM element is available
118+
dispatch(tr: Transaction) {
119+
if (this.view) {
120+
this.view.dispatch(tr);
121+
} else {
122+
// before view has been initialized
123+
this._state = this.state.apply(tr);
124+
}
122125
}
123126

124127
/**
@@ -164,3 +167,9 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
164167
}
165168
};
166169
}
170+
171+
(BlockNoteTipTapEditor.prototype as any).createView = () => {
172+
// no-op
173+
// Disable default call to `createView` in the Editor constructor.
174+
// We should call `createView` manually only when a DOM element is available
175+
};

0 commit comments

Comments
 (0)