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(editor): add toolbar registry extension #9572

Merged
merged 1 commit into from
Mar 6, 2025

Conversation

fundon
Copy link
Contributor

@fundon fundon commented Jan 7, 2025

What's Changed!

Added

Manage various types of toolbars uniformly in one place.

  • affine-toolbar-widget
  • ToolbarRegistryExtension

The toolbar currently supports and handles several scenarios:

  1. Select blocks: BlockSelection
  2. Select text: TextSelection or NativeSelection
  3. Hover a link: affine-link and affine-reference

Removed

Remove redundant toolbar implementations.

  • attachment toolbar
  • bookmark toolbar
  • embed toolbar
  • formatting toolbar
  • affine-link toolbar
  • affine-reference toolbar

How to migrate?

Here is an example that can help us migrate some unrefactored toolbars:

Check out the more detailed types of ToolbarModuleConfig.

  1. Add toolbar configuration file to a block type, such as bookmark block: config.ts
export const builtinToolbarConfig = {
  actions: [
    {
      id: 'a.preview',
      content(ctx) {
        const model = ctx.getCurrentModelBy(BlockSelection, BookmarkBlockModel);
        if (!model) return null;

        const { url } = model;

        return html`<affine-link-preview .url=${url}></affine-link-preview>`;
      },
    },
    {
      id: 'b.conversions',
      actions: [
        {
          id: 'inline',
          label: 'Inline view',
          run(ctx) {
          },
        },
        {
          id: 'card',
          label: 'Card view',
          disabled: true,
        },
        {
          id: 'embed',
          label: 'Embed view',
          disabled(ctx) {
          },
          run(ctx) {
          },
        },
      ],
      content(ctx) {
      },
    } satisfies ToolbarActionGroup<ToolbarAction>,
    {
      id: 'c.style',
      actions: [
        {
          id: 'horizontal',
          label: 'Large horizontal style',
        },
        {
          id: 'list',
          label: 'Small horizontal style',
        },
      ],
      content(ctx) {
      },
    } satisfies ToolbarActionGroup<ToolbarAction>,
    {
      id: 'd.caption',
      tooltip: 'Caption',
      icon: CaptionIcon(),
      run(ctx) {
      },
    },
    {
      placement: ActionPlacement.More,
      id: 'a.clipboard',
      actions: [
        {
          id: 'copy',
          label: 'Copy',
          icon: CopyIcon(),
          run(ctx) {
          },
        },
        {
          id: 'duplicate',
          label: 'Duplicate',
          icon: DuplicateIcon(),
          run(ctx) {
          },
        },
      ],
    },
    {
      placement: ActionPlacement.More,
      id: 'b.refresh',
      label: 'Reload',
      icon: ResetIcon(),
      run(ctx) {
      },
    },
    {
      placement: ActionPlacement.More,
      id: 'c.delete',
      label: 'Delete',
      icon: DeleteIcon(),
      variant: 'destructive',
      run(ctx) {
      },
    },
  ],
} as const satisfies ToolbarModuleConfig;
  1. Add configuration extension to a block spec: bookmark's spec
const flavour = BookmarkBlockSchema.model.flavour;

export const BookmarkBlockSpec: ExtensionType[] = [
  ...,
  ToolbarModuleExtension({
    id: BlockFlavourIdentifier(flavour),
    config: builtinToolbarConfig,
  }),
].flat();
  1. If the bock type already has a toolbar configuration built in, we can customize it in the following ways:

Check out the editor's config file.

// Defines a toolbar configuration for the bookmark block type
const customBookmarkToolbarConfig = {
  actions: [
    ...
  ]
} as const satisfies ToolbarModuleConfig;

// Adds it into the editor's config
 ToolbarModuleExtension({
    id: BlockFlavourIdentifier('custom:affine:bookmark'),
    config: customBookmarkToolbarConfig,
 }),
  1. If we want to extend the global:
