Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrating legacy tools #8471

Merged
merged 1 commit into from
Oct 22, 2024
Merged

Conversation

doouding
Copy link
Member

@doouding doouding commented Sep 26, 2024

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.

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.

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).

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.

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

Copy link

vercel bot commented Sep 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
blocksuite ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 22, 2024 7:24am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
blocksuite-docs ⬜️ Ignored (Inspect) Visit Preview Oct 22, 2024 7:24am

Copy link

changeset-bot bot commented Sep 26, 2024

⚠️ No Changeset found

Latest commit: 65fbde6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

graphite-app bot commented Sep 26, 2024

Your org has enabled the Graphite merge queue for merging into master

Add the label “merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

Copy link
Member Author

doouding commented Sep 26, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @doouding and the rest of your teammates on Graphite Graphite

Copy link

nx-cloud bot commented Sep 26, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 65fbde6. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@doouding doouding changed the title fix: tool refactor: migrating legacy tools Oct 10, 2024
@doouding doouding force-pushed the 09-24-refactor_edgeless_tool_manager branch from 69247c7 to a4b887f Compare October 11, 2024 03:08
doouding added a commit that referenced this pull request Oct 22, 2024
The `EdgelessToolManager` has been renamed to `GfxToolController`. All the legacy tools will be rewrited using the new infra.

#### How to add a new tool
Let's take an example of pan tool.

```typescript
import { BaseTool } from '@blocksuite/block-std/gfx';

// extend the BaseTool
export class PanTool extends BaseTool {
  // every tool class should have static toolName
  static override toolName = 'pan';

  dragStart(e) {
    this._lastPoint = [e.x, e.y];
  }

  dragEnd(e) {
    this._lastPoint = null;
  }

  dragMove(e) {
    if (!this._lastPoint) return;

    const { viewport } = this._service;
    const { zoom } = viewport;

    const [lastX, lastY] = this._lastPoint;
    const deltaX = lastX - e.x;
    const deltaY = lastY - e.y;

    this._lastPoint = [e.x, e.y];

    this.gfx.viewport.applyDeltaCenter(deltaX / zoom, deltaY / zoom);
  }
}
```

Once you finish defining the tool, just append it to the spec array.

```typescript
// edgeless-spec.ts file
import { PanTool } from '../tools/pan-tool.ts';

export const EdgelessSpec = [
   // .... omit the other part of spec

   // append it to the array
   PanTool
]
```

Continue in #8471
@doouding doouding force-pushed the 09-24-refactor_edgeless_tool_manager branch from 887e8ea to 27561d0 Compare October 22, 2024 03:24
Copy link
Member Author

doouding commented Oct 22, 2024

Merge activity

  • Oct 21, 11:25 PM EDT: Graphite disabled "merge when ready" on this PR due to: a merge conflict with the target branch; resolve the conflict and try again..
  • Oct 22, 3:23 AM EDT: A user added this pull request to the Graphite merge queue.
  • Oct 22, 3:28 AM EDT: A user merged this pull request with the Graphite merge queue.

Copy link
Contributor

github-actions bot commented Oct 22, 2024

size-limit report 📦

Path Size
@blocksuite/affine 13 B (0%)
@blocksuite/affine/effects 2.06 MB (-0.01% 🔽)
@blocksuite/affine/block-std 156.97 KB (+1.06% 🔺)
@blocksuite/affine/block-std/gfx 80.1 KB (+2.56% 🔺)
@blocksuite/affine/global 13 B (0%)
@blocksuite/affine/global/utils 13.83 KB (+0.06% 🔺)
@blocksuite/affine/global/env 210 B (0%)
@blocksuite/affine/global/exceptions 552 B (0%)
@blocksuite/affine/global/di 1.65 KB (0%)
@blocksuite/affine/global/types 13 B (0%)
@blocksuite/affine/store 78.54 KB (0%)
@blocksuite/affine/inline 32.03 KB (0%)
@blocksuite/affine/inline/consts 51 B (0%)
@blocksuite/affine/inline/types 13 B (0%)
@blocksuite/affine/presets 1.86 MB (-0.06% 🔽)
@blocksuite/affine/blocks 1.95 MB (-0.05% 🔽)
@blocksuite/affine/blocks/schemas 88.72 KB (+1.06% 🔺)

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
@graphite-app graphite-app bot merged commit 65fbde6 into master Oct 22, 2024
36 checks passed
@graphite-app graphite-app bot deleted the 09-25-refactor_rewrite_tool branch October 22, 2024 07:28
doouding added a commit that referenced this pull request Oct 24, 2024
## Feat
- feat(blocks): add more html transformer apis (#8573)
- feat(blocks): support more markdown transformer api (#8564)
- feat(blocks): add abort controller to peekviewservice (#8558)
- feat: add docLinkMiddleware for linked doc export (#8553)
## Fix
- fix: mind map dragging issue (#8563)
- fix(database): can't use horizontal scrollbar (#8576)
- fix: note width is less than minimum width after resize and arrange (#8550)
- fix: newly created note alignments cannot be stored persistently (#8551)
- fix: inline range calculation not expected (#8552)
- fix(database): avoid filter bar flickering (#8562)
- fix(database): drag and drop block into database block (#8561)
- fix(blocks): split paragraphs based on newlines (#8556)
- fix(database): incorrect delete row logic (#8544)
- fix: note height less than minimum height (#8542)
## Chore
- chore: add changelog generating script (#8582)
- chore: optimize zip transformer api (#8580)
- chore: adjust attachment click events, like image, to support opening in peek view (#8579)
- chore: remove useless import (#8571)
## Refactor
- refactor: convert built-in component to widget (#8577)
- refactor: migrating legacy tools (#8471)
- refactor: edgeless tool manager (#8438)
- refactor(playground): organize export and import menu items into submenus in the debug menu (#8557)
- refactor: combine unsafeCSS and cssVar (#8545)
## Perf
- perf(edgeless): use css var to place collaboration cursors-n-selections on board zoom change (#8543)
This was referenced Oct 24, 2024
akumatus added a commit that referenced this pull request Nov 5, 2024
@doouding This code snippet was missing after PR #8471, which was first added in PR #8529.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants