mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-09 05:05:52 +08:00
@@ -1,4 +1,11 @@
|
||||
import type { DatabaseBlockModel } from '@blocksuite/affine-model';
|
||||
import type { BlockCommands, Command } from '@blocksuite/block-std';
|
||||
import type { BlockModel, Store } from '@blocksuite/store';
|
||||
|
||||
import {
|
||||
DatabaseBlockDataSource,
|
||||
databaseViewInitTemplate,
|
||||
} from './data-source';
|
||||
|
||||
export const insertDatabaseBlockCommand: Command<
|
||||
'selectedModels',
|
||||
@@ -17,8 +24,7 @@ export const insertDatabaseBlockCommand: Command<
|
||||
? selectedModels[0]
|
||||
: selectedModels[selectedModels.length - 1];
|
||||
|
||||
const service = std.getService('affine:database');
|
||||
if (!service || !targetModel) return;
|
||||
if (!targetModel) return;
|
||||
|
||||
const result = std.store.addSiblingBlocks(
|
||||
targetModel,
|
||||
@@ -29,7 +35,7 @@ export const insertDatabaseBlockCommand: Command<
|
||||
|
||||
if (string == null) return;
|
||||
|
||||
service.initDatabaseBlock(std.store, targetModel, string, viewType, false);
|
||||
initDatabaseBlock(std.store, targetModel, string, viewType, false);
|
||||
|
||||
if (removeEmptyLine && targetModel.text?.length === 0) {
|
||||
std.store.deleteBlock(targetModel);
|
||||
@@ -38,6 +44,28 @@ export const insertDatabaseBlockCommand: Command<
|
||||
next({ insertedDatabaseBlockId: string });
|
||||
};
|
||||
|
||||
export const initDatabaseBlock = (
|
||||
doc: Store,
|
||||
model: BlockModel,
|
||||
databaseId: string,
|
||||
viewType: string,
|
||||
isAppendNewRow = true
|
||||
) => {
|
||||
const blockModel = doc.getBlock(databaseId)?.model as
|
||||
| DatabaseBlockModel
|
||||
| undefined;
|
||||
if (!blockModel) {
|
||||
return;
|
||||
}
|
||||
const datasource = new DatabaseBlockDataSource(blockModel);
|
||||
databaseViewInitTemplate(datasource, viewType);
|
||||
if (isAppendNewRow) {
|
||||
const parent = doc.getParent(model);
|
||||
if (!parent) return;
|
||||
doc.addBlock('affine:paragraph', {}, parent.id);
|
||||
}
|
||||
};
|
||||
|
||||
export const commands: BlockCommands = {
|
||||
insertDatabaseBlock: insertDatabaseBlockCommand,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user