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
@@ -1,16 +1,16 @@
import {
databaseBlockColumns,
DatabaseBlockDataSource,
type DatabaseBlockModel,
type ListType,
type ParagraphType,
type ViewBasicDataType,
} from '@blocksuite/blocks';
import { groupTraitKey } from '@blocksuite/data-view';
import { propertyPresets } from '@blocksuite/data-view/property-presets';
import { viewPresets } from '@blocksuite/data-view/view-presets';
import { assertExists } from '@blocksuite/global/utils';
import { Text, type Workspace } from '@blocksuite/store';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { propertyPresets } from '../../../../affine/data-view/src/property-presets';
import type { InitFn } from './utils.js';
export const database: InitFn = (collection: Workspace, id: string) => {
@@ -37,105 +37,74 @@ export const database: InitFn = (collection: Workspace, id: string) => {
},
noteId
);
const database = doc.getBlockById(databaseId) as DatabaseBlockModel;
const datasource = new DatabaseBlockDataSource(database);
datasource.viewManager.viewAdd('table');
database.title = new Text(title);
const richTextId = datasource.propertyAdd(
'end',
databaseBlockColumns.richTextColumnConfig.type
);
Object.values([
propertyPresets.multiSelectPropertyConfig,
propertyPresets.datePropertyConfig,
propertyPresets.numberPropertyConfig,
databaseBlockColumns.linkColumnConfig,
propertyPresets.checkboxPropertyConfig,
propertyPresets.progressPropertyConfig,
]).forEach(column => {
datasource.propertyAdd('end', column.type);
});
if (group) {
const groupTrait =
datasource.viewManager.currentView$.value?.traitGet(groupTraitKey);
groupTrait?.changeGroup(database.columns[1].id);
}
const paragraphTypes: ParagraphType[] = [
'text',
'quote',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
];
paragraphTypes.forEach(type => {
const id = doc.addBlock(
'affine:paragraph',
{ type: type, text: new Text(`Paragraph type ${type}`) },
databaseId
);
datasource.cellValueChange(
id,
richTextId,
new Text(`Paragraph type ${type}`)
);
});
const listTypes: ListType[] = ['numbered', 'bulleted', 'todo', 'toggle'];
new Promise(resolve => requestAnimationFrame(resolve))
.then(() => {
const service = window.host.std.getService('affine:database');
if (!service) return;
service.initDatabaseBlock(
doc,
model,
databaseId,
viewPresets.tableViewMeta.type,
true
);
const database = doc.getBlockById(databaseId) as DatabaseBlockModel;
database.title = new Text(title);
const richTextId = service.addColumn(
database,
'end',
databaseBlockColumns.richTextColumnConfig.create(
databaseBlockColumns.richTextColumnConfig.config.name
)
);
Object.values([
propertyPresets.multiSelectPropertyConfig,
propertyPresets.datePropertyConfig,
propertyPresets.numberPropertyConfig,
databaseBlockColumns.linkColumnConfig,
propertyPresets.checkboxPropertyConfig,
propertyPresets.progressPropertyConfig,
]).forEach(column => {
service.addColumn(
database,
'end',
column.create(column.config.name)
);
});
service.updateView(database, database.views[0].id, () => {
return {
groupBy: group
? {
columnId: database.columns[1].id,
type: 'groupBy',
name: 'select',
}
: undefined,
} as Partial<ViewBasicDataType>;
});
const paragraphTypes: ParagraphType[] = [
'text',
'quote',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
];
paragraphTypes.forEach(type => {
const id = doc.addBlock(
'affine:paragraph',
{ type: type, text: new Text(`Paragraph type ${type}`) },
databaseId
);
service.updateCell(database, id, {
columnId: richTextId,
value: new Text(`Paragraph type ${type}`),
});
});
const listTypes: ListType[] = [
'numbered',
'bulleted',
'todo',
'toggle',
];
listTypes.forEach(type => {
const id = doc.addBlock(
'affine:list',
{ type: type, text: new Text(`List type ${type}`) },
databaseId
);
datasource.cellValueChange(
id,
richTextId,
new Text(`List type ${type}`)
);
});
// Add a paragraph after database
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
datasource.viewManager.viewAdd(viewPresets.kanbanViewMeta.type);
listTypes.forEach(type => {
const id = doc.addBlock(
'affine:list',
{ type: type, text: new Text(`List type ${type}`) },
databaseId
);
service.updateCell(database, id, {
columnId: richTextId,
value: new Text(`List type ${type}`),
});
});
// Add a paragraph after database
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
service.databaseViewAddView(
database,
viewPresets.kanbanViewMeta.type
);
doc.resetHistory();
})
.catch(console.error);
doc.resetHistory();
};
// Add database block inside note block
addDatabase('Database 1', false);