mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
refactor(editor): add runtime type checks to database cell values (#10770)
This commit is contained in:
@@ -4,16 +4,17 @@ import type { Block, BlockModel } from '@blocksuite/store';
|
||||
|
||||
type PropertyMeta<
|
||||
T extends BlockModel = BlockModel,
|
||||
Value = unknown,
|
||||
RawValue = unknown,
|
||||
JsonValue = unknown,
|
||||
ColumnData extends NonNullable<unknown> = NonNullable<unknown>,
|
||||
> = {
|
||||
name: string;
|
||||
key: string;
|
||||
metaConfig: PropertyMetaConfig<string, ColumnData, Value>;
|
||||
metaConfig: PropertyMetaConfig<string, ColumnData, RawValue, JsonValue>;
|
||||
getColumnData?: (block: T) => ColumnData;
|
||||
setColumnData?: (block: T, data: ColumnData) => void;
|
||||
get: (block: T) => Value;
|
||||
set?: (block: T, value: Value) => void;
|
||||
get: (block: T) => RawValue;
|
||||
set?: (block: T, value: RawValue) => void;
|
||||
updated: (block: T, callback: () => void) => DisposableMember;
|
||||
};
|
||||
export type BlockMeta<T extends BlockModel = BlockModel> = {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { richTextColumnConfig } from '@blocksuite/affine-block-database';
|
||||
import { type ListBlockModel, ListBlockSchema } from '@blocksuite/affine-model';
|
||||
import { propertyPresets } from '@blocksuite/data-view/property-presets';
|
||||
|
||||
import { createBlockMeta } from './base.js';
|
||||
|
||||
@@ -13,48 +11,3 @@ export const todoMeta = createBlockMeta<ListBlockModel>({
|
||||
return (block.model as ListBlockModel).type === 'todo';
|
||||
},
|
||||
});
|
||||
todoMeta.addProperty({
|
||||
name: 'Content',
|
||||
key: 'todo-title',
|
||||
metaConfig: richTextColumnConfig,
|
||||
get: block => block.text.yText,
|
||||
set: (_block, _value) => {
|
||||
//
|
||||
},
|
||||
updated: (block, callback) => {
|
||||
block.text?.yText.observe(callback);
|
||||
return {
|
||||
dispose: () => {
|
||||
block.text?.yText.unobserve(callback);
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
todoMeta.addProperty({
|
||||
name: 'Checked',
|
||||
key: 'todo-checked',
|
||||
metaConfig: propertyPresets.checkboxPropertyConfig,
|
||||
get: block => block.checked,
|
||||
set: (block, value) => {
|
||||
block.checked = value ?? false;
|
||||
},
|
||||
updated: (block, callback) => {
|
||||
return block.propsUpdated.subscribe(({ key }) => {
|
||||
if (key === 'checked') {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
todoMeta.addProperty({
|
||||
name: 'Source',
|
||||
key: 'todo-source',
|
||||
metaConfig: propertyPresets.textPropertyConfig,
|
||||
get: block => block.doc.meta?.title ?? '',
|
||||
updated: (block, callback) => {
|
||||
return block.doc.workspace.slots.docListUpdated.subscribe(() => {
|
||||
callback();
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -10,9 +10,12 @@ export const queryBlockColumns = [
|
||||
propertyPresets.multiSelectPropertyConfig,
|
||||
propertyPresets.checkboxPropertyConfig,
|
||||
];
|
||||
export const queryBlockHiddenColumns: PropertyMetaConfig<string, any, any>[] = [
|
||||
richTextColumnConfig,
|
||||
];
|
||||
export const queryBlockHiddenColumns: PropertyMetaConfig<
|
||||
string,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>[] = [richTextColumnConfig];
|
||||
const queryBlockAllColumns = [...queryBlockColumns, ...queryBlockHiddenColumns];
|
||||
export const queryBlockAllColumnMap = Object.fromEntries(
|
||||
queryBlockAllColumns.map(v => [v.type, v as PropertyMetaConfig])
|
||||
|
||||
@@ -199,7 +199,7 @@ export class BlockQueryDataSource extends DataSourceBase {
|
||||
const property = this.getProperty(propertyId);
|
||||
return (
|
||||
property.getColumnData?.(this.blocks[0].model) ??
|
||||
property.metaConfig.config.defaultData()
|
||||
property.metaConfig.config.propertyData.default()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -284,7 +284,8 @@ export class BlockQueryDataSource extends DataSourceBase {
|
||||
|
||||
currentCells as any
|
||||
) ?? {
|
||||
property: databaseBlockAllPropertyMap[toType].config.defaultData(),
|
||||
property:
|
||||
databaseBlockAllPropertyMap[toType].config.propertyData.default(),
|
||||
cells: currentCells.map(() => undefined),
|
||||
};
|
||||
this.block.doc.captureSync();
|
||||
|
||||
Reference in New Issue
Block a user