fix(editor): edgeless selected block is missing in the return of getSelectedBlockCommand (#11813)

This commit is contained in:
L-Sun
2025-04-18 13:52:43 +00:00
parent 5fcf34d848
commit f16bcdbe2b
7 changed files with 46 additions and 11 deletions

View File

@@ -65,7 +65,7 @@ export class PageClipboard extends ReadOnlyClipboard {
const e = ctx.get('clipboardState').raw;
e.preventDefault();
this._copySelected(() => {
this._copySelectedInPage(() => {
this.std.command
.chain()
.try<{}>(cmd => [

View File

@@ -84,11 +84,11 @@ export const clipboardConfigs: ExtensionType[] = [
export class ReadOnlyClipboard extends LifeCycleWatcher {
static override key = 'affine-readonly-clipboard';
protected readonly _copySelected = (onCopy?: () => void) => {
protected readonly _copySelectedInPage = (onCopy?: () => void) => {
return this.std.command
.chain()
.with({ onCopy })
.pipe(getSelectedModelsCommand)
.pipe(getSelectedModelsCommand, { types: ['block', 'text', 'image'] })
.pipe(draftSelectedModelsCommand)
.pipe(copySelectedModelsCommand);
};
@@ -118,7 +118,7 @@ export class ReadOnlyClipboard extends LifeCycleWatcher {
const e = ctx.get('clipboardState').raw;
e.preventDefault();
this._copySelected().run();
this._copySelectedInPage().run();
};
override mounted(): void {

View File

@@ -1,4 +1,9 @@
import type { BlockSelection, Command, TextSelection } from '@blocksuite/std';
import type {
BlockSelection,
Command,
SurfaceSelection,
TextSelection,
} from '@blocksuite/std';
import { BlockComponent } from '@blocksuite/std';
import type { RoleType } from '@blocksuite/store';
@@ -9,11 +14,13 @@ export const getSelectedBlocksCommand: Command<
currentTextSelection?: TextSelection;
currentBlockSelections?: BlockSelection[];
currentImageSelections?: ImageSelection[];
currentSurfaceSelection?: SurfaceSelection;
textSelection?: TextSelection;
blockSelections?: BlockSelection[];
imageSelections?: ImageSelection[];
surfaceSelection?: SurfaceSelection;
filter?: (el: BlockComponent) => boolean;
types?: Array<'image' | 'text' | 'block'>;
types?: Array<'image' | 'text' | 'block' | 'surface'>;
roles?: RoleType[];
mode?: 'all' | 'flat' | 'highest';
},
@@ -22,7 +29,7 @@ export const getSelectedBlocksCommand: Command<
}
> = (ctx, next) => {
const {
types = ['block', 'text', 'image'],
types = ['block', 'text', 'image', 'surface'],
roles = ['content'],
mode = 'flat',
} = ctx;
@@ -109,6 +116,15 @@ export const getSelectedBlocksCommand: Command<
dirtyResult.push(...selectedBlocks);
}
const surfaceSelection = ctx.surfaceSelection ?? ctx.currentSurfaceSelection;
if (types.includes('surface') && surfaceSelection) {
const viewStore = ctx.std.view;
const selectedBlocks = surfaceSelection.elements
.map(id => viewStore.getBlock(id))
.filter(block => !!block);
dirtyResult.push(...selectedBlocks);
}
if (ctx.filter) {
dirtyResult = dirtyResult.filter(ctx.filter);
}

View File

@@ -5,6 +5,7 @@ import { getSelectedBlocksCommand } from '../block-crud/get-selected-blocks';
import {
getBlockSelectionsCommand,
getImageSelectionsCommand,
getSurfaceSelectionCommand,
getTextSelectionCommand,
} from '../selection';
@@ -12,7 +13,7 @@ import {
* Retrieves the selected models based on the provided selection types and mode.
*
* @param ctx - The command context, which includes the types of selections to be retrieved and the mode of the selection.
* @param ctx.types - The selection types to be retrieved. Can be an array of 'block', 'text', or 'image'.
* @param ctx.types - The selection types to be retrieved. Can be an array of 'block', 'text', 'image', or 'surface'.
* @param ctx.mode - The mode of the selection. Can be 'all', 'flat', or 'highest'.
* @example
* // Assuming `commandContext` is an instance of the command context
@@ -37,14 +38,14 @@ import {
*/
export const getSelectedModelsCommand: Command<
{
types?: Array<'image' | 'text' | 'block'>;
types?: Array<'image' | 'text' | 'block' | 'surface'>;
mode?: 'all' | 'flat' | 'highest';
},
{
selectedModels: BlockModel[];
}
> = (ctx, next) => {
const types = ctx.types ?? ['block', 'text', 'image'];
const types = ctx.types ?? ['block', 'text', 'image', 'surface'];
const mode = ctx.mode ?? 'flat';
const selectedModels: BlockModel[] = [];
ctx.std.command
@@ -53,6 +54,7 @@ export const getSelectedModelsCommand: Command<
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
chain.pipe(getImageSelectionsCommand),
chain.pipe(getSurfaceSelectionCommand),
])
.pipe(getSelectedBlocksCommand, { types, mode })
.pipe(ctx => {

View File

@@ -0,0 +1,10 @@
import { SurfaceSelection } from '@blocksuite/std';
import type { GetSelectionCommand } from './types';
export const getSurfaceSelectionCommand: GetSelectionCommand = (ctx, next) => {
const currentSurfaceSelection = ctx.std.selection.filter(SurfaceSelection)[0];
if (!currentSurfaceSelection) return;
next({ currentSurfaceSelection });
};

View File

@@ -7,5 +7,6 @@ export {
getSelectionRectsCommand,
type SelectionRect,
} from './get-selection-rects';
export { getSurfaceSelectionCommand } from './get-surface-selection';
export { getTextSelectionCommand } from './get-text-selection.js';
export { isNothingSelectedCommand } from './is-nothing-selected.js';

View File

@@ -1,4 +1,9 @@
import type { BlockSelection, Command, TextSelection } from '@blocksuite/std';
import type {
BlockSelection,
Command,
SurfaceSelection,
TextSelection,
} from '@blocksuite/std';
import type { ImageSelection } from '../../selection/image';
@@ -8,5 +13,6 @@ export type GetSelectionCommand = Command<
currentTextSelection?: TextSelection;
currentBlockSelections?: BlockSelection[];
currentImageSelections?: ImageSelection[];
currentSurfaceSelection?: SurfaceSelection;
}
>;