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 (#2639)
siam7d350912b5dc
diff --git a/packages/drawing-ui/src/controllers/image-cropper.controller.ts b/packages/drawing-ui/src/controllers/image-cropper.controller.tsindex baea26c785..5bb6f3e27e 100644--- a/packages/drawing-ui/src/controllers/image-cropper.controller.ts+++ b/packages/drawing-ui/src/controllers/image-cropper.controller.ts@@ -14,15 +14,16 @@* limitations under the License.*/-import type { ICommandInfo, ISrcRect, Nullable } from '@univerjs/core';-import { checkIfMove, Disposable, ICommandService, IUniverInstanceService, LifecycleStages, LocaleService, OnLifecycle } from '@univerjs/core';+import type { ICommandInfo, ISrcRect, Nullable, Workbook } from '@univerjs/core';+import { checkIfMove, Disposable, ICommandService, IUniverInstanceService, LifecycleStages, LocaleService, OnLifecycle, UniverInstanceType } from '@univerjs/core';+import { MessageType } from '@univerjs/design';import type { IDrawingSearch, IImageData, ITransformState } from '@univerjs/drawing';import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService } from '@univerjs/drawing';import type { BaseObject, Scene } from '@univerjs/engine-render';import { CURSOR_TYPE, degToRad, Image, IRenderManagerService, precisionTo, Vector2 } from '@univerjs/engine-render';import { IMessageService } from '@univerjs/ui';import { Inject } from '@wendellhu/redi';-import { MessageType } from '@univerjs/design';+import { filter, switchMap } from 'rxjs';import type { IOpenImageCropOperationBySrcRectParams } from '../commands/operations/image-crop.operation';import { AutoImageCropOperation, CloseImageCropOperation, CropType, OpenImageCropOperation } from '../commands/operations/image-crop.operation';import { ImageCropperObject } from '../views/crop/image-cropper-object';@@ -335,6 +336,16 @@ export class ImageCropperController extends Disposable {imageCropperObject?.dispose();}));+ const sheetUnit = this._univerInstanceService+ .getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET)+ .pipe(+ filter((workbook) => Boolean(workbook)),+ switchMap((workbook) => workbook!.activeSheet$)+ );++ this.disposeWithMe(sheetUnit.subscribe(() => {+ this._commandService.syncExecuteCommand(CloseImageCropOperation.id);+ }));}private _getApplyObjectByCropObject(cropObject: ImageCropperObject): Nullable<Image> {diff --git a/packages/drawing-ui/src/views/panel/DrawingTransform.tsx b/packages/drawing-ui/src/views/panel/DrawingTransform.tsxindex 32762f0d0b..5a6597c2a2 100644--- a/packages/drawing-ui/src/views/panel/DrawingTransform.tsx+++ b/packages/drawing-ui/src/views/panel/DrawingTransform.tsx@@ -100,17 +100,13 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {limitTop = -ancestorTop;}- if (limitLeft + width + ancestorLeft > topSceneWidth) {- limitWidth = topSceneWidth - limitLeft - ancestorLeft;- }+ limitWidth = topSceneWidth - limitLeft - ancestorLeft;if (limitWidth < MIN_DRAWING_WIDTH_LIMIT) {limitWidth = MIN_DRAWING_WIDTH_LIMIT;}- if (limitTop + height + ancestorTop > topSceneHeight) {- limitHeight = topSceneHeight - limitTop - ancestorTop;- }+ limitHeight = topSceneHeight - limitTop - ancestorTop;if (limitHeight < MIN_DRAWING_WIDTH_LIMIT) {limitHeight = MIN_DRAWING_WIDTH_LIMIT;@@ -250,15 +246,18 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {val = Math.max(val, MIN_DRAWING_WIDTH_LIMIT);- const { limitWidth } = checkMoveBoundary(xPosition, yPosition, val, height);+ const { limitWidth, limitHeight } = checkMoveBoundary(xPosition, yPosition, val, height);- val = limitWidth;+ val = Math.min(val, limitWidth);const updateParam: IDrawingParam = { unitId, subUnitId, drawingId, drawingType, transform: { width: val } };if (lockRatio) {let heightFix = (val / width) * height;heightFix = Math.max(heightFix, MIN_DRAWING_HEIGHT_LIMIT);+ if (heightFix > limitHeight) {+ return;+ }setHeight(heightFix);updateParam.transform!.height = heightFix;}@@ -274,18 +273,20 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {if (val == null) {return;}-val = Math.max(val, MIN_DRAWING_WIDTH_LIMIT);- const { limitHeight } = checkMoveBoundary(xPosition, yPosition, width, val);+ const { limitHeight, limitWidth } = checkMoveBoundary(xPosition, yPosition, width, val);- val = limitHeight;+ val = Math.min(val, limitHeight); ;const updateParam: IDrawingParam = { unitId, subUnitId, drawingId, drawingType, transform: { height: val } };if (lockRatio) {let widthFix = (val / height) * width;widthFix = Math.max(widthFix, MIN_DRAWING_WIDTH_LIMIT);+ if (widthFix > limitWidth) {+ return;+ }setWidth(widthFix);updateParam.transform!.width = widthFix;}diff --git a/packages/engine-render/src/scene.transformer.ts b/packages/engine-render/src/scene.transformer.tsindex 13a422dcb9..06a6cd378b 100644--- a/packages/engine-render/src/scene.transformer.ts+++ b/packages/engine-render/src/scene.transformer.ts@@ -706,7 +706,7 @@ export class Transformer extends Disposable implements ITransformerConfig {case TransformerManagerType.RESIZE_LB:state.width = width - moveLeft < MINI_WIDTH_LIMIT ? MINI_WIDTH_LIMIT : width - moveLeft;state.height = height + moveTop < MINI_HEIGHT_LIMIT ? MINI_HEIGHT_LIMIT : height + moveTop;- state.left = left + moveLeft;+ state.left = state.width <= MINI_WIDTH_LIMIT ? left : left + moveLeft;break;case TransformerManagerType.RESIZE_CB:state.height = moveTop + height < MINI_HEIGHT_LIMIT ? MINI_HEIGHT_LIMIT : moveTop + height;diff --git a/packages/sheets-drawing-ui/src/basics/transform-position.ts b/packages/sheets-drawing-ui/src/basics/transform-position.tsindex 8204e13e88..0a045ed6e3 100644--- a/packages/sheets-drawing-ui/src/basics/transform-position.ts+++ b/packages/sheets-drawing-ui/src/basics/transform-position.ts@@ -17,9 +17,9 @@import type { ITransformState, Nullable } from '@univerjs/core';import { precisionTo } from '@univerjs/engine-render';import type { ISheetDrawingPosition } from '@univerjs/sheets-drawing';-import type { ISelectionRenderService } from '@univerjs/sheets-ui';+import type { ISelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';-export function drawingPositionToTransform(position: ISheetDrawingPosition, selectionRenderService: ISelectionRenderService): Nullable<ITransformState> {+export function drawingPositionToTransform(position: ISheetDrawingPosition, selectionRenderService: ISelectionRenderService, sheetSkeletonManagerService: SheetSkeletonManagerService): Nullable<ITransformState> {const { from, to } = position;const { column: fromColumn, columnOffset: fromColumnOffset, row: fromRow, rowOffset: fromRowOffset } = from;const { column: toColumn, columnOffset: toColumnOffset, row: toRow, rowOffset: toRowOffset } = to;@@ -50,8 +50,8 @@ export function drawingPositionToTransform(position: ISheetDrawingPosition, seleconst { startX: endSelectionX, startY: endSelectionY } = endSelectionCell;- const left = precisionTo(startSelectionX + fromColumnOffset, 1);- const top = precisionTo(startSelectionY + fromRowOffset, 1);+ let left = precisionTo(startSelectionX + fromColumnOffset, 1);+ let top = precisionTo(startSelectionY + fromRowOffset, 1);let width = precisionTo(endSelectionX + toColumnOffset - left, 1);let height = precisionTo(endSelectionY + toRowOffset - top, 1);@@ -64,6 +64,17 @@ export function drawingPositionToTransform(position: ISheetDrawingPosition, seleheight = 0;}+ const skeleton = sheetSkeletonManagerService.getCurrentSkeleton();+ const sheetWidth = skeleton.rowHeaderWidth + skeleton.columnTotalWidth;+ const sheetHeight = skeleton.columnHeaderHeight + skeleton.rowTotalHeight;++ if ((left + width) > sheetWidth) {+ left = sheetWidth - width;+ }+ if ((top + height) > sheetHeight) {+ top = sheetHeight - height;+ }+return {left,top,diff --git a/packages/sheets-drawing-ui/src/controllers/drawing-popup-menu.controller.ts b/packages/sheets-drawing-ui/src/controllers/drawing-popup-menu.controller.tsindex b8e31d31ee..6d912fac09 100644--- a/packages/sheets-drawing-ui/src/controllers/drawing-popup-menu.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/drawing-popup-menu.controller.ts@@ -95,7 +95,7 @@ export class DrawingPopupMenuController extends RxDisposable {if (!transformer) {return;}- const disposePopups: IDisposable[] = [];+ let singletonPopupDisposer: IDisposable;this.disposeWithMe(toDisposable(transformer.createControl$.subscribe(() => {@@ -107,7 +107,7 @@ export class DrawingPopupMenuController extends RxDisposable {const selectedObjects = transformer.getSelectedObjectMap();if (selectedObjects.size > 1) {- disposePopups.forEach((dispose) => dispose.dispose());+ singletonPopupDisposer?.dispose();return;}@@ -123,15 +123,15 @@ export class DrawingPopupMenuController extends RxDisposable {}const { unitId, subUnitId, drawingId } = drawingParam;-- disposePopups.push(this.disposeWithMe(this._canvasPopManagerService.attachPopupToObject(object, {+ singletonPopupDisposer?.dispose();+ singletonPopupDisposer = this.disposeWithMe(this._canvasPopManagerService.attachPopupToObject(object, {componentKey: COMPONENT_IMAGE_POPUP_MENU,direction: 'horizontal',offset: [2, 0],extraProps: {menuItems: this._getImageMenuItems(unitId, subUnitId, drawingId),},- })));+ }));this._drawingManagerService.focusDrawing([{unitId,@@ -143,14 +143,14 @@ export class DrawingPopupMenuController extends RxDisposable {);this.disposeWithMe(transformer.clearControl$.subscribe(() => {- disposePopups.forEach((dispose) => dispose.dispose());+ singletonPopupDisposer?.dispose();this._contextService.setContextValue(FOCUSING_COMMON_DRAWINGS, false);this._drawingManagerService.focusDrawing(null);}));this.disposeWithMe(transformer.changing$.subscribe(() => {- disposePopups.forEach((dispose) => dispose.dispose());+ singletonPopupDisposer?.dispose();}));}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.tsnew file mode 100644index 0000000000..d7080535e7--- /dev/null+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts@@ -0,0 +1,215 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IMutationInfo, IRange, Nullable } from '@univerjs/core';+import { Disposable, LifecycleStages, OnLifecycle, Tools } from '@univerjs/core';+import type { IDiscreteRange, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';+import { COPY_TYPE, ISelectionRenderService, ISheetClipboardService, PREDEFINED_HOOK_NAME, virtualizeDiscreteRanges } from '@univerjs/sheets-ui';+import { Inject, Injector } from '@wendellhu/redi';+import type { ISheetDrawing } from '@univerjs/sheets-drawing';+import { DrawingApplyType, ISheetDrawingService, SetDrawingApplyMutation, SheetDrawingAnchorType } from '@univerjs/sheets-drawing';+import type { IDrawingJsonUndo1 } from '@univerjs/drawing';++@OnLifecycle(LifecycleStages.Ready, SheetsDrawingCopyPasteController)+export class SheetsDrawingCopyPasteController extends Disposable {+ private _copyInfo: Nullable<{+ drawings: ISheetDrawing[];+ unitId: string;+ subUnitId: string;+ }>;++ constructor(+ @ISheetClipboardService private _sheetClipboardService: ISheetClipboardService,+ @ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService,+ @ISheetDrawingService private readonly _sheetDrawingService: ISheetDrawingService,+ @Inject(Injector) private _injector: Injector+ ) {+ super();+ this._initCopyPaste();+ }++ private _initCopyPaste() {+ this._sheetClipboardService.addClipboardHook({+ id: 'SHEET_IMAGE_UI_PLUGIN',+ onBeforeCopy: (unitId, subUnitId, range) => this._collect(unitId, subUnitId, range),+ onPasteCells: (pasteFrom, pasteTo, data, payload) => {+ const { copyType = COPY_TYPE.COPY, pasteType } = payload;+ const { range: copyRange } = pasteFrom || {};+ const { range: pastedRange, unitId, subUnitId } = pasteTo;+ return this._generateMutations(pastedRange, { copyType, pasteType, copyRange, unitId, subUnitId });+ },+ onPastePlainText: (pasteTo: ISheetDiscreteRangeLocation, clipText: string) => {+ return { undos: [], redos: [] };+ },+ });+ }++ private _collect(unitId: string, subUnitId: string, range: IRange) {+ const selectionRect = this._selectionRenderService.attachRangeWithCoord(range);+ if (!selectionRect) {+ return;+ }++ 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];+ const { transform } = drawing;+ if (drawing.anchorType !== SheetDrawingAnchorType.Both) {+ return;+ }+ 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);+ }+ });+ if (containedDrawings.length) {+ this._copyInfo = {+ drawings: containedDrawings,+ unitId,+ subUnitId,+ };+ }+ }++ // eslint-disable-next-line max-lines-per-function+ private _generateMutations(+ pastedRange: IDiscreteRange,+ copyInfo: {+ copyType: COPY_TYPE;+ copyRange?: IDiscreteRange;+ pasteType: string;+ unitId: string;+ subUnitId: string;+ }+ ) {+ if (!this._copyInfo) {+ return { redos: [], undos: [] };+ }++ if (+ [+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,+ ].includes(+ copyInfo.pasteType+ )+ ) {+ return { redos: [], undos: [] };+ }++ const { copyRange } = copyInfo;+ if (!copyRange) {+ return { redos: [], undos: [] };+ }+ const { drawings, unitId, subUnitId } = this._copyInfo;+ const { ranges: [vCopyRange, vPastedRange], mapFunc } = virtualizeDiscreteRanges([copyRange, pastedRange]);+ const { row: copyRow, col: copyCol } = mapFunc(vCopyRange.startRow, vCopyRange.startColumn);+ const { row: pasteRow, col: pasteCol } = mapFunc(vPastedRange.startRow, vPastedRange.startColumn);++ const copyRect = this._selectionRenderService.attachRangeWithCoord({+ startRow: copyRow,+ endRow: copyRow,+ startColumn: copyCol,+ endColumn: copyCol,+ });+ const pasteRect = this._selectionRenderService.attachRangeWithCoord({+ startRow: pasteRow,+ endRow: pasteRow,+ startColumn: pasteCol,+ endColumn: pasteCol,+ });+ if (!copyRect || !pasteRect) {+ return { redos: [], undos: [] };+ }+ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];+ const leftOffset = pasteRect.startX - copyRect.startX;+ const topOffset = pasteRect.startY - copyRect.startY;+ const rowOffset = pasteRow - copyRow;+ const columnOffset = pasteCol - copyCol;+ const isCut = copyInfo.copyType === COPY_TYPE.CUT;+ const { _sheetDrawingService } = this;+ drawings.forEach((drawing) => {+ const { transform, sheetTransform } = drawing;+ if (!transform) {+ return;+ }++ const drawingObject: Partial<ISheetDrawing> = {+ ...drawing,+ unitId,+ subUnitId,+ drawingId: isCut ? drawing.drawingId : Tools.generateRandomId(),+ transform: {+ ...transform,+ left: transform.left! + leftOffset,+ top: transform.top! + topOffset,+ },+ sheetTransform: {+ to: { ...sheetTransform.to, row: sheetTransform.to.row + rowOffset, column: sheetTransform.to.column + columnOffset },+ from: { ...sheetTransform.from, row: sheetTransform.from.row + rowOffset, column: sheetTransform.from.column + columnOffset },+ },+ };++ if (isCut) {+ const { undo, redo, objects } = _sheetDrawingService.getBatchUpdateOp([drawingObject] as ISheetDrawing[]) as IDrawingJsonUndo1;+ redos.push({+ id: SetDrawingApplyMutation.id,+ params: {+ unitId,+ subUnitId,+ type: DrawingApplyType.UPDATE,+ op: redo,+ objects,+ },+ });+ undos.push({+ id: SetDrawingApplyMutation.id,+ params: {+ unitId,+ subUnitId,+ type: DrawingApplyType.UPDATE,+ op: undo,+ objects,+ },+ });+ } else {+ const { undo, redo, objects } = _sheetDrawingService.getBatchAddOp([drawingObject] as ISheetDrawing[]) as IDrawingJsonUndo1;+ redos.push({ id: SetDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DrawingApplyType.INSERT } });+ undos.push({ id: SetDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DrawingApplyType.REMOVE } });+ }+ });++ return {+ redos,+ undos,+ };+ }+}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 8f7dc97648..5ed37fc624 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@@ -14,15 +14,16 @@* limitations under the License.*/-import type { ICommandInfo, IMutationInfo, IRange, Nullable } from '@univerjs/core';+import type { ICommandInfo, IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';import { Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle } from '@univerjs/core';import { Inject } from '@wendellhu/redi';-import { ISelectionRenderService } from '@univerjs/sheets-ui';+import { ISelectionRenderService, 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, IMoveRangeCommandParams, IRemoveRowColCommandParams, ISetColHiddenMutationParams, ISetColVisibleMutationParams, ISetRowHiddenMutationParams, ISetRowVisibleMutationParams, ISetSpecificColsVisibleCommandParams, ISetSpecificRowsVisibleCommandParams, ISetWorksheetActiveOperationParams, ISetWorksheetColWidthMutationParams, ISetWorksheetRowHeightMutationParams, ISetWorksheetRowIsAutoHeightMutationParams } from '@univerjs/sheets';+import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, DeltaColumnWidthCommand, DeltaRowHeightCommand, getSheetCommandTarget, InsertColCommand, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowCommand, MoveRangeCommand, 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 { drawingPositionToTransform, transformToDrawingPosition } from '../basics/transform-position';import { ClearSheetDrawingTransformerOperation } from '../commands/operations/clear-drawing-transformer.operation';@@ -50,6 +51,7 @@ const UPDATE_COMMANDS = [SetSpecificRowsVisibleCommand.id,SetSpecificColsVisibleCommand.id,SetColHiddenCommand.id,+ MoveRangeCommand.id,];const REFRESH_MUTATIONS = [@@ -62,14 +64,16 @@ const REFRESH_MUTATIONS = [];@OnLifecycle(LifecycleStages.Rendered, SheetDrawingTransformAffectedController)-export class SheetDrawingTransformAffectedController extends Disposable {+export class SheetDrawingTransformAffectedController extends Disposable implements IRenderModule {constructor(+ private readonly _context: IRenderContext<Workbook>,@ICommandService private readonly _commandService: ICommandService,@Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,@ISheetDrawingService private readonly _sheetDrawingService: ISheetDrawingService,@IDrawingManagerService private readonly _drawingManagerService: IDrawingManagerService,@ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService,- @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,+ @Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService) {super();@@ -87,7 +91,6 @@ export class SheetDrawingTransformAffectedController extends Disposable {private _sheetInterceptorListener() {this.disposeWithMe(this._sheetInterceptorService.interceptCommand({-getMutations: (commandInfo) => {if (!UPDATE_COMMANDS.includes(commandInfo.id)) {return { redos: [], undos: [] };@@ -104,6 +107,8 @@ export class SheetDrawingTransformAffectedController extends Disposable {return this._moveRowInterceptor(commandInfo.params as IRemoveRowColCommandParams, 'remove');} else if (cId === RemoveColCommand.id) {return this._moveColInterceptor(commandInfo.params as IRemoveRowColCommandParams, 'remove');+ } else if (cId === MoveRangeCommand.id) {+ return this._moveRangeInterceptor(commandInfo.params as IMoveRangeCommandParams);} else if (cId === DeleteRangeMoveLeftCommand.id) {const { range } = commandInfo.params as IRemoveRowColCommandParams;return this._getRangeMoveUndo(range, RangeMoveUndoType.deleteLeft);@@ -243,7 +248,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {}if (newSheetTransform != null && newTransform != null) {- const newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ const newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);updateDrawings.push({ ...drawing, sheetTransform: newSheetTransform, transform: newTransform });}@@ -548,7 +553,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {}}- const newTransform = drawingPositionToTransform({ ...sheetTransform }, this._selectionRenderService);+ const newTransform = drawingPositionToTransform({ ...sheetTransform }, this._selectionRenderService, this._sheetSkeletonManagerService);if (newTransform != null) {updateDrawings.push({...drawing,@@ -620,7 +625,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {const { 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 {@@ -665,6 +670,111 @@ export class SheetDrawingTransformAffectedController extends Disposable {};}+ private _moveRangeInterceptor(params: IMoveRangeCommandParams) {+ const { fromRange, toRange } = params;+ const target = getSheetCommandTarget(this._univerInstanceService);+ if (!target) {+ return { redos: [], undos: [] };+ }+ const { unitId, subUnitId } = target;+ const data = this._sheetDrawingService.getDrawingData(unitId, subUnitId);+ const drawings: Partial<ISheetDrawing>[] = [];+ // get rect info of toRange and fromRange+ const fromRangeRect = this._selectionRenderService.attachRangeWithCoord(fromRange);+ const toRangeRect = this._selectionRenderService.attachRangeWithCoord(toRange);+ if (!fromRangeRect || !toRangeRect) {+ return { redos: [], undos: [] };+ }+ Object.keys(data).forEach((drawingId) => {+ const drawing = data[drawingId];+ const { sheetTransform, transform, anchorType = SheetDrawingAnchorType.Position } = drawing;++ if (+ !sheetTransform || !transform || anchorType === SheetDrawingAnchorType.None) {+ 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,+ };++ const contained = fromRangeRect.startX <= drawingStartX && drawingEndX <= fromRangeRect.endX && fromRangeRect.startY <= drawingEndY && drawingStartY <= fromRangeRect.endY;+ // If range does not contain drawing, skip+ if (!contained) {+ return;+ }++ const { to, from } = sheetTransform;++ const rowOffset = toRange.startRow - fromRange.startRow;+ const colOffset = toRange.startColumn - fromRange.startColumn;++ let newSheetTransform: Nullable<ISheetDrawingPosition>;+ let newTransform: Nullable<ITransformState>;+ if (anchorType === SheetDrawingAnchorType.Both) {+ const drawingFromRect = this._selectionRenderService.attachRangeWithCoord({+ startRow: to.row,+ endRow: from.row,+ startColumn: to.column,+ endColumn: from.column,+ });+ const drawingToRect = this._selectionRenderService.attachRangeWithCoord({+ startRow: to.row + rowOffset,+ endRow: from.row + rowOffset,+ startColumn: to.column + colOffset,+ endColumn: from.column + colOffset,+ });+ if (!drawingToRect || !drawingFromRect) {+ return;+ }+ // Both anchor drawing should calculate the offset of the width and height+ const widthOffset = (drawingToRect.endX - drawingToRect.startX) - (drawingFromRect.endX - drawingFromRect.startX);+ const heightOffset = (drawingToRect.endY - drawingToRect.startY) - (drawingFromRect.endY - drawingFromRect.startY);++ newSheetTransform = {+ to: { ...to, row: to.row + rowOffset, column: to.column + colOffset, columnOffset: to.columnOffset + widthOffset },+ from: { ...from, row: from.row + rowOffset, column: from.column + colOffset, columnOffset: to.columnOffset + widthOffset, rowOffset: to.rowOffset + heightOffset },+ };+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);+ } else {+ // Position anchor drawing only do the position offset+ const xOffset = toRangeRect.startX - fromRangeRect.startX;+ const yOffset = toRangeRect.startY - fromRangeRect.startY;++ newTransform = {+ ...transform,+ left: left + xOffset,+ top: top + yOffset,+ };++ newSheetTransform = transformToDrawingPosition(newTransform, this._selectionRenderService);+ }++ if (!newSheetTransform || !newTransform) {+ return;+ }++ const params = { unitId, subUnitId, drawingId, transform: newTransform, sheetTransform: newSheetTransform };+ drawings.push(params);+ });++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ if (drawings.length > 0) {+ const updateJsonOp = this._sheetDrawingService.getBatchUpdateOp(drawings 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 } });+ }++ return { redos, undos };+ }+private _moveColInterceptor(params: IInsertColCommandParams | IRemoveRowColCommandParams, type: 'insert' | 'remove') {const ids = this._getUnitIdAndSubUnitId(params, type);if (ids == null) {@@ -700,7 +810,8 @@ export class SheetDrawingTransformAffectedController extends Disposable {const { from, to } = sheetTransform;const { column: fromColumn } = from;const { column: toColumn } = to;- if (fromColumn >= colStartIndex && toColumn <= colEndIndex) {+ const contained = fromColumn >= colStartIndex && toColumn <= colEndIndex;+ if (contained && anchorType === SheetDrawingAnchorType.Both) {// delete drawingdeleteDrawings.push({ unitId, subUnitId, drawingId });} else {@@ -768,7 +879,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {// from: { ...from, column: fromColumn + colCount },// to: { ...to, column: toColumn + colCount },// };- // newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ // newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);const selectionCell = this._selectionRenderService.attachRangeWithCoord({ startColumn: colStartIndex, endColumn: colEndIndex, startRow: from.row, endRow: to.row });if (selectionCell == null) {@@ -783,7 +894,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {from: { ...from },to: { ...to, column: toColumn + colCount },};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);} else {return {newSheetTransform: transformToDrawingPosition({ ...transform }, this._selectionRenderService),@@ -818,14 +929,30 @@ export class SheetDrawingTransformAffectedController extends Disposable {if (fromColumn > colEndIndex) {// shrink start and end col left only- newSheetTransform = {- from: { ...from, column: fromColumn - colCount },- to: { ...to, column: toColumn - colCount },- };- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ const selectionCell = this._selectionRenderService.attachRangeWithCoord({ startColumn: colStartIndex, endColumn: colEndIndex, startRow: from.row, endRow: to.row });+ if (selectionCell == null) {+ return;+ }+ newTransform = { ...transform, left: (transform.left || 0) - (selectionCell.endX - selectionCell.startX) };+ newSheetTransform = transformToDrawingPosition(newTransform, this._selectionRenderService);} else if (fromColumn >= colStartIndex && toColumn <= colEndIndex) {// delete drawing- return null;+ const selectionRect = this._selectionRenderService.attachRangeWithCoord({+ startColumn: colStartIndex,+ endColumn: colEndIndex,+ startRow: 0,+ endRow: 0,+ });++ newSheetTransform = {+ from: { ...from, columnOffset: 0 },+ to: { ...to, columnOffset: to.columnOffset - from.columnOffset },+ };++ newTransform = {+ ...transform,+ left: selectionRect?.startX,+ };} else if (fromColumn < colStartIndex && toColumn > colEndIndex) {// shrink end left onlyif (anchorType === SheetDrawingAnchorType.Both) {@@ -833,7 +960,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {from: { ...from },to: { ...to, column: toColumn - colCount },};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);} else {return {newSheetTransform: transformToDrawingPosition({ ...transform }, this._selectionRenderService),@@ -867,7 +994,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {from: { ...from },to: { ...to, column: colStartIndex - 1, columnOffset: selectionCell.endX - selectionCell.startX },};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);}if (newSheetTransform != null && newTransform != null) {@@ -916,7 +1043,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {row: toRow + rowCount,},};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);} else {return {newSheetTransform: transformToDrawingPosition({ ...transform }, this._selectionRenderService),@@ -959,10 +1086,25 @@ export class SheetDrawingTransformAffectedController extends Disposable {from: { ...from, row: fromRow - rowCount },to: { ...to, row: toRow - rowCount },};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);} else if (fromRow >= rowStartIndex && toRow <= rowEndIndex) {// delete drawing- return null;+ const selectionRect = this._selectionRenderService.attachRangeWithCoord({+ startColumn: 0,+ endColumn: 0,+ startRow: rowStartIndex,+ endRow: rowEndIndex,+ });++ newSheetTransform = {+ from: { ...from, rowOffset: 0 },+ to: { ...to, rowOffset: to.rowOffset - from.rowOffset },+ };++ newTransform = {+ ...transform,+ top: selectionRect?.startY,+ };} else if (fromRow < rowStartIndex && toRow > rowEndIndex) {// shrink end up onlyif (anchorType === SheetDrawingAnchorType.Both) {@@ -970,7 +1112,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {from: { ...from },to: { ...to, row: toRow - rowCount },};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);} else {return {newSheetTransform: transformToDrawingPosition({ ...transform }, this._selectionRenderService),@@ -978,7 +1120,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {};}} 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 {@@ -1000,7 +1142,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {from: { ...from },to: { ...to, row: rowStartIndex - 1, rowOffset: selectionCell.endY - selectionCell.startY },};- newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService);+ newTransform = drawingPositionToTransform(newSheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService);}if (newSheetTransform != null && newTransform != null) {@@ -1094,7 +1236,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {) || fromRow > endRow || fromColumn > endColumn) {updateDrawings.push({...drawing,- transform: drawingPositionToTransform(sheetTransform, this._selectionRenderService),+ transform: drawingPositionToTransform(sheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService),});break;}diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.tsindex d788568ddc..5136afa253 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts@@ -22,9 +22,10 @@ import { DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGimport type { ISheetDrawing, ISheetDrawingPosition } from '@univerjs/sheets-drawing';import { ISheetDrawingService } from '@univerjs/sheets-drawing';import { SelectionManagerService } from '@univerjs/sheets';-import { ISelectionRenderService } from '@univerjs/sheets-ui';+import { ISelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';import { IMessageService } from '@univerjs/ui';import { MessageType } from '@univerjs/design';+import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';import { IRenderManagerService } from '@univerjs/engine-render';import type { IInsertImageOperationParams } from '../commands/operations/insert-image.operation';import { InsertCellImageOperation, InsertFloatImageOperation } from '../commands/operations/insert-image.operation';@@ -38,8 +39,9 @@ import { UngroupSheetDrawingCommand } from '../commands/commands/ungroup-sheet-dimport { drawingPositionToTransform, transformToDrawingPosition } from '../basics/transform-position';@OnLifecycle(LifecycleStages.Rendered, SheetDrawingUpdateController)-export class SheetDrawingUpdateController extends Disposable {+export class SheetDrawingUpdateController extends Disposable implements IRenderModule {constructor(+ private readonly _context: IRenderContext<Workbook>,@ICommandService private readonly _commandService: ICommandService,@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,@@ -50,7 +52,8 @@ export class SheetDrawingUpdateController extends Disposable {@IContextService private readonly _contextService: IContextService,@IMessageService private readonly _messageService: IMessageService,@Inject(LocaleService) private readonly _localeService: LocaleService,- @IRenderManagerService private readonly _renderManagerService: IRenderManagerService+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,+ @Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService) {super();@@ -177,7 +180,7 @@ export class SheetDrawingUpdateController extends Disposable {drawingType: DrawingTypeEnum.DRAWING_IMAGE,imageSourceType,source,- transform: drawingPositionToTransform(sheetTransform, this._selectionRenderService),+ transform: drawingPositionToTransform(sheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService),sheetTransform,};@@ -333,7 +336,7 @@ export class SheetDrawingUpdateController extends Disposable {const newDrawing: Partial<ISheetDrawing> = {...param,- transform: { ...sheetDrawing.transform, ...transform, ...drawingPositionToTransform(sheetTransform, this._selectionRenderService) },+ transform: { ...sheetDrawing.transform, ...transform, ...drawingPositionToTransform(sheetTransform, this._selectionRenderService, this._sheetSkeletonManagerService) },sheetTransform: { ...sheetTransform },};diff --git a/packages/sheets-drawing-ui/src/plugin.ts b/packages/sheets-drawing-ui/src/plugin.tsindex 0b451f6465..b470fbb811 100644--- a/packages/sheets-drawing-ui/src/plugin.ts+++ b/packages/sheets-drawing-ui/src/plugin.ts@@ -20,6 +20,7 @@ import { Inject, Injector } from '@wendellhu/redi';import { UniverDrawingUIPlugin } from '@univerjs/drawing-ui';import { UniverSheetsDrawingPlugin } from '@univerjs/sheets-drawing';import { UniverDrawingPlugin } from '@univerjs/drawing';+import { IRenderManagerService } from '@univerjs/engine-render';import { DrawingPopupMenuController } from './controllers/drawing-popup-menu.controller';import { SheetDrawingUpdateController } from './controllers/sheet-drawing-update.controller';import type { IUniverSheetsDrawingConfig } from './controllers/sheet-drawing.controller';@@ -28,6 +29,7 @@ import { SheetDrawingTransformAffectedController } from './controllers/sheet-draimport { SheetCanvasFloatDomManagerService } from './services/canvas-float-dom-manager.service';import { SheetDrawingPrintingController } from './controllers/sheet-drawing-printing.controller';import { SheetDrawingPermissionController } from './controllers/sheet-drawing-permission.controller';+import { SheetsDrawingCopyPasteController } from './controllers/sheet-drawing-copy-paste.controller';const PLUGIN_NAME = 'SHEET_IMAGE_UI_PLUGIN';@@ -41,7 +43,8 @@ export class UniverSheetsDrawingUIPlugin extends Plugin {constructor(config: Partial<IUniverSheetsDrawingConfig> = {},@Inject(Injector) protected _injector: Injector,- @Inject(LocaleService) private readonly _localeService: LocaleService+ @Inject(LocaleService) private readonly _localeService: LocaleService,+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService) {super();@@ -65,14 +68,18 @@ export class UniverSheetsDrawingUIPlugin extends Plugin {useFactory: () => this._injector.createInstance(SheetDrawingUIController, this._pluginConfig),},],- [SheetDrawingUpdateController],[DrawingPopupMenuController],- [SheetDrawingTransformAffectedController],[SheetDrawingPrintingController],[SheetDrawingPermissionController],+ [SheetsDrawingCopyPasteController],+ ];+ const renderModules = [+ SheetDrawingUpdateController,+ SheetDrawingTransformAffectedController,];dependencies.forEach((dependency) => injector.add(dependency));+ renderModules.forEach((controller) => this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, controller));}}