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

This commit is contained in:
Saul-Mirone
2025-03-19 15:52:21 +00:00
parent 2701be8d08
commit b3c6333694
4 changed files with 39 additions and 54 deletions
@@ -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';
@@ -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;
@@ -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,
}
);
@@ -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();