Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

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

L-Sun5fcf34d848b1
diff --git a/blocksuite/affine/blocks/root/src/configs/toolbar.ts b/blocksuite/affine/blocks/root/src/configs/toolbar.ts
index acd548093b..8a74dc0a55 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 31b6f18747..5266a6e6b8 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<DraftModel<BlockModel<object>>[]>;
selectedModels?: BlockModel[];
+ parentModel?: BlockModel;
+ index?: number;
}> = (ctx, next) => {
- const { std, draftedModels, selectedModels } = ctx;
- if (!draftedModels || !selectedModels) return;
-
- const model = selectedModels[selectedModels.length - 1];
+ const { std, selectedModels } = ctx;
+ let { parentModel, index } = ctx;
+ if (!selectedModels) return;
- const parentModel = std.store.getParent(model.id);
- if (!parentModel) return;
+ const [_, { draftedModels }] = ctx.std.command.exec(
+ draftSelectedModelsCommand,
+ { selectedModels }
+ );
+ if (!draftedModels) 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 3e50e2359f..1d9d3b2408 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();
},