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

feat(edgeless): add views and event support for canvas elements #8882

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

doouding
Copy link
Member

@doouding doouding commented Dec 6, 2024

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.

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

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

Copy link

vercel bot commented Dec 6, 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 Dec 18, 2024 3:49am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
blocksuite-docs ⬜️ Ignored (Inspect) Visit Preview Dec 18, 2024 3:49am

Copy link

changeset-bot bot commented Dec 6, 2024

⚠️ No Changeset found

Latest commit: 91118b8

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 Dec 6, 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 Dec 6, 2024


How to use the Graphite Merge Queue

Add the label merge to this PR to add it to the merge queue.

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

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

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

Copy link

nx-cloud bot commented Dec 6, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 91118b8. 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 force-pushed the 12-06-feat_canvas_event_support branch from 06990c0 to f9b0419 Compare December 17, 2024 09:17
@doouding doouding marked this pull request as ready for review December 17, 2024 09:24
@doouding doouding requested a review from a team as a code owner December 17, 2024 09:24
@doouding doouding changed the title feat: add event support feat(edgeless): add views and event support for canvas elements Dec 17, 2024
@doouding doouding force-pushed the 12-05-feat_add_local_element_support branch from 6d487de to a61fbb6 Compare December 17, 2024 09:38
@doouding doouding force-pushed the 12-06-feat_canvas_event_support branch from f9b0419 to 73b5e55 Compare December 17, 2024 09:38
@doouding doouding requested review from doodlewind and L-Sun December 18, 2024 02:24
@doouding doouding changed the base branch from 12-05-feat_add_local_element_support to graphite-base/8882 December 18, 2024 02:37
@doouding doouding force-pushed the 12-06-feat_canvas_event_support branch from ce9a665 to a7a07eb Compare December 18, 2024 02:44
@doouding doouding changed the base branch from graphite-base/8882 to master December 18, 2024 02:44
@doouding doouding force-pushed the 12-06-feat_canvas_event_support branch from a7a07eb to fe3d272 Compare December 18, 2024 02:44
Copy link
Contributor

github-actions bot commented Dec 18, 2024

size-limit report 📦

Path Size
@blocksuite/affine 13 B (0%)
@blocksuite/affine/effects 2.1 MB (-0.01% 🔽)
@blocksuite/affine/block-std 158.45 KB (+0.26% 🔺)
@blocksuite/affine/block-std/gfx 82.44 KB (+0.71% 🔺)
@blocksuite/affine/global 13 B (0%)
@blocksuite/affine/global/utils 13.88 KB (0%)
@blocksuite/affine/global/env 217 B (0%)
@blocksuite/affine/global/exceptions 562 B (0%)
@blocksuite/affine/global/di 1.65 KB (0%)
@blocksuite/affine/global/types 13 B (0%)
@blocksuite/affine/store 78.66 KB (0%)
@blocksuite/affine/inline 32.66 KB (0%)
@blocksuite/affine/inline/consts 242 B (0%)
@blocksuite/affine/inline/types 13 B (0%)
@blocksuite/affine/presets 1.9 MB (+0.03% 🔺)
@blocksuite/affine/blocks 1.99 MB (+0.05% 🔺)
@blocksuite/affine/blocks/schemas 89.95 KB (+0.11% 🔺)

@L-Sun
Copy link
Contributor

L-Sun commented Dec 18, 2024

Can these listener be disposed 🤔?

Copy link
Contributor

@L-Sun L-Sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

graphite-app bot commented Dec 18, 2024

Merge activity

### **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`
@doouding doouding force-pushed the 12-06-feat_canvas_event_support branch from fe3d272 to 91118b8 Compare December 18, 2024 03:48
@graphite-app graphite-app bot merged commit 91118b8 into master Dec 18, 2024
36 checks passed
@graphite-app graphite-app bot deleted the 12-06-feat_canvas_event_support branch December 18, 2024 03:53
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.

2 participants