// Defines a toolbar configuration
const customWildcardToolbarConfig = {
  actions: [
    ...
  ]
} as const satisfies ToolbarModuleConfig;

// Adds it into the editor's config
 ToolbarModuleExtension({
    id: BlockFlavourIdentifier('custom:affine:*'),
    config: customWildcardToolbarConfig,
 }),

Currently, only most toolbars in page mode have been refactored. Next is edgeless mode.

Copy link
Contributor Author

fundon commented Jan 7, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch 2 times, most recently from 092379f to aec362c Compare January 7, 2025 12:08
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from 814e297 to ea53486 Compare January 7, 2025 12:15
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch from aec362c to f518365 Compare January 7, 2025 12:15
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from ea53486 to 014cf31 Compare January 9, 2025 06:36
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch 3 times, most recently from 0998144 to ee14113 Compare January 9, 2025 10:58
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from 014cf31 to 166301c Compare January 10, 2025 02:51
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch from ee14113 to 7603178 Compare January 10, 2025 02:51
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from 166301c to 46d0ebf Compare January 10, 2025 09:25
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch from 7603178 to 69a85b7 Compare January 10, 2025 09:25
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from 46d0ebf to 8a9cff9 Compare January 10, 2025 09:31
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch 4 times, most recently from 33cbdaa to c172c76 Compare January 10, 2025 10:11
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from 8a9cff9 to 72ca86c Compare January 11, 2025 16:35
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch 7 times, most recently from 33b7c0c to b16de62 Compare January 13, 2025 02:53
@fundon fundon force-pushed the fundon/01_07_add_affine_toolbar_widget branch from 72ca86c to 6dc3ae2 Compare January 13, 2025 06:20
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch from b16de62 to 211f17a Compare January 13, 2025 06:20
@fundon fundon force-pushed the fundon/01_07_add_toolbar_registry_extension branch 3 times, most recently from 693f7be to 9835944 Compare March 6, 2025 02:35
@Saul-Mirone Saul-Mirone force-pushed the fundon/01_07_add_toolbar_registry_extension branch from 9835944 to f97e18a Compare March 6, 2025 04:46
@Saul-Mirone Saul-Mirone force-pushed the fundon/01_07_add_toolbar_registry_extension branch 2 times, most recently from e8803e5 to e0a9696 Compare March 6, 2025 06:06
Copy link

graphite-app bot commented Mar 6, 2025

Merge activity

### What's Changed!

#### Added
Manage various types of toolbars uniformly in one place.

* `affine-toolbar-widget`
* `ToolbarRegistryExtension`

The toolbar currently supports and handles several scenarios:

1.  Select blocks: `BlockSelection`
2. Select text: `TextSelection` or `NativeSelection`
3. Hover a link: `affine-link` and `affine-reference`

#### Removed
Remove redundant toolbar implementations.

* `attachment` toolbar
* `bookmark` toolbar
* `embed` toolbar
* `formatting` toolbar
* `affine-link` toolbar
* `affine-reference` toolbar

### How to migrate?

Here is an example that can help us migrate some unrefactored toolbars:

