Skip to content

Commit

Permalink
feat: add insert cols
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Feb 5, 2025
1 parent be6d562 commit 7f2a63b
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 13 deletions.
40 changes: 30 additions & 10 deletions packages/sheets-ui/src/controllers/menu.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
ClearSelectionFormatCommand, CopySheetCommand,
InsertColAfterCommand,

Check failure on line 27 in packages/sheets-ui/src/controllers/menu.schema.ts

View workflow job for this annotation

GitHub Actions / eslint

'InsertColAfterCommand' is defined but never used
InsertColBeforeCommand,
InsertMultiColsLeftCommand,
InsertMultiColsRightCommand,
InsertMultiRowsAboveCommand,
InsertMultiRowsAfterCommand,
InsertRowAfterCommand,
Expand Down Expand Up @@ -99,6 +101,8 @@ import {
ColInsertMenuItemFactory,

Check failure on line 101 in packages/sheets-ui/src/controllers/menu.schema.ts

View workflow job for this annotation

GitHub Actions / eslint

'ColInsertMenuItemFactory' is defined but never used
InsertColAfterMenuItemFactory,

Check failure on line 102 in packages/sheets-ui/src/controllers/menu.schema.ts

View workflow job for this annotation

GitHub Actions / eslint

'InsertColAfterMenuItemFactory' is defined but never used
InsertColBeforeMenuItemFactory,
InsertMultiColsLeftMenuItemFactory,
InsertMultiColsRightMenuItemFactory,
InsertMultiRowsAboveMenuItemFactory,
InsertMultiRowsAfterMenuItemFactory,
InsertRangeMoveDownMenuItemFactory,
Expand Down Expand Up @@ -462,17 +466,33 @@ export const menuSchema: MenuSchemaType = {
},
[ContextMenuGroup.LAYOUT]: {
order: 1,
[COL_INSERT_MENU_ID]: {
// [COL_INSERT_MENU_ID]: {
// order: 0,
// menuItemFactory: ColInsertMenuItemFactory,
// [InsertColBeforeCommand.id]: {
// order: 0,
// menuItemFactory: InsertColBeforeMenuItemFactory,
// },
// [InsertColAfterCommand.id]: {
// order: 1,
// menuItemFactory: InsertColAfterMenuItemFactory,
// },
// [InsertMultiColsLeftCommand.id]: {
// order: 2,
// menuItemFactory: InsertMultiColsLeftMenuItemFactory,
// },
// [InsertMultiColsRightCommand.id]: {
// order: 3,
// menuItemFactory: InsertMultiColsRightMenuItemFactory,
// },
// },
[InsertMultiColsLeftCommand.id]: {
order: 0,
menuItemFactory: ColInsertMenuItemFactory,
[InsertColBeforeCommand.id]: {
order: 0,
menuItemFactory: InsertColBeforeMenuItemFactory,
},
[InsertColAfterCommand.id]: {
order: 1,
menuItemFactory: InsertColAfterMenuItemFactory,
},
menuItemFactory: InsertMultiColsLeftMenuItemFactory,
},
[InsertMultiColsRightCommand.id]: {
order: 0,
menuItemFactory: InsertMultiColsRightMenuItemFactory,
},
[HideColConfirmCommand.id]: {
order: 1,
Expand Down
47 changes: 47 additions & 0 deletions packages/sheets-ui/src/controllers/menu/insert.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
import {
InsertColAfterCommand,
InsertColBeforeCommand,
InsertMultiColsLeftCommand,
InsertMultiRowsAboveCommand,
InsertMultiRowsAfterCommand,
InsertRowAfterCommand,
Expand Down Expand Up @@ -182,3 +183,49 @@ export function InsertMultiRowsAboveMenuItemFactory(accessor: IAccessor): IMenuB
hidden$: getInsertAfterMenuHidden$(accessor, 'row'),
};
}

export function InsertMultiColsLeftMenuItemFactory(accessor: IAccessor): IMenuButtonItem<number> {
return {
id: InsertMultiColsLeftCommand.id,
type: MenuItemType.BUTTON,
icon: 'LeftInsertColumn',
label: {
name: MENU_ITEM_INPUT_COMPONENT,
props: {
prefix: 'rightClick.insertColsLeft',
min: 1,
max: 1000,
suffix: 'rightClick.insertColsLeftSuffix',
},
},
value$: new Observable<number>((subscriber) => {
subscriber.next(1);
subscriber.complete();
}),
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetInsertRowPermission, WorksheetEditPermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
hidden$: getInsertAfterMenuHidden$(accessor, 'col'),
};
}

export function InsertMultiColsRightMenuItemFactory(accessor: IAccessor): IMenuButtonItem<number> {
return {
id: InsertMultiRowsAboveCommand.id,
type: MenuItemType.BUTTON,
icon: 'RightInsertColumn',
label: {
name: MENU_ITEM_INPUT_COMPONENT,
props: {
prefix: 'rightClick.insertColsRight',
min: 1,
max: 1000,
suffix: 'rightClick.insertColsRightSuffix',
},
},
value$: new Observable<number>((subscriber) => {
subscriber.next(1);
subscriber.complete();
}),
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetInsertRowPermission, WorksheetEditPermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
hidden$: getInsertAfterMenuHidden$(accessor, 'col'),
};
}
4 changes: 4 additions & 0 deletions packages/sheets-ui/src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ const locale: typeof zhCN = {
insertRowsAboveSuffix: 'rows above',
insertColumn: 'Insert Column',
insertColumnBefore: 'Insert Column Before',
insertColsLeft: 'Insert',
insertColsRight: 'Insert',
insertColsLeftSuffix: 'cols left',
insertColsRightSuffix: 'cols right',
delete: 'Delete',
deleteCell: 'Delete Cell',
insertCell: 'Insert Cell',
Expand Down
4 changes: 4 additions & 0 deletions packages/sheets-ui/src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ const locale = {
insertRowsAboveSuffix: '行',
insertColumn: '插入列',
insertColumnBefore: '在左侧插入列',
insertColsLeft: '在左侧插入列',
insertColsRight: '在右侧插入列',
insertColsLeftSuffix: '列',
insertColsRightSuffix: '列',
deleteCell: '删除单元格',
insertCell: '插入单元格',
deleteSelected: '删除选中',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import {
InsertColBeforeCommand,
InsertColByRangeCommand,
InsertColCommand,
InsertMultiColsLeftCommand,
InsertMultiColsRightCommand,
InsertMultiRowsAboveCommand,
InsertMultiRowsAfterCommand,
InsertRowAfterCommand,
InsertRowBeforeCommand,
InsertRowByRangeCommand,
Expand All @@ -68,8 +72,13 @@ describe('Test insert and remove rows cols commands', () => {
InsertRowCommand,
InsertRowBeforeCommand,
InsertRowAfterCommand,
InsertMultiRowsAboveCommand,
InsertMultiRowsAfterCommand,
InsertRowByRangeCommand,
InsertColAfterCommand,
InsertColBeforeCommand,
InsertMultiColsLeftCommand,
InsertMultiColsRightCommand,
InsertColCommand,
InsertColByRangeCommand,
InsertRowByRangeCommand,
Expand Down
99 changes: 96 additions & 3 deletions packages/sheets/src/commands/commands/insert-row-col.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ export const InsertRowAfterCommand: ICommand = {
},
};

export interface IInsertMultiRowCommandParams {
export interface IInsertMultiRowsCommandParams {
value: number;
}

export const InsertMultiRowsAboveCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.insert-multi-rows-above',
handler: async (accessor: IAccessor, params: IInsertMultiRowCommandParams) => {
handler: async (accessor: IAccessor, params: IInsertMultiRowsCommandParams) => {
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
let range: IRange;
Expand Down Expand Up @@ -315,7 +315,7 @@ export const InsertMultiRowsAboveCommand: ICommand = {
export const InsertMultiRowsAfterCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.insert-multi-rows-after',
handler: async (accessor: IAccessor, params: IInsertMultiRowCommandParams) => {
handler: async (accessor: IAccessor, params: IInsertMultiRowsCommandParams) => {
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
let range: IRange;
Expand Down Expand Up @@ -558,3 +558,96 @@ export const InsertColAfterCommand: ICommand = {
return accessor.get(ICommandService).executeCommand(InsertColCommand.id, insertColParams);
},
};

export interface IInsertMultiColsCommandParams {
value: number;
}

export const InsertMultiColsLeftCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.insert-multi-cols-before',
handler: async (accessor: IAccessor, params: IInsertMultiRowsCommandParams) => {
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections();
let range: IRange;

if (selections?.length === 1) {
range = selections[0].range;
} else {
return false;
}

const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
if (!target) return false;

const { worksheet, unitId, subUnitId } = target;
const count = params.value || 0;
const startColumn = range.startColumn;
const endColumn = range.startColumn + count - 1;
const startRow = 0;
const endRow = worksheet.getRowCount() - 1;

const insertColParams: IInsertColCommandParams = {
unitId,
subUnitId,
direction: Direction.LEFT,
range: {
startColumn,
endColumn,
startRow,
endRow,
rangeType: RANGE_TYPE.COLUMN,
},

// copy styles from the column before
cellValue: copyRangeStyles(worksheet, startRow, endRow, startColumn, endColumn, false, startColumn - 1),
};

return accessor.get(ICommandService).executeCommand(InsertColCommand.id, insertColParams);
},
};

export const InsertMultiColsRightCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.insert-multi-cols-right',
handler: async (accessor: IAccessor, params: IInsertMultiRowsCommandParams) => {
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections();
let range: IRange;

if (selections?.length === 1) {
range = selections[0].range;
} else {
return false;
}

const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
if (!target) return false;

const { worksheet, unitId, subUnitId } = target;
const count = params.value || 0;

const startColumn = range.endColumn + 1;
const endColumn = range.endColumn + count;
const startRow = 0;
const endRow = worksheet.getRowCount() - 1;

const insertColParams: IInsertColCommandParams = {
unitId,
subUnitId,
direction: Direction.RIGHT,
range: {
startColumn,
endColumn,
startRow,
endRow,
},
// copy styles from the column after
cellValue: copyRangeStyles(worksheet, startRow, endRow, startColumn, endColumn, false, range.endColumn),
};

return accessor.get(ICommandService).executeCommand(InsertColCommand.id, insertColParams);
},
};
4 changes: 4 additions & 0 deletions packages/sheets/src/controllers/basic-worksheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
InsertColBeforeCommand,
InsertColByRangeCommand,
InsertColCommand,
InsertMultiColsLeftCommand,
InsertMultiColsRightCommand,
InsertMultiRowsAboveCommand,
InsertMultiRowsAfterCommand,
InsertRowAfterCommand,
Expand Down Expand Up @@ -217,6 +219,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
DeltaRowHeightCommand,
InsertColAfterCommand,
InsertColBeforeCommand,
InsertMultiColsLeftCommand,
InsertMultiColsRightCommand,
InsertColByRangeCommand,
InsertColCommand,
InsertRangeMoveDownCommand,
Expand Down
2 changes: 2 additions & 0 deletions packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ export {
InsertColBeforeCommand,
InsertColByRangeCommand,
InsertColCommand,
InsertMultiColsLeftCommand,
InsertMultiColsRightCommand,
InsertMultiRowsAboveCommand,
InsertMultiRowsAfterCommand,
InsertRowAfterCommand,
Expand Down

0 comments on commit 7f2a63b

Please sign in to comment.