Files
AFFiNE-Mirror/blocksuite/affine/blocks/table/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

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 });
};