From b3c633369451ac96654bc634574ab92b75839954 Mon Sep 17 00:00:00 2001 From: Saul-Mirone Date: Wed, 19 Mar 2025 15:52:21 +0000 Subject: [PATCH] refactor(editor): remove note block service (#11015) --- .../affine/blocks/block-note/src/index.ts | 1 - .../blocks/block-note/src/note-block.ts | 7 +- .../src/{note-service.ts => note-keymap.ts} | 79 ++++++++----------- .../affine/blocks/block-note/src/note-spec.ts | 6 +- 4 files changed, 39 insertions(+), 54 deletions(-) rename blocksuite/affine/blocks/block-note/src/{note-service.ts => note-keymap.ts} (91%) diff --git a/blocksuite/affine/blocks/block-note/src/index.ts b/blocksuite/affine/blocks/block-note/src/index.ts index af41585d04..fb54f9e98d 100644 --- a/blocksuite/affine/blocks/block-note/src/index.ts +++ b/blocksuite/affine/blocks/block-note/src/index.ts @@ -4,5 +4,4 @@ export * from './components/edgeless-note-background'; export * from './config'; export * from './note-block'; export * from './note-edgeless-block'; -export * from './note-service'; export * from './note-spec'; diff --git a/blocksuite/affine/blocks/block-note/src/note-block.ts b/blocksuite/affine/blocks/block-note/src/note-block.ts index 15a1cd8d05..b1ee64fad1 100644 --- a/blocksuite/affine/blocks/block-note/src/note-block.ts +++ b/blocksuite/affine/blocks/block-note/src/note-block.ts @@ -2,12 +2,7 @@ import type { NoteBlockModel } from '@blocksuite/affine-model'; import { BlockComponent } from '@blocksuite/block-std'; import { css, html } from 'lit'; -import type { NoteBlockService } from './note-service.js'; - -export class NoteBlockComponent extends BlockComponent< - NoteBlockModel, - NoteBlockService -> { +export class NoteBlockComponent extends BlockComponent { static override styles = css` .affine-note-block-container { display: flow-root; diff --git a/blocksuite/affine/blocks/block-note/src/note-service.ts b/blocksuite/affine/blocks/block-note/src/note-keymap.ts similarity index 91% rename from blocksuite/affine/blocks/block-note/src/note-service.ts rename to blocksuite/affine/blocks/block-note/src/note-keymap.ts index 0929682d19..1090bca4fe 100644 --- a/blocksuite/affine/blocks/block-note/src/note-service.ts +++ b/blocksuite/affine/blocks/block-note/src/note-keymap.ts @@ -14,18 +14,21 @@ import { getPrevBlockCommand, getTextSelectionCommand, } from '@blocksuite/affine-shared/commands'; -import { matchModels } from '@blocksuite/affine-shared/utils'; +import { + asyncGetBlockComponent, + matchModels, +} from '@blocksuite/affine-shared/utils'; import { type BlockComponent, BlockSelection, - BlockService, type BlockStdScope, type Chain, + KeymapExtension, TextSelection, type UIEventHandler, type UIEventStateContext, } from '@blocksuite/block-std'; -import type { BaseSelection, BlockModel } from '@blocksuite/store'; +import type { BaseSelection } from '@blocksuite/store'; import { dedentBlocks, @@ -38,8 +41,8 @@ import { import { moveBlockConfigs } from './move-block'; import { quickActionConfig } from './quick-action'; -export class NoteBlockService extends BlockService { - static override readonly flavour = NoteBlockSchema.model.flavour; +class NoteKeymap { + constructor(readonly std: BlockStdScope) {} private _anchorSel: BlockSelection | null = null; @@ -116,18 +119,23 @@ export class NoteBlockService extends BlockService { } const [codeModel] = newModels; - onModelElementUpdated(ctx.std, codeModel, codeElement => { - this._std.selection.setGroup('note', [ - this._std.selection.create(TextSelection, { - from: { - blockId: codeElement.blockId, - index: 0, - length: codeModel.text?.length ?? 0, - }, - to: null, - }), - ]); - }).catch(console.error); + asyncGetBlockComponent(ctx.std.host, codeModel.id) + .then(codeElement => { + if (!codeElement) { + return; + } + this._std.selection.setGroup('note', [ + this._std.selection.create(TextSelection, { + from: { + blockId: codeElement.blockId, + index: 0, + length: codeModel.text?.length ?? 0, + }, + to: null, + }), + ]); + }) + .catch(console.error); next(); }) @@ -468,6 +476,7 @@ export class NoteBlockService extends BlockService { event.preventDefault(); selection.setGroup('note', [sel]); + this._reset(); return next(); }) @@ -554,17 +563,8 @@ export class NoteBlockService extends BlockService { return this.std; } - override mounted() { - super.mounted(); - this.handleEvent('keyDown', ctx => { - const state = ctx.get('keyboardState'); - if (['Control', 'Meta', 'Shift'].includes(state.raw.key)) { - return; - } - this._reset(); - }); - - this.bindHotKey({ + get hotKeys(): Record { + return { ...this._bindMoveBlockHotKey(), ...this._bindQuickActionHotKey(), ...this._bindTextConversionHotKey(), @@ -599,22 +599,13 @@ export class NoteBlockService extends BlockService { Escape: this._onEsc, Enter: this._onEnter, 'Mod-a': this._onSelectAll, - }); + }; } } -async function onModelElementUpdated( - std: BlockStdScope, - model: BlockModel, - callback: (block: BlockComponent) => void -) { - const page = model.doc; - if (!page.root) return; - - const rootComponent = std.view.getBlock(page.root.id); - if (!rootComponent) return; - await rootComponent.updateComplete; - - const element = std.view.getBlock(model.id); - if (element) callback(element); -} +export const NoteKeymapExtension = KeymapExtension( + std => new NoteKeymap(std).hotKeys, + { + flavour: NoteBlockSchema.model.flavour, + } +); diff --git a/blocksuite/affine/blocks/block-note/src/note-spec.ts b/blocksuite/affine/blocks/block-note/src/note-spec.ts index 26d2a17826..9d7c6b849f 100644 --- a/blocksuite/affine/blocks/block-note/src/note-spec.ts +++ b/blocksuite/affine/blocks/block-note/src/note-spec.ts @@ -9,23 +9,23 @@ import { } from './adapters/index'; import { NoteSlashMenuConfigExtension } from './configs/slash-menu'; import { createBuiltinToolbarConfigExtension } from './configs/toolbar'; -import { NoteBlockService } from './note-service'; +import { NoteKeymapExtension } from './note-keymap.js'; const flavour = NoteBlockSchema.model.flavour; export const NoteBlockSpec: ExtensionType[] = [ FlavourExtension(flavour), - NoteBlockService, BlockViewExtension(flavour, literal`affine-note`), DocNoteBlockAdapterExtensions, NoteSlashMenuConfigExtension, + NoteKeymapExtension, ].flat(); export const EdgelessNoteBlockSpec: ExtensionType[] = [ FlavourExtension(flavour), - NoteBlockService, BlockViewExtension(flavour, literal`affine-edgeless-note`), EdgelessNoteBlockAdapterExtensions, NoteSlashMenuConfigExtension, createBuiltinToolbarConfigExtension(flavour), + NoteKeymapExtension, ].flat();