mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
refactor(editor): adjust parameters of duplicate block command (#11812)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user