mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 22:38:56 +08:00
08d6c5a97c
### 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 -->
194 lines
5.3 KiB
TypeScript
194 lines
5.3 KiB
TypeScript
import { OverlayIdentifier } from '@blocksuite/affine-block-surface';
|
|
import {
|
|
DefaultTheme,
|
|
type FrameBlockModel,
|
|
FrameBlockSchema,
|
|
} from '@blocksuite/affine-model';
|
|
import { ThemeProvider } from '@blocksuite/affine-shared/services';
|
|
import { Bound } from '@blocksuite/global/gfx';
|
|
import { GfxBlockComponent } from '@blocksuite/std';
|
|
import {
|
|
type BoxSelectionContext,
|
|
getTopElements,
|
|
GfxViewInteractionExtension,
|
|
type SelectedContext,
|
|
} from '@blocksuite/std/gfx';
|
|
import { cssVarV2 } from '@toeverything/theme/v2';
|
|
import { html } from 'lit';
|
|
import { state } from 'lit/decorators.js';
|
|
import { styleMap } from 'lit/directives/style-map.js';
|
|
|
|
import {
|
|
EdgelessFrameManagerIdentifier,
|
|
type FrameOverlay,
|
|
} from './frame-manager';
|
|
|
|
export class FrameBlockComponent extends GfxBlockComponent<FrameBlockModel> {
|
|
override connectedCallback() {
|
|
super.connectedCallback();
|
|
|
|
this._disposables.add(
|
|
this.store.slots.blockUpdated.subscribe(({ type, id }) => {
|
|
if (id === this.model.id && type === 'update') {
|
|
this.requestUpdate();
|
|
}
|
|
})
|
|
);
|
|
this._disposables.add(
|
|
this.gfx.viewport.viewportUpdated.subscribe(() => {
|
|
this.requestUpdate();
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Due to potentially very large frame sizes, CSS scaling can cause iOS Safari to crash.
|
|
* To mitigate this issue, we combine size calculations within the rendering rect.
|
|
*/
|
|
override getCSSTransform(): string {
|
|
return '';
|
|
}
|
|
|
|
override getRenderingRect() {
|
|
const viewport = this.gfx.viewport;
|
|
const { translateX, translateY, zoom } = viewport;
|
|
const { xywh, rotate } = this.model;
|
|
const bound = Bound.deserialize(xywh);
|
|
|
|
const scaledX = bound.x * zoom + translateX;
|
|
const scaledY = bound.y * zoom + translateY;
|
|
|
|
return {
|
|
x: scaledX,
|
|
y: scaledY,
|
|
w: bound.w * zoom,
|
|
h: bound.h * zoom,
|
|
rotate,
|
|
zIndex: this.toZIndex(),
|
|
};
|
|
}
|
|
|
|
override onSelected(context: SelectedContext): boolean | void {
|
|
const { x, y } = context.position;
|
|
|
|
if (
|
|
!context.fallback &&
|
|
// if the frame is selected by title, then ignore it because the title selection is handled by the title widget
|
|
(this.model.externalBound?.containsPoint([x, y]) ||
|
|
// otherwise if the frame has title, then ignore it because in this case the frame cannot be selected by frame body
|
|
this.model.props.title.length)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return super.onSelected(context);
|
|
}
|
|
|
|
override onBoxSelected(context: BoxSelectionContext) {
|
|
const { box } = context;
|
|
const bound = new Bound(box.x, box.y, box.w, box.h);
|
|
const elementBound = this.model.elementBound;
|
|
|
|
return (
|
|
this.model.childElements.length === 0 || bound.contains(elementBound)
|
|
);
|
|
}
|
|
|
|
override renderGfxBlock() {
|
|
const { model, showBorder, std } = this;
|
|
const backgroundColor = std
|
|
.get(ThemeProvider)
|
|
.generateColorProperty(model.props.background, DefaultTheme.transparent);
|
|
const _isNavigator =
|
|
this.gfx.tool.currentToolName$.value === 'frameNavigator';
|
|
const frameIndex = this.gfx.layer.getZIndex(model);
|
|
|
|
return html`
|
|
<div
|
|
class="affine-frame-container"
|
|
style=${styleMap({
|
|
zIndex: `${frameIndex}`,
|
|
backgroundColor,
|
|
height: '100%',
|
|
width: '100%',
|
|
borderRadius: '2px',
|
|
border:
|
|
_isNavigator || !showBorder
|
|
? 'none'
|
|
: `1px solid ${cssVarV2('edgeless/frame/border/default')}`,
|
|
})}
|
|
></div>
|
|
`;
|
|
}
|
|
|
|
@state()
|
|
accessor showBorder = true;
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
'affine-frame': FrameBlockComponent;
|
|
}
|
|
}
|
|
|
|
export const FrameBlockInteraction =
|
|
GfxViewInteractionExtension<FrameBlockComponent>(
|
|
FrameBlockSchema.model.flavour,
|
|
{
|
|
handleResize: context => {
|
|
const { model, std } = context;
|
|
|
|
return {
|
|
onResizeStart(context): void {
|
|
context.default(context);
|
|
model.stash('childElementIds');
|
|
},
|
|
|
|
onResizeMove(context): void {
|
|
const { newBound } = context;
|
|
const frameManager = std.getOptional(
|
|
EdgelessFrameManagerIdentifier
|
|
);
|
|
const overlay = std.getOptional(
|
|
OverlayIdentifier('frame')
|
|
) as FrameOverlay;
|
|
|
|
model.xywh = newBound.serialize();
|
|
|
|
if (!frameManager) {
|
|
return;
|
|
}
|
|
|
|
const oldChildren = frameManager.getChildElementsInFrame(model);
|
|
|
|
const newChildren = getTopElements(
|
|
frameManager.getElementsInFrameBound(model)
|
|
).concat(
|
|
oldChildren.filter(oldChild => {
|
|
return model.intersectsBound(oldChild.elementBound);
|
|
})
|
|
);
|
|
|
|
frameManager.removeAllChildrenFromFrame(model);
|
|
frameManager.addElementsToFrame(model, newChildren);
|
|
|
|
overlay?.highlight(model, true, false);
|
|
},
|
|
onResizeEnd(context): void {
|
|
context.default(context);
|
|
model.pop('childElementIds');
|
|
},
|
|
};
|
|
},
|
|
handleRotate: () => {
|
|
return {
|
|
beforeRotate(context): void {
|
|
context.set({
|
|
rotatable: false,
|
|
});
|
|
},
|
|
};
|
|
},
|
|
}
|
|
);
|