refactor(editor): remove database-service (#9769)

close: BS-2426
This commit is contained in:
zzj3720
2025-01-18 05:36:15 +00:00
parent ad814a0f4f
commit 95c0f59d96
30 changed files with 448 additions and 427 deletions
@@ -136,16 +136,15 @@ export class DataViewPropertiesSettingView extends SignalWatcher(
});
renderProperty = (property: Property) => {
const isTitle = property.type$.value === 'title';
const icon = property.hide$.value ? InvisibleIcon() : ViewIcon();
const changeVisible = () => {
if (property.type$.value !== 'title') {
if (property.hideCanSet) {
property.hideSet(!property.hide$.value);
}
};
const classList = classMap({
'property-item-op-icon': true,
disabled: isTitle,
disabled: !property.hideCanSet,
});
return html` <div
${dragHandler(property.id)}
@@ -251,7 +250,7 @@ export const popPropertiesSetting = (
const isAllShowed = items.every(v => !v.hide$.value);
const clickChangeAll = () => {
props.view.propertiesWithoutFilter$.value.forEach(id => {
if (props.view.propertyTypeGet(id) !== 'title') {
if (props.view.propertyCanHide(id)) {
props.view.propertyHideSet(id, isAllShowed);
}
});
@@ -36,13 +36,13 @@ export const typeConfig = (property: Property) => {
items: [
menu.subMenu({
name: 'Type',
hide: () => !property.typeSet || property.type$.value === 'title',
hide: () => !property.typeCanSet,
postfix: html` <div
class="affine-database-column-type-icon"
style="color: var(--affine-text-secondary-color);gap:4px;font-size: 14px;"
>
${renderUniLit(property.icon)}
${property.view.propertyMetas.find(
${property.view.propertyMetas$.value.find(
v => v.type === property.type$.value
)?.config.name}
</div>`,
@@ -52,7 +52,7 @@ export const typeConfig = (property: Property) => {
},
items: [
menu.group({
items: property.view.propertyMetas.map(config => {
items: property.view.propertyMetas$.value.map(config => {
return menu.action({
isSelected: config.type === property.type$.value,
name: config.config.name,
@@ -1,3 +1,4 @@
import type { Column } from '@blocksuite/affine-model';
import type { InsertToPosition } from '@blocksuite/affine-shared/utils';
import { computed, type ReadonlySignal } from '@preact/signals-core';
@@ -26,7 +27,8 @@ export interface DataSource {
rowDelete(ids: string[]): void;
rowMove(rowId: string, position: InsertToPosition): void;
propertyMetas: PropertyMetaConfig[];
propertyMetas$: ReadonlySignal<PropertyMetaConfig[]>;
allPropertyMetas$: ReadonlySignal<PropertyMetaConfig[]>;
propertyNameGet$(propertyId: string): ReadonlySignal<string | undefined>;
propertyNameGet(propertyId: string): string;
@@ -35,6 +37,7 @@ export interface DataSource {
propertyTypeGet(propertyId: string): string | undefined;
propertyTypeGet$(propertyId: string): ReadonlySignal<string | undefined>;
propertyTypeSet(propertyId: string, type: string): void;
propertyTypeCanSet(propertyId: string): boolean;
propertyDataGet(propertyId: string): Record<string, unknown>;
propertyDataGet$(
@@ -52,8 +55,12 @@ export interface DataSource {
propertyMetaGet(type: string): PropertyMetaConfig;
propertyAdd(insertToPosition: InsertToPosition, type?: string): string;
propertyDuplicate(propertyId: string): string;
propertyDuplicate(propertyId: string): string | undefined;
propertyCanDuplicate(propertyId: string): boolean;
propertyDelete(id: string): void;
propertyCanDelete(propertyId: string): boolean;
contextGet<T>(key: DataViewContextKey<T>): T;
@@ -82,13 +89,24 @@ export interface DataSource {
}
export abstract class DataSourceBase implements DataSource {
propertyTypeCanSet(propertyId: string): boolean {
return !this.isFixedProperty(propertyId);
}
propertyCanDuplicate(propertyId: string): boolean {
return !this.isFixedProperty(propertyId);
}
propertyCanDelete(propertyId: string): boolean {
return !this.isFixedProperty(propertyId);
}
context = new Map<symbol, unknown>();
abstract featureFlags$: ReadonlySignal<DatabaseFlags>;
abstract properties$: ReadonlySignal<string[]>;
abstract propertyMetas: PropertyMetaConfig[];
abstract propertyMetas$: ReadonlySignal<PropertyMetaConfig[]>;
abstract allPropertyMetas$: ReadonlySignal<PropertyMetaConfig[]>;
abstract readonly$: ReadonlySignal<boolean>;
@@ -159,7 +177,7 @@ export abstract class DataSourceBase implements DataSource {
abstract propertyDelete(id: string): void;
abstract propertyDuplicate(propertyId: string): string;
abstract propertyDuplicate(propertyId: string): string | undefined;
abstract propertyMetaGet(type: string): PropertyMetaConfig;
@@ -223,4 +241,31 @@ export abstract class DataSourceBase implements DataSource {
viewMetaGetById$(viewId: string): ReadonlySignal<ViewMeta | undefined> {
return computed(() => this.viewMetaGetById(viewId));
}
fixedProperties$ = computed(() => {
return this.allPropertyMetas$.value
.filter(v => v.config.fixed)
.map(v => v.type);
});
fixedPropertySet = computed(() => {
return new Set(this.fixedProperties$.value);
});
protected abstract getNormalPropertyAndIndex(propertyId: string):
| {
column: Column<Record<string, unknown>>;
index: number;
}
| undefined;
isFixedProperty(propertyId: string) {
if (this.fixedPropertySet.value.has(propertyId)) {
return true;
}
const result = this.getNormalPropertyAndIndex(propertyId);
if (result) {
return this.fixedPropertySet.value.has(result.column.type);
}
return false;
}
}
@@ -119,7 +119,7 @@ export class RecordDetail extends SignalWatcher(
},
items: [
menu.group({
items: this.view.propertyMetas.map(meta => {
items: this.view.propertyMetas$.value.map(meta => {
return menu.action({
name: meta.config.name,
prefix: renderUniLit(this.view.propertyIconGet(meta.type)),
@@ -184,8 +184,7 @@ export class RecordField extends SignalWatcher(
menu.action({
name: 'Duplicate',
prefix: DuplicateIcon(),
hide: () =>
!this.column.duplicate || this.column.type$.value === 'title',
hide: () => !this.column.canDuplicate,
select: () => {
this.column.duplicate?.();
},
@@ -193,8 +192,7 @@ export class RecordField extends SignalWatcher(
menu.action({
name: 'Delete',
prefix: DeleteIcon(),
hide: () =>
!this.column.delete || this.column.type$.value === 'title',
hide: () => !this.column.canDelete,
select: () => {
this.column.delete?.();
},
@@ -0,0 +1 @@
export * from './trait.js';
@@ -3,6 +3,7 @@ export * from './component/index.js';
export { DataSourceBase } from './data-source/base.js';
export { DataView } from './data-view.js';
export * from './filter/index.js';
export * from './group-by';
export * from './logical/index.js';
export * from './property/index.js';
export type { DataViewSelection } from './types.js';
@@ -15,6 +15,12 @@ export type PropertyConfig<
Value = unknown,
> = {
name: string;
hide?: boolean;
fixed?: {
defaultData: Data;
defaultOrder?: string;
defaultShow?: boolean;
};
defaultData: () => Data;
type: (
config: WithCommonPropertyConfig<{
@@ -50,7 +56,7 @@ export type PropertyConfig<
};
cellToJson: (
config: WithCommonPropertyConfig<{
value: Value;
value?: Value;
data: Data;
}>
) => DVJSON;
@@ -23,7 +23,10 @@ export interface Property<
readonly icon?: UniComponent;
readonly delete?: () => void;
get canDelete(): boolean;
readonly duplicate?: () => void;
get canDuplicate(): boolean;
cellGet(rowId: string): Cell<Value>;
@@ -32,12 +35,14 @@ export interface Property<
readonly type$: ReadonlySignal<string>;
readonly typeSet?: (type: string) => void;
get typeCanSet(): boolean;
readonly name$: ReadonlySignal<string>;
nameSet(name: string): void;
readonly hide$: ReadonlySignal<boolean>;
hideSet(hide: boolean): void;
get hideCanSet(): boolean;
valueGet(rowId: string): Value | undefined;
valueSet(rowId: string, value: Value | undefined): void;
@@ -123,6 +128,18 @@ export abstract class PropertyBase<
public view: SingleView,
public propertyId: string
) {}
get canDelete(): boolean {
return this.view.propertyCanDelete(this.id);
}
get canDuplicate(): boolean {
return this.view.propertyCanDuplicate(this.id);
}
get typeCanSet(): boolean {
return this.view.propertyTypeCanSet(this.id);
}
get hideCanSet(): boolean {
return this.view.propertyCanHide(this.id);
}
cellGet(rowId: string): Cell<Value> {
return this.view.cellGet(rowId, this.id) as Cell<Value>;
@@ -80,13 +80,15 @@ export interface SingleView {
rowNextGet(rowId: string): string | undefined;
readonly propertyMetas: PropertyMetaConfig[];
readonly propertyMetas$: ReadonlySignal<PropertyMetaConfig[]>;
propertyAdd(toAfterOfProperty: InsertToPosition, type?: string): string;
propertyDelete(propertyId: string): void;
propertyCanDelete(propertyId: string): boolean;
propertyDuplicate(propertyId: string): void;
propertyCanDuplicate(propertyId: string): boolean;
propertyGet(propertyId: string): Property;
@@ -103,10 +105,12 @@ export interface SingleView {
propertyTypeGet(propertyId: string): string | undefined;
propertyTypeSet(propertyId: string, type: string): void;
propertyTypeCanSet(propertyId: string): boolean;
propertyHideGet(propertyId: string): boolean;
propertyHideSet(propertyId: string, hide: boolean): void;
propertyCanHide(propertyId: string): boolean;
propertyDataGet(propertyId: string): Record<string, unknown>;
@@ -215,8 +219,8 @@ export abstract class SingleViewBase<
return this.dataSource.viewMetaGet(this.type);
}
get propertyMetas(): PropertyMetaConfig[] {
return this.dataSource.propertyMetas;
get propertyMetas$() {
return this.dataSource.propertyMetas$;
}
abstract get type(): string;
@@ -225,6 +229,18 @@ export abstract class SingleViewBase<
public manager: ViewManager,
public id: string
) {}
propertyCanDelete(propertyId: string): boolean {
return this.dataSource.propertyCanDelete(propertyId);
}
propertyCanDuplicate(propertyId: string): boolean {
return this.dataSource.propertyCanDuplicate(propertyId);
}
propertyTypeCanSet(propertyId: string): boolean {
return this.dataSource.propertyTypeCanSet(propertyId);
}
propertyCanHide(propertyId: string): boolean {
return this.propertyTypeGet(propertyId) !== 'title';
}
private searchRowsMapping(rows: string[], searchString: string): string[] {
return rows.filter(id => {
@@ -370,6 +386,9 @@ export abstract class SingleViewBase<
propertyDuplicate(propertyId: string): void {
const id = this.dataSource.propertyDuplicate(propertyId);
if (!id) {
return;
}
this.propertyMove(id, {
before: false,
id: propertyId,