-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(edgeless): add views and event support for canvas elements (#8882)
### **Introducing Views for Canvas Elements** This PR introduces the concept of views for canvas elements. Each canvas element, including those local element added via `addLocalElement`, now has an associated view instance. Views handle logic related to the view layer, such as binding mouse events to elements. ```typescript const modelView = std.gfx.view.get(model); view.on('pointerenter', () => { console.log('mouse entered the element.'); }); ``` You don’t need to manually instantiate views for elements. They are automatically managed by the `ViewManager`. ### **Custom View Classes** By default, each element's view is an instance of the `GfxElementModelView` class. However, you can define custom view classes for specific element types ```typescript import { GfxElementModelView } from '@blocksuite/block-std/gfx'; export class ShapeView extends GfxElementModelView { // Required static property: the model type this view is associated with static override type = 'shape'; onCreated() { this.on('click', () => { enterShapeTextEditor(this.model); }); } } // surface-spec.ts export surfaceSpec = [ //... other extensions ShapeView ]; ``` ### **Supported Events** The following mouse events are currently supported by views: - `click` - `dblclick` - `pointerdown` - `pointerenter` - `pointerleave` - `pointermove` - `pointerup`
- Loading branch information
Showing
12 changed files
with
467 additions
and
19 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/blocks/src/root-block/edgeless/gfx-tool/default-tool-ext/event-ext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { PointerEventState } from '@blocksuite/block-std'; | ||
import type { GfxElementModelView } from '@blocksuite/block-std/gfx'; | ||
|
||
import { Bound, last } from '@blocksuite/global/utils'; | ||
|
||
import { DefaultModeDragType, DefaultToolExt } from './ext.js'; | ||
|
||
export class CanvasElementEventExt extends DefaultToolExt { | ||
private _currentStackedElm: GfxElementModelView[] = []; | ||
|
||
override supportedDragTypes: DefaultModeDragType[] = [ | ||
DefaultModeDragType.None, | ||
]; | ||
|
||
private _callInReverseOrder( | ||
callback: (view: GfxElementModelView) => void, | ||
arr = this._currentStackedElm | ||
) { | ||
for (let i = arr.length - 1; i >= 0; i--) { | ||
const view = arr[i]; | ||
|
||
callback(view); | ||
} | ||
} | ||
|
||
override click(_evt: PointerEventState): void { | ||
last(this._currentStackedElm)?.dispatch('click', _evt); | ||
} | ||
|
||
override dblClick(_evt: PointerEventState): void { | ||
last(this._currentStackedElm)?.dispatch('dblclick', _evt); | ||
} | ||
|
||
override pointerDown(_evt: PointerEventState): void { | ||
last(this._currentStackedElm)?.dispatch('pointerdown', _evt); | ||
} | ||
|
||
override pointerMove(_evt: PointerEventState): void { | ||
const [x, y] = this.gfx.viewport.toModelCoord(_evt.x, _evt.y); | ||
const hoveredElmViews = this.gfx.grid | ||
.search(new Bound(x, y, 1, 1), { | ||
filter: ['canvas', 'local'], | ||
}) | ||
.map(model => this.gfx.view.get(model)) as GfxElementModelView[]; | ||
const currentStackedViews = new Set(this._currentStackedElm); | ||
const visited = new Set<GfxElementModelView>(); | ||
|
||
this._callInReverseOrder(view => { | ||
if (currentStackedViews.has(view)) { | ||
visited.add(view); | ||
view.dispatch('pointermove', _evt); | ||
} else { | ||
view.dispatch('pointerenter', _evt); | ||
} | ||
}, hoveredElmViews); | ||
this._callInReverseOrder( | ||
view => !visited.has(view) && view.dispatch('pointerleave', _evt) | ||
); | ||
this._currentStackedElm = hoveredElmViews; | ||
} | ||
|
||
override pointerUp(_evt: PointerEventState): void { | ||
last(this._currentStackedElm)?.dispatch('pointerup', _evt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.