Check out the more detailed types of [`ToolbarModuleConfig`](https://github.com/toeverything/AFFiNE/blob/c178debf2d49c40b753e1bcaa6f07270bdde7401/blocksuite/affine/shared/src/services/toolbar-service/config.ts).

1.  Add toolbar configuration file to a block type, such as bookmark block: [`config.ts`](https://github.com/toeverything/AFFiNE/blob/c178debf2d49c40b753e1bcaa6f07270bdde7401/blocksuite/affine/block-bookmark/src/configs/toolbar.ts)

```ts
export const builtinToolbarConfig = {
  actions: [
    {
      id: 'a.preview',
      content(ctx) {
        const model = ctx.getCurrentModelBy(BlockSelection, BookmarkBlockModel);
        if (!model) return null;

        const { url } = model;

        return html`<affine-link-preview .url=${url}></affine-link-preview>`;
      },
    },
    {
      id: 'b.conversions',
      actions: [
        {
          id: 'inline',
          label: 'Inline view',
          run(ctx) {
          },
        },
        {
          id: 'card',
          label: 'Card view',
          disabled: true,
        },
        {
          id: 'embed',
          label: 'Embed view',
          disabled(ctx) {
          },
          run(ctx) {
          },
        },
      ],
      content(ctx) {
      },
    } satisfies ToolbarActionGroup<ToolbarAction>,
    {
      id: 'c.style',
      actions: [
        {
          id: 'horizontal',
          label: 'Large horizontal style',
        },
        {
          id: 'list',
          label: 'Small horizontal style',
        },
      ],
      content(ctx) {
      },
    } satisfies ToolbarActionGroup<ToolbarAction>,
    {
      id: 'd.caption',
      tooltip: 'Caption',
      icon: CaptionIcon(),
      run(ctx) {
      },
    },
    {
      placement: ActionPlacement.More,
      id: 'a.clipboard',
      actions: [
        {
          id: 'copy',
          label: 'Copy',
          icon: CopyIcon(),
          run(ctx) {
          },
        },
        {
          id: 'duplicate',
          label: 'Duplicate',
          icon: DuplicateIcon(),
          run(ctx) {
          },
        },
      ],
    },
    {
      placement: ActionPlacement.More,
      id: 'b.refresh',
      label: 'Reload',
      icon: ResetIcon(),
      run(ctx) {
      },
    },
    {
      placement: ActionPlacement.More,
      id: 'c.delete',
      label: 'Delete',
      icon: DeleteIcon(),
      variant: 'destructive',
      run(ctx) {
      },
    },
  ],
} as const satisfies ToolbarModuleConfig;
```

2. Add configuration extension to a block spec: [bookmark's spec](https://github.com/toeverything/AFFiNE/blob/c178debf2d49c40b753e1bcaa6f07270bdde7401/blocksuite/affine/block-bookmark/src/bookmark-spec.ts)

```ts
const flavour = BookmarkBlockSchema.model.flavour;

export const BookmarkBlockSpec: ExtensionType[] = [
  ...,
  ToolbarModuleExtension({
    id: BlockFlavourIdentifier(flavour),
    config: builtinToolbarConfig,
  }),
].flat();
```

3. If the bock type already has a toolbar configuration built in, we can customize it in the following ways:

Check out the [editor's config](https://github.com/toeverything/AFFiNE/blob/c178debf2d49c40b753e1bcaa6f07270bdde7401/packages/frontend/core/src/blocksuite/extensions/editor-config/index.ts#L51C4-L54C8) file.

```ts
// Defines a toolbar configuration for the bookmark block type
const customBookmarkToolbarConfig = {
  actions: [
    ...
  ]
} as const satisfies ToolbarModuleConfig;

// Adds it into the editor's config
 ToolbarModuleExtension({
    id: BlockFlavourIdentifier('custom:affine:bookmark'),
    config: customBookmarkToolbarConfig,
 }),
```

4. If we want to extend the global:

```ts
// Defines a toolbar configuration
const customWildcardToolbarConfig = {
  actions: [
    ...
  ]
} as const satisfies ToolbarModuleConfig;

// Adds it into the editor's config
 ToolbarModuleExtension({
    id: BlockFlavourIdentifier('custom:affine:*'),
    config: customWildcardToolbarConfig,
 }),
```

Currently, only most toolbars in page mode have been refactored. Next is edgeless mode.
@graphite-app graphite-app bot force-pushed the fundon/01_07_add_toolbar_registry_extension branch from e0a9696 to ec9bd1f Compare March 6, 2025 06:46
@graphite-app graphite-app bot merged commit ec9bd1f into canary Mar 6, 2025
67 checks passed
@graphite-app graphite-app bot deleted the fundon/01_07_add_toolbar_registry_extension branch March 6, 2025 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app:core app:server test Related to test cases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants