Files
AFFiNE-Mirror/blocksuite/affine/blocks/database/src/commands.ts
Saul-Mirone 1f45cc5dec refactor(editor): unify directories naming (#11516)
**Directory Structure Changes**

- Renamed multiple block-related directories by removing the "block-" prefix:
  - `block-attachment` → `attachment`
  - `block-bookmark` → `bookmark`
  - `block-callout` → `callout`
  - `block-code` → `code`
  - `block-data-view` → `data-view`
  - `block-database` → `database`
  - `block-divider` → `divider`
  - `block-edgeless-text` → `edgeless-text`
  - `block-embed` → `embed`
2025-04-07 12:34:40 +00:00

70 lines
1.7 KiB
TypeScript

import type { DatabaseBlockModel } from '@blocksuite/affine-model';
import type { Command } from '@blocksuite/std';
import type { BlockModel, Store } from '@blocksuite/store';
import {
DatabaseBlockDataSource,
databaseViewInitTemplate,
} from './data-source';
export const insertDatabaseBlockCommand: Command<
{
selectedModels?: BlockModel[];
viewType: string;
place?: 'after' | 'before';
removeEmptyLine?: boolean;
},
{
insertedDatabaseBlockId: string;
}
> = (ctx, next) => {
const { selectedModels, viewType, place, removeEmptyLine, std } = ctx;
if (!selectedModels?.length) return;
const targetModel =
place === 'before'
? selectedModels[0]
: selectedModels[selectedModels.length - 1];
if (!targetModel) return;
const result = std.store.addSiblingBlocks(
targetModel,
[{ flavour: 'affine:database' }],
place
);
const string = result[0];
if (string == null) return;
initDatabaseBlock(std.store, targetModel, string, viewType, false);
if (removeEmptyLine && targetModel.text?.length === 0) {
std.store.deleteBlock(targetModel);
}
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);
}
};