refactor(editor): flat data for table block (#10010)

close: BS-2548
This commit is contained in:
Saul-Mirone
2025-02-10 19:09:33 +00:00
parent a5f36eb1d8
commit c6b8f2b584
9 changed files with 125 additions and 98 deletions

View File

@@ -44,6 +44,9 @@ export class Block {
const onChange = !options.onChange
? undefined
: (key: string, value: unknown) => {
if (!this._syncController || !this.model) {
return;
}
options.onChange?.(this, key, value);
};
const flavour = yBlock.get('sys:flavour') as string;

View File

@@ -50,7 +50,7 @@ export class FlatSyncController {
model.schema = schema;
model.id = this.id;
model.keys = Array.from(props).map(key => key.replace('prop:', ''));
model.keys = Array.from(props);
model.yBlock = this.yBlock;
const reactive = new ReactiveFlatYMap(
this.yBlock,
@@ -69,8 +69,7 @@ export class FlatSyncController {
const defaultProps = schema.model.props?.(internalPrimitives);
if (defaultProps) {
Object.entries(defaultProps).forEach(([key, value]) => {
const keyWithProp = `prop:${key}`;
if (keyWithProp in proxy) {
if (key in proxy) {
return;
}
proxy[key] = value;
@@ -149,9 +148,8 @@ export class FlatSyncController {
// Set default props if not exists
if (defaultProps) {
Object.keys(defaultProps).forEach(key => {
const keyWithProp = `prop:${key}`;
if (props.has(keyWithProp)) return;
props.add(keyWithProp);
if (props.has(key)) return;
props.add(key);
});
}

View File

@@ -1,7 +1,7 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import * as Y from 'yjs';
import { native2Y } from '../../reactive/index.js';
import { isPureObject, native2Y } from '../../reactive/index.js';
import type { Schema } from '../../schema/index.js';
import type { BlockModel } from '../block/block-model.js';
import type { YBlock } from '../block/types.js';
@@ -95,11 +95,26 @@ export class DocCRUD {
delete props.flavour;
delete props.children;
Object.entries(props).forEach(([key, value]) => {
if (value === undefined) return;
const isFlatData = schema.model.isFlatData;
if (isFlatData) {
const run = (obj: unknown, basePath: string) => {
if (isPureObject(obj)) {
Object.entries(obj).forEach(([key, value]) => {
const fullPath = basePath ? `${basePath}.${key}` : key;
run(value, fullPath);
});
} else {
yBlock.set(`prop:${basePath}`, native2Y(obj));
}
};
run(props, '');
} else {
Object.entries(props).forEach(([key, value]) => {
if (value === undefined) return;
yBlock.set(`prop:${key}`, native2Y(value));
});
yBlock.set(`prop:${key}`, native2Y(value));
});
}
const parentId =
parent ?? (schema.model.role === 'root' ? null : this.root);

View File

@@ -1,7 +1,6 @@
import type { z } from 'zod';
import { SYS_KEYS } from '../../consts.js';
import { native2Y } from '../../reactive/index.js';
import type { BlockModel } from '../block/block-model.js';
import type { BlockProps, YBlock } from '../block/types.js';
import type { BlockSchema } from '../block/zod.js';
@@ -32,6 +31,6 @@ export function syncBlockProps(
}
// @ts-expect-error allow props
model[key] = native2Y(value);
model[key] = value;
});
}