Skip to content

Commit 0a60c34

Browse files
committed
test: use latest version of tiptap with changes for transaction handling
1 parent 1a4801d commit 0a60c34

File tree

6 files changed

+304
-115
lines changed

6 files changed

+304
-115
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,5 @@
5959
"start": "serve playground/dist -c ../serve.json",
6060
"test": "nx run-many --target=test",
6161
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,css,scss,md}\""
62-
},
63-
"overrides": {
64-
"@tiptap/core": "^3.0.0",
65-
"@tiptap/pm": "^3.0.0",
66-
"@tiptap/react": "^3.0.0"
6762
}
6863
}

packages/core/src/api/positionMapping.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ describe("PositionStorage with local editor", () => {
1919

2020
editor._tiptapEditor.destroy();
2121
});
22+
23+
it("should register transaction handler on creation & mount", () => {
24+
const editor = BlockNoteEditor.create();
25+
// editor.mount(document.createElement("div"));
26+
27+
editor._tiptapEditor.on = vi.fn();
28+
trackPosition(editor, 0);
29+
30+
expect(editor._tiptapEditor.on).toHaveBeenCalledWith(
31+
"transaction",
32+
expect.any(Function),
33+
);
34+
35+
editor._tiptapEditor.destroy();
36+
});
2237
});
2338

2439
describe("set and get positions", () => {
@@ -89,6 +104,49 @@ describe("PositionStorage with local editor", () => {
89104
editor._tiptapEditor.destroy();
90105
});
91106

107+
it("should update mapping for local transactions before the position (unmounted)", () => {
108+
const editor = BlockNoteEditor.create();
109+
110+
// Set initial content
111+
editor.insertBlocks(
112+
[
113+
{
114+
id: "1",
115+
type: "paragraph",
116+
content: [
117+
{
118+
type: "text",
119+
text: "Hello World",
120+
styles: {},
121+
},
122+
],
123+
},
124+
],
125+
editor.document[0],
126+
"before",
127+
);
128+
129+
// Start tracking
130+
const getPos = trackPosition(editor, 10);
131+
132+
// Move the cursor to the start of the document
133+
editor.setTextCursorPosition(editor.document[0], "start");
134+
135+
// Insert text at the start of the document
136+
editor.insertInlineContent([
137+
{
138+
type: "text",
139+
text: "Test",
140+
styles: {},
141+
},
142+
]);
143+
144+
// Position should be updated according to mapping
145+
expect(getPos()).toBe(14);
146+
147+
editor._tiptapEditor.destroy();
148+
});
149+
92150
it("should not update mapping for local transactions after the position", () => {
93151
const editor = BlockNoteEditor.create();
94152
editor.mount(document.createElement("div"));

packages/core/src/api/positionMapping.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export function trackPosition(
6161
*/
6262
side: "left" | "right" = "left",
6363
): () => number {
64-
const ySyncPluginState = ySyncPluginKey.getState(
65-
editor._tiptapEditor.state,
66-
) as {
64+
const ySyncPluginState = ySyncPluginKey.getState(editor.prosemirrorState) as {
6765
doc: Y.Doc;
6866
binding: ProsemirrorBinding;
6967
};
@@ -95,7 +93,7 @@ export function trackPosition(
9593

9694
return () => {
9795
const curYSyncPluginState = ySyncPluginKey.getState(
98-
editor._tiptapEditor.state,
96+
editor.prosemirrorState,
9997
) as typeof ySyncPluginState;
10098
const pos = relativePositionToAbsolutePosition(
10199
curYSyncPluginState.doc,
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// import { BlockNoteEditor, getBlockInfo, getNodeById } from "@blocknote/core";
2+
import { getBlockInfo, getNodeById } from "@blocknote/core";
23
import { getEditorWithFormattingAndMentions } from "./editors/formattingAndMentions.js";
34
import { DocumentOperationTestCase } from "./index.js";
45

@@ -22,39 +23,39 @@ export const combinedOperationsTestCases: DocumentOperationTestCase[] = [
2223
userPrompt:
2324
"add a new paragraph with the text 'You look great today!' after the first paragraph and translate 'Hello, world' to dutch",
2425
},
25-
// {
26-
// editor: getEditorWithFormattingAndMentions,
27-
// description: "add paragraph and update selection",
28-
// // Note: this test is important because it will validate whether the
29-
// // positions of `getTestSelection` are mapped correctly after the first toolcall
30-
// baseToolCalls: [
31-
// {
32-
// type: "add",
33-
// blocks: [{ content: "You look great today!" }],
34-
// referenceId: "ref2",
35-
// position: "before",
36-
// },
37-
// {
38-
// type: "update",
39-
// id: "ref2",
40-
// block: {
41-
// content: [{ type: "text", text: "Hallo", styles: {} }],
42-
// },
43-
// },
44-
// ],
45-
// getTestSelection: (editor: BlockNoteEditor<any, any, any>) => {
46-
// const posInfo = getNodeById("ref2", editor.prosemirrorState.doc)!;
26+
{
27+
editor: getEditorWithFormattingAndMentions,
28+
description: "add paragraph and update selection",
29+
// Note: this test is important because it will validate whether the
30+
// positions of `getTestSelection` are mapped correctly after the first toolcall
31+
baseToolCalls: [
32+
{
33+
type: "add",
34+
blocks: [{ content: "You look great today!" }],
35+
referenceId: "ref2",
36+
position: "before",
37+
},
38+
{
39+
type: "update",
40+
id: "ref2",
41+
block: {
42+
content: [{ type: "text", text: "Hallo", styles: {} }],
43+
},
44+
},
45+
],
46+
getTestSelection: (editor) => {
47+
const posInfo = getNodeById("ref2", editor.prosemirrorState.doc)!;
4748

48-
// const block = getBlockInfo(posInfo);
49-
// if (!block.isBlockContainer) {
50-
// throw new Error("Block is not a block container");
51-
// }
52-
// return {
53-
// from: block.blockContent.beforePos + 1,
54-
// to: block.blockContent.beforePos + 1 + "Hello".length,
55-
// };
56-
// },
57-
// userPrompt:
58-
// "add a paragraph with the text 'You look great today!' at the beginning and translate selection to German",
59-
// },
49+
const block = getBlockInfo(posInfo);
50+
if (!block.isBlockContainer) {
51+
throw new Error("Block is not a block container");
52+
}
53+
return {
54+
from: block.blockContent.beforePos + 1,
55+
to: block.blockContent.beforePos + 1 + "Hello".length,
56+
};
57+
},
58+
userPrompt:
59+
"add a paragraph with the text 'You look great today!' at the beginning and translate selection to German",
60+
},
6061
];

0 commit comments

Comments
 (0)