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

View File

@@ -1,4 +1,4 @@
import type { ParagraphBlockModel } from '@blocksuite/affine-model';
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import type { BlockModel } from '@blocksuite/store';
import { matchFlavours } from '../model/checker.js';
@@ -15,7 +15,7 @@ export function calculateCollapsedSiblings(
const collapsedEdgeIndex = children.findIndex((child, i) => {
if (
i > index &&
matchFlavours(child, ['affine:paragraph']) &&
matchFlavours(child, [ParagraphBlockModel]) &&
child.type.startsWith('h')
) {
const modelLevel = parseInt(model.type.slice(1));
@@ -46,7 +46,7 @@ export function getNearestHeadingBefore(
for (let i = index - 1; i >= 0; i--) {
const sibling = parent.children[i];
if (
matchFlavours(sibling, ['affine:paragraph']) &&
matchFlavours(sibling, [ParagraphBlockModel]) &&
sibling.type.startsWith('h')
) {
return sibling;

View File

@@ -1,3 +1,4 @@
import { DatabaseBlockModel, ListBlockModel } from '@blocksuite/affine-model';
import type { BlockComponent } from '@blocksuite/block-std';
import { type Point, Rect } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
@@ -55,7 +56,7 @@ export function calcDropTarget(
.every(m => children.includes(m.flavour));
}
if (!shouldAppendToDatabase && !matchFlavours(model, ['affine:database'])) {
if (!shouldAppendToDatabase && !matchFlavours(model, [DatabaseBlockModel])) {
const databaseBlockComponent =
element.closest<BlockComponent>('affine-database');
if (databaseBlockComponent) {
@@ -149,7 +150,7 @@ export function calcDropTarget(
const hasChild = (element as BlockComponent).childBlocks.length;
if (
allowSublist &&
matchFlavours(model, ['affine:list']) &&
matchFlavours(model, [ListBlockModel]) &&
!hasChild &&
point.x > domRect.x + BLOCK_CHILDREN_CONTAINER_PADDING_LEFT
) {

View File

@@ -1,3 +1,4 @@
import { DatabaseBlockModel } from '@blocksuite/affine-model';
import { BLOCK_ID_ATTR } from '@blocksuite/block-std';
import type { Point } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
@@ -24,7 +25,7 @@ export function getDropRectByPoint(
flag: DropFlags.Normal,
};
const isDatabase = matchFlavours(model, ['affine:database']);
const isDatabase = matchFlavours(model, [DatabaseBlockModel]);
if (isDatabase) {
const table = getDatabaseBlockTableElement(element);

View File

@@ -1,4 +1,6 @@
import { NoteBlockModel, RootBlockModel } from '@blocksuite/affine-model';
import { BLOCK_ID_ATTR, type BlockComponent } from '@blocksuite/block-std';
import { SurfaceBlockModel } from '@blocksuite/block-std/gfx';
import type { Point, Rect } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
@@ -35,10 +37,9 @@ function hasBlockId(element: Element): element is BlockComponent {
*/
function isRootOrNoteOrSurface(element: BlockComponent) {
return matchFlavours(element.model, [
'affine:page',
'affine:note',
// @ts-expect-error TODO: migrate surface model to @blocksuite/affine-model
'affine:surface',
RootBlockModel,
NoteBlockModel,
SurfaceBlockModel,
]);
}

View File

@@ -1,15 +1,19 @@
import type { BlockModel, DraftModel, Store } from '@blocksuite/store';
import { minimatch } from 'minimatch';
import type { BlockModel, Store } from '@blocksuite/store';
export function matchFlavours<Key extends (keyof BlockSuite.BlockModels)[]>(
model: DraftModel | null,
expected: Key
): model is BlockSuite.BlockModels[Key[number]] {
type ConstructorType<U> = { new (): U };
type ModelList<T> =
T extends Array<infer U>
? U extends ConstructorType<infer C>
? Array<C>
: never
: never;
export function matchFlavours<
const Model extends ConstructorType<BlockModel>[],
U extends ModelList<Model>[number] = ModelList<Model>[number],
>(model: unknown, expected: Model): model is U {
return (
!!model &&
expected.some(key =>
minimatch(model.flavour as keyof BlockSuite.BlockModels, key)
)
!!model && expected.some(expectedModel => model instanceof expectedModel)
);
}

View File

@@ -16,7 +16,6 @@ export function createDefaultDoc(
title,
});
// @ts-expect-error FIXME: will be fixed when surface model migrated to affine-model
doc.addBlock('affine:surface', {}, rootId);
const noteId = doc.addBlock('affine:note', {}, rootId);
doc.addBlock('affine:paragraph', {}, noteId);

View File

@@ -1,3 +1,4 @@
import { FrameBlockModel } from '@blocksuite/affine-model';
import type { EditorHost } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -73,7 +74,7 @@ export function getPrevContentBlock(
const prev = getPrev(model);
if (prev) {
if (prev.role === 'content' && !matchFlavours(prev, ['affine:frame'])) {
if (prev.role === 'content' && !matchFlavours(prev, [FrameBlockModel])) {
return prev;
}

View File

@@ -1,4 +1,4 @@
import { type NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import { NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import type { BlockComponent, EditorHost } from '@blocksuite/block-std';
import type { BlockModel, Store } from '@blocksuite/store';
@@ -37,7 +37,7 @@ export async function asyncGetBlockComponent(
export function findNoteBlockModel(model: BlockModel) {
return findAncestorModel(model, m =>
matchFlavours(m, ['affine:note'])
matchFlavours(m, [NoteBlockModel])
) as NoteBlockModel | null;
}
@@ -48,7 +48,7 @@ export function getLastNoteBlock(doc: Store) {
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
if (
matchFlavours(child, ['affine:note']) &&
matchFlavours(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
) {
note = child as NoteBlockModel;

View File

@@ -1,4 +1,4 @@
import type { ListBlockModel } from '@blocksuite/affine-model';
import { ListBlockModel } from '@blocksuite/affine-model';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel, Store } from '@blocksuite/store';
@@ -23,7 +23,7 @@ export function getNextContinuousNumberedLists(
const firstNotNumberedListIndex = parent.children.findIndex(
(model, i) =>
i > modelIndex &&
(!matchFlavours(model, ['affine:list']) || model.type !== 'numbered')
(!matchFlavours(model, [ListBlockModel]) || model.type !== 'numbered')
);
const newContinuousLists = parent.children.slice(
modelIndex + 1,
@@ -32,7 +32,7 @@ export function getNextContinuousNumberedLists(
if (
!newContinuousLists.every(
model =>
matchFlavours(model, ['affine:list']) && model.type === 'numbered'
matchFlavours(model, [ListBlockModel]) && model.type === 'numbered'
)
)
return [];
@@ -56,7 +56,7 @@ export function toNumberedList(
// if there is a numbered list before, the order continues from the previous list
if (
prevSibling &&
matchFlavours(prevSibling, ['affine:list']) &&
matchFlavours(prevSibling, [ListBlockModel]) &&
prevSibling.type === 'numbered'
) {
doc.transact(() => {