refactor(editor): add schema on block model (#9815)

This commit is contained in:
Saul-Mirone
2025-01-21 03:56:10 +00:00
parent d2bde09ef6
commit f744002808
7 changed files with 28 additions and 23 deletions

View File

@@ -23,9 +23,9 @@ export class ImageResizeManager {
assertExists(this._imageContainer); assertExists(this._imageContainer);
const dragModel = getModelByElement(this._activeComponent); const dragModel = getModelByElement(this._activeComponent);
dragModel?.page.captureSync(); dragModel?.doc.captureSync();
const { width, height } = this._imageContainer.getBoundingClientRect(); const { width, height } = this._imageContainer.getBoundingClientRect();
dragModel?.page.updateBlock(dragModel, { dragModel?.doc.updateBlock(dragModel, {
width: width / this._zoom, width: width / this._zoom,
height: height / this._zoom, height: height / this._zoom,
}); });

View File

@@ -60,7 +60,7 @@ export const splitParagraphCommand: Command<
store.captureSync(); store.captureSync();
const right = model.text.split(splitIndex, splitLength); const right = model.text.split(splitIndex, splitLength);
const id = store.addBlock( const id = store.addBlock(
model.flavour, model.flavour as BlockSuite.Flavour,
{ {
text: right, text: right,
type: model.type, type: model.type,

View File

@@ -134,7 +134,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
const rightText = model.text.split(range.index); const rightText = model.text.split(range.index);
const newId = store.addBlock( const newId = store.addBlock(
model.flavour, model.flavour as BlockSuite.Flavour,
{ type: model.type, text: rightText }, { type: model.type, text: rightText },
parent, parent,
index + collapsedSiblings.length + 1 index + collapsedSiblings.length + 1

View File

@@ -38,8 +38,6 @@ export const ParagraphBlockSchema = defineBlockSchema({
}); });
export class ParagraphBlockModel extends BlockModel<ParagraphProps> { export class ParagraphBlockModel extends BlockModel<ParagraphProps> {
override flavour!: 'affine:paragraph';
override text!: Text; override text!: Text;
override isEmpty(): boolean { override isEmpty(): boolean {

View File

@@ -4,7 +4,7 @@ import { computed, type Signal, signal } from '@preact/signals-core';
import type { Text } from '../../reactive/index.js'; import type { Text } from '../../reactive/index.js';
import type { Store } from '../store/store.js'; import type { Store } from '../store/store.js';
import type { YBlock } from './types.js'; import type { YBlock } from './types.js';
import type { RoleType } from './zod.js'; import type { BlockSchemaType } from './zod.js';
type SignaledProps<Props> = Props & { type SignaledProps<Props> = Props & {
[P in keyof Props & string as `${P}$`]: Signal<Props[P]>; [P in keyof Props & string as `${P}$`]: Signal<Props[P]>;
@@ -35,15 +35,12 @@ export class BlockModel<
> extends MagicProps()<PropsSignal> { > extends MagicProps()<PropsSignal> {
private readonly _children = signal<string[]>([]); private readonly _children = signal<string[]>([]);
/** private _store!: Store;
* @deprecated use doc instead
*/
page!: Store;
private readonly _childModels = computed(() => { private readonly _childModels = computed(() => {
const value: BlockModel[] = []; const value: BlockModel[] = [];
this._children.value.forEach(id => { this._children.value.forEach(id => {
const block = this.page.getBlock$(id); const block = this._store.getBlock$(id);
if (block) { if (block) {
value.push(block.model); value.push(block.model);
} }
@@ -66,10 +63,10 @@ export class BlockModel<
deleted = new Slot(); deleted = new Slot();
flavour!: string;
id!: string; id!: string;
schema!: BlockSchemaType;
isEmpty() { isEmpty() {
return this.children.length === 0; return this.children.length === 0;
} }
@@ -83,33 +80,41 @@ export class BlockModel<
propsUpdated = new Slot<{ key: string }>(); propsUpdated = new Slot<{ key: string }>();
role!: RoleType;
stash!: (prop: keyof Props & string) => void; stash!: (prop: keyof Props & string) => void;
// text is optional // text is optional
text?: Text; text?: Text;
version!: number;
yBlock!: YBlock; yBlock!: YBlock;
get flavour(): string {
return this.schema.model.flavour;
}
get version() {
return this.schema.version;
}
get children() { get children() {
return this._childModels.value; return this._childModels.value;
} }
get doc() { get doc() {
return this.page; return this._store;
} }
set doc(doc: Store) { set doc(doc: Store) {
this.page = doc; this._store = doc;
} }
get parent() { get parent() {
return this.doc.getParent(this); return this.doc.getParent(this);
} }
get role() {
return this.schema.model.role;
}
constructor() { constructor() {
super(); super();
this._onCreated = this.created.once(() => { this._onCreated = this.created.once(() => {

View File

@@ -130,6 +130,7 @@ export class SyncController {
} }
const model = schema.model.toModel?.() ?? new BlockModel<object>(); const model = schema.model.toModel?.() ?? new BlockModel<object>();
model.schema = schema;
const signalWithProps = Object.entries(props).reduce( const signalWithProps = Object.entries(props).reduce(
(acc, [key, value]) => { (acc, [key, value]) => {
const data = signal(value); const data = signal(value);
@@ -153,10 +154,7 @@ export class SyncController {
Object.assign(model, signalWithProps); Object.assign(model, signalWithProps);
model.id = this.id; model.id = this.id;
model.version = this.version;
model.keys = Object.keys(props); model.keys = Object.keys(props);
model.flavour = schema.model.flavour;
model.role = schema.model.role;
model.yBlock = this.yBlock; model.yBlock = this.yBlock;
model.stash = this.stash; model.stash = this.stash;
model.pop = this.pop; model.pop = this.pop;

View File

@@ -20,6 +20,10 @@ export class Schema {
} }
}; };
get(flavour: string) {
return this.flavourSchemaMap.get(flavour);
}
validate = ( validate = (
flavour: string, flavour: string,
parentFlavour?: string, parentFlavour?: string,