Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor(editor): remove note block service (#11015)

Saul-Mironeb3c633369451
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<NoteBlockModel> {
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<string, UIEventHandler> {
+ 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();