From 04efca362e991ccb8a968a43e3eff1cd83d90e94 Mon Sep 17 00:00:00 2001 From: yoyoyohamapi <8338436+yoyoyohamapi@users.noreply.github.com> Date: Fri, 14 Mar 2025 02:35:21 +0000 Subject: [PATCH] feat(editor): is nothing selected command (#10721) ### TL;DR Added a new command to check if nothing is currently selected in the editor. ### What changed? - Created new `isNothingSelectedCommand` to determine if there are no active selections --- .../shared/src/commands/selection/index.ts | 3 +- .../commands/selection/is-nothing-selected.ts | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 blocksuite/affine/shared/src/commands/selection/is-nothing-selected.ts diff --git a/blocksuite/affine/shared/src/commands/selection/index.ts b/blocksuite/affine/shared/src/commands/selection/index.ts index 4aaa0fd62..99731e795 100644 --- a/blocksuite/affine/shared/src/commands/selection/index.ts +++ b/blocksuite/affine/shared/src/commands/selection/index.ts @@ -7,4 +7,5 @@ export { getSelectionRectsCommand, type SelectionRect, } from './get-selection-rects'; -export { getTextSelectionCommand } from './get-text-selection'; +export { getTextSelectionCommand } from './get-text-selection.js'; +export { isNothingSelectedCommand } from './is-nothing-selected.js'; diff --git a/blocksuite/affine/shared/src/commands/selection/is-nothing-selected.ts b/blocksuite/affine/shared/src/commands/selection/is-nothing-selected.ts new file mode 100644 index 000000000..0c31c9da2 --- /dev/null +++ b/blocksuite/affine/shared/src/commands/selection/is-nothing-selected.ts @@ -0,0 +1,28 @@ +import { + BlockSelection, + type Command, + TextSelection, +} from '@blocksuite/block-std'; + +import { ImageSelection } from '../../selection'; + +export const isNothingSelectedCommand: Command< + { + currentTextSelection?: TextSelection; + currentImageSelections?: ImageSelection[]; + currentBlockSelections?: BlockSelection[]; + }, + { isNothingSelected: boolean } +> = (ctx, next) => { + const textSelection = + ctx.currentTextSelection || ctx.std.selection.find(TextSelection); + const imageSelections = + ctx.currentImageSelections || ctx.std.selection.filter(ImageSelection); + const blockSelections = + ctx.currentBlockSelections || ctx.std.selection.filter(BlockSelection); + const isNothingSelected = + !textSelection && + imageSelections.length === 0 && + blockSelections.length === 0; + next({ isNothingSelected }); +};