Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheets-drawing): preserve position anchored drawing size (#7815)

wpxp1234566615a129d944
diff --git a/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts b/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts
index 649a48cae3..4a5d0d46be 100644
--- a/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts
+++ b/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts
@@ -21,7 +21,9 @@ import { DrawingTypeEnum, ImageSourceType, RedoCommandId, Tools, UndoCommandId }
import { IDrawingManagerService } from '@univerjs/drawing';
import { RemoveColByRangeCommand, RemoveRowByRangeCommand, SheetSkeletonService } from '@univerjs/sheets';
import {
+ applySheetDrawingPlacement,
drawingPositionToTransform,
+ getSheetDrawingPlacement,
InsertSheetDrawingCommand,
ISheetDrawingService,
SheetDrawingAnchorType,
@@ -132,6 +134,40 @@ describe('SheetDrawingActiveRenderController', () => {
}
});
+ it('preserves one-cell anchored size during activation after column widths change', async () => {
+ const bed = createTestBed();
+ try {
+ const drawing = await insertDrawing(bed, SheetDrawingAnchorType.Position);
+ bed.get(SheetDrawingActiveRenderController);
+ const sheetService = bed.get(ISheetDrawingService);
+ const manager = bed.get(IDrawingManagerService);
+ manager.registerDrawingData(bed.unitId, Tools.deepClone(sheetService.getDrawingDataForUnit(bed.unitId)));
+ const before = Tools.deepClone(sheetService.getDrawingByParam(drawing)!);
+ const columnManager = bed.workbook.getActiveSheet().getColumnManager();
+ columnManager.setColumnWidth(1, 140);
+ for (let column = 3; column < 6; column++) {
+ columnManager.setColumnWidth(column, 20);
+ }
+ const skeleton = bed.get(SheetSkeletonService).getSkeleton(bed.unitId, bed.subUnitId)!;
+ skeleton.makeDirty(true);
+ skeleton.calculate();
+ const expected = applySheetDrawingPlacement(before, getSheetDrawingPlacement(before), skeleton);
+
+ bed.activated$.next(true);
+ vi.runOnlyPendingTimers();
+
+ const after = sheetService.getDrawingByParam(drawing)!;
+ expect(after.transform).toEqual(expected.transform);
+ expect(after.transform!.width).toBe(before.transform!.width);
+ expect(after.transform!.height).toBe(before.transform!.height);
+ expect(after.transform!.left).not.toBe(before.transform!.left);
+ expect(manager.getDrawingByParam(drawing)).toEqual(after);
+ } finally {
+ bed.univer.dispose();
+ bed.activated$.complete();
+ }
+ });
+
it.each(['deactivated', 'disposed'] as const)('cancels pending activation when the render module is %s', async (state) => {
const bed = createTestBed();
try {
diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts
index 501267f0e3..eab86b149d 100644
--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts
+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts
@@ -21,7 +21,12 @@ import type { ISheetDrawing } from '@univerjs/sheets-drawing';
import { Disposable, ICommandService, Inject } from '@univerjs/core';
import { IDrawingManagerService } from '@univerjs/drawing';
import { SetWorksheetActiveOperation, SheetSkeletonService } from '@univerjs/sheets';
-import { drawingPositionToTransform, ISheetDrawingService, SheetDrawingAnchorType } from '@univerjs/sheets-drawing';
+import {
+ applySheetDrawingPlacement,
+ getSheetDrawingPlacement,
+ ISheetDrawingService,
+ SheetDrawingAnchorType,
+} from '@univerjs/sheets-drawing';
export class SheetDrawingActiveRenderController extends Disposable implements IRenderModule {
private _renderTimeout: ReturnType<typeof setTimeout> | undefined;
@@ -121,10 +126,11 @@ export class SheetDrawingActiveRenderController extends Disposable implements IR
if (unitId === showUnitId && subUnitId === showSubunitId) {
const drawing = drawingData[drawingId] as ISheetDrawing;
if (sheetSkeletonParam && drawing.sheetTransform && drawing.anchorType !== SheetDrawingAnchorType.None) {
- refreshedDrawings.push({
- ...drawing,
- transform: drawingPositionToTransform(drawing.sheetTransform, sheetSkeletonParam),
- });
+ refreshedDrawings.push(applySheetDrawingPlacement(
+ drawing,
+ getSheetDrawingPlacement(drawing),
+ sheetSkeletonParam.skeleton
+ ));
}
insertDrawings.push(drawingData[drawingId]);
} else {
diff --git a/packages/sheets-drawing/src/controllers/__tests__/sheet-drawing-transform-affected.controller.spec.ts b/packages/sheets-drawing/src/controllers/__tests__/sheet-drawing-transform-affected.controller.spec.ts
index 3482ae380c..7f7a720be9 100644
--- a/packages/sheets-drawing/src/controllers/__tests__/sheet-drawing-transform-affected.controller.spec.ts
+++ b/packages/sheets-drawing/src/controllers/__tests__/sheet-drawing-transform-affected.controller.spec.ts
@@ -15,11 +15,15 @@
*/
import type { Workbook } from '@univerjs/core';
+import type { ISheetDrawingPlacement } from '../../services/sheet-drawing-placement';
+import type { ISheetDrawing } from '../../services/sheet-drawing.service';
import { Direction, DrawingTypeEnum, ICommandService, ImageSourceType, Injector, IUniverInstanceService, RANGE_TYPE, RedoCommandId, Tools, UndoCommandId, UniverInstanceType } from '@univerjs/core';
import { IDrawingManagerService } from '@univerjs/drawing';
import {
DeleteRangeMoveLeftCommand,
DeleteRangeMoveUpCommand,
+ DeltaColumnWidthCommand,
+ DeltaRowHeightCommand,
InsertColCommand,
InsertRangeMoveDownCommand,
InsertRangeMoveRightCommand,
@@ -41,9 +45,6 @@ import {
SheetsSelectionsService,
} from '@univerjs/sheets';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-
-import type { ISheetDrawingPlacement } from '../../services/sheet-drawing-placement';
-import type { ISheetDrawing } from '../../services/sheet-drawing.service';
import { createSheetsDrawingTestBed } from '../../__tests__/create-sheets-drawing-test-bed';
import { drawingPositionToTransform } from '../../basics/transform-position';
import { InsertSheetDrawingCommand } from '../../commands/commands/insert-sheet-drawing.command';
@@ -656,6 +657,14 @@ describe('sheet drawing transforms without UI plugins', () => {
value: 120,
ranges: [{ startRow: 0, endRow: 19, startColumn: 2, endColumn: 2 }],
}],
+ ['resize row by dragging', DeltaRowHeightCommand.id, {
+ anchorRow: 2,
+ deltaY: 40,
+ }],
+ ['resize column by dragging', DeltaColumnWidthCommand.id, {
+ anchorCol: 2,
+ deltaX: 50,
+ }],
['hide row', SetRowHiddenCommand.id, {
unitId: 'test',
subUnitId: 'sheet1',
@@ -711,6 +720,13 @@ describe('sheet drawing transforms without UI plugins', () => {
...base,
drawingId: `placement-${index}`,
}, placement, placement.kind === SheetDrawingAnchorType.None ? undefined : skeleton));
+ drawings.push({
+ ...applySheetDrawingPlacement({
+ ...base,
+ drawingId: 'implicit-position',
+ }, placements[0], skeleton),
+ anchorType: undefined,
+ });
expect(await testBed.commandService.executeCommand(InsertSheetDrawingCommand.id, {
unitId: 'test',
drawings,
@@ -726,6 +742,20 @@ describe('sheet drawing transforms without UI plugins', () => {
});
const before = readDrawings();
+ if (commandId === DeltaRowHeightCommand.id || commandId === DeltaColumnWidthCommand.id) {
+ testBed.get(SheetsSelectionsService).setSelections([{
+ range: {
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ rangeType: RANGE_TYPE.NORMAL,
+ },
+ primary: null,
+ style: null,
+ }]);
+ }
+
expect(await testBed.commandService.executeCommand(commandId, params)).toBe(true);
const after = readDrawings();
expect(after[0].transform).not.toEqual(before[0].transform);
@@ -733,6 +763,8 @@ describe('sheet drawing transforms without UI plugins', () => {
expect(getSheetDrawingPlacement(after[2])).toEqual(getSheetDrawingPlacement(before[2]));
expect(after[0].transform?.width).toBe(before[0].transform?.width);
expect(after[0].transform?.height).toBe(before[0].transform?.height);
+ expect(after[3].transform?.width).toBe(before[3].transform?.width);
+ expect(after[3].transform?.height).toBe(before[3].transform?.height);
if (commandId === SetRowHiddenCommand.id || commandId === SetColHiddenCommand.id) {
expect(after[1]).not.toEqual(before[1]);
} else {
@@ -746,6 +778,7 @@ describe('sheet drawing transforms without UI plugins', () => {
SheetDrawingAnchorType.Position,
SheetDrawingAnchorType.Both,
SheetDrawingAnchorType.None,
+ SheetDrawingAnchorType.Position,
]);
expect(await testBed.commandService.executeCommand(UndoCommandId)).toBe(true);
diff --git a/packages/sheets-drawing/src/controllers/sheet-drawing-transform-affected.controller.ts b/packages/sheets-drawing/src/controllers/sheet-drawing-transform-affected.controller.ts
index c4828e8df4..dec08c1871 100644
--- a/packages/sheets-drawing/src/controllers/sheet-drawing-transform-affected.controller.ts
+++ b/packages/sheets-drawing/src/controllers/sheet-drawing-transform-affected.controller.ts
@@ -20,8 +20,6 @@ import type { SpreadsheetSkeleton } from '@univerjs/engine-render';
import type {
IDeleteRangeMoveLeftCommandParams,
IDeleteRangeMoveUpCommandParams,
- IDeltaColumnWidthCommandParams,
- IDeltaRowHeightCommandParams,
IInsertColCommandParams,
IInsertRangeMoveDownCommandParams,
IInsertRangeMoveRightCommandParams,
@@ -30,12 +28,8 @@ import type {
IMoveRangeCommandParams,
IMoveRowsCommandParams,
IRemoveRowColCommandParams,
- ISetColHiddenCommandParams,
ISetColHiddenMutationParams,
ISetColVisibleMutationParams,
- ISetColWidthCommandParams,
- ISetRowHeightCommandParams,
- ISetRowHiddenCommandParams,
ISetRowHiddenMutationParams,
ISetRowVisibleMutationParams,
ISetSpecificColsVisibleCommandParams,
@@ -46,7 +40,9 @@ import type {
ISetWorksheetRowIsAutoHeightMutationParams,
ISheetSkeletonManagerParam,
} from '@univerjs/sheets';
-import { Disposable, ICommandService, Inject, IUniverInstanceService, RANGE_TYPE, Rectangle } from '@univerjs/core';
+import type { ISheetDrawingTransformExtensionResult, ISheetDrawingTransformPlan } from '../services/sheet-drawing-transform-plan.service';
+import type { ISheetDrawing, ISheetDrawingPosition } from '../services/sheet-drawing.service';
+import { Disposable, ICommandService, Inject, IUniverInstanceService, Rectangle } from '@univerjs/core';
import { IDrawingManagerService } from '@univerjs/drawing';
import {
attachRangeWithCoord,
@@ -80,11 +76,7 @@ import {
SetWorksheetRowIsAutoHeightMutation,
SheetInterceptorService,
SheetSkeletonService,
- SheetsSelectionsService,
} from '@univerjs/sheets';
-
-import type { ISheetDrawingTransformExtensionResult, ISheetDrawingTransformPlan } from '../services/sheet-drawing-transform-plan.service';
-import type { ISheetDrawing, ISheetDrawingPosition } from '../services/sheet-drawing.service';
import { drawingPositionToTransform, transformToAxisAlignPosition, transformToDrawingPosition } from '../basics/transform-position';
import { DrawingApplyType, SetDrawingApplyMutation } from '../commands/mutations/set-drawing-apply.mutation';
import { ClearSheetDrawingTransformerOperation } from '../commands/operations/clear-drawing-transformer.operation';
@@ -141,7 +133,6 @@ export class SheetDrawingTransformAffectedController extends Disposable {
@ICommandService private readonly _commandService: ICommandService,
@Inject(SheetSkeletonService) private readonly _sheetSkeletonService: SheetSkeletonService,
@Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,
- @Inject(SheetsSelectionsService) private readonly _selectionManagerService: SheetsSelectionsService,
@ISheetDrawingService private readonly _sheetDrawingService: ISheetDrawingService,
@IDrawingManagerService private readonly _drawingManagerService: IDrawingManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@@ -212,72 +203,18 @@ export class SheetDrawingTransformAffectedController extends Disposable {
const { range } = params as IInsertRangeMoveRightCommandParams;
return this._getRangeMoveUndo(range, RangeMoveUndoType.insertRight);
} else if (id === SetRowHiddenCommand.id || id === SetSpecificRowsVisibleCommand.id) {
- const _params = params as ISetRowHiddenCommandParams | ISetSpecificRowsVisibleCommandParams;
- const target = getSheetCommandTarget(this._univerInstanceService, _params);
- if (!target) {
- return { redos: [], undos: [] };
- }
-
- const { unitId, subUnitId } = target;
- const ranges = _params.ranges || this._selectionManagerService.getCurrentSelections()?.map((s) => s.range).filter((r) => r.rangeType === RANGE_TYPE.ROW);
- if (!ranges || ranges.length === 0) {
- return { redos: [], undos: [] };
- }
-
+ const { unitId, subUnitId, ranges } = params as ISetRowHiddenMutationParams | ISetSpecificRowsVisibleCommandParams;
return this._getDrawingUndoForRowVisible(unitId, subUnitId, ranges);
} else if (id === SetColHiddenCommand.id || id === SetSpecificColsVisibleCommand.id) {
- const _params = params as ISetColHiddenCommandParams | ISetSpecificColsVisibleCommandParams;
- const target = getSheetCommandTarget(this._univerInstanceService, _params);
- if (!target) {
- return { redos: [], undos: [] };
- }
-
- const { unitId, subUnitId } = target;
- const ranges = _params.ranges || this._selectionManagerService.getCurrentSelections()?.map((s) => s.range).filter((r) => r.rangeType === RANGE_TYPE.COLUMN);
- if (!ranges || ranges.length === 0) {
- return { redos: [], undos: [] };
- }
-
+ const { unitId, subUnitId, ranges } = params as ISetColHiddenMutationParams | ISetSpecificColsVisibleCommandParams;
return this._getDrawingUndoForColVisible(unitId, subUnitId, ranges);
- } else if (id === DeltaRowHeightCommand.id || id === DeltaColumnWidthCommand.id) {
- const target = getSheetCommandTarget(this._univerInstanceService);
- if (!target) {
- return { redos: [], undos: [] };
- }
-
- const { unitId, subUnitId, worksheet } = target;
- const ranges: IRange[] = [];
-
- if (id === DeltaRowHeightCommand.id) {
- ranges.push({
- startRow: (params as IDeltaRowHeightCommandParams).anchorRow,
- endRow: (params as IDeltaRowHeightCommandParams).anchorRow,
- startColumn: 0,
- endColumn: worksheet.getColumnCount() - 1,
- });
- } else {
- ranges.push({
- startRow: 0,
- endRow: worksheet.getRowCount() - 1,
- startColumn: (params as IDeltaColumnWidthCommandParams).anchorCol,
- endColumn: (params as IDeltaColumnWidthCommandParams).anchorCol,
- });
- }
-
- return this._getDrawingUndoForRowAndColSize(unitId, subUnitId, ranges);
- } else if (id === SetRowHeightCommand.id || id === SetColWidthCommand.id) {
- const _params = params as ISetRowHeightCommandParams | ISetColWidthCommandParams;
- const target = getSheetCommandTarget(this._univerInstanceService, _params);
- if (!target) {
- return { redos: [], undos: [] };
- }
-
- const { unitId, subUnitId } = target;
- const ranges = _params.ranges || this._selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!ranges || ranges.length === 0) {
- return { redos: [], undos: [] };
- }
-
+ } else if (
+ id === DeltaRowHeightCommand.id ||
+ id === DeltaColumnWidthCommand.id ||
+ id === SetRowHeightCommand.id ||
+ id === SetColWidthCommand.id
+ ) {
+ const { unitId, subUnitId, ranges } = params as ISetWorksheetRowHeightMutationParams | ISetWorksheetColWidthMutationParams;
return this._getDrawingUndoForRowAndColSize(unitId, subUnitId, ranges);
}