mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
**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`
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import {
|
|
type TableBlockModel,
|
|
TableModelFlavour,
|
|
} from '@blocksuite/affine-model';
|
|
import type { Command } from '@blocksuite/std';
|
|
import { type BlockModel } from '@blocksuite/store';
|
|
|
|
import { TableDataManager } from './table-data-manager';
|
|
|
|
export const insertTableBlockCommand: Command<
|
|
{
|
|
place?: 'after' | 'before';
|
|
removeEmptyLine?: boolean;
|
|
selectedModels?: BlockModel[];
|
|
},
|
|
{
|
|
insertedTableBlockId: string;
|
|
}
|
|
> = (ctx, next) => {
|
|
const { selectedModels, 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: TableModelFlavour }],
|
|
place
|
|
);
|
|
const blockId = result[0];
|
|
|
|
if (blockId == null) return;
|
|
|
|
const model = std.store.getBlock(blockId)?.model as TableBlockModel;
|
|
if (model == null) return;
|
|
|
|
const dataManager = new TableDataManager(model);
|
|
|
|
dataManager.addNRow(2);
|
|
dataManager.addNColumn(2);
|
|
|
|
if (removeEmptyLine && targetModel.text?.length === 0) {
|
|
std.store.deleteBlock(targetModel);
|
|
}
|
|
|
|
next({ insertedTableBlockId: blockId });
|
|
};
|