mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 05:48:59 +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)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user