fix: rewrite selection logic and frame selection handling logic (#12421)
Fixes [BS-3528](https://linear.app/affine-design/issue/BS-3528) Fixes [BS-3331](https://linear.app/affine-design/issue/BS-3331/frame-移动逻辑很奇怪) ### Changed - Remove `onSelected` method from gfx view, use `handleSelection` provided by `GfxViewInteraction` instead. - Add `selectable` to allow model to filter out itself from selection. - Frame can be selected by body only if it's locked or its background is not transparent. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced selection behavior for frames, edgeless text, notes, and mind map elements with refined control based on lock state and background transparency. - Introduced group-aware selection logic promoting selection of appropriate group ancestors. - Added support for element selection events in interactivity extensions. - **Bug Fixes** - Resolved frame selection issues by enabling selection via title clicks and restricting body selection to locked frames or those with non-transparent backgrounds. - **Documentation** - Added clarifying comments for group retrieval methods. - **Tests** - Updated and added end-to-end tests for frame and lock selection reflecting new selection conditions. - **Refactor** - Unified and simplified selection handling by moving logic from component methods to interaction handlers and removing deprecated selection methods. - Streamlined selection candidate processing with extension-driven target suggestion. - Removed legacy group element retrieval and selection helper methods to simplify interaction logic. - **Style** - Renamed types and improved type signatures for selection context and interaction configurations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
45
blocksuite/affine/gfx/group/src/interaction-ext.ts
Normal file
45
blocksuite/affine/gfx/group/src/interaction-ext.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { GroupElementModel } from '@blocksuite/affine-model';
|
||||
import { InteractivityExtension } from '@blocksuite/std/gfx';
|
||||
|
||||
export class GroupInteractionExtension extends InteractivityExtension {
|
||||
static override key = 'group-selection';
|
||||
|
||||
override mounted(): void {
|
||||
this.action.onElementSelect(context => {
|
||||
const { candidates, suggest } = context;
|
||||
const { activeGroup } = this.gfx.selection;
|
||||
let target = context.target;
|
||||
|
||||
if (activeGroup && activeGroup.hasDescendant(target)) {
|
||||
const groups = target.groups;
|
||||
const activeGroupIdx = groups.indexOf(activeGroup);
|
||||
|
||||
if (activeGroupIdx !== -1) {
|
||||
target =
|
||||
groups
|
||||
.slice(0, activeGroupIdx)
|
||||
.findLast(
|
||||
group =>
|
||||
group instanceof GroupElementModel &&
|
||||
candidates.includes(group)
|
||||
) ?? target;
|
||||
}
|
||||
} else {
|
||||
const groups = target.groups;
|
||||
|
||||
target =
|
||||
groups.findLast(group => {
|
||||
return (
|
||||
group instanceof GroupElementModel && candidates.includes(group)
|
||||
);
|
||||
}) ?? target;
|
||||
}
|
||||
|
||||
if (target !== context.target) {
|
||||
suggest({
|
||||
id: target.id,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import { effects } from './effects';
|
||||
import { GroupElementRendererExtension } from './element-renderer';
|
||||
import { GroupElementView, GroupInteraction } from './element-view';
|
||||
import { GroupInteractionExtension } from './interaction-ext';
|
||||
import { groupToolbarExtension } from './toolbar/config';
|
||||
|
||||
export class GroupViewExtension extends ViewExtensionProvider {
|
||||
@@ -23,6 +24,7 @@ export class GroupViewExtension extends ViewExtensionProvider {
|
||||
if (this.isEdgeless(context.scope)) {
|
||||
context.register(groupToolbarExtension);
|
||||
context.register(GroupInteraction);
|
||||
context.register(GroupInteractionExtension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
type BoxSelectionContext,
|
||||
GfxElementModelView,
|
||||
GfxViewInteractionExtension,
|
||||
type SelectedContext,
|
||||
} from '@blocksuite/std/gfx';
|
||||
|
||||
import { handleLayout } from './utils.js';
|
||||
@@ -335,33 +334,6 @@ export class MindMapView extends GfxElementModelView<MindmapElementModel> {
|
||||
return collapseButton;
|
||||
}
|
||||
|
||||
override onSelected(context: SelectedContext): void | boolean {
|
||||
const { position } = context;
|
||||
const target = this.model.childElements.find(child => {
|
||||
if (child.elementBound.containsPoint([position.x, position.y])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
if (target) {
|
||||
if (this.model.isLocked()) {
|
||||
return super.onSelected(context);
|
||||
}
|
||||
|
||||
if (context.multiSelect) {
|
||||
this.gfx.selection.toggle(target);
|
||||
} else {
|
||||
this.gfx.selection.set({ elements: [target.id] });
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
override onBoxSelected(context: BoxSelectionContext) {
|
||||
const { box } = context;
|
||||
const bound = new Bound(box.x, box.y, box.w, box.h);
|
||||
@@ -383,11 +355,24 @@ export class MindMapView extends GfxElementModelView<MindmapElementModel> {
|
||||
}
|
||||
}
|
||||
|
||||
export const MindMapInteraction = GfxViewInteractionExtension(
|
||||
export const MindMapInteraction = GfxViewInteractionExtension<MindMapView>(
|
||||
MindMapView.type,
|
||||
{
|
||||
resizeConstraint: {
|
||||
allowedHandlers: [],
|
||||
},
|
||||
handleSelection: () => {
|
||||
return {
|
||||
onSelect(context) {
|
||||
const { model } = context;
|
||||
|
||||
if (model.isLocked()) {
|
||||
return context.default(context);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
};
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user