mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-20 03:26:47 +08:00
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:
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
Reference in New Issue
Block a user