mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
refactor(editor): rewrite resize and rotate (#12054)
### Changed
This pr split the old `edgeless-selected-rect` into four focused modules:
- `edgeless-selected-rect`: Provide an entry point for user operation on view layer only, no further logic here.
- `GfxViewInteractionExtension`: Allow you to plug in custom resize/rotate behaviors for block or canvas element. If you don’t register an extension, it falls back to the default behaviours.
- `InteractivityManager`: Provide the API that accepts resize/rotate requests, invokes any custom behaviors you’ve registered, tracks the lifecycle and intermediate state, then hands off to the math engine.
- `ResizeController`: A pure math engine that listens for pointer moves and pointer ups and calculates new sizes, positions, and angles. It doesn’t call any business APIs.
### Customizing an element’s resize/rotate behavior
Call `GfxViewInteractionExtension` with the element’s flavour or type plus a config object. In the config you can define:
- `resizeConstraint` (min/max width & height, lock ratio)
- `handleResize(context)` method that returns an object containing `beforeResize`、`onResizeStart`、`onResizeMove`、`onResizeEnd`
- `handleRotate(context)` method that returns an object containing `beforeRotate`、`onRotateStart`、`onRotateMove`、`onRotateEnd`
```typescript
import { GfxViewInteractionExtension } from '@blocksuite/std/gfx';
GfxViewInteractionExtension(
flavourOrElementType,
{
resizeConstraint: {
minWidth,
maxWidth,
lockRatio,
minHeight,
maxHeight
},
handleResize(context) {
return {
beforeResize(context) {},
onResizeStart(context) {},
onResizeMove(context) {},
onResizeEnd(context) {}
};
},
handleRotate(context) {
return {
beforeRotate(context) {},
onRotateStart(context) {},
onRotateMove(context) {},
onRotateEnd(context) {}
};
}
}
);
```
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
- **New Features**
- Added interaction extensions for edgeless variants of attachment, bookmark, edgeless text, embedded docs, images, notes, frames, AI chat blocks, and various embed blocks (Figma, GitHub, HTML, iframe, Loom, YouTube).
- Introduced interaction extensions for graphical elements including connectors, groups, mind maps, shapes, and text, supporting constrained resizing and rotation disabling where applicable.
- Implemented a unified interaction extension framework enabling configurable resize and rotate lifecycle handlers.
- Enhanced autocomplete overlay behavior based on selection context.
- **Refactor**
- Removed legacy resize manager and element-specific resize/rotate logic, replacing with a centralized, extensible interaction system.
- Simplified resize handle rendering to a data-driven approach with improved cursor management.
- Replaced complex cursor rotation calculations with fixed-angle mappings for resize handles.
- Streamlined selection rectangle component to use interactivity services for resize and rotate handling.
- **Bug Fixes**
- Fixed connector update triggers to reduce unnecessary updates.
- Improved resize constraints enforcement and interaction state tracking.
- **Tests**
- Refined end-to-end tests to use higher-level resize utilities and added finer-grained assertions on element dimensions.
- Enhanced mouse movement granularity in drag tests for better simulation fidelity.
- **Chores**
- Added new workspace dependencies and project references for the interaction framework modules.
- Extended public API exports to include new interaction types and extensions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import { toEdgelessEmbedBlock } from '@blocksuite/affine-block-embed';
|
||||
import {
|
||||
createEmbedEdgelessBlockInteraction,
|
||||
toEdgelessEmbedBlock,
|
||||
} from '@blocksuite/affine-block-embed';
|
||||
import {
|
||||
EdgelessCRUDIdentifier,
|
||||
reassociateConnectorsCommand,
|
||||
} from '@blocksuite/affine-block-surface';
|
||||
import { EmbedLinkedDocBlockSchema } from '@blocksuite/affine-model';
|
||||
import {
|
||||
EMBED_CARD_HEIGHT,
|
||||
EMBED_CARD_WIDTH,
|
||||
@@ -61,3 +65,7 @@ export class EmbedEdgelessLinkedDocBlockComponent extends toEdgelessEmbedBlock(
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const EmbedLinkedDocInteraction = createEmbedEdgelessBlockInteraction(
|
||||
EmbedLinkedDocBlockSchema.model.flavour
|
||||
);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { literal } from 'lit/static-html.js';
|
||||
import { EmbedLinkedDocBlockAdapterExtensions } from './adapters/extension';
|
||||
import { LinkedDocSlashMenuConfigExtension } from './configs/slash-menu';
|
||||
import { createBuiltinToolbarConfigExtension } from './configs/toolbar';
|
||||
import { EmbedLinkedDocInteraction } from './embed-edgeless-linked-doc-block';
|
||||
|
||||
const flavour = EmbedLinkedDocBlockSchema.model.flavour;
|
||||
|
||||
@@ -27,5 +28,6 @@ export const EmbedLinkedDocViewExtensions: ExtensionType[] = [
|
||||
: literal`affine-embed-linked-doc-block`;
|
||||
}),
|
||||
createBuiltinToolbarConfigExtension(flavour),
|
||||
EmbedLinkedDocInteraction,
|
||||
LinkedDocSlashMenuConfigExtension,
|
||||
].flat();
|
||||
|
||||
@@ -2,5 +2,6 @@ export * from './adapters';
|
||||
export * from './commands';
|
||||
export { LinkedDocSlashMenuConfigIdentifier } from './configs/slash-menu';
|
||||
export * from './edgeless-clipboard-config';
|
||||
export * from './embed-edgeless-linked-doc-block';
|
||||
export * from './embed-linked-doc-block';
|
||||
export * from './embed-linked-doc-spec';
|
||||
|
||||
@@ -3,7 +3,12 @@ import {
|
||||
EdgelessCRUDIdentifier,
|
||||
reassociateConnectorsCommand,
|
||||
} from '@blocksuite/affine-block-surface';
|
||||
import type { AliasInfo } from '@blocksuite/affine-model';
|
||||
import {
|
||||
type AliasInfo,
|
||||
EmbedSyncedDocBlockSchema,
|
||||
SYNCED_MIN_HEIGHT,
|
||||
SYNCED_MIN_WIDTH,
|
||||
} from '@blocksuite/affine-model';
|
||||
import {
|
||||
EMBED_CARD_HEIGHT,
|
||||
EMBED_CARD_WIDTH,
|
||||
@@ -12,8 +17,9 @@ import {
|
||||
ThemeExtensionIdentifier,
|
||||
ThemeProvider,
|
||||
} from '@blocksuite/affine-shared/services';
|
||||
import { Bound } from '@blocksuite/global/gfx';
|
||||
import { Bound, clamp } from '@blocksuite/global/gfx';
|
||||
import { type BlockComponent, BlockStdScope } from '@blocksuite/std';
|
||||
import { GfxViewInteractionExtension } from '@blocksuite/std/gfx';
|
||||
import { html, nothing } from 'lit';
|
||||
import { query, queryAsync } from 'lit/decorators.js';
|
||||
import { choose } from 'lit/directives/choose.js';
|
||||
@@ -199,3 +205,60 @@ export class EmbedEdgelessSyncedDocBlockComponent extends toEdgelessEmbedBlock(
|
||||
|
||||
override accessor useCaptionEditor = true;
|
||||
}
|
||||
|
||||
export const EmbedSyncedDocInteraction =
|
||||
GfxViewInteractionExtension<EmbedEdgelessSyncedDocBlockComponent>(
|
||||
EmbedSyncedDocBlockSchema.model.flavour,
|
||||
{
|
||||
resizeConstraint: {
|
||||
minWidth: SYNCED_MIN_WIDTH,
|
||||
minHeight: SYNCED_MIN_HEIGHT,
|
||||
},
|
||||
|
||||
handleRotate: () => {
|
||||
return {
|
||||
beforeRotate(context) {
|
||||
context.set({
|
||||
rotatable: false,
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
handleResize: ({ model }) => {
|
||||
const initialScale = model.props.scale ?? 1;
|
||||
|
||||
return {
|
||||
onResizeStart: context => {
|
||||
context.default(context);
|
||||
model.stash('scale');
|
||||
},
|
||||
onResizeMove: context => {
|
||||
const { lockRatio, originalBound, constraint, newBound } = context;
|
||||
|
||||
let scale = initialScale;
|
||||
const realWidth = originalBound.w / initialScale;
|
||||
|
||||
if (lockRatio) {
|
||||
scale = newBound.w / realWidth;
|
||||
}
|
||||
|
||||
const newWidth = newBound.w / scale;
|
||||
|
||||
newBound.w =
|
||||
clamp(newWidth, constraint.minWidth, constraint.maxWidth) * scale;
|
||||
newBound.h =
|
||||
clamp(newBound.h, constraint.minHeight, constraint.maxHeight) *
|
||||
scale;
|
||||
|
||||
model.props.scale = scale;
|
||||
model.xywh = newBound.serialize();
|
||||
},
|
||||
onResizeEnd: context => {
|
||||
context.default(context);
|
||||
model.pop('scale');
|
||||
},
|
||||
};
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -5,6 +5,7 @@ import { literal } from 'lit/static-html.js';
|
||||
|
||||
import { EmbedSyncedDocBlockAdapterExtensions } from './adapters/extension';
|
||||
import { createBuiltinToolbarConfigExtension } from './configs/toolbar';
|
||||
import { EmbedSyncedDocInteraction } from './embed-edgeless-synced-doc-block';
|
||||
import { HeightInitializationExtension } from './init-height-extension';
|
||||
|
||||
const flavour = EmbedSyncedDocBlockSchema.model.flavour;
|
||||
@@ -29,4 +30,5 @@ export const EmbedSyncedDocViewExtensions: ExtensionType[] = [
|
||||
}),
|
||||
createBuiltinToolbarConfigExtension(flavour),
|
||||
HeightInitializationExtension,
|
||||
EmbedSyncedDocInteraction,
|
||||
].flat();
|
||||
|
||||
@@ -2,6 +2,7 @@ export * from './adapters';
|
||||
export * from './commands';
|
||||
export * from './configs';
|
||||
export * from './edgeless-clipboard-config';
|
||||
export * from './embed-edgeless-synced-doc-block';
|
||||
export * from './embed-synced-doc-block';
|
||||
export * from './embed-synced-doc-spec';
|
||||
export { SYNCED_MIN_HEIGHT, SYNCED_MIN_WIDTH } from '@blocksuite/affine-model';
|
||||
|
||||
@@ -6,10 +6,12 @@ import {
|
||||
import { effects } from './effects';
|
||||
import {
|
||||
EdgelessClipboardEmbedLinkedDocConfig,
|
||||
EmbedLinkedDocInteraction,
|
||||
EmbedLinkedDocViewExtensions,
|
||||
} from './embed-linked-doc-block';
|
||||
import {
|
||||
EdgelessClipboardEmbedSyncedDocConfig,
|
||||
EmbedSyncedDocInteraction,
|
||||
EmbedSyncedDocViewExtensions,
|
||||
} from './embed-synced-doc-block';
|
||||
|
||||
@@ -30,6 +32,8 @@ export class EmbedDocViewExtension extends ViewExtensionProvider {
|
||||
context.register([
|
||||
EdgelessClipboardEmbedLinkedDocConfig,
|
||||
EdgelessClipboardEmbedSyncedDocConfig,
|
||||
EmbedLinkedDocInteraction,
|
||||
EmbedSyncedDocInteraction,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user