fix(editor): exisiting slice import should be move instead of add (#9616)

Closes: [BS-2339](https://linear.app/affine-design/issue/BS-2339/%E6%8B%96%E6%8B%BD%E9%87%8A%E6%94%BE%E5%90%8E%E7%9A%84%E4%BD%8D%E7%BD%AE%E4%B8%8D%E5%AF%B9)
This commit is contained in:
Saul-Mirone
2025-01-09 12:14:53 +00:00
parent 126ab18967
commit 6feb4def2f
2 changed files with 53 additions and 61 deletions
@@ -256,7 +256,26 @@ export class Job {
const blockTree = await this._convertFlatSnapshots(flatSnapshots);
await this._insertBlockTree(blockTree.children, doc, parent, index);
const first = content[0];
// check if the slice is already in the doc
if (first && doc.hasBlock(first.id)) {
// if the slice is already in the doc, we need to move the blocks instead of adding them
const models = flatSnapshots
.map(flat => doc.getBlock(flat.snapshot.id)?.model)
.filter(Boolean) as BlockModel[];
const parentModel = parent ? doc.getBlock(parent)?.model : undefined;
if (!parentModel) {
throw new BlockSuiteError(
ErrorCode.TransformerError,
'Parent block not found in doc when moving slice'
);
}
const targetSibling =
index !== undefined ? parentModel.children[index] : null;
doc.moveBlocks(models, parentModel, targetSibling);
} else {
await this._insertBlockTree(blockTree.children, doc, parent, index);
}
const contentBlocks = blockTree.children
.map(tree => doc.getBlockById(tree.draft.id))