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
feat(img): support img flip (#6590)
VicKun31e6cd65187b
diff --git a/packages/engine-render/src/shape/image.ts b/packages/engine-render/src/shape/image.tsindex baead24309..79f77f6997 100644--- a/packages/engine-render/src/shape/image.ts+++ b/packages/engine-render/src/shape/image.ts@@ -21,7 +21,7 @@ import type { UniverRenderingContext } from '../context';import type { Scene } from '../scene';import type { IShapeProps } from './shape';import { ObjectType } from '../base-object';-import { RENDER_CLASS_TYPE, Vector2 } from '../basics';+import { RENDER_CLASS_TYPE, Transform, Vector2 } from '../basics';import { offsetRotationAxis } from '../basics/offset-rotation-axis';import { Shape } from './shape';@@ -286,7 +286,17 @@ export class Image extends Shape<IImageProps> {const m = this.transform.getMatrix();mainCtx.save();- mainCtx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);+ // if (this.flipX || this.flipY) {+ // const centerX = this.left + this.width / 2;+ // const centerY = this.top + this.height / 2;+ // mainCtx.transform(m[0], m[1], m[2], m[3], centerX, centerY);+ // }else {++ // mainCtx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);+ // }+ const centerX = this.left + this.width / 2;+ const centerY = this.top + this.height / 2;+ mainCtx.transform(m[0], m[1], m[2], m[3], centerX, centerY);if (this.opacity !== 1) {mainCtx.globalAlpha = this.opacity;}@@ -303,11 +313,11 @@ export class Image extends Shape<IImageProps> {if (!this._renderByCropper && this.srcRect) {const { left = 0, top = 0, right = 0, bottom = 0 } = this.srcRect;ctx.beginPath();- ctx.rect(0, 0, this.width, this.height);+ ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);ctx.clip();- ctx.drawImage(this._native, -left, -top, this.width + right + left, this.height + bottom + top);+ ctx.drawImage(this._native, -left - this.width / 2, -top - this.height / 2, this.width + right + left, this.height + bottom + top);} else {- ctx.drawImage(this._native, 0, 0, this.width, this.height);+ ctx.drawImage(this._native, -this.width / 2, -this.height / 2, this.width, this.height);}}@@ -361,4 +371,32 @@ export class Image extends Shape<IImageProps> {});}}++ override isHit(coord: Vector2) {+ // Build the same effective transform used in render():+ // [m[0], m[1], m[2], m[3], centerX, centerY]+ const m = this.transform.getMatrix();+ const centerX = this.left + this.width / 2;+ const centerY = this.top + this.height / 2;+ const renderTransform = new Transform([m[0], m[1], m[2], m[3], centerX, centerY]);++ // Account for parent group transforms if applicable+ const parent = this.getParent();+ const effectiveTransform = this.isInGroup && parent?.classType === RENDER_CLASS_TYPE.GROUP+ ? parent.ancestorTransform.multiply(renderTransform)+ : renderTransform;++ const oCoord = effectiveTransform.invert().applyPoint(coord);+ const halfWidth = this.width / 2;+ const halfHeight = this.height / 2;+ if (+ oCoord.x >= -halfWidth - this.strokeWidth / 2 &&+ oCoord.x <= halfWidth + this.strokeWidth / 2 &&+ oCoord.y >= -halfHeight - this.strokeWidth / 2 &&+ oCoord.y <= halfHeight + this.strokeWidth / 2+ ) {+ return true;+ }+ return false;+ }}diff --git a/packages/sheets-drawing-ui/src/commands/commands/flip-drawings.command.ts b/packages/sheets-drawing-ui/src/commands/commands/flip-drawings.command.tsnew file mode 100644index 0000000000..7e5bd5dad4--- /dev/null+++ b/packages/sheets-drawing-ui/src/commands/commands/flip-drawings.command.ts@@ -0,0 +1,185 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 { IAccessor, ICommand, ITransformState } from '@univerjs/core';+import type { IDrawingJsonUndo1 } from '@univerjs/drawing';+import type { Image } from '@univerjs/engine-render';+import type { ISheetDrawingPosition } from '@univerjs/sheets-drawing';+import {+ CommandType,+ DrawingTypeEnum,+ ICommandService,+ IUndoRedoService,+ sequenceExecute,+} from '@univerjs/core';+import { getDrawingShapeKeyByDrawingSearch } from '@univerjs/drawing';+import { IRenderManagerService } from '@univerjs/engine-render';+import { DrawingApplyType, ISheetDrawingService, SetDrawingApplyMutation } from '@univerjs/sheets-drawing';+import { ISheetSelectionRenderService } from '@univerjs/sheets-ui';+import { transformToAxisAlignPosition, transformToDrawingPosition } from '../../basics/transform-position';+import { ClearSheetDrawingTransformerOperation } from '../operations/clear-drawing-transformer.operation';++interface IFlipDrawingCommandParam {+ unitId: string;+ subUnitId: string;+ drawingId: string;+}+export interface IFlipSheetDrawingCommandParams {+ unitId: string;+ drawings: IFlipDrawingCommandParam[];+ flipH?: boolean;+ flipV?: boolean;+}+/**+ * The command to flip sheet drawing elements+ */+export const FlipSheetDrawingCommand: ICommand = {+ id: 'sheet.command.toggle-flip-drawings',+ type: CommandType.COMMAND,+ // eslint-disable-next-line max-lines-per-function, complexity+ handler: (accessor: IAccessor, params?: IFlipSheetDrawingCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);++ const sheetDrawingService = accessor.get(ISheetDrawingService);+ const renderManagerService = accessor.get(IRenderManagerService);++ if (!params) return false;++ const { drawings } = params;++ const flipH = params.flipH;+ const flipV = params.flipV;++ const unitIds: string[] = [];++ const updateParams: any[] = [];++ for (const param of drawings) {+ const { unitId, subUnitId, drawingId } = param;+ unitIds.push(unitId);++ const drawingData = sheetDrawingService.getDrawingData(unitId, subUnitId);+ const existing = drawingData?.[drawingId];+ if (!existing) {+ continue;+ }++ const transform = { ...existing.transform } as ITransformState;++ if (flipH) {+ transform.flipX = !transform.flipX;+ }+ if (flipV) {+ transform.flipY = !transform.flipY;+ }++ const render = renderManagerService.getRenderById(unitId);+ const selectionRenderService = render?.with(ISheetSelectionRenderService);+ if (!selectionRenderService) {+ continue;+ }++ const sheetTransform = transformToDrawingPosition(transform, selectionRenderService);+ const axisAlignSheetTransform = transformToAxisAlignPosition(transform, selectionRenderService) as ISheetDrawingPosition;++ const updateParamItem: any = {+ unitId,+ subUnitId,+ drawingType: existing.drawingType,+ drawingId,+ transform,+ sheetTransform,+ axisAlignSheetTransform,+ };++ const drawingType = existing.drawingType;+ if (drawingType === DrawingTypeEnum.DRAWING_IMAGE) {+ const scene = getSceneByDrawingSearch(accessor, unitId);+ if (scene) {+ const drawingShapeKey = getDrawingShapeKeyByDrawingSearch({ unitId, subUnitId, drawingId });+ const imageShape = scene.getObject(drawingShapeKey) as Image;++ if (imageShape) {+ const srcRect = imageShape.srcRect;+ if (srcRect) {+ let newSrcRect;+ const { left = 0, top = 0, right = 0, bottom = 0 } = srcRect;+ if (flipH) {+ const centerX = left + (right - left) / 2;+ const newLeft = centerX - (right - left) / 2;+ const newRight = centerX + (right - left) / 2;+ newSrcRect = {+ left: newLeft,+ top,+ right: newRight,+ bottom,+ };+ }+ if (flipV) {+ const centerY = top + (bottom - top) / 2;+ const newTop = centerY - (bottom - top) / 2;+ const newBottom = centerY + (bottom - top) / 2;+ newSrcRect = {+ left,+ top: newTop,+ right,+ bottom: newBottom,+ };+ }++ if (newSrcRect) {+ updateParamItem.srcRect = newSrcRect;+ }+ }+ }+ }+ }+ updateParams.push(updateParamItem);+ }++ if (updateParams.length === 0) return false;++ const jsonOp = sheetDrawingService.getBatchUpdateOp(updateParams) as IDrawingJsonUndo1;+ const { unitId: opUnitId, subUnitId: opSubUnitId, undo, redo, objects } = jsonOp;++ const updateMutation = { id: SetDrawingApplyMutation.id, params: { unitId: opUnitId, subUnitId: opSubUnitId, op: redo, objects, type: DrawingApplyType.UPDATE } };+ const undoUpdateMutation = { id: SetDrawingApplyMutation.id, params: { unitId: opUnitId, subUnitId: opSubUnitId, op: undo, objects, type: DrawingApplyType.UPDATE } };++ const result = sequenceExecute([updateMutation], commandService);++ if (result) {+ undoRedoService.pushUndoRedo({+ unitID: opUnitId,+ undoMutations: [undoUpdateMutation, { id: ClearSheetDrawingTransformerOperation.id, params: unitIds }],+ redoMutations: [updateMutation, { id: ClearSheetDrawingTransformerOperation.id, params: unitIds }],+ });++ return true;+ }++ return false;+ },+};++function getSceneByDrawingSearch(accessor: IAccessor, unitId: string) {+ const renderManagerService = accessor.get(IRenderManagerService);+ const render = renderManagerService.getRenderById(unitId);+ if (!render) {+ return null;+ }+ return render.scene;+}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 87cfd0b6bb..c151bb5f59 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@@ -27,6 +27,7 @@ import { SheetCanvasPopManagerService } from '@univerjs/sheets-ui';import { IMessageService } from '@univerjs/ui';import { RemoveSheetDrawingCommand } from '../commands/commands/remove-sheet-drawing.command';import { EditSheetDrawingOperation } from '../commands/operations/edit-sheet-drawing.operation';+import { FlipSheetDrawingCommand } from '../commands/commands/flip-drawings.command';export class DrawingPopupMenuController extends RxDisposable {private _initImagePopupMenu = new Set<string>();@@ -143,7 +144,7 @@ export class DrawingPopupMenuController extends RxDisposable {}const { unitId, subUnitId, drawingId, drawingType } = drawingParam;- // drawingParam should be ICanvasFloatDom, use for disable popup dialog+ // drawingParam should be ICanvasFloatDom, use for disable popup dialogconst data = (drawingParam as ISheetFloatDom).data as Record<string, boolean>;if (data && data.disablePopup) {return;@@ -205,6 +206,20 @@ export class DrawingPopupMenuController extends RxDisposable {commandParams: { unitId, subUnitId, drawingId },disable: drawingType === DrawingTypeEnum.DRAWING_DOM,},+ {+ label: 'image-popup.flipH',+ index: 2,+ commandId: FlipSheetDrawingCommand.id,+ commandParams: { unitId, flipH: true, drawings: [{ unitId, subUnitId, drawingId }] },+ disable: drawingType === DrawingTypeEnum.DRAWING_DOM,+ },+ {+ label: 'image-popup.flipV',+ index: 2,+ commandId: FlipSheetDrawingCommand.id,+ commandParams: { unitId, flipV: true, drawings: [{ unitId, subUnitId, drawingId }] },+ disable: drawingType === DrawingTypeEnum.DRAWING_DOM,+ },{label: 'image-popup.reset',index: 3,diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing.controller.tsindex 5fc30c0256..112d0ef383 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing.controller.ts@@ -36,6 +36,7 @@ import { COMPONENT_SHEET_DRAWING_PANEL } from '../views/sheet-image-panel/componimport { SheetDrawingPanel } from '../views/sheet-image-panel/SheetDrawingPanel';import { menuSchema } from './menu.schema';import { DeleteDrawingsShortcutItem, MoveDrawingDownShortcutItem, MoveDrawingLeftShortcutItem, MoveDrawingRightShortcutItem, MoveDrawingUpShortcutItem } from './shortcuts/drawing.shortcut';+import { FlipSheetDrawingCommand } from '../commands/commands/flip-drawings.command';export class SheetDrawingUIController extends Disposable {constructor(@@ -77,6 +78,7 @@ export class SheetDrawingUIController extends Disposable {DeleteDrawingsCommand,SetDrawingArrangeCommand,SaveCellImagesCommand,+ FlipSheetDrawingCommand,].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));}diff --git a/packages/sheets-drawing-ui/src/locale/ca-ES.ts b/packages/sheets-drawing-ui/src/locale/ca-ES.tsindex 1103021178..6775a55b22 100644--- a/packages/sheets-drawing-ui/src/locale/ca-ES.ts+++ b/packages/sheets-drawing-ui/src/locale/ca-ES.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: 'Edita',crop: 'Retalla',reset: 'Restableix la mida',+ flipH: 'Gira horizontalmente',+ flipV: 'Gira verticalmente',},'drawing-anchor': {title: 'Propietats d’ancoratge',diff --git a/packages/sheets-drawing-ui/src/locale/en-US.ts b/packages/sheets-drawing-ui/src/locale/en-US.tsindex 24d2606982..febb0c838a 100644--- a/packages/sheets-drawing-ui/src/locale/en-US.ts+++ b/packages/sheets-drawing-ui/src/locale/en-US.ts@@ -47,6 +47,8 @@ const locale = {edit: 'Edit',crop: 'Crop',reset: 'Reset Size',+ flipH: 'Flip Horizontal',+ flipV: 'Flip Vertical',},'drawing-anchor': {title: 'Anchor Properties',diff --git a/packages/sheets-drawing-ui/src/locale/es-ES.ts b/packages/sheets-drawing-ui/src/locale/es-ES.tsindex cedcc2bea2..5883fd7aa0 100644--- a/packages/sheets-drawing-ui/src/locale/es-ES.ts+++ b/packages/sheets-drawing-ui/src/locale/es-ES.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: 'Editar',crop: 'Recortar',reset: 'Restablecer tamaño',+ flipH: 'Voltear horizontalmente',+ flipV: 'Voltear verticalmente',},'drawing-anchor': {title: 'Propiedades de anclaje',diff --git a/packages/sheets-drawing-ui/src/locale/fa-IR.ts b/packages/sheets-drawing-ui/src/locale/fa-IR.tsindex 0a7218b242..14814977b9 100644--- a/packages/sheets-drawing-ui/src/locale/fa-IR.ts+++ b/packages/sheets-drawing-ui/src/locale/fa-IR.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: 'ویرایش',crop: 'کراپ',reset: 'بازنشانی اندازه',+ flipH: 'چرخش افقی',+ flipV: 'چرخش عمودی',},'drawing-anchor': {title: 'خواص لنگر',diff --git a/packages/sheets-drawing-ui/src/locale/fr-FR.ts b/packages/sheets-drawing-ui/src/locale/fr-FR.tsindex a7fb8291c3..3b95f29eeb 100644--- a/packages/sheets-drawing-ui/src/locale/fr-FR.ts+++ b/packages/sheets-drawing-ui/src/locale/fr-FR.ts@@ -49,6 +49,9 @@ const locale: typeof enUS = {edit: 'Éditer',crop: 'Rogner',reset: 'Réinitialiser la taille',+ flipH: 'Retournement horizontal',+ flipV: 'Retournement vertical',+},'drawing-anchor': {title: 'Propriétés de l\'ancre',diff --git a/packages/sheets-drawing-ui/src/locale/ja-JP.ts b/packages/sheets-drawing-ui/src/locale/ja-JP.tsindex 4720036837..3ae1fec98a 100644--- a/packages/sheets-drawing-ui/src/locale/ja-JP.ts+++ b/packages/sheets-drawing-ui/src/locale/ja-JP.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: '編集',crop: 'トリミング',reset: '元のサイズに戻す',+ flipH: '左右反転',+ flipV: '上下反転',},'drawing-anchor': {title: 'プロパティ',diff --git a/packages/sheets-drawing-ui/src/locale/ko-KR.ts b/packages/sheets-drawing-ui/src/locale/ko-KR.tsindex e3b5540c8a..8a9e76dda6 100644--- a/packages/sheets-drawing-ui/src/locale/ko-KR.ts+++ b/packages/sheets-drawing-ui/src/locale/ko-KR.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: '편집',crop: '자르기',reset: '원래 크기',+ flipH: '가로 뒤집기',+ flipV: '세로 뒤집기',},'drawing-anchor': {title: '고정 위치 설정',diff --git a/packages/sheets-drawing-ui/src/locale/ru-RU.ts b/packages/sheets-drawing-ui/src/locale/ru-RU.tsindex 2507e49045..bd99877330 100644--- a/packages/sheets-drawing-ui/src/locale/ru-RU.ts+++ b/packages/sheets-drawing-ui/src/locale/ru-RU.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: 'Редактировать',crop: 'Обрезать',reset: 'Сбросить размер',+ flipH: 'Отразить по горизонтали',+ flipV: 'Отразить по вертикали',},'drawing-anchor': {title: 'Свойства привязки',diff --git a/packages/sheets-drawing-ui/src/locale/sk-SK.ts b/packages/sheets-drawing-ui/src/locale/sk-SK.tsindex c9a65939bd..279401c211 100644--- a/packages/sheets-drawing-ui/src/locale/sk-SK.ts+++ b/packages/sheets-drawing-ui/src/locale/sk-SK.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: 'Upraviť',crop: 'Orezať',reset: 'Obnoviť veľkosť',+ flipH: 'Prevrátiť vodorovne',+ flipV: 'Prevrátiť zvisle',},'drawing-anchor': {title: 'Vlastnosti ukotvenia',diff --git a/packages/sheets-drawing-ui/src/locale/vi-VN.ts b/packages/sheets-drawing-ui/src/locale/vi-VN.tsindex 45712708fd..a4213d043f 100644--- a/packages/sheets-drawing-ui/src/locale/vi-VN.ts+++ b/packages/sheets-drawing-ui/src/locale/vi-VN.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: 'Chỉnh sửa',crop: 'Cắt',reset: 'Đặt lại kích thước',+ flipH: 'Lật ngang',+ flipV: 'Lật dọc',},'drawing-anchor': {title: 'Thuộc tính neo',diff --git a/packages/sheets-drawing-ui/src/locale/zh-CN.ts b/packages/sheets-drawing-ui/src/locale/zh-CN.tsindex b3e174435e..1e3aeab8af 100644--- a/packages/sheets-drawing-ui/src/locale/zh-CN.ts+++ b/packages/sheets-drawing-ui/src/locale/zh-CN.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: '编辑',crop: '裁剪',reset: '重置大小',+ flipH: '水平翻转',+ flipV: '垂直翻转',},'drawing-anchor': {title: '锚点属性',diff --git a/packages/sheets-drawing-ui/src/locale/zh-TW.ts b/packages/sheets-drawing-ui/src/locale/zh-TW.tsindex 6d9621ab16..aa88db5b97 100644--- a/packages/sheets-drawing-ui/src/locale/zh-TW.ts+++ b/packages/sheets-drawing-ui/src/locale/zh-TW.ts@@ -49,6 +49,8 @@ const locale: typeof enUS = {edit: '編輯',crop: '裁切',reset: '重置大小',+ flipH: '水平翻轉',+ flipV: '垂直翻轉',},'drawing-anchor': {title: '錨點屬性',