mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 18:16:15 +08:00
c28f918527
Part of: [BS-2269](https://linear.app/affine-design/issue/BS-2269/%E8%BF%81%E7%A7%BB-database-block-%E5%88%B0-affine-%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%8B%E5%B9%B6%E5%BC%80%E5%90%AF-nouncheckedindexedaccess)
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import type { BlockCommands, Command } from '@blocksuite/block-std';
|
|
|
|
export const insertDatabaseBlockCommand: Command<
|
|
'selectedModels',
|
|
'insertedDatabaseBlockId',
|
|
{
|
|
viewType: string;
|
|
place?: 'after' | 'before';
|
|
removeEmptyLine?: boolean;
|
|
}
|
|
> = (ctx, next) => {
|
|
const { selectedModels, viewType, place, removeEmptyLine, std } = ctx;
|
|
if (!selectedModels?.length) return;
|
|
|
|
const targetModel =
|
|
place === 'before'
|
|
? selectedModels[0]
|
|
: selectedModels[selectedModels.length - 1];
|
|
|
|
const service = std.getService('affine:database');
|
|
if (!service) return;
|
|
|
|
const result = std.doc.addSiblingBlocks(
|
|
targetModel,
|
|
[{ flavour: 'affine:database' }],
|
|
place
|
|
);
|
|
if (result.length === 0) return;
|
|
|
|
service.initDatabaseBlock(std.doc, targetModel, result[0], viewType, false);
|
|
|
|
if (removeEmptyLine && targetModel.text?.length === 0) {
|
|
std.doc.deleteBlock(targetModel);
|
|
}
|
|
|
|
next({ insertedDatabaseBlockId: result[0] });
|
|
};
|
|
|
|
export const commands: BlockCommands = {
|
|
insertDatabaseBlock: insertDatabaseBlockCommand,
|
|
};
|