@@ -2,15 +2,15 @@ import { Fragment, Slice } from "prosemirror-model";
22import type { Transaction } from "prosemirror-state" ;
33import { ReplaceStep } from "prosemirror-transform" ;
44import { Block , PartialBlock } from "../../../../blocks/defaultBlocks.js" ;
5+ import { resolveLocation } from "../../../../locations/location.js" ;
6+ import { Location } from "../../../../locations/types.js" ;
57import {
6- BlockIdentifier ,
78 BlockSchema ,
89 InlineContentSchema ,
910 StyleSchema ,
1011} from "../../../../schema/index.js" ;
1112import { blockToNode } from "../../../nodeConversions/blockToNode.js" ;
1213import { nodeToBlock } from "../../../nodeConversions/nodeToBlock.js" ;
13- import { getNodeById } from "../../../nodeUtil.js" ;
1414import { getPmSchema } from "../../../pmUtil.js" ;
1515
1616export function insertBlocks <
@@ -20,25 +20,16 @@ export function insertBlocks<
2020> (
2121 tr : Transaction ,
2222 blocksToInsert : PartialBlock < BSchema , I , S > [ ] ,
23- referenceBlock : BlockIdentifier ,
23+ referenceBlock : Location ,
2424 placement : "before" | "after" = "before" ,
2525) : Block < BSchema , I , S > [ ] {
26- const id =
27- typeof referenceBlock === "string" ? referenceBlock : referenceBlock . id ;
2826 const pmSchema = getPmSchema ( tr ) ;
2927 const nodesToInsert = blocksToInsert . map ( ( block ) =>
3028 blockToNode ( block , pmSchema ) ,
3129 ) ;
3230
33- const posInfo = getNodeById ( id , tr . doc ) ;
34- if ( ! posInfo ) {
35- throw new Error ( `Block with ID ${ id } not found` ) ;
36- }
37-
38- let pos = posInfo . posBeforeNode ;
39- if ( placement === "after" ) {
40- pos += posInfo . node . nodeSize ;
41- }
31+ const resolved = resolveLocation ( tr . doc , referenceBlock ) ;
32+ const pos = placement === "after" ? resolved . head + 1 : resolved . anchor ;
4233
4334 tr . step (
4435 new ReplaceStep ( pos , pos , new Slice ( Fragment . from ( nodesToInsert ) , 0 , 0 ) ) ,
0 commit comments