Skip to content

Commit

Permalink
feat(sheets): remove rows support multiple ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Dec 25, 2024
1 parent 862e70b commit d75d3ce
Show file tree
Hide file tree
Showing 20 changed files with 774 additions and 583 deletions.
71 changes: 69 additions & 2 deletions examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { UniverDocsMentionUIPlugin } from '@univerjs/docs-mention-ui';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { UniverSheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting';
Expand Down Expand Up @@ -127,7 +126,75 @@ userManagerService.setCurrentUser(mockUser);

// create univer sheet instance
if (!IS_E2E) {
univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
univer.createUnit(UniverInstanceType.UNIVER_SHEET, {
id: 'test',
appVersion: '3.0.0-alpha',
sheets: {
// 1
// 2-3-
// 4
// |
sheet1: {
id: 'sheet1',
cellData: {
0: {
0: {
v: 'A1',
s: 's1',
},
},
1: {
1: {
v: 'B2',
s: 's2',
},
4: {
v: 'E2',
s: 's3',
},
},
2: {
1: {
v: 'B3',
s: 's4',
},
},
},
mergeData: [
{ startRow: 1, endRow: 1, startColumn: 2, endColumn: 3 },
{
startRow: 2,
endRow: 3,
startColumn: 2,
endColumn: 2,
},
{
startRow: 10,
endRow: 15,
startColumn: 2,
endColumn: 2,
},
{
startRow: 10,
endRow: 10,
startColumn: 10,
endColumn: 15,
},
],
rowCount: 20,
columnCount: 20,
},
},
locale: LocaleType.ZH_CN,
name: '',
sheetOrder: [],
styles: {
s1: { bg: { rgb: '#ff0000' } },
s2: { bl: 0 },
s3: { bl: 1 },
s4: { fs: 12 },
},
});
}

setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICommandInfo, IDrawingParam, IMutationInfo, IRange, ITransformState, Nullable, Workbook } from '@univerjs/core';
import type { IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveRowColCommandParams, ISetColHiddenMutationParams, ISetColVisibleMutationParams, ISetRowHiddenMutationParams, ISetRowVisibleMutationParams, ISetSpecificColsVisibleCommandParams, ISetSpecificRowsVisibleCommandParams, ISetWorksheetActiveOperationParams, ISetWorksheetColWidthMutationParams, ISetWorksheetRowHeightMutationParams, ISetWorksheetRowIsAutoHeightMutationParams } from '@univerjs/sheets';
import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, InsertRangeMoveDownCommandParams, InsertRangeMoveRightCommandParams, IRemoveRowColCommandParams, ISetColHiddenMutationParams, ISetColVisibleMutationParams, ISetRowHiddenMutationParams, ISetRowVisibleMutationParams, ISetSpecificColsVisibleCommandParams, ISetSpecificRowsVisibleCommandParams, ISetWorksheetActiveOperationParams, ISetWorksheetColWidthMutationParams, ISetWorksheetRowHeightMutationParams, ISetWorksheetRowIsAutoHeightMutationParams } from '@univerjs/sheets';
import type { ISheetDrawing, ISheetDrawingPosition } from '@univerjs/sheets-drawing';
import { Disposable, ICommandService, Inject, IUniverInstanceService, Rectangle } from '@univerjs/core';
import { type IDrawingJsonUndo1, IDrawingManagerService } from '@univerjs/drawing';
Expand Down Expand Up @@ -87,6 +87,7 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
this.disposeWithMe(
this._sheetInterceptorService.interceptCommand({

// eslint-disable-next-line complexity
getMutations: (commandInfo) => {
if (!UPDATE_COMMANDS.includes(commandInfo.id)) {
return { redos: [], undos: [] };
Expand All @@ -106,16 +107,16 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
} else if (cId === RemoveColCommand.id) {
return this._moveColInterceptor(commandInfo.params as IRemoveRowColCommandParams, 'remove');
} else if (cId === DeleteRangeMoveLeftCommand.id) {
const { range } = commandInfo.params as IRemoveRowColCommandParams;
const { range } = commandInfo.params as IDeleteRangeMoveLeftCommandParams;
return this._getRangeMoveUndo(range, RangeMoveUndoType.deleteLeft);
} else if (cId === DeleteRangeMoveUpCommand.id) {
const { range } = commandInfo.params as IRemoveRowColCommandParams;
const { range } = commandInfo.params as IDeleteRangeMoveUpCommandParams;
return this._getRangeMoveUndo(range, RangeMoveUndoType.deleteUp);
} else if (cId === InsertRangeMoveDownCommand.id) {
const { range } = commandInfo.params as IRemoveRowColCommandParams;
const { range } = commandInfo.params as InsertRangeMoveDownCommandParams;
return this._getRangeMoveUndo(range, RangeMoveUndoType.insertDown);
} else if (cId === InsertRangeMoveRightCommand.id) {
const { range } = commandInfo.params as IRemoveRowColCommandParams;
const { range } = commandInfo.params as InsertRangeMoveRightCommandParams;
return this._getRangeMoveUndo(range, RangeMoveUndoType.insertRight);
} else if (cId === SetRowHiddenCommand.id || cId === SetSpecificRowsVisibleCommand.id) {
const params = commandInfo.params as ISetRowHiddenMutationParams | ISetSpecificRowsVisibleCommandParams;
Expand Down Expand Up @@ -569,7 +570,7 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
return this._createUndoAndRedoMutation(unitId, subUnitId, updateDrawings);
}

private _getUnitIdAndSubUnitId(params: IInsertRowCommandParams | IRemoveRowColCommandParams, type: 'insert' | 'remove') {
private _getUnitIdAndSubUnitId(params: IInsertRowCommandParams | IRemoveRowColCommandParams | IInsertColCommandParams, type: 'insert' | 'remove') {
let unitId: string;
let subUnitId: string;
if (type === 'insert') {
Expand Down Expand Up @@ -677,16 +678,20 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
return { redos, undos };
}

// eslint-disable-next-line max-lines-per-function
private _moveRowInterceptor(params: IInsertRowCommandParams | IRemoveRowColCommandParams, type: 'insert' | 'remove') {
const ids = this._getUnitIdAndSubUnitId(params, type);
if (ids == null) {
return { redos: [], undos: [] };
}
const { unitId, subUnitId } = ids;
const { range } = params;

const rowStartIndex = range.startRow;
const rowEndIndex = range.endRow;
let ranges: IRange[] = [];
if (type === 'insert') {
ranges = [(params as IInsertRowCommandParams).range];
} else {
ranges = (params as IRemoveRowColCommandParams).ranges;
}

const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
Expand All @@ -695,37 +700,42 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
const updateDrawings: Partial<ISheetDrawing>[] = [];
const deleteDrawings: Partial<ISheetDrawing>[] = [];

Object.keys(data).forEach((drawingId) => {
const drawing = data[drawingId];
const { sheetTransform, transform, anchorType = SheetDrawingAnchorType.Position } = drawing;
ranges.forEach((range) => {
const rowStartIndex = range.startRow;
const rowEndIndex = range.endRow;

if (sheetTransform == null || transform == null) {
return;
}
let newSheetTransform: Nullable<ISheetDrawingPosition>;
let newTransform: Nullable<ITransformState>;
if (type === 'insert') {
const param = this._expandRow(sheetTransform, transform, rowStartIndex, rowEndIndex, anchorType);
newSheetTransform = param?.newSheetTransform;
newTransform = param?.newTransform;
} else {
const { from, to } = sheetTransform;
const { row: fromRow } = from;
const { row: toRow } = to;
if (anchorType === SheetDrawingAnchorType.Both && fromRow >= rowStartIndex && toRow <= rowEndIndex) {
// delete drawing
deleteDrawings.push({ unitId, subUnitId, drawingId });
} else {
const param = this._shrinkRow(sheetTransform, transform, rowStartIndex, rowEndIndex, anchorType);
Object.keys(data).forEach((drawingId) => {
const drawing = data[drawingId];
const { sheetTransform, transform, anchorType = SheetDrawingAnchorType.Position } = drawing;

if (sheetTransform == null || transform == null) {
return;
}
let newSheetTransform: Nullable<ISheetDrawingPosition>;
let newTransform: Nullable<ITransformState>;
if (type === 'insert') {
const param = this._expandRow(sheetTransform, transform, rowStartIndex, rowEndIndex, anchorType);
newSheetTransform = param?.newSheetTransform;
newTransform = param?.newTransform;
} else {
const { from, to } = sheetTransform;
const { row: fromRow } = from;
const { row: toRow } = to;
if (anchorType === SheetDrawingAnchorType.Both && fromRow >= rowStartIndex && toRow <= rowEndIndex) {
// delete drawing
deleteDrawings.push({ unitId, subUnitId, drawingId });
} else {
const param = this._shrinkRow(sheetTransform, transform, rowStartIndex, rowEndIndex, anchorType);
newSheetTransform = param?.newSheetTransform;
newTransform = param?.newTransform;
}
}
}
if (!newSheetTransform || !newTransform) {
return;
}
const params = { unitId, subUnitId, drawingId, transform: newTransform, sheetTransform: newSheetTransform };
updateDrawings.push(params);
if (!newSheetTransform || !newTransform) {
return;
}
const params = { unitId, subUnitId, drawingId, transform: newTransform, sheetTransform: newSheetTransform };
updateDrawings.push(params);
});
});

if (updateDrawings.length === 0 && deleteDrawings.length === 0) {
Expand Down Expand Up @@ -757,16 +767,20 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
};
}

// eslint-disable-next-line max-lines-per-function
private _moveColInterceptor(params: IInsertColCommandParams | IRemoveRowColCommandParams, type: 'insert' | 'remove') {
const ids = this._getUnitIdAndSubUnitId(params, type);
if (ids == null) {
return { redos: [], undos: [] };
}
const { unitId, subUnitId } = ids;
const { range } = params;

const colStartIndex = range.startColumn;
const colEndIndex = range.endColumn;
let ranges: IRange[] = [];
if (type === 'insert') {
ranges = [(params as IInsertColCommandParams).range];
} else {
ranges = (params as IRemoveRowColCommandParams).ranges;
}

const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
Expand All @@ -775,39 +789,44 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen
const updateDrawings: Partial<ISheetDrawing>[] = [];
const deleteDrawings: Partial<ISheetDrawing>[] = [];

Object.keys(data).forEach((drawingId) => {
const drawing = data[drawingId];
const { sheetTransform, transform, anchorType = SheetDrawingAnchorType.Position } = drawing;
ranges.forEach((range) => {
const colStartIndex = range.startColumn;
const colEndIndex = range.endColumn;

if (sheetTransform == null || transform == null) {
return;
}
let newSheetTransform: Nullable<ISheetDrawingPosition>;
let newTransform: Nullable<ITransformState>;
if (type === 'insert') {
const param = this._expandCol(sheetTransform, transform, colStartIndex, colEndIndex, anchorType);
newSheetTransform = param?.newSheetTransform;
newTransform = param?.newTransform;
} else {
const { from, to } = sheetTransform;
const { column: fromColumn } = from;
const { column: toColumn } = to;
if (anchorType === SheetDrawingAnchorType.Both && fromColumn >= colStartIndex && toColumn <= colEndIndex) {
// delete drawing
deleteDrawings.push({ unitId, subUnitId, drawingId });
} else {
const param = this._shrinkCol(sheetTransform, transform, colStartIndex, colEndIndex, anchorType);
Object.keys(data).forEach((drawingId) => {
const drawing = data[drawingId];
const { sheetTransform, transform, anchorType = SheetDrawingAnchorType.Position } = drawing;

if (sheetTransform == null || transform == null) {
return;
}
let newSheetTransform: Nullable<ISheetDrawingPosition>;
let newTransform: Nullable<ITransformState>;
if (type === 'insert') {
const param = this._expandCol(sheetTransform, transform, colStartIndex, colEndIndex, anchorType);
newSheetTransform = param?.newSheetTransform;
newTransform = param?.newTransform;
} else {
const { from, to } = sheetTransform;
const { column: fromColumn } = from;
const { column: toColumn } = to;
if (anchorType === SheetDrawingAnchorType.Both && fromColumn >= colStartIndex && toColumn <= colEndIndex) {
// delete drawing
deleteDrawings.push({ unitId, subUnitId, drawingId });
} else {
const param = this._shrinkCol(sheetTransform, transform, colStartIndex, colEndIndex, anchorType);
newSheetTransform = param?.newSheetTransform;
newTransform = param?.newTransform;
}
}
}

if (!newSheetTransform || !newTransform) {
return;
}
if (!newSheetTransform || !newTransform) {
return;
}

const params = { unitId, subUnitId, drawingId, transform: newTransform, sheetTransform: newSheetTransform };
updateDrawings.push(params);
const params = { unitId, subUnitId, drawingId, transform: newTransform, sheetTransform: newSheetTransform };
updateDrawings.push(params);
});
});

if (updateDrawings.length === 0 && deleteDrawings.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import type { IWorkbookData, Workbook } from '@univerjs/core';
import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, LocaleType, Plugin, RANGE_TYPE, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import type { IRemoveRowColCommandParams, ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { CopySheetCommand, InsertColMutation, InsertSheetMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets';
import { SHEET_FILTER_SNAPSHOT_ID, SheetsFilterService } from '../../services/sheet-filter.service';
import { SheetsFilterController } from '../sheets-filter.controller';
Expand Down Expand Up @@ -317,13 +317,17 @@ describe('test controller of sheets filter', () => {
},
],
})).toBeTruthy();
expect(await commandService.executeCommand(RemoveRowCommand.id, {
unitId: 'test',
subUnitId: 'sheet1',
range: { startRow: 0, startColumn: 0, endRow: 0, endColumn: 5 },
})).toBeTruthy();

const params: IRemoveRowColCommandParams = {
ranges: [
{ startRow: 0, startColumn: 0, endRow: 0, endColumn: 5 },
{ startRow: 1, startColumn: 0, endRow: 1, endColumn: 5 },
],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getRange())
.toEqual({ startRow: 2, startColumn: 0, endRow: 4, endColumn: 5 });
.toEqual({ startRow: 1, startColumn: 0, endRow: 3, endColumn: 5 });
});
});

Expand Down
Loading

0 comments on commit d75d3ce

Please sign in to comment.