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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: drawing issues (#3245)
siamd848d33a692a
diff --git a/packages/drawing-ui/src/services/drawing-render.service.ts b/packages/drawing-ui/src/services/drawing-render.service.tsindex 5b7910e354..f667df8e17 100644--- a/packages/drawing-ui/src/services/drawing-render.service.ts+++ b/packages/drawing-ui/src/services/drawing-render.service.ts@@ -80,6 +80,11 @@ export class DrawingRenderService {shouldBeCache = true;}+ if (scene.getObject(imageShapeKey)) {+ // The image maybe already added in the time we are getting the source of the image+ continue;+ }+imageConfig.printable = true;const image = new Image(imageShapeKey, imageConfig);if (shouldBeCache) {diff --git a/packages/drawing/src/services/drawing-manager-impl.service.ts b/packages/drawing/src/services/drawing-manager-impl.service.tsindex 49437698ee..872c1e8a0b 100644--- a/packages/drawing/src/services/drawing-manager-impl.service.ts+++ b/packages/drawing/src/services/drawing-manager-impl.service.ts@@ -228,12 +228,21 @@ export class UnitDrawingService<T extends IDrawingParam> implements IUnitDrawinggetBatchRemoveOp(removeParams: IDrawingSearch[]): IDrawingJsonUndo1 {const ops: JSONOp[] = [];const invertOps: JSONOp[] = [];+removeParams.forEach((removeParam) => {const { op, invertOp } = this._removeByParam(removeParam);- ops.push(op);+ /**+ * ot-json compose case+ * two remove ops to does composition+ * ops: [[unit, sheetUnit, order, 0, { r: true }], [unit, sheetUnit, order, 1, { r: true }]]+ * We expected them to composed as [unit, sheetUnit, order, [0, { r: true }], [1, { r: true }]]+ * But extremely confusing to get [unit, sheetUnit, order, 0, { r: true }, 2, { r: true }]+ * And We apply this composed op to data, it's not a 2-index item for us to remove.+ * So use unshift api instead of push here.+ */+ ops.unshift(op);invertOps.push(invertOp);});-const op = ops.reduce(json1.type.compose, null);const invertOp = invertOps.reduce(json1.type.compose, null);diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.tsindex 54d23296a7..2eb5abc3f2 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts@@ -61,10 +61,10 @@ export class SheetsDrawingCopyPasteController extends Disposable {}private _collect(unitId: string, subUnitId: string, range: IRange) {- const skeletonManagetService = this._renderManagerService.getRenderById(unitId)?.with(SheetSkeletonManagerService);- if (!skeletonManagetService) return;+ const skeletonManagerService = this._renderManagerService.getRenderById(unitId)?.with(SheetSkeletonManagerService);+ if (!skeletonManagerService) return;- const selectionRect = skeletonManagetService.attachRangeWithCoord(range);+ const selectionRect = skeletonManagerService.attachRangeWithCoord(range);if (!selectionRect) {return;}@@ -139,19 +139,19 @@ export class SheetsDrawingCopyPasteController extends Disposable {const { row: copyRow, col: copyCol } = mapFunc(vCopyRange.startRow, vCopyRange.startColumn);const { row: pasteRow, col: pasteCol } = mapFunc(vPastedRange.startRow, vPastedRange.startColumn);- const skeletonManagetService = this._renderManagerService.getRenderById(unitId)?.with(SheetSkeletonManagerService);+ const skeletonManagerService = this._renderManagerService.getRenderById(unitId)?.with(SheetSkeletonManagerService);- if (!skeletonManagetService) {+ if (!skeletonManagerService) {return { redos: [], undos: [] };}- const copyRect = skeletonManagetService.attachRangeWithCoord({+ const copyRect = skeletonManagerService.attachRangeWithCoord({startRow: copyRow,endRow: copyRow,startColumn: copyCol,endColumn: copyCol,});- const pasteRect = skeletonManagetService.attachRangeWithCoord({+ const pasteRect = skeletonManagerService.attachRangeWithCoord({startRow: pasteRow,endRow: pasteRow,startColumn: pasteCol,diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.tsindex c615c9f6a8..599ad2f03f 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts@@ -16,13 +16,13 @@import type { ICommandInfo, IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';import { Disposable, ICommandService, Inject, IUniverInstanceService, Rectangle } from '@univerjs/core';-import { ISheetSelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';+import { attachRangeWithCoord, ISheetSelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';import { type IDrawingJsonUndo1, IDrawingManagerService, type IDrawingParam, type ITransformState } from '@univerjs/drawing';-import type { IInsertColCommandParams, IInsertRowCommandParams, IRemoveRowColCommandParams, ISetColHiddenMutationParams, ISetColVisibleMutationParams, ISetRowHiddenMutationParams, ISetRowVisibleMutationParams, ISetSpecificColsVisibleCommandParams, ISetSpecificRowsVisibleCommandParams, ISetWorksheetActiveOperationParams, ISetWorksheetColWidthMutationParams, ISetWorksheetRowHeightMutationParams, ISetWorksheetRowIsAutoHeightMutationParams } from '@univerjs/sheets';-import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, DeltaColumnWidthCommand, DeltaRowHeightCommand, getSheetCommandTarget, InsertColCommand, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetActiveOperation, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SheetInterceptorService } from '@univerjs/sheets';+import type { IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveRowColCommandParams, ISetColHiddenMutationParams, ISetColVisibleMutationParams, ISetRowHiddenMutationParams, ISetRowVisibleMutationParams, ISetSpecificColsVisibleCommandParams, ISetSpecificRowsVisibleCommandParams, ISetWorksheetActiveOperationParams, ISetWorksheetColWidthMutationParams, ISetWorksheetRowHeightMutationParams, ISetWorksheetRowIsAutoHeightMutationParams } from '@univerjs/sheets';+import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, DeltaColumnWidthCommand, DeltaRowHeightCommand, getSheetCommandTarget, InsertColCommand, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowCommand, MoveColsCommand, MoveRangeCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetActiveOperation, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SheetInterceptorService } from '@univerjs/sheets';import type { ISheetDrawing, ISheetDrawingPosition } from '@univerjs/sheets-drawing';import { DrawingApplyType, ISheetDrawingService, SetDrawingApplyMutation, SheetDrawingAnchorType } from '@univerjs/sheets-drawing';-import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';+import { type IRenderContext, IRenderManagerService, type IRenderModule } from '@univerjs/engine-render';import { drawingPositionToTransform, transformToDrawingPosition } from '../basics/transform-position';import { ClearSheetDrawingTransformerOperation } from '../commands/operations/clear-drawing-transformer.operation';@@ -50,6 +50,9 @@ const UPDATE_COMMANDS = [SetSpecificRowsVisibleCommand.id,SetSpecificColsVisibleCommand.id,SetColHiddenCommand.id,+ MoveColsCommand.id,+ MoveRowsCommand.id,+ MoveRangeCommand.id,];const REFRESH_MUTATIONS = [@@ -64,6 +67,7 @@ const REFRESH_MUTATIONS = [export class SheetDrawingTransformAffectedController extends Disposable implements IRenderModule {constructor(private readonly _context: IRenderContext<Workbook>,+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ICommandService private readonly _commandService: ICommandService,@ISheetSelectionRenderService private readonly _selectionRenderService: ISheetSelectionRenderService,@Inject(SheetSkeletonManagerService) private readonly _skeletonManagerService: SheetSkeletonManagerService,@@ -93,6 +97,8 @@ export class SheetDrawingTransformAffectedController extends Disposable implemenconst cId = commandInfo.id;if (cId === InsertRowCommand.id) {return this._moveRowInterceptor(commandInfo.params as IInsertRowCommandParams, 'insert');+ } else if ([MoveColsCommand.id, MoveRowsCommand.id, MoveRangeCommand.id].includes(cId)) {+ return this._moveRangeInterceptor(commandInfo.params as IMoveRangeCommandParams);} else if (cId === InsertColCommand.id) {return this._moveColInterceptor(commandInfo.params as IInsertColCommandParams, 'insert');} else if (cId === RemoveRowCommand.id) {@@ -581,6 +587,96 @@ export class SheetDrawingTransformAffectedController extends Disposable implemenreturn { unitId, subUnitId };}+ private _moveRangeInterceptor(params: IMoveRangeCommandParams | IMoveRowsCommandParams | IMoveColsCommandParams) {+ const { toRange, fromRange } = params;+ const target = getSheetCommandTarget(this._univerInstanceService);+ if (!target) {+ return { redos: [], undos: [] };+ }++ const { unitId, subUnitId } = target;++ const skeleton = this._renderManagerService.getRenderById(unitId)?.with(SheetSkeletonManagerService)?.getCurrentSkeleton();+ if (!skeleton) {+ return { redos: [], undos: [] };+ }++ const selectionRect = attachRangeWithCoord(skeleton, fromRange);+ if (!selectionRect) {+ return { redos: [], undos: [] };+ }++ const { startX, endX, startY, endY } = selectionRect;+ const drawings = this._sheetDrawingService.getDrawingData(unitId, subUnitId);+ const containedDrawings: ISheetDrawing[] = [];++ Object.keys(drawings).forEach((drawingId) => {+ const drawing = drawings[drawingId];+ if (drawing.anchorType !== SheetDrawingAnchorType.Both) {+ return;+ }+ const { transform } = drawing;++ if (!transform) {+ return;+ }++ const { left = 0, top = 0, width = 0, height = 0 } = transform;+ const { drawingStartX, drawingEndX, drawingStartY, drawingEndY } = {+ drawingStartX: left,+ drawingEndX: left + width,+ drawingStartY: top,+ drawingEndY: top + height,+ };++ if (startX <= drawingStartX && drawingEndX <= endX && startY <= drawingStartY && drawingEndY <= endY) {+ containedDrawings.push(drawing);+ }+ });++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];+ const rowOffset = toRange.startRow - fromRange.startRow;+ const colOffset = toRange.startColumn - fromRange.startColumn;++ const updateDrawings = containedDrawings.map((drawing) => {+ const oldSheetTransform = drawing.sheetTransform;+ const sheetTransform = {+ to: { ...oldSheetTransform.to, row: oldSheetTransform.to.row + rowOffset, column: oldSheetTransform.to.column + colOffset },+ from: { ...oldSheetTransform.from, row: oldSheetTransform.from.row + rowOffset, column: oldSheetTransform.from.column + colOffset },+ };+ const transform = drawingPositionToTransform(sheetTransform, this._selectionRenderService, this._skeletonManagerService);+ const params = {+ unitId,+ subUnitId,+ drawingId: drawing.drawingId,+ transform,+ sheetTransform,+ };++ return params;+ });+ if (updateDrawings.length) {+ // redos.push({ id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: redo, objects, type: DrawingApplyType.UPDATE } });+ // undos.push({ id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: undo, objects, type: DrawingApplyType.UPDATE } });+ // const params = { unitId, subUnitId, drawingId, transform: newTransform, sheetTransform: newSheetTransform };++ const updateJsonOp = this._sheetDrawingService.getBatchUpdateOp(updateDrawings as ISheetDrawing[]) as IDrawingJsonUndo1;+ const { undo, redo, objects } = updateJsonOp;+ redos.push({ id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: redo, objects, type: DrawingApplyType.UPDATE } });+ undos.push({ id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: undo, objects, type: DrawingApplyType.UPDATE } });+ // updateDrawings.push(params);+ // this._copyInfo = {+ // drawings: containedDrawings,+ // unitId,+ // subUnitId,+ // };+ }++ // console.log(params, '_moveRangeInterceptor')+ return { redos, undos };+ }+private _moveRowInterceptor(params: IInsertRowCommandParams | IRemoveRowColCommandParams, type: 'insert' | 'remove') {const ids = this._getUnitIdAndSubUnitId(params, type);if (ids == null) {@@ -616,7 +712,7 @@ export class SheetDrawingTransformAffectedController extends Disposable implemenconst { from, to } = sheetTransform;const { row: fromRow } = from;const { row: toRow } = to;- if (fromRow >= rowStartIndex && toRow <= rowEndIndex) {+ if (anchorType === SheetDrawingAnchorType.Both && fromRow >= rowStartIndex && toRow <= rowEndIndex) {// delete drawingdeleteDrawings.push({ unitId, subUnitId, drawingId });} else {@@ -696,7 +792,7 @@ export class SheetDrawingTransformAffectedController extends Disposable implemenconst { from, to } = sheetTransform;const { column: fromColumn } = from;const { column: toColumn } = to;- if (fromColumn >= colStartIndex && toColumn <= colEndIndex) {+ if (anchorType === SheetDrawingAnchorType.Both && fromColumn >= colStartIndex && toColumn <= colEndIndex) {// delete drawingdeleteDrawings.push({ unitId, subUnitId, drawingId });} else {@@ -974,7 +1070,7 @@ export class SheetDrawingTransformAffectedController extends Disposable implemen};}} else if (fromRow >= rowStartIndex && fromRow <= rowEndIndex) {- // shrink start and end row up, then set fromRowOffset to 0+ // shrink start and end row up, then set fromRowOffset to 0if (fromRow === rowStartIndex) {newTransform = { ...transform, top: (transform.top || 0) - sheetTransform.from.rowOffset };} else {@@ -1073,7 +1169,7 @@ export class SheetDrawingTransformAffectedController extends Disposable implemenObject.keys(drawingData).forEach((drawingId) => {const drawing = drawingData[drawingId] as ISheetDrawing;- const { sheetTransform, anchorType = SheetDrawingAnchorType.Position } = drawing;+ const { sheetTransform, transform, anchorType = SheetDrawingAnchorType.Position } = drawing;if (anchorType === SheetDrawingAnchorType.None) {return true;}@@ -1092,9 +1188,15 @@ export class SheetDrawingTransformAffectedController extends Disposable implemenstartRow: fromRow, endRow: toRow, startColumn: fromColumn, endColumn: toColumn,}) || fromRow > endRow || fromColumn > endColumn) {+ const isPositionAnchor = anchorType === SheetDrawingAnchorType.Position;+ const newTransform = drawingPositionToTransform(sheetTransform, this._selectionRenderService, this._skeletonManagerService);updateDrawings.push({...drawing,- transform: drawingPositionToTransform(sheetTransform, this._selectionRenderService, this._skeletonManagerService),+ transform: {+ ...newTransform,+ width: isPositionAnchor ? transform?.width : newTransform?.width,+ height: isPositionAnchor ? transform?.height : newTransform?.height,+ },});break;}