mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 05:07:06 +08:00
@@ -1,7 +1,12 @@
|
||||
import { TableModelFlavour } from '@blocksuite/affine-model';
|
||||
import { generateFractionalIndexingKeyBetween } from '@blocksuite/affine-shared/utils';
|
||||
import {
|
||||
type TableBlockModel,
|
||||
TableModelFlavour,
|
||||
} from '@blocksuite/affine-model';
|
||||
import type { Command } from '@blocksuite/block-std';
|
||||
import { type BlockModel, nanoid, Text } from '@blocksuite/store';
|
||||
import { type BlockModel } from '@blocksuite/store';
|
||||
|
||||
import { TableDataManager } from './table-data-manager';
|
||||
|
||||
export const insertTableBlockCommand: Command<
|
||||
{
|
||||
place?: 'after' | 'before';
|
||||
@@ -22,41 +27,25 @@ export const insertTableBlockCommand: Command<
|
||||
|
||||
if (!targetModel) return;
|
||||
|
||||
const row1Id = nanoid();
|
||||
const row2Id = nanoid();
|
||||
const col1Id = nanoid();
|
||||
const col2Id = nanoid();
|
||||
const order1 = generateFractionalIndexingKeyBetween(null, null);
|
||||
const order2 = generateFractionalIndexingKeyBetween(order1, null);
|
||||
|
||||
const initialTableData = {
|
||||
rows: {
|
||||
[row1Id]: { rowId: row1Id, order: order1 },
|
||||
[row2Id]: { rowId: row2Id, order: order2 },
|
||||
},
|
||||
columns: {
|
||||
[col1Id]: { columnId: col1Id, order: order1 },
|
||||
[col2Id]: { columnId: col2Id, order: order2 },
|
||||
},
|
||||
cells: {
|
||||
[`${row1Id}:${col1Id}`]: { text: new Text() },
|
||||
[`${row1Id}:${col2Id}`]: { text: new Text() },
|
||||
[`${row2Id}:${col1Id}`]: { text: new Text() },
|
||||
[`${row2Id}:${col2Id}`]: { text: new Text() },
|
||||
},
|
||||
};
|
||||
|
||||
const result = std.store.addSiblingBlocks(
|
||||
targetModel,
|
||||
[{ flavour: TableModelFlavour, ...initialTableData }],
|
||||
[{ flavour: TableModelFlavour }],
|
||||
place
|
||||
);
|
||||
const blockId = result[0];
|
||||
|
||||
if (blockId == null) return;
|
||||
|
||||
if (removeEmptyLine && targetModel.text?.length === 0) {
|
||||
std.store.deleteBlock(targetModel);
|
||||
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 && model.text?.length === 0) {
|
||||
std.store.deleteBlock(model);
|
||||
}
|
||||
|
||||
next({ insertedTableBlockId: blockId });
|
||||
|
||||
Reference in New Issue
Block a user