mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-18 06:47:02 +08:00
refactor(editor): add schema on block model (#9815)
This commit is contained in:
@@ -4,7 +4,7 @@ import { computed, type Signal, signal } from '@preact/signals-core';
|
||||
import type { Text } from '../../reactive/index.js';
|
||||
import type { Store } from '../store/store.js';
|
||||
import type { YBlock } from './types.js';
|
||||
import type { RoleType } from './zod.js';
|
||||
import type { BlockSchemaType } from './zod.js';
|
||||
|
||||
type SignaledProps<Props> = Props & {
|
||||
[P in keyof Props & string as `${P}$`]: Signal<Props[P]>;
|
||||
@@ -35,15 +35,12 @@ export class BlockModel<
|
||||
> extends MagicProps()<PropsSignal> {
|
||||
private readonly _children = signal<string[]>([]);
|
||||
|
||||
/**
|
||||
* @deprecated use doc instead
|
||||
*/
|
||||
page!: Store;
|
||||
private _store!: Store;
|
||||
|
||||
private readonly _childModels = computed(() => {
|
||||
const value: BlockModel[] = [];
|
||||
this._children.value.forEach(id => {
|
||||
const block = this.page.getBlock$(id);
|
||||
const block = this._store.getBlock$(id);
|
||||
if (block) {
|
||||
value.push(block.model);
|
||||
}
|
||||
@@ -66,10 +63,10 @@ export class BlockModel<
|
||||
|
||||
deleted = new Slot();
|
||||
|
||||
flavour!: string;
|
||||
|
||||
id!: string;
|
||||
|
||||
schema!: BlockSchemaType;
|
||||
|
||||
isEmpty() {
|
||||
return this.children.length === 0;
|
||||
}
|
||||
@@ -83,33 +80,41 @@ export class BlockModel<
|
||||
|
||||
propsUpdated = new Slot<{ key: string }>();
|
||||
|
||||
role!: RoleType;
|
||||
|
||||
stash!: (prop: keyof Props & string) => void;
|
||||
|
||||
// text is optional
|
||||
text?: Text;
|
||||
|
||||
version!: number;
|
||||
|
||||
yBlock!: YBlock;
|
||||
|
||||
get flavour(): string {
|
||||
return this.schema.model.flavour;
|
||||
}
|
||||
|
||||
get version() {
|
||||
return this.schema.version;
|
||||
}
|
||||
|
||||
get children() {
|
||||
return this._childModels.value;
|
||||
}
|
||||
|
||||
get doc() {
|
||||
return this.page;
|
||||
return this._store;
|
||||
}
|
||||
|
||||
set doc(doc: Store) {
|
||||
this.page = doc;
|
||||
this._store = doc;
|
||||
}
|
||||
|
||||
get parent() {
|
||||
return this.doc.getParent(this);
|
||||
}
|
||||
|
||||
get role() {
|
||||
return this.schema.model.role;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._onCreated = this.created.once(() => {
|
||||
|
||||
@@ -130,6 +130,7 @@ export class SyncController {
|
||||
}
|
||||
|
||||
const model = schema.model.toModel?.() ?? new BlockModel<object>();
|
||||
model.schema = schema;
|
||||
const signalWithProps = Object.entries(props).reduce(
|
||||
(acc, [key, value]) => {
|
||||
const data = signal(value);
|
||||
@@ -153,10 +154,7 @@ export class SyncController {
|
||||
Object.assign(model, signalWithProps);
|
||||
|
||||
model.id = this.id;
|
||||
model.version = this.version;
|
||||
model.keys = Object.keys(props);
|
||||
model.flavour = schema.model.flavour;
|
||||
model.role = schema.model.role;
|
||||
model.yBlock = this.yBlock;
|
||||
model.stash = this.stash;
|
||||
model.pop = this.pop;
|
||||
|
||||
@@ -20,6 +20,10 @@ export class Schema {
|
||||
}
|
||||
};
|
||||
|
||||
get(flavour: string) {
|
||||
return this.flavourSchemaMap.get(flavour);
|
||||
}
|
||||
|
||||
validate = (
|
||||
flavour: string,
|
||||
parentFlavour?: string,
|
||||
|
||||
Reference in New Issue
Block a user