mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00: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,4 +1,4 @@
|
||||
import { defineBlockSchema, type SchemaToModel } from '@blocksuite/store';
|
||||
import { BlockModel, defineBlockSchema } from '@blocksuite/store';
|
||||
|
||||
export const RootBlockSchema = defineBlockSchema({
|
||||
flavour: 'test:page',
|
||||
@@ -15,7 +15,9 @@ export const RootBlockSchema = defineBlockSchema({
|
||||
},
|
||||
});
|
||||
|
||||
export type RootBlockModel = SchemaToModel<typeof RootBlockSchema>;
|
||||
export class RootBlockModel extends BlockModel<
|
||||
ReturnType<(typeof RootBlockSchema)['model']['props']>
|
||||
> {}
|
||||
|
||||
export const NoteBlockSchema = defineBlockSchema({
|
||||
flavour: 'test:note',
|
||||
@@ -28,7 +30,9 @@ export const NoteBlockSchema = defineBlockSchema({
|
||||
},
|
||||
});
|
||||
|
||||
export type NoteBlockModel = SchemaToModel<typeof NoteBlockSchema>;
|
||||
export class NoteBlockModel extends BlockModel<
|
||||
ReturnType<(typeof NoteBlockSchema)['model']['props']>
|
||||
> {}
|
||||
|
||||
export const HeadingBlockSchema = defineBlockSchema({
|
||||
flavour: 'test:heading',
|
||||
@@ -43,7 +47,9 @@ export const HeadingBlockSchema = defineBlockSchema({
|
||||
},
|
||||
});
|
||||
|
||||
export type HeadingBlockModel = SchemaToModel<typeof HeadingBlockSchema>;
|
||||
export class HeadingBlockModel extends BlockModel<
|
||||
ReturnType<(typeof HeadingBlockSchema)['model']['props']>
|
||||
> {}
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
|
||||
@@ -4,9 +4,9 @@ import * as Y from 'yjs';
|
||||
|
||||
import {
|
||||
Block,
|
||||
BlockModel,
|
||||
defineBlockSchema,
|
||||
internalPrimitives,
|
||||
type SchemaToModel,
|
||||
} from '../model/block/index.js';
|
||||
import type { YBlock } from '../model/block/types.js';
|
||||
import { Schema } from '../schema/index.js';
|
||||
@@ -54,9 +54,15 @@ const flatTableSchema = defineBlockSchema({
|
||||
isFlatData: true,
|
||||
},
|
||||
});
|
||||
type RootModel = SchemaToModel<typeof pageSchema>;
|
||||
type TableModel = SchemaToModel<typeof tableSchema>;
|
||||
type FlatTableModel = SchemaToModel<typeof flatTableSchema>;
|
||||
class RootModel extends BlockModel<
|
||||
ReturnType<(typeof pageSchema)['model']['props']>
|
||||
> {}
|
||||
class TableModel extends BlockModel<
|
||||
ReturnType<(typeof tableSchema)['model']['props']>
|
||||
> {}
|
||||
class FlatTableModel extends BlockModel<
|
||||
ReturnType<(typeof flatTableSchema)['model']['props']>
|
||||
> {}
|
||||
|
||||
function createTestOptions() {
|
||||
const idGenerator = createAutoIncrementIdGenerator();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { defineBlockSchema, type SchemaToModel } from '../model/index.js';
|
||||
import { BlockModel, defineBlockSchema } from '../model/index.js';
|
||||
|
||||
export const RootBlockSchema = defineBlockSchema({
|
||||
flavour: 'affine:page',
|
||||
@@ -14,7 +14,9 @@ export const RootBlockSchema = defineBlockSchema({
|
||||
},
|
||||
});
|
||||
|
||||
export type RootBlockModel = SchemaToModel<typeof RootBlockSchema>;
|
||||
export class RootBlockModel extends BlockModel<
|
||||
ReturnType<(typeof RootBlockSchema)['model']['props']>
|
||||
> {}
|
||||
|
||||
export const NoteBlockSchema = defineBlockSchema({
|
||||
flavour: 'affine:note',
|
||||
|
||||
@@ -2,8 +2,8 @@ import { expect, test } from 'vitest';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
import { MemoryBlobCRUD } from '../adapter/index.js';
|
||||
import type { BlockModel } from '../model/block/block-model.js';
|
||||
import { defineBlockSchema, type SchemaToModel } from '../model/block/zod.js';
|
||||
import { BlockModel } from '../model/block/block-model.js';
|
||||
import { defineBlockSchema } from '../model/block/zod.js';
|
||||
import { Text } from '../reactive/index.js';
|
||||
import { Schema } from '../schema/index.js';
|
||||
import { createAutoIncrementIdGenerator } from '../test/index.js';
|
||||
@@ -39,7 +39,9 @@ const docSchema = defineBlockSchema({
|
||||
},
|
||||
});
|
||||
|
||||
type RootBlockModel = SchemaToModel<typeof docSchema>;
|
||||
class RootBlockModel extends BlockModel<
|
||||
ReturnType<(typeof docSchema)['model']['props']>
|
||||
> {}
|
||||
|
||||
function createTestOptions() {
|
||||
const idGenerator = createAutoIncrementIdGenerator();
|
||||
|
||||
@@ -20,9 +20,7 @@ type SignaledProps<Props> = Props & {
|
||||
* myBlock.foo = 'bar';
|
||||
* ```
|
||||
*/
|
||||
function MagicProps(): {
|
||||
new <Props>(): Props;
|
||||
} {
|
||||
function MagicProps(): { new <Props>(): Props } {
|
||||
return class {} as never;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,18 +51,6 @@ export type PropsGetter<Props> = (
|
||||
internalPrimitives: InternalPrimitives
|
||||
) => Props;
|
||||
|
||||
export type SchemaToModel<
|
||||
Schema extends {
|
||||
model: {
|
||||
props: PropsGetter<object>;
|
||||
flavour: string;
|
||||
};
|
||||
},
|
||||
> = BlockModel<ReturnType<Schema['model']['props']>> &
|
||||
ReturnType<Schema['model']['props']> & {
|
||||
flavour: Schema['model']['flavour'];
|
||||
};
|
||||
|
||||
export function defineBlockSchema<
|
||||
Flavour extends string,
|
||||
Role extends RoleType,
|
||||
|
||||
@@ -451,20 +451,6 @@ export class Store {
|
||||
}
|
||||
}
|
||||
|
||||
addBlock<Key extends BlockSuite.Flavour>(
|
||||
flavour: Key,
|
||||
blockProps?: BlockSuite.ModelProps<BlockSuite.BlockModels[Key]>,
|
||||
parent?: BlockModel | string | null,
|
||||
parentIndex?: number
|
||||
): string;
|
||||
|
||||
addBlock(
|
||||
flavour: never,
|
||||
blockProps?: Partial<BlockProps & Omit<BlockProps, 'flavour'>>,
|
||||
parent?: BlockModel | string | null,
|
||||
parentIndex?: number
|
||||
): string;
|
||||
|
||||
addBlock(
|
||||
flavour: string,
|
||||
blockProps: Partial<BlockProps & Omit<BlockProps, 'flavour'>> = {},
|
||||
|
||||
Reference in New Issue
Block a user