Skip to content

Commit 9db70e3

Browse files
committed
chore: use a replace step to insert the nodes
1 parent d69bb6e commit 9db70e3

File tree

2 files changed

+15
-3
lines changed
  • examples/01-basic/01-minimal
  • packages/core/src/api/blockManipulation/commands/insertBlocks

2 files changed

+15
-3
lines changed

examples/01-basic/01-minimal/App.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,12 @@ export default function App() {
88
const editor = useCreateBlockNote();
99

1010
// Renders the editor instance using a React component.
11-
return <BlockNoteView editor={editor} />;
11+
return (
12+
<BlockNoteView
13+
editor={editor}
14+
onChange={(t, ctx) => {
15+
console.log("changes", ctx.getChanges());
16+
}}
17+
/>
18+
);
1219
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Node } from "prosemirror-model";
1+
import { Fragment, Node, Slice } from "prosemirror-model";
22

33
import { Block, PartialBlock } from "../../../../blocks/defaultBlocks.js";
44
import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor";
@@ -11,6 +11,7 @@ import {
1111
import { blockToNode } from "../../../nodeConversions/blockToNode.js";
1212
import { nodeToBlock } from "../../../nodeConversions/nodeToBlock.js";
1313
import { getNodeById } from "../../../nodeUtil.js";
14+
import { ReplaceStep } from "prosemirror-transform";
1415

1516
export function insertBlocks<
1617
BSchema extends BlockSchema,
@@ -43,7 +44,11 @@ export function insertBlocks<
4344
pos += posInfo.node.nodeSize;
4445
}
4546

46-
editor.dispatch(tr.insert(pos, nodesToInsert));
47+
tr.step(
48+
new ReplaceStep(pos, pos, new Slice(Fragment.from(nodesToInsert), 0, 0))
49+
);
50+
51+
editor.dispatch(tr);
4752

4853
// Now that the `PartialBlock`s have been converted to nodes, we can
4954
// re-convert them into full `Block`s.

0 commit comments

Comments
 (0)