-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrating legacy tools (#8471)
Continue #8438 There're some depended modules that need to be refactor also during migration. I'll list them here. ### GfxExtension Used to extend the `GfxController`. The `GfxExtension` can perform tasks during the life cycle of `GfxController`. ```typescript import { GfxExtension } from '@blocksuite/std/gfx'; export class ExampleExtension extends GfxExtension { // the key is required static override key = 'example'; // you can access this.gfx inside the class context mounted() { // called when gfx has mounted } unmounted() { // called when gfx has unmounted } } // edgeless-spec.ts export const edgelessSpec = [ // append it on the spec list ExampleExtension ] ``` ### KeyboardController `KeyboardController` provide the pressing status of common key such as shift, space. So that you don't have to maintain thoese status everywhere. Now you can access it through `gfx.keyboard`. ### EdgelessSelectionManager The legacy `EdgelessSelectionManager` has move to `block-std/gfx` and become a first-party supported module. Now you can access it through `gfx.selection`. ### OverlayExtension The legacy overlay has to be instantiated and added manually, which is a bit of a hassle. The newly refactored overlay utilizes the new Extension infra to automatically instantiate and add itself to the overlay renderer. ```typescript import { Overlay } from './renderer/overlay.js'; export class ExampleOverlay extends Overlay { static override overlayName = 'example'; // the methods remain the same } // edgeless-spec.ts import { ExampleOverlay } from './example-overlay.js' export const edgelessSpec = [ // append it on the spec list ExampleOverlay ] ``` When you need to get the instance of that overlay, just use the `std.get(OverlayIdentifier(theOverlayName)`. ```typescript import { OverlayIdentifier } from './renderer/overlay.js'; const exampleOverlayInst = std.get(OverlayIdentifier('example'); ``` Note that you don’t have to use the OverlayExtension if you only need a local overlay. You can still create and add it to the renderer manually. ### SurfaceMiddlewareExtesion The new surface middleware is aimed to extend the functionality of `addElement`, `deleteElement` and `updateElement`. It's a pure local infra which is helpful when you need to do something when the previously mentioned methods are called. You can load the middleware manually using `surfaceModel.applyMiddlewares` method. But we provide a more common way to extend the middleware using the Extension infra. ```typescript import { type SurfaceMiddlewareBuilder } from '@blocksuite/block-std/gfx'; export const ExampleMiddlewareBuilder: SurfaceMiddlewareBuilder = (std: BlockStdScope) => { /** * you can access std here * the builder must return a middleware function which will be apply to surface model **/ return (ctx: MiddlewareCtx) => { // this middleware function will be executed every time when the surface methods get called. } } // edgeless-spec.ts import { SurfaceMiddlewareExtension } from '@blocksuite/block-std/gfx'; import { ExampleMiddlewareBuilder } from './middlewares.js' export const edgelessSpec = [ // ... // the surface builder should be wrapped in the `SurfaceMiddlewareExtension` function to get injected SurfaceMiddlewareExtension([ExampleMiddlewareBuilder]) ] ``` ### Review guide This PR is a bit huge, but most of changes are just migration from legacy implementation. Here the list that worth attention. - Every change under block-std package - New extendable overlay under block-surface/src/renderer/overlay.ts
- Loading branch information
Showing
154 changed files
with
3,715 additions
and
4,289 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Extension } from '@blocksuite/block-std'; | ||
import { | ||
type GfxController, | ||
GfxControllerIdentifier, | ||
} from '@blocksuite/block-std/gfx'; | ||
import { type Container, createIdentifier } from '@blocksuite/global/di'; | ||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; | ||
|
||
import type { RoughCanvas } from '../utils/rough/canvas.js'; | ||
import type { CanvasRenderer } from './canvas-renderer.js'; | ||
|
||
/** | ||
* An overlay is a layer covered on top of elements, | ||
* can be used for rendering non-CRDT state indicators. | ||
*/ | ||
export abstract class Overlay extends Extension { | ||
static overlayName: string = ''; | ||
|
||
protected _renderer: CanvasRenderer | null = null; | ||
|
||
constructor(protected gfx: GfxController) { | ||
super(); | ||
} | ||
|
||
static override setup(di: Container): void { | ||
if (!this.overlayName) { | ||
throw new BlockSuiteError( | ||
ErrorCode.ValueNotExists, | ||
`The overlay constructor '${this.name}' should have a static 'overlayName' property.` | ||
); | ||
} | ||
|
||
di.addImpl(OverlayIdentifier(this.overlayName), this, [ | ||
GfxControllerIdentifier, | ||
]); | ||
} | ||
|
||
clear() {} | ||
|
||
abstract render(ctx: CanvasRenderingContext2D, rc: RoughCanvas): void; | ||
|
||
setRenderer(renderer: CanvasRenderer | null) { | ||
this._renderer = renderer; | ||
} | ||
} | ||
|
||
export const OverlayIdentifier = createIdentifier<Overlay>('Overlay'); |
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.