Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

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

Saul-Mironef744002808b8
diff --git a/blocksuite/affine/block-image/src/image-resize-manager.ts b/blocksuite/affine/block-image/src/image-resize-manager.ts
index 0738ee1f55..0e49c7d855 100644
--- a/blocksuite/affine/block-image/src/image-resize-manager.ts
+++ b/blocksuite/affine/block-image/src/image-resize-manager.ts
@@ -23,9 +23,9 @@ export class ImageResizeManager {
assertExists(this._imageContainer);
const dragModel = getModelByElement(this._activeComponent);
- dragModel?.page.captureSync();
+ dragModel?.doc.captureSync();
const { width, height } = this._imageContainer.getBoundingClientRect();
- dragModel?.page.updateBlock(dragModel, {
+ dragModel?.doc.updateBlock(dragModel, {
width: width / this._zoom,
height: height / this._zoom,
});
diff --git a/blocksuite/affine/block-paragraph/src/commands/split-paragraph.ts b/blocksuite/affine/block-paragraph/src/commands/split-paragraph.ts
index d238f4a86c..81a78d0fc8 100644
--- a/blocksuite/affine/block-paragraph/src/commands/split-paragraph.ts
+++ b/blocksuite/affine/block-paragraph/src/commands/split-paragraph.ts
@@ -60,7 +60,7 @@ export const splitParagraphCommand: Command<
store.captureSync();
const right = model.text.split(splitIndex, splitLength);
const id = store.addBlock(
- model.flavour,
+ model.flavour as BlockSuite.Flavour,
{
text: right,
type: model.type,
diff --git a/blocksuite/affine/block-paragraph/src/paragraph-keymap.ts b/blocksuite/affine/block-paragraph/src/paragraph-keymap.ts
index d7ca743df3..ec5586993f 100644
--- a/blocksuite/affine/block-paragraph/src/paragraph-keymap.ts
+++ b/blocksuite/affine/block-paragraph/src/paragraph-keymap.ts
@@ -134,7 +134,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
const rightText = model.text.split(range.index);
const newId = store.addBlock(
- model.flavour,
+ model.flavour as BlockSuite.Flavour,
{ type: model.type, text: rightText },
parent,
index + collapsedSiblings.length + 1
diff --git a/blocksuite/affine/model/src/blocks/paragraph/paragraph-model.ts b/blocksuite/affine/model/src/blocks/paragraph/paragraph-model.ts
index f30b642b03..b42155c221 100644
--- a/blocksuite/affine/model/src/blocks/paragraph/paragraph-model.ts
+++ b/blocksuite/affine/model/src/blocks/paragraph/paragraph-model.ts
@@ -38,8 +38,6 @@ export const ParagraphBlockSchema = defineBlockSchema({
});
export class ParagraphBlockModel extends BlockModel<ParagraphProps> {
- override flavour!: 'affine:paragraph';
-
override text!: Text;
override isEmpty(): boolean {
diff --git a/blocksuite/framework/store/src/model/block/block-model.ts b/blocksuite/framework/store/src/model/block/block-model.ts
index c210cc6ff5..1feb39c5db 100644
--- a/blocksuite/framework/store/src/model/block/block-model.ts
+++ b/blocksuite/framework/store/src/model/block/block-model.ts
@@ -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(() => {
diff --git a/blocksuite/framework/store/src/model/block/sync-controller.ts b/blocksuite/framework/store/src/model/block/sync-controller.ts
index ff08517fe1..5126e0467f 100644
--- a/blocksuite/framework/store/src/model/block/sync-controller.ts
+++ b/blocksuite/framework/store/src/model/block/sync-controller.ts
@@ -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;
diff --git a/blocksuite/framework/store/src/schema/schema.ts b/blocksuite/framework/store/src/schema/schema.ts
index 4cc41719ba..a74dd47d30 100644
--- a/blocksuite/framework/store/src/schema/schema.ts
+++ b/blocksuite/framework/store/src/schema/schema.ts
@@ -20,6 +20,10 @@ export class Schema {
}
};
+ get(flavour: string) {
+ return this.flavourSchemaMap.get(flavour);
+ }
+
validate = (
flavour: string,
parentFlavour?: string,