refactor(editor): adjust parameters of duplicate block command (#11812)

This commit is contained in:
L-Sun
2025-04-18 13:52:43 +00:00
parent c0ff567a2a
commit 5fcf34d848
3 changed files with 34 additions and 12 deletions

View File

@@ -1,19 +1,43 @@
import type { Command } from '@blocksuite/std';
import { type BlockModel, type DraftModel, Slice } from '@blocksuite/store';
import { type BlockModel, Slice } from '@blocksuite/store';
import { draftSelectedModelsCommand } from './draft-selected-models';
/**
* @description Duplicate the selected models
* @param selectedModels - The selected models for duplicate
* @param parentModel - The parent model of duplicated models, default is the last selected model's parent model
* @param index - The index of the duplicated models in the parent model, default is the last selected model's index + 1
*/
export const duplicateSelectedModelsCommand: Command<{
draftedModels?: Promise<DraftModel<BlockModel<object>>[]>;
selectedModels?: BlockModel[];
parentModel?: BlockModel;
index?: number;
}> = (ctx, next) => {
const { std, draftedModels, selectedModels } = ctx;
if (!draftedModels || !selectedModels) return;
const { std, selectedModels } = ctx;
let { parentModel, index } = ctx;
if (!selectedModels) return;
const model = selectedModels[selectedModels.length - 1];
const [_, { draftedModels }] = ctx.std.command.exec(
draftSelectedModelsCommand,
{ selectedModels }
);
if (!draftedModels) return;
const parentModel = std.store.getParent(model.id);
if (!parentModel) return;
const index = parentModel.children.findIndex(x => x.id === model.id);
if (parentModel) {
if (
index === undefined ||
index < 0 ||
index >= parentModel.children.length
) {
index = parentModel.children.length;
}
} else {
const model = selectedModels[selectedModels.length - 1];
parentModel = std.store.getParent(model.id) ?? undefined;
if (!parentModel) return;
index = parentModel.children.findIndex(x => x.id === model.id) + 1;
}
draftedModels
.then(models => {
@@ -22,7 +46,7 @@ export const duplicateSelectedModelsCommand: Command<{
slice,
std.store,
parentModel.id,
index + 1
index
);
})
.catch(console.error);