refactor(editor): remove global types in model (#10082)

Closes: [BS-2249](https://linear.app/affine-design/issue/BS-2249/remove-global-types-in-model)

```ts
// before
matchFlavours(model, ['affine:page']);
// after
matchFlavours(model, [PageBlockModel]);
```
This commit is contained in:
Saul-Mirone
2025-02-11 08:18:57 +00:00
parent 64bb6c5a71
commit 652865c7cf
97 changed files with 492 additions and 323 deletions
@@ -1,9 +1,9 @@
import { effects } from '@blocksuite/affine-block-note/effects';
import { ShadowlessElement, SurfaceSelection } from '@blocksuite/block-std';
import type { NoteBlockModel } from '@blocksuite/blocks';
import {
changeNoteDisplayMode,
matchFlavours,
NoteBlockModel,
NoteDisplayMode,
} from '@blocksuite/blocks';
import {
@@ -155,7 +155,7 @@ export class OutlinePanelBody extends SignalWatcher(
const noteIndex = new Map<NoteBlockModel, number>();
children.forEach((block, index) => {
if (matchFlavours(block, ['affine:note'])) {
if (matchFlavours(block, [NoteBlockModel])) {
noteIndex.set(block, index);
}
});
@@ -171,7 +171,7 @@ export class OutlinePanelBody extends SignalWatcher(
if (targetIndex === null) return;
const removeSelectedNoteFilter = (block: BlockModel) =>
!matchFlavours(block, ['affine:note']) || !selected.includes(block);
!matchFlavours(block, [NoteBlockModel]) || !selected.includes(block);
const leftPart = children
.slice(0, targetIndex)
@@ -200,7 +200,7 @@ export class OutlinePanelBody extends SignalWatcher(
private _selectNote(e: SelectEvent) {
const { selected, id, multiselect } = e.detail;
const note = this.doc.getBlock(id)?.model;
if (!note || !matchFlavours(note, ['affine:note'])) return;
if (!note || !matchFlavours(note, [NoteBlockModel])) return;
let selectedNotes = this._selectedNotes$.peek();
@@ -231,7 +231,7 @@ export class OutlinePanelBody extends SignalWatcher(
.filter(SurfaceSelection)
.map(({ blockId }) => doc.getBlock(blockId)?.model)
.filter(model => {
return !!model && matchFlavours(model, ['affine:note']);
return !!model && matchFlavours(model, [NoteBlockModel]);
});
const preSelected = this._selectedNotes$.peek();
@@ -122,7 +122,7 @@ export class OutlineBlockPreview extends SignalWatcher(
const showPreviewIcon = this._context.showIcons$.value;
switch (block.flavour as keyof BlockSuite.BlockModels) {
switch (block.flavour) {
case 'affine:page':
assertType<RootBlockModel>(block);
return block.title.length > 0
@@ -1,6 +1,11 @@
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import { PropTypes, requiredProperties } from '@blocksuite/block-std';
import { matchFlavours, NoteDisplayMode } from '@blocksuite/blocks';
import {
matchFlavours,
NoteDisplayMode,
ParagraphBlockModel,
RootBlockModel,
} from '@blocksuite/blocks';
import { SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
import { signal } from '@preact/signals-core';
@@ -129,11 +134,11 @@ export class MobileOutlineMenu extends SignalWatcher(
renderItem = (item: BlockModel) => {
let className = '';
let text = '';
if (matchFlavours(item, ['affine:page'])) {
if (matchFlavours(item, [RootBlockModel])) {
className = 'title';
text = item.title$.value.toString();
} else if (
matchFlavours(item, ['affine:paragraph']) &&
matchFlavours(item, [ParagraphBlockModel]) &&
['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(item.type$.value)
) {
className = item.type$.value;
@@ -1,10 +1,10 @@
import {
BlocksUtils,
matchFlavours,
type NoteBlockModel,
NoteBlockModel,
NoteDisplayMode,
type ParagraphBlockModel,
type RootBlockModel,
ParagraphBlockModel,
RootBlockModel,
} from '@blocksuite/blocks';
import type { BlockModel, Store } from '@blocksuite/store';
@@ -24,7 +24,7 @@ export function getNotesFromDoc(
const notes: NoteBlockModel[] = [];
rootModel.children.forEach(block => {
if (!matchFlavours(block, ['affine:note'])) return;
if (!matchFlavours(block, [NoteBlockModel])) return;
if (modes.includes(block.displayMode$.value)) {
notes.push(block);
@@ -35,14 +35,14 @@ export function getNotesFromDoc(
}
export function isRootBlock(block: BlockModel): block is RootBlockModel {
return BlocksUtils.matchFlavours(block, ['affine:page']);
return BlocksUtils.matchFlavours(block, [RootBlockModel]);
}
export function isHeadingBlock(
block: BlockModel
): block is ParagraphBlockModel {
return (
BlocksUtils.matchFlavours(block, ['affine:paragraph']) &&
BlocksUtils.matchFlavours(block, [ParagraphBlockModel]) &&
headingKeys.has(block.type$.value)
);
}