diff --git a/blocksuite/affine/blocks/root/src/configs/toolbar.ts b/blocksuite/affine/blocks/root/src/configs/toolbar.ts index acd548093..8a74dc0a5 100644 --- a/blocksuite/affine/blocks/root/src/configs/toolbar.ts +++ b/blocksuite/affine/blocks/root/src/configs/toolbar.ts @@ -325,7 +325,6 @@ export const builtinToolbarConfig = { types: ['block', 'image'], mode: 'highest', }) - .pipe(draftSelectedModelsCommand) .pipe(duplicateSelectedModelsCommand) .run(); }, diff --git a/blocksuite/affine/shared/src/commands/model-crud/duplicate-selected-model.ts b/blocksuite/affine/shared/src/commands/model-crud/duplicate-selected-model.ts index 31b6f1874..5266a6e6b 100644 --- a/blocksuite/affine/shared/src/commands/model-crud/duplicate-selected-model.ts +++ b/blocksuite/affine/shared/src/commands/model-crud/duplicate-selected-model.ts @@ -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>[]>; 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); diff --git a/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts b/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts index 3e50e2359..1d9d3b240 100644 --- a/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts +++ b/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts @@ -1111,7 +1111,6 @@ export const defaultKeyboardToolbarConfig: KeyboardToolbarConfig = { std.command .chain() .pipe(getSelectedModelsCommand) - .pipe(draftSelectedModelsCommand) .pipe(duplicateSelectedModelsCommand) .run(); },