Skip to content

Commit 0b03c0c

Browse files
committed
feat: use locations for insertBlocks operations
1 parent 5e77569 commit 0b03c0c

File tree

3 files changed

+203
-153
lines changed

3 files changed

+203
-153
lines changed

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { Fragment, Slice } from "prosemirror-model";
22
import type { Transaction } from "prosemirror-state";
33
import { ReplaceStep } from "prosemirror-transform";
44
import { Block, PartialBlock } from "../../../../blocks/defaultBlocks.js";
5+
import { resolveLocation } from "../../../../locations/location.js";
6+
import { Location } from "../../../../locations/types.js";
57
import {
6-
BlockIdentifier,
78
BlockSchema,
89
InlineContentSchema,
910
StyleSchema,
1011
} from "../../../../schema/index.js";
1112
import { blockToNode } from "../../../nodeConversions/blockToNode.js";
1213
import { nodeToBlock } from "../../../nodeConversions/nodeToBlock.js";
13-
import { getNodeById } from "../../../nodeUtil.js";
1414
import { getPmSchema } from "../../../pmUtil.js";
1515

1616
export 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

Comments
 (0)