mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-22 04:26:23 +08:00
refactor(editor): add runtime type checks to database cell values (#10770)
This commit is contained in:
@@ -107,7 +107,7 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
return this._model.doc;
|
||||
}
|
||||
|
||||
allPropertyMetas$ = computed<PropertyMetaConfig<any, any, any>[]>(() => {
|
||||
allPropertyMetas$ = computed<PropertyMetaConfig<any, any, any, any>[]>(() => {
|
||||
return databaseBlockPropertyList;
|
||||
});
|
||||
|
||||
@@ -151,23 +151,27 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
this._runCapture();
|
||||
|
||||
const type = this.propertyTypeGet(propertyId);
|
||||
const update = this.propertyMetaGet(type).config.valueUpdate;
|
||||
let newValue = value;
|
||||
if (update) {
|
||||
const old = this.cellValueGet(rowId, propertyId);
|
||||
newValue = update({
|
||||
value: old,
|
||||
data: this.propertyDataGet(propertyId),
|
||||
dataSource: this,
|
||||
newValue: value,
|
||||
const update = this.propertyMetaGet(type)?.config.rawValue.setValue;
|
||||
const old = this.cellValueGet(rowId, propertyId);
|
||||
const updateFn =
|
||||
update ??
|
||||
(({ setValue, newValue }) => {
|
||||
setValue(newValue);
|
||||
});
|
||||
}
|
||||
if (this._model.columns$.value.some(v => v.id === propertyId)) {
|
||||
updateCell(this._model, rowId, {
|
||||
columnId: propertyId,
|
||||
value: newValue,
|
||||
});
|
||||
}
|
||||
updateFn({
|
||||
value: old,
|
||||
data: this.propertyDataGet(propertyId),
|
||||
dataSource: this,
|
||||
newValue: value,
|
||||
setValue: newValue => {
|
||||
if (this._model.columns$.value.some(v => v.id === propertyId)) {
|
||||
updateCell(this._model, rowId, {
|
||||
columnId: propertyId,
|
||||
value: newValue,
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
cellValueGet(rowId: string, propertyId: string): unknown {
|
||||
@@ -183,14 +187,32 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
const model = this.getModelById(rowId);
|
||||
return model?.text;
|
||||
}
|
||||
return getCell(this._model, rowId, propertyId)?.value;
|
||||
const meta = this.propertyMetaGet(type);
|
||||
if (!meta) {
|
||||
return;
|
||||
}
|
||||
const rawValue =
|
||||
getCell(this._model, rowId, propertyId)?.value ??
|
||||
meta.config.rawValue.default();
|
||||
const schema = meta.config.rawValue.schema;
|
||||
const result = schema.safeParse(rawValue);
|
||||
if (result.success) {
|
||||
return result.data;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
propertyAdd(insertToPosition: InsertToPosition, type?: string): string {
|
||||
propertyAdd(
|
||||
insertToPosition: InsertToPosition,
|
||||
type?: string
|
||||
): string | undefined {
|
||||
this.doc.captureSync();
|
||||
const property = this.propertyMetaGet(
|
||||
type ?? propertyPresets.multiSelectPropertyConfig.type
|
||||
);
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
const result = addProperty(
|
||||
this._model,
|
||||
insertToPosition,
|
||||
@@ -233,6 +255,9 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
}
|
||||
if (this.isFixedProperty(propertyId)) {
|
||||
const meta = this.propertyMetaGet(propertyId);
|
||||
if (!meta) {
|
||||
return;
|
||||
}
|
||||
const defaultData = meta.config.fixed?.defaultData ?? {};
|
||||
return {
|
||||
column: {
|
||||
@@ -288,7 +313,10 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
}
|
||||
const { column } = result;
|
||||
const meta = this.propertyMetaGet(column.type);
|
||||
return meta.config.type({
|
||||
if (!meta) {
|
||||
return;
|
||||
}
|
||||
return meta.config?.jsonValue.type({
|
||||
data: column.data,
|
||||
dataSource: this,
|
||||
});
|
||||
@@ -335,15 +363,8 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
return id;
|
||||
}
|
||||
|
||||
propertyMetaGet(type: string): PropertyMetaConfig {
|
||||
const property = databaseBlockAllPropertyMap[type];
|
||||
if (!property) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.DatabaseBlockError,
|
||||
`Unknown property type: ${type}`
|
||||
);
|
||||
}
|
||||
return property;
|
||||
propertyMetaGet(type: string): PropertyMetaConfig | undefined {
|
||||
return databaseBlockAllPropertyMap[type];
|
||||
}
|
||||
|
||||
propertyNameGet(propertyId: string): string {
|
||||
@@ -382,6 +403,10 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
if (this.isFixedProperty(propertyId)) {
|
||||
return;
|
||||
}
|
||||
const meta = this.propertyMetaGet(toType);
|
||||
if (!meta) {
|
||||
return;
|
||||
}
|
||||
const currentType = this.propertyTypeGet(propertyId);
|
||||
const currentData = this.propertyDataGet(propertyId);
|
||||
const rows = this.rows$.value;
|
||||
@@ -396,7 +421,7 @@ export class DatabaseBlockDataSource extends DataSourceBase {
|
||||
|
||||
currentCells as any
|
||||
) ?? {
|
||||
property: this.propertyMetaGet(toType).config.defaultData(),
|
||||
property: meta.config.propertyData.default(),
|
||||
cells: currentCells.map(() => undefined),
|
||||
};
|
||||
this.doc.captureSync();
|
||||
|
||||
Reference in New Issue
Block a user