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.

feat(image): add event and enum (#4429)

Univerb413d6809ede
diff --git a/packages/core/src/services/image-io/image-io.service.ts b/packages/core/src/services/image-io/image-io.service.ts
index 18d544196b..3cafdad888 100644
--- a/packages/core/src/services/image-io/image-io.service.ts
+++ b/packages/core/src/services/image-io/image-io.service.ts
@@ -18,9 +18,21 @@ import type { Observable } from 'rxjs';
import type { Nullable } from '../../shared/types';
import { createIdentifier } from '../../common/di';
+/**
+ * The type of image data source
+ */
export enum ImageSourceType {
+ /**
+ * The image source is a URL, for example: https://avatars.githubusercontent.com/u/61444807?s=48&v=4
+ */
URL = 'URL',
+ /**
+ * The image source is a UUID, this ID is generated by the Univer image hosting service. For specific generation rules, please refer to the Univer image hosting service API documentation.
+ */
UUID = 'UUID',
+ /**
+ * The image source is BASE64, for example: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB7ZJ...
+ */
BASE64 = 'BASE64',
}
diff --git a/packages/core/src/shared/check-if-move.ts b/packages/core/src/shared/check-if-move.ts
index fa068e5ff6..c96733106e 100644
--- a/packages/core/src/shared/check-if-move.ts
+++ b/packages/core/src/shared/check-if-move.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ITransformState } from '../types/interfaces/i-document-data';
+import type { ITransformState } from '../types/interfaces/i-drawing';
import type { Nullable } from './types';
export const MOVE_BUFFER_VALUE = 2;
diff --git a/packages/core/src/types/interfaces/i-document-data.ts b/packages/core/src/types/interfaces/i-document-data.ts
index 82924e08c1..a0b66dcec9 100644
--- a/packages/core/src/types/interfaces/i-document-data.ts
+++ b/packages/core/src/types/interfaces/i-document-data.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import type { IAbsoluteTransform, ISize } from '../../shared/shape';
-import type { Nullable } from '../../shared/types';
+import type { ISize } from '../../shared/shape';
import type { BooleanNumber, CellValueType, HorizontalAlign, LocaleType, TextDirection, VerticalAlign, WrapStrategy } from '../enum';
+import type { IDrawingParam } from './i-drawing';
import type { IMention } from './i-mention';
import type { IColorStyle, IStyleBase } from './i-style-data';
@@ -1084,66 +1084,3 @@ export enum PageOrientType {
// #region - tech dept
-// TODO@Jocs: these types are here because of drawing coupled into the core of the document's model, which
-// is an anti-pattern. After fixing the problem, these types should be removed.
-
-/** @deprecated */
-export enum ArrangeTypeEnum {
- forward,
- backward,
- front,
- back,
-}
-
-/** @deprecated */
-export enum DrawingTypeEnum {
- UNRECOGNIZED = -1,
- DRAWING_IMAGE = 0,
- DRAWING_SHAPE = 1,
- DRAWING_CHART = 2,
- DRAWING_TABLE = 3,
- DRAWING_SMART_ART = 4,
- DRAWING_VIDEO = 5,
- DRAWING_GROUP = 6,
- DRAWING_UNIT = 7,
- DRAWING_DOM = 8,
-}
-
-/** @deprecated */
-export type DrawingType = DrawingTypeEnum | number;
-
-/** @deprecated */
-export interface IDrawingSpace {
- unitId: string;
- subUnitId: string; //sheetId, pageId and so on, it has a default name in doc business
-}
-
-/** @deprecated */
-export interface IDrawingSearch extends IDrawingSpace {
- drawingId: string;
-}
-
-/** @deprecated */
-export interface IRotationSkewFlipTransform {
- angle?: number;
- skewX?: number;
- skewY?: number;
- flipX?: boolean;
- flipY?: boolean;
-}
-
-/** @deprecated */
-export interface ITransformState extends IAbsoluteTransform, IRotationSkewFlipTransform {}
-
-/** @deprecated */
-export interface IDrawingParam extends IDrawingSearch {
- drawingType: DrawingType;
- transform?: Nullable<ITransformState>;
- transforms?: Nullable<ITransformState[]>;
- // The same drawing render in different place, like image in header and footer.
- // The default value is BooleanNumber.FALSE. if it's true, Please use transforms.
- isMultiTransform?: BooleanNumber;
- groupId?: string;
-}
-
-// #endregion
diff --git a/packages/core/src/types/interfaces/i-drawing.ts b/packages/core/src/types/interfaces/i-drawing.ts
new file mode 100644
index 0000000000..69d705d2e8
--- /dev/null
+++ b/packages/core/src/types/interfaces/i-drawing.ts
@@ -0,0 +1,120 @@
+/**
+ * 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 { IAbsoluteTransform } from '../../shared/shape';
+import type { Nullable } from '../../shared/types';
+import type { BooleanNumber } from '../enum/text-style';
+
+/**
+ * The layer type of Drawing, used to distinguish between forward, backward, front, and back
+ */
+export enum ArrangeTypeEnum {
+ /**
+ * Move the current object one layer up, possibly covering other objects
+ */
+ forward,
+ /**
+ * Move the current object one layer down, possibly being covered by other objects
+ */
+ backward,
+ /**
+ * Move the current object to the top layer
+ */
+ front,
+ /**
+ * Move the current object to the bottom layer
+ */
+ back,
+}
+
+/**
+ * Types of drawings, used to distinguish between images, shapes, charts, tables, SmartArt, videos, DrawingGroup, Unit, Dom, etc.
+ */
+export enum DrawingTypeEnum {
+ /**
+ * Unrecognized drawing type, requires user to determine
+ */
+ UNRECOGNIZED = -1,
+ /**
+ * Image
+ */
+ DRAWING_IMAGE = 0,
+ /**
+ * Shape, similar to shapes in Office, including circles, rectangles, lines, etc.
+ */
+ DRAWING_SHAPE = 1,
+ /**
+ * Chart
+ */
+ DRAWING_CHART = 2,
+ /**
+ * Table
+ */
+ DRAWING_TABLE = 3,
+ /**
+ * SmartArt, similar to SmartArt in Office
+ */
+ DRAWING_SMART_ART = 4,
+ /**
+ * Video
+ */
+ DRAWING_VIDEO = 5,
+ /**
+ * Drawing group
+ */
+ DRAWING_GROUP = 6,
+ /**
+ * Univer object, allows inserting images, tables, documents, slides as floating objects into the document
+ */
+ DRAWING_UNIT = 7,
+ /**
+ * Dom element, allows inserting HTML elements as floating objects into the document
+ */
+ DRAWING_DOM = 8,
+}
+
+export type DrawingType = DrawingTypeEnum | number;
+
+export interface IDrawingSpace {
+ unitId: string;
+ subUnitId: string; //sheetId, pageId and so on, it has a default name in doc business
+}
+
+export interface IDrawingSearch extends IDrawingSpace {
+ drawingId: string;
+}
+
+export interface IRotationSkewFlipTransform {
+ angle?: number;
+ skewX?: number;
+ skewY?: number;
+ flipX?: boolean;
+ flipY?: boolean;
+}
+
+export interface ITransformState extends IAbsoluteTransform, IRotationSkewFlipTransform {}
+
+export interface IDrawingParam extends IDrawingSearch {
+ drawingType: DrawingType;
+ transform?: Nullable<ITransformState>;
+ transforms?: Nullable<ITransformState[]>;
+ // The same drawing render in different place, like image in header and footer.
+ // The default value is BooleanNumber.FALSE. if it's true, Please use transforms.
+ isMultiTransform?: BooleanNumber;
+ groupId?: string;
+}
+
+// #endregion
diff --git a/packages/core/src/types/interfaces/i-slide-data.ts b/packages/core/src/types/interfaces/i-slide-data.ts
index f613f73043..e941e1820f 100644
--- a/packages/core/src/types/interfaces/i-slide-data.ts
+++ b/packages/core/src/types/interfaces/i-slide-data.ts
@@ -19,7 +19,8 @@ import type { IKeyType, Nullable } from '../../shared/types';
import type { IWorksheetData } from '../../sheets/typedef';
import type { LocaleType, ThemeColorType } from '../enum';
import type { ShapeType } from '../enum/prst-geom-type';
-import type { ICustomBlock, IDocumentData, ILists, ITransformState } from './i-document-data';
+import type { ICustomBlock, IDocumentData, ILists } from './i-document-data';
+import type { ITransformState } from './i-drawing';
import type { IImageProperties } from './i-image-properties';
import type { IPlaceholder } from './i-placeholder';
import type { IShapeProperties } from './i-shape-properties';
diff --git a/packages/core/src/types/interfaces/index.ts b/packages/core/src/types/interfaces/index.ts
index e9cef83e08..d6843b045e 100644
--- a/packages/core/src/types/interfaces/index.ts
+++ b/packages/core/src/types/interfaces/index.ts
@@ -16,6 +16,7 @@
export * from './i-document-data';
export * from './i-document-data-interceptor';
+export * from './i-drawing';
export * from './i-image-properties';
export * from './i-mention';
export * from './i-need-check-disposable';
diff --git a/packages/drawing-ui/src/controllers/drawing-update.controller.ts b/packages/drawing-ui/src/controllers/drawing-update.controller.ts
index 36cb0d95ae..d3dd24483f 100644
--- a/packages/drawing-ui/src/controllers/drawing-update.controller.ts
+++ b/packages/drawing-ui/src/controllers/drawing-update.controller.ts
@@ -25,7 +25,7 @@ import {
IUniverInstanceService,
toDisposable,
} from '@univerjs/core';
-import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService } from '@univerjs/drawing';
+import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService, SetDrawingSelectedOperation } from '@univerjs/drawing';
import { DRAWING_OBJECT_LAYER_INDEX, Group, IRenderManagerService, RENDER_CLASS_TYPE } from '@univerjs/engine-render';
import { AlignType, SetDrawingAlignOperation } from '../commands/operations/drawing-align.operation';
import { CloseImageCropOperation } from '../commands/operations/image-crop.operation';
@@ -663,6 +663,8 @@ export class DrawingUpdateController extends Disposable {
}
drawingShape.transformByState({ left, top, width, height, angle, flipX, flipY, skewX, skewY });
+
+ scene.getTransformer()?.debounceRefreshControls();
});
})
);
@@ -793,9 +795,9 @@ export class DrawingUpdateController extends Disposable {
}).filter((transform) => transform != null) as ITransformState[];
if (drawings.length > 0) {
- this._drawingManagerService.focusDrawing(drawings);
+ this._commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, drawings);
} else {
- this._drawingManagerService.focusDrawing(null);
+ this._commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, []);
}
})
)
diff --git a/packages/drawing-ui/src/controllers/image-cropper.controller.ts b/packages/drawing-ui/src/controllers/image-cropper.controller.ts
index 5dbc95dace..6a5a586f63 100644
--- a/packages/drawing-ui/src/controllers/image-cropper.controller.ts
+++ b/packages/drawing-ui/src/controllers/image-cropper.controller.ts
@@ -20,7 +20,7 @@ import type { BaseObject, Scene } from '@univerjs/engine-render';
import type { IOpenImageCropOperationBySrcRectParams } from '../commands/operations/image-crop.operation';
import { checkIfMove, Disposable, ICommandService, Inject, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
import { MessageType } from '@univerjs/design';
-import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService } from '@univerjs/drawing';
+import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService, SetDrawingSelectedOperation } from '@univerjs/drawing';
import { CURSOR_TYPE, degToRad, Image, IRenderManagerService, precisionTo, Vector2 } from '@univerjs/engine-render';
import { IMessageService } from '@univerjs/ui';
import { filter, switchMap } from 'rxjs';
@@ -262,7 +262,7 @@ export class ImageCropperController extends Disposable {
transformer?.refreshControls();
imageCropperObject.makeDirty(true);
- this._drawingManagerService.focusDrawing([{ unitId, subUnitId, drawingId }]);
+ this._commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, [{ unitId, subUnitId, drawingId }]);
})
);
}
diff --git a/packages/drawing-ui/src/controllers/image-update.controller.ts b/packages/drawing-ui/src/controllers/image-update.controller.ts
index 71963eabbe..f6b13a1be2 100644
--- a/packages/drawing-ui/src/controllers/image-update.controller.ts
+++ b/packages/drawing-ui/src/controllers/image-update.controller.ts
@@ -25,7 +25,7 @@ import {
IUniverInstanceService,
toDisposable,
} from '@univerjs/core';
-import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService, IImageIoService } from '@univerjs/drawing';
+import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService, IImageIoService, SetDrawingSelectedOperation } from '@univerjs/drawing';
import { CURSOR_TYPE, IRenderManagerService } from '@univerjs/engine-render';
import { IDialogService } from '@univerjs/ui';
import { ImageResetSizeOperation } from '../commands/operations/image-reset-size.operation';
@@ -151,7 +151,7 @@ export class ImageUpdateController extends Disposable {
transformer.refreshControls().changeNotification();
});
- this._drawingManagerService.focusDrawing(params);
+ this._commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, params);
}
private _drawingAddListener() {
diff --git a/packages/drawing/src/commands/operations/set-drawing-selected.operation.ts b/packages/drawing/src/commands/operations/set-drawing-selected.operation.ts
new file mode 100644
index 0000000000..4b029e411c
--- /dev/null
+++ b/packages/drawing/src/commands/operations/set-drawing-selected.operation.ts
@@ -0,0 +1,35 @@
+/**
+ * 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 { IAccessor, IDrawingSearch, IOperation } from '@univerjs/core';
+import {
+ CommandType,
+} from '@univerjs/core';
+import { IDrawingManagerService } from '../../services/drawing-manager.service';
+
+export const SetDrawingSelectedOperation: IOperation<IDrawingSearch[]> = {
+ id: 'drawing.operation.set-drawing-selected',
+ type: CommandType.OPERATION,
+ handler: (accessor: IAccessor, params?: IDrawingSearch[]) => {
+ const drawingManagerService = accessor.get(IDrawingManagerService);
+
+ if (params == null) {
+ return false;
+ }
+ drawingManagerService.focusDrawing(params);
+ return true;
+ },
+};
diff --git a/packages/drawing/src/index.ts b/packages/drawing/src/index.ts
index 1991f44ff2..adfacf294f 100644
--- a/packages/drawing/src/index.ts
+++ b/packages/drawing/src/index.ts
@@ -34,3 +34,5 @@ export {
type IDrawingVisibleParam,
type IUnitDrawingService,
} from './services/drawing-manager.service';
+
+export { SetDrawingSelectedOperation } from './commands/operations/set-drawing-selected.operation';
diff --git a/packages/drawing/src/plugin.ts b/packages/drawing/src/plugin.ts
index 531002a772..5f22b5374c 100644
--- a/packages/drawing/src/plugin.ts
+++ b/packages/drawing/src/plugin.ts
@@ -17,7 +17,8 @@
import type { Dependency } from '@univerjs/core';
import type { IUniverDrawingConfig } from './controllers/config.schema';
-import { IConfigService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';
+import { ICommandService, IConfigService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';
+import { SetDrawingSelectedOperation } from './commands/operations/set-drawing-selected.operation';
import { defaultPluginConfig, DRAWING_PLUGIN_CONFIG_KEY } from './controllers/config.schema';
import { DrawingManagerService } from './services/drawing-manager-impl.service';
import { IDrawingManagerService } from './services/drawing-manager.service';
@@ -32,7 +33,8 @@ export class UniverDrawingPlugin extends Plugin {
constructor(
private readonly _config: Partial<IUniverDrawingConfig> = defaultPluginConfig,
@Inject(Injector) protected _injector: Injector,
- @IConfigService private readonly _configService: IConfigService
+ @IConfigService private readonly _configService: IConfigService,
+ @ICommandService private readonly _commandService: ICommandService
) {
super();
@@ -46,6 +48,7 @@ export class UniverDrawingPlugin extends Plugin {
}
override onStarting(): void {
+ this._initCommands();
this._initDependencies();
}
@@ -58,4 +61,10 @@ export class UniverDrawingPlugin extends Plugin {
const dependency = mergeOverrideWithDependencies(dependencies, this._config?.override);
dependency.forEach((d) => this._injector.add(d));
}
+
+ private _initCommands() {
+ [
+ SetDrawingSelectedOperation,
+ ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
+ }
}
diff --git a/packages/drawing/src/services/drawing-manager-impl.service.ts b/packages/drawing/src/services/drawing-manager-impl.service.ts
index f8c63bf01c..043cf90f1f 100644
--- a/packages/drawing/src/services/drawing-manager-impl.service.ts
+++ b/packages/drawing/src/services/drawing-manager-impl.service.ts
@@ -470,7 +470,7 @@ export class UnitDrawingService<T extends IDrawingParam> implements IUnitDrawing
}
focusDrawing(params: Nullable<IDrawingSearch[]>): void {
- if (params == null) {
+ if (params == null || params.length === 0) {
this._focusDrawings = [];
this._focus$.next([]);
return;
diff --git a/packages/sheets-drawing-ui/src/commands/operations/edit-sheet-drawing.operation.ts b/packages/sheets-drawing-ui/src/commands/operations/edit-sheet-drawing.operation.ts
index a294b82f4e..5a9bfc2f9f 100644
--- a/packages/sheets-drawing-ui/src/commands/operations/edit-sheet-drawing.operation.ts
+++ b/packages/sheets-drawing-ui/src/commands/operations/edit-sheet-drawing.operation.ts
@@ -19,20 +19,21 @@ import {
CommandType,
ICommandService,
} from '@univerjs/core';
-import { IDrawingManagerService } from '@univerjs/drawing';
+import { SetDrawingSelectedOperation } from '@univerjs/drawing';
import { SidebarSheetDrawingOperation } from './open-drawing-panel.operation';
export const EditSheetDrawingOperation: IOperation<IDrawingSearch> = {
id: 'sheet.operation.edit-sheet-image',
type: CommandType.OPERATION,
handler: (accessor: IAccessor, params?: IDrawingSearch) => {
- const drawingManagerService = accessor.get(IDrawingManagerService);
+ // const drawingManagerService = accessor.get(IDrawingManagerService);
const commandService = accessor.get(ICommandService);
if (params == null) {
return false;
}
- drawingManagerService.focusDrawing([params]);
+
+ commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, [params]);
commandService.executeCommand(SidebarSheetDrawingOperation.id, { value: 'open' });
return true;
},
diff --git a/packages/sheets-drawing-ui/src/commands/operations/open-drawing-panel.operation.ts b/packages/sheets-drawing-ui/src/commands/operations/open-drawing-panel.operation.ts
index c742b5f3b8..bbd5b07443 100644
--- a/packages/sheets-drawing-ui/src/commands/operations/open-drawing-panel.operation.ts
+++ b/packages/sheets-drawing-ui/src/commands/operations/open-drawing-panel.operation.ts
@@ -15,10 +15,10 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
-import { CommandType, IUniverInstanceService, LocaleService } from '@univerjs/core';
-import { ISidebarService } from '@univerjs/ui';
+import { CommandType, ICommandService, IUniverInstanceService, LocaleService } from '@univerjs/core';
+import { SetDrawingSelectedOperation } from '@univerjs/drawing';
import { getSheetCommandTarget } from '@univerjs/sheets';
-import { IDrawingManagerService } from '@univerjs/drawing';
+import { ISidebarService } from '@univerjs/ui';
import { COMPONENT_SHEET_DRAWING_PANEL } from '../../views/sheet-image-panel/component-name';
export interface IUIComponentCommandParams {
@@ -32,7 +32,8 @@ export const SidebarSheetDrawingOperation: ICommand = {
const sidebarService = accessor.get(ISidebarService);
const localeService = accessor.get(LocaleService);
const univerInstanceService = accessor.get(IUniverInstanceService);
- const drawingManagerService = accessor.get(IDrawingManagerService);
+ // const drawingManagerService = accessor.get(IDrawingManagerService);
+ const commandService = accessor.get(ICommandService);
const target = getSheetCommandTarget(univerInstanceService);
if (!target) return false;
@@ -43,7 +44,7 @@ export const SidebarSheetDrawingOperation: ICommand = {
header: { title: localeService.t('sheetImage.panel.title') },
children: { label: COMPONENT_SHEET_DRAWING_PANEL },
onClose: () => {
- drawingManagerService.focusDrawing(null);
+ commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, []);
},
width: 360,
});
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.ts
index dec6d4911c..c94f940665 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
@@ -17,8 +17,8 @@
import type { IDisposable, Nullable, Workbook } from '@univerjs/core';
import type { BaseObject, Scene } from '@univerjs/engine-render';
import type { ISheetFloatDom } from '@univerjs/sheets-drawing';
-import { connectInjector, DrawingTypeEnum, FOCUSING_COMMON_DRAWINGS, IContextService, Inject, Injector, IUniverInstanceService, RxDisposable, toDisposable, UniverInstanceType } from '@univerjs/core';
-import { IDrawingManagerService } from '@univerjs/drawing';
+import { connectInjector, DrawingTypeEnum, FOCUSING_COMMON_DRAWINGS, ICommandService, IContextService, Inject, Injector, IUniverInstanceService, RxDisposable, toDisposable, UniverInstanceType } from '@univerjs/core';
+import { IDrawingManagerService, SetDrawingSelectedOperation } from '@univerjs/drawing';
import { COMPONENT_IMAGE_POPUP_MENU, ImageCropperObject, ImageResetSizeOperation, OpenImageCropOperation } from '@univerjs/drawing-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetCanvasPopManagerService } from '@univerjs/sheets-ui';
@@ -39,7 +39,8 @@ export class DrawingPopupMenuController extends RxDisposable {
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@IContextService private readonly _contextService: IContextService,
- @Inject(IUIPartsService) private readonly _uiPartsService: IUIPartsService
+ @Inject(IUIPartsService) private readonly _uiPartsService: IUIPartsService,
+ @ICommandService private readonly _commandService: ICommandService
) {
super();
@@ -136,12 +137,6 @@ export class DrawingPopupMenuController extends RxDisposable {
menuItems: menus || this._getImageMenuItems(unitId, subUnitId, drawingId, drawingType),
},
}));
-
- this._drawingManagerService.focusDrawing([{
- unitId,
- subUnitId,
- drawingId,
- }]);
})
)
);
@@ -149,7 +144,7 @@ export class DrawingPopupMenuController extends RxDisposable {
transformer.clearControl$.subscribe(() => {
singletonPopupDisposer?.dispose();
this._contextService.setContextValue(FOCUSING_COMMON_DRAWINGS, false);
- this._drawingManagerService.focusDrawing(null);
+ this._commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, []);
})
);
this.disposeWithMe(
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.ts
index 56e33b54d2..8afd9e669b 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
@@ -23,7 +23,7 @@ import type { ISetDrawingArrangeCommandParams } from '../commands/commands/set-d
import { BooleanNumber, BuildTextUtils, createDocumentModelWithStyle, Disposable, DrawingTypeEnum, FOCUSING_COMMON_DRAWINGS, ICommandService, IContextService, Inject, Injector, LocaleService, ObjectRelativeFromH, ObjectRelativeFromV, PositionedObjectLayoutType, WrapTextType } from '@univerjs/core';
import { MessageType } from '@univerjs/design';
import { docDrawingPositionToTransform } from '@univerjs/docs-ui';
-import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, getImageSize, IDrawingManagerService, IImageIoService, ImageUploadStatusType } from '@univerjs/drawing';
+import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, getImageSize, IDrawingManagerService, IImageIoService, ImageUploadStatusType, SetDrawingSelectedOperation } from '@univerjs/drawing';
import { type IRenderContext, IRenderManagerService, type IRenderModule } from '@univerjs/engine-render';
import { SetRangeValuesCommand, SheetsSelectionsService } from '@univerjs/sheets';
import { ISheetDrawingService } from '@univerjs/sheets-drawing';
@@ -36,13 +36,29 @@ import { SetDrawingArrangeCommand } from '../commands/commands/set-drawing-arran
import { SetSheetDrawingCommand } from '../commands/commands/set-sheet-drawing.command';
import { UngroupSheetDrawingCommand } from '../commands/commands/ungroup-sheet-drawing.command';
-function rotatedBoundingBox(width: number, height: number, angleDegrees: number) {
- const angle = angleDegrees * Math.PI / 180; // 将角度转换为弧度
+/**
+ * Calculate the bounding box after rotation
+ * @param {number} width Width
+ * @param {number} height Height
+ * @param {number} angleDegrees Rotation angle in degrees (0-360)
+ * @returns {{ rotatedWidth: number; rotatedHeight: number }} Rotated width and height
+ */
+function rotatedBoundingBox(width: number, height: number, angleDegrees: number): { rotatedWidth: number; rotatedHeight: number } {
+ const angle = angleDegrees * Math.PI / 180; // Convert angle to radians
const rotatedWidth = Math.abs(width * Math.cos(angle)) + Math.abs(height * Math.sin(angle));
const rotatedHeight = Math.abs(width * Math.sin(angle)) + Math.abs(height * Math.cos(angle));
return { rotatedWidth, rotatedHeight };
}
+/**
+ * Get the size of the drawing within the cell
+ * @param {IAccessor} accessor Accessor
+ * @param {ISheetLocationBase} location Cell location
+ * @param {number} originImageWidth Original image width
+ * @param {number} originImageHeight Original image height
+ * @param {number} angle Rotation angle in degrees (0-360)
+ * @returns {{ width: number; height: number }} Drawing size
+ */
export function getDrawingSizeByCell(
accessor: IAccessor,
location: ISheetLocationBase,
@@ -477,7 +493,7 @@ export class SheetDrawingUpdateController extends Disposable implements IRenderM
this.disposeWithMe(this._drawingManagerService.featurePluginGroupUpdate$.subscribe((params) => {
this._commandService.executeCommand(GroupSheetDrawingCommand.id, params);
const { unitId, subUnitId, drawingId } = params[0].parent;
- this._drawingManagerService.focusDrawing([{ unitId, subUnitId, drawingId }]);
+ this._commandService.syncExecuteCommand(SetDrawingSelectedOperation.id, [{ unitId, subUnitId, drawingId }]);
}));
this.disposeWithMe(this._drawingManagerService.featurePluginUngroupUpdate$.subscribe((params) => {
diff --git a/packages/sheets-drawing-ui/src/facade/f-enum.ts b/packages/sheets-drawing-ui/src/facade/f-enum.ts
new file mode 100644
index 0000000000..a631d08014
--- /dev/null
+++ b/packages/sheets-drawing-ui/src/facade/f-enum.ts
@@ -0,0 +1,43 @@
+/**
+ * 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 { DrawingTypeEnum, FEnum, ImageSourceType } from '@univerjs/core';
+import { SheetDrawingAnchorType } from '@univerjs/sheets-drawing';
+
+export interface IFDrawingEnumMixin {
+ /** Please refer to {@link DrawingTypeEnum}. */
+ DrawingType: typeof DrawingTypeEnum;
+
+ /** Please refer to {@link ImageSourceType}. */
+ ImageSourceType: Omit<typeof ImageSourceType, 'UUID'>;
+
+ /** Please refer to {@link SheetDrawingAnchorType}. */
+ SheetDrawingAnchorType: typeof SheetDrawingAnchorType;
+}
+
+export class FDrawingEnumMixin extends FEnum implements IFDrawingEnumMixin {
+ override get DrawingType(): typeof DrawingTypeEnum { return DrawingTypeEnum; };
+
+ override get ImageSourceType(): Omit<typeof ImageSourceType, 'UUID'> { return ImageSourceType; };
+
+ override get SheetDrawingAnchorType(): typeof SheetDrawingAnchorType { return SheetDrawingAnchorType; };
+}
+
+FEnum.extend(FDrawingEnumMixin);
+declare module '@univerjs/core' {
+ // eslint-disable-next-line ts/naming-convention
+ interface FEnum extends IFDrawingEnumMixin { }
+}
diff --git a/packages/sheets-drawing-ui/src/facade/f-event.ts b/packages/sheets-drawing-ui/src/facade/f-event.ts
new file mode 100644
index 0000000000..f8389a93de
--- /dev/null
+++ b/packages/sheets-drawing-ui/src/facade/f-event.ts
@@ -0,0 +1,306 @@
+/**
+ * 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 { IDrawingSearch, IEventBase } from '@univerjs/core';
+import type { ISheetImage } from '@univerjs/sheets-drawing';
+import type { FWorkbook } from '@univerjs/sheets/facade';
+import type { FOverGridImage } from './f-over-grid-image';
+import { FEventName } from '@univerjs/core';
+
+interface IFDrawingEventNameMixin {
+ /**
+ * Triggered before floating image insertion.
+ * Type of the event parameter is {@link IBeforeOverGridImageInsertParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.BeforeOverGridImageInsert, (param) => {
+ * const { workbook, insertImageParams } = param;
+ * // do something
+ * console.log(workbook, insertImageParams);
+ * // Cancel the insertion operation
+ * param.cancel = true;
+ * })
+ * ```
+ */
+ readonly BeforeOverGridImageInsert: 'BeforeOverGridImageInsert';
+
+ /**
+ * Triggered after floating image insertion.
+ * Type of the event parameter is {@link IOverGridImageInsertedParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.OverGridImageInserted, (param) => {
+ * const { workbook, images } = param;
+ * // do something
+ * console.log(workbook, images);
+ * })
+ * ```
+ */
+ readonly OverGridImageInserted: 'OverGridImageInserted';
+
+ /**
+ * Triggered before floating image removal.
+ * Type of the event parameter is {@link IBeforeOverGridImageRemoveParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.BeforeOverGridImageRemove, (param) => {
+ * const { workbook, images } = param;
+ * // do something
+ * console.log(workbook, images);
+ * // Cancel the removal operation
+ * param.cancel = true;
+ * })
+ * ```
+ */
+ readonly BeforeOverGridImageRemove: 'BeforeOverGridImageRemove';
+
+ /**
+ * Triggered after floating image removal.
+ * Type of the event parameter is {@link IOverGridImageRemovedParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.OverGridImageRemoved, (param) => {
+ * const { workbook, removeImageParams } = param;
+ * // do something
+ * console.log(workbook, removeImageParams);
+ * })
+ * ```
+ */
+ readonly OverGridImageRemoved: 'OverGridImageRemoved';
+
+ /**
+ * Triggered before floating image change.
+ * Type of the event parameter is {@link IBeforeOverGridImageChangeParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.BeforeOverGridImageChange, (param) => {
+ * const { workbook, images } = param;
+ * // do something
+ * console.log(workbook, images);
+ * // Cancel the image change
+ * param.cancel = true;
+ * })
+ * ```
+ */
+ readonly BeforeOverGridImageChange: 'BeforeOverGridImageChange';
+
+ /**
+ * Triggered after floating image change.
+ * Type of the event parameter is {@link IOverGridImageChangedParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.OverGridImageChanged, (param) => {
+ * const { workbook, images } = param;
+ * // do something
+ * console.log(workbook, images);
+ * })
+ * ```
+ */
+ readonly OverGridImageChanged: 'OverGridImageChanged';
+
+ /**
+ * Triggered before floating image selection.
+ * Type of the event parameter is {@link IBeforeOverGridImageSelectParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.BeforeOverGridImageSelect, (param) => {
+ * const { workbook, selectedImages, oldSelectedImages } = param;
+ * // do something
+ * console.log(workbook, selectedImages, oldSelectedImages);
+ * // cancel the event
+ * param.cancel = true;
+ * })
+ * ```
+ */
+ readonly BeforeOverGridImageSelect: 'BeforeOverGridImageSelect';
+
+ /**
+ * Triggered after floating image selection.
+ * Type of the event parameter is {@link IOverGridImageSelectedParam}
+ * @example
+ * ```ts
+ * univerAPI.addEvent(univerAPI.Event.OverGridImageSelected, (param) => {
+ * const { workbook, selectedImages } = param;
+ * // do something
+ * console.log(workbook, selectedImages);
+ * })
+ * ```
+ */
+ readonly OverGridImageSelected: 'OverGridImageSelected';
+}
+
+export class FDrawingEventNameMixin extends FEventName implements IFDrawingEventNameMixin {
+ override get BeforeOverGridImageChange(): 'BeforeOverGridImageChange' {
+ return 'BeforeOverGridImageChange' as const;
+ }
+
+ override get OverGridImageChanged(): 'OverGridImageChanged' {
+ return 'OverGridImageChanged' as const;
+ }
+
+ override get BeforeOverGridImageInsert(): 'BeforeOverGridImageInsert' {
+ return 'BeforeOverGridImageInsert' as const;
+ }
+
+ override get OverGridImageInserted(): 'OverGridImageInserted' {
+ return 'OverGridImageInserted' as const;
+ }
+
+ override get BeforeOverGridImageRemove(): 'BeforeOverGridImageRemove' {
+ return 'BeforeOverGridImageRemove' as const;
+ }
+
+ override get OverGridImageRemoved(): 'OverGridImageRemoved' {
+ return 'OverGridImageRemoved' as const;
+ }
+
+ override get BeforeOverGridImageSelect(): 'BeforeOverGridImageSelect' {
+ return 'BeforeOverGridImageSelect' as const;
+ }
+
+ override get OverGridImageSelected(): 'OverGridImageSelected' {
+ return 'OverGridImageSelected' as const;
+ }
+}
+
+export interface IBeforeOverGridImageChangeParamObject {
+ /**
+ * Parameters of the image to be updated. {@link ISheetImage}
+ */
+ changeParam: Partial<ISheetImage>;
+
+ /**
+ * Instance of the image to be updated. {@link FOverGridImage}
+ */
+ image: FOverGridImage;
+}
+
+export interface IBeforeOverGridImageChangeParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+
+ /**
+ * Collection of parameters of the images to be updated, there may be multiple images to be updated. {@link IBeforeOverGridImageChangeParamObject}
+ */
+ images: IBeforeOverGridImageChangeParamObject[];
+}
+
+export interface IOverGridImageChangedParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+
+ /**
+ * Instances of the images after the update. {@link FOverGridImage}
+ */
+ images: FOverGridImage[];
+}
+
+export interface IBeforeOverGridImageInsertParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+
+ /**
+ * Collection of parameters of the images to be inserted. {@link ISheetImage}
+ */
+ insertImageParams: ISheetImage[];
+}
+
+export interface IOverGridImageInsertedParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+
+ /**
+ * Collection of instances of the images after insertion. {@link FOverGridImage}
+ */
+ images: FOverGridImage[];
+}
+
+export interface IBeforeOverGridImageRemoveParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+
+ /**
+ * Collection of instances of the images to be removed. {@link FOverGridImage}
+ */
+ images: FOverGridImage[];
+}
+
+export interface IOverGridImageRemovedParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+
+ /**
+ * Collection of parameters of the images after removal. {@link ISheetImage}
+ */
+ removeImageParams: IDrawingSearch[];
+}
+
+export interface IBeforeOverGridImageSelectParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+ /**
+ * The images before selection. {@link FOverGridImage}
+ */
+ oldSelectedImages: FOverGridImage[];
+
+ selectedImages: FOverGridImage[];
+}
+
+export interface IOverGridImageSelectedParam extends IEventBase {
+ /**
+ * The workbook instance currently being operated on. {@link FWorkbook}
+ */
+ workbook: FWorkbook;
+ /**
+ * The selected images. {@link FOverGridImage}
+ */
+ selectedImages: FOverGridImage[];
+}
+
+interface IFSheetsUIEventParamConfig {
+ BeforeOverGridImageChange: IBeforeOverGridImageChangeParam;
+ OverGridImageChanged: IOverGridImageChangedParam;
+
+ BeforeOverGridImageInsert: IBeforeOverGridImageInsertParam;
+ OverGridImageInserted: IOverGridImageInsertedParam;
+
+ BeforeOverGridImageRemove: IBeforeOverGridImageRemoveParam;
+ OverGridImageRemoved: IOverGridImageRemovedParam;
+
+ BeforeOverGridImageSelect: IBeforeOverGridImageSelectParam;
+ OverGridImageSelected: IOverGridImageSelectedParam;
+}
+
+FEventName.extend(FDrawingEventNameMixin);
+declare module '@univerjs/core' {
+ // eslint-disable-next-line ts/naming-convention
+ interface FEventName extends IFDrawingEventNameMixin { }
+ interface IEventParamConfig extends IFSheetsUIEventParamConfig { }
+}
diff --git a/packages/sheets-drawing-ui/src/facade/f-over-grid-image.ts b/packages/sheets-drawing-ui/src/facade/f-over-grid-image.ts
index 3d8b25a861..916c0912e5 100644
--- a/packages/sheets-drawing-ui/src/facade/f-over-grid-image.ts
+++ b/packages/sheets-drawing-ui/src/facade/f-over-grid-image.ts
@@ -17,16 +17,22 @@
import type { IRotationSkewFlipTransform, ISize } from '@univerjs/core';
import type { ICellOverGridPosition } from '@univerjs/sheets';
import type { ISheetImage, SheetDrawingAnchorType } from '@univerjs/sheets-drawing';
-import { DrawingTypeEnum, FBase, generateRandomId, ICommandService, ImageSourceType, Inject, Injector } from '@univerjs/core';
+import { ArrangeTypeEnum, DrawingTypeEnum, FBase, generateRandomId, ICommandService, ImageSourceType, Inject, Injector } from '@univerjs/core';
import { getImageSize } from '@univerjs/drawing';
import { IRenderManagerService } from '@univerjs/engine-render';
-import { SetSheetDrawingCommand } from '@univerjs/sheets-drawing-ui';
+import { SetDrawingArrangeCommand, SetSheetDrawingCommand } from '@univerjs/sheets-drawing-ui';
import { convertPositionCellToSheetOverGrid, convertPositionSheetOverGridToAbsolute, ISheetSelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
export interface IFOverGridImage extends Omit<ISheetImage, 'sheetTransform' | 'transform'>, ICellOverGridPosition, IRotationSkewFlipTransform, Required<ISize> {
}
+/**
+ * Convert the image parameter to a FOverGridImage
+ * @param {ISheetImage} sheetImage The image parameter
+ * @param {SheetSkeletonManagerService} sheetSkeletonManagerService The skeleton manager service
+ * @returns {IFOverGridImage} The FOverGridImage {@link IFOverGridImage}
+ */
function convertSheetImageToFOverGridImage(sheetImage: ISheetImage, sheetSkeletonManagerService: SheetSkeletonManagerService): IFOverGridImage {
const { from, to, flipY = false, flipX = false, angle = 0, skewX = 0, skewY = 0 } = sheetImage.sheetTransform;
@@ -54,6 +60,13 @@ function convertSheetImageToFOverGridImage(sheetImage: ISheetImage, sheetSkeleto
};
}
+/**
+ * Convert the FOverGridImage to a ISheetImage
+ * @param {IFOverGridImage} fOverGridImage The FOverGridImage
+ * @param {ISheetSelectionRenderService} selectionRenderService The selection render service
+ * @param {SheetSkeletonManagerService} sheetSkeletonManagerService The skeleton manager service
+ * @returns {ISheetImage} The ISheetImage {@link ISheetImage}
+ */
function convertFOverGridImageToSheetImage(fOverGridImage: IFOverGridImage, selectionRenderService: ISheetSelectionRenderService, sheetSkeletonManagerService: SheetSkeletonManagerService): ISheetImage {
const { column: fromColumn, columnOffset: fromColumnOffset, row: fromRow, rowOffset: fromRowOffset, flipY = false, flipX = false, angle = 0, skewX = 0, skewY = 0, width, height } = fOverGridImage;
@@ -109,6 +122,27 @@ export class FOverGridImageBuilder {
};
}
+ /**
+ * Set the initial image configuration for the image builder.
+ * @param image The image to be set. {@link ISheetImage}
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setImage({
+ * drawingId: '123456',
+ * drawingType: univerAPI.Enum.DrawingType.DRAWING_IMAGE,
+ * imageSourceType: univerAPI.Enum.ImageSourceType.BASE64,
+ * source: 'https://avatars.githubusercontent.com/u/61444807?s=48&v=4',
+ * unitId: activeSpreadsheet.getId(),
+ * subUnitId: activeSheet.getSheetId(),
+ * }).setColumn(5).setRow(5).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setImage(image: ISheetImage): FOverGridImageBuilder {
const renderManagerService = this._injector.get(IRenderManagerService);
const render = renderManagerService.getRenderById(image.unitId);
@@ -117,6 +151,22 @@ export class FOverGridImageBuilder {
}
const skeletonManagerService = render.with(SheetSkeletonManagerService);
+ if (image.sheetTransform == null) {
+ image.sheetTransform = {
+ from: {
+ column: 0,
+ columnOffset: 0,
+ row: 0,
+ rowOffset: 0,
+ },
+ to: {
+ column: 0,
+ columnOffset: 0,
+ row: 0,
+ rowOffset: 0,
+ },
+ };
+ }
this._image = convertSheetImageToFOverGridImage(image, skeletonManagerService);
return this;
}
@@ -131,7 +181,7 @@ export class FOverGridImageBuilder {
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
* const activeSheet = activeSpreadsheet.getActiveSheet();
* const imageBuilder = activeSheet.newOverGridImage();
- * const param = imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).build();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).buildAsync();
* activeSheet.insertImages([param]);
* ```
*/
@@ -152,59 +202,213 @@ export class FOverGridImageBuilder {
return this._image.imageSourceType;
}
+ /**
+ * Set the position of the image
+ * @param column The sheet start column of the image
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setColumn(column: number): FOverGridImageBuilder {
this._image.column = column;
return this;
}
+ /**
+ * Set the position of the image
+ * @param row The sheet start row of the image
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setRow(row: number): FOverGridImageBuilder {
this._image.row = row;
return this;
}
+ /**
+ * Set the column offset of the image in a unit
+ * @param offset The offset in pixels
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setColumnOffset(10).setRowOffset(10).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setColumnOffset(offset: number): FOverGridImageBuilder {
this._image.columnOffset = offset;
return this;
}
+ /**
+ * Set the row offset of the image in a unit
+ * @param offset The offset in pixels
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setColumnOffset(10).setRowOffset(10).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setRowOffset(offset: number): FOverGridImageBuilder {
this._image.rowOffset = offset;
return this;
}
+ /**
+ * set the width of the image
+ * @param width The width of the image, in pixels
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setWidth(120).setHeight(50).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setWidth(width: number): FOverGridImageBuilder {
this._image.width = width;
return this;
}
+ /**
+ * Set the height of the image
+ * @param height The height of the image, in pixels
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setWidth(120).setHeight(50).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setHeight(height: number): FOverGridImageBuilder {
this._image.height = height;
return this;
}
+ /**
+ * Set the anchor type of the image, whether the position and size change with the cell
+ * @param anchorType The anchor type {@link SheetDrawingAnchorType}
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setAnchorType(univerAPI.Enum.SheetDrawingAnchorType.None).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setAnchorType(anchorType: SheetDrawingAnchorType): FOverGridImageBuilder {
this._image.anchorType = anchorType;
return this;
}
+ /**
+ * Set the cropping region of the image by defining the top edges, thereby displaying the specific part of the image you want.
+ * @param top The number of pixels to crop from the top of the image.
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setCropTop(10).setCropLeft(10).setCropBottom(10).setCropRight(10).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setCropTop(top: number): FOverGridImageBuilder {
this._initializeSrcRect();
this._image.srcRect!.top = top;
return this;
}
+ /**
+ * Set the cropping region of the image by defining the left edges, thereby displaying the specific part of the image you want.
+ * @param left The number of pixels to crop from the left side of the image.
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setCropTop(10).setCropLeft(10).setCropBottom(10).setCropRight(10).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setCropLeft(left: number): FOverGridImageBuilder {
this._initializeSrcRect();
this._image.srcRect!.left = left;
return this;
}
+ /**
+ * Set the cropping region of the image by defining the bottom edges, thereby displaying the specific part of the image you want.
+ * @param bottom The number of pixels to crop from the bottom of the image.
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setCropTop(10).setCropLeft(10).setCropBottom(10).setCropRight(10).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setCropBottom(bottom: number): FOverGridImageBuilder {
this._initializeSrcRect();
this._image.srcRect!.bottom = bottom;
return this;
}
+ /**
+ * Set the cropping region of the image by defining the right edges, thereby displaying the specific part of the image you want.
+ * @param right The number of pixels to crop from the right side of the image.
+ * @returns The builder. {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setCropTop(10).setCropLeft(10).setCropBottom(10).setCropRight(10).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
+ */
setCropRight(right: number): FOverGridImageBuilder {
this._initializeSrcRect();
this._image.srcRect!.right = right;
@@ -225,7 +429,16 @@ export class FOverGridImageBuilder {
/**
* Set the rotation angle of the image
* @param angle Degree of rotation of the image, for example, 90, 180, 270, etc.
- * @returns The builder
+ * @returns The builder {@link FOverGridImageBuilder}
+ * @example
+ * ```ts
+ * // create a new image builder.
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const imageBuilder = activeSheet.newOverGridImage();
+ * const param = await imageBuilder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).setRotate(90).buildAsync();
+ * activeSheet.insertImages([param]);
+ * ```
*/
setRotate(angle: number): FOverGridImageBuilder {
this._image.angle = angle;
@@ -247,7 +460,7 @@ export class FOverGridImageBuilder {
return this;
}
- async build(): Promise<ISheetImage> {
+ async buildAsync(): Promise<ISheetImage> {
const renderManagerService = this._injector.get(IRenderManagerService);
const render = renderManagerService.getRenderById(this._image.unitId);
if (!render) {
@@ -299,7 +512,7 @@ export class FOverGridImage extends FBase {
* // remove the image from the sheet
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
* const activeSheet = activeSpreadsheet.getActiveSheet();
- * const image = activeSheet.getImages[0];
+ * const image = activeSheet.getImages()[0];
* console.log('Delete state is ', image?.remove());
* ```
*/
@@ -315,10 +528,10 @@ export class FOverGridImage extends FBase {
* // convert the image to a builder
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
* const activeSheet = activeSpreadsheet.getActiveSheet();
- * const image = activeSheet.getImages[0];
+ * const image = activeSheet.getImages()[0];
* const builder = image.toBuilder();
- * builder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).build();
- * activeSheet.updateImages([builder.build()]);
+ * const param = await builder.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4').setColumn(5).setRow(5).buildAsync();
+ * activeSheet.updateImages([param]);
* ```
*/
toBuilder(): FOverGridImageBuilder {
@@ -336,14 +549,14 @@ export class FOverGridImage extends FBase {
* // set the source of the image
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
* const activeSheet = activeSpreadsheet.getActiveSheet();
- * const image = activeSheet.getImages[0];
+ * const image = activeSheet.getImages()[0];
* console.log('Set source state is ', image.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4'));
*/
setSource(source: string): boolean;
/**
* Set the source of the image, change image display.
* @param source The source of the image
- * @param sourceType The source type of the image
+ * @param sourceType The source type of the image, {@link ImageSourceType}
* @returns success or not
*/
setSource(source: string, sourceType?: ImageSourceType): boolean;
@@ -364,20 +577,29 @@ export class FOverGridImage extends FBase {
* // set the position of the image
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
* const activeSheet = activeSpreadsheet.getActiveSheet();
- * const image = activeSheet.getImages[0];
+ * const image = activeSheet.getImages()[0];
* console.log('Set position state is ', image.setPosition(5, 5));
* ```
*/
- setPosition(row: number, column: number): boolean;
+ async setPositionAsync(row: number, column: number): Promise<boolean>;
/**
*
* @param column The sheet start column of the image
* @param row The sheet start row of the image
* @param rowOffset The offset of the start row
* @param columnOffset The offset of the start column
+ * @returns success or not
+ * @example
+ * ```ts
+ * // set the position of the image
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Set position state is ', image.setPosition(5, 5, 10, 10));
+ * ```
*/
- setPosition(row: number, column: number, rowOffset?: number, columnOffset?: number): boolean;
- setPosition(row: number, column: number, rowOffset?: number, columnOffset?: number): boolean {
+ async setPositionAsync(row: number, column: number, rowOffset?: number, columnOffset?: number): Promise<boolean>;
+ async setPositionAsync(row: number, column: number, rowOffset?: number, columnOffset?: number): Promise<boolean> {
const builder = this.toBuilder();
builder.setColumn(column);
builder.setRow(row);
@@ -387,7 +609,8 @@ export class FOverGridImage extends FBase {
if (columnOffset != null) {
builder.setColumnOffset(columnOffset);
}
- return this._commandService.syncExecuteCommand(SetSheetDrawingCommand.id, { unitId: this._image.unitId, drawings: [builder.build()] });
+ const param = await builder.buildAsync();
+ return this._commandService.syncExecuteCommand(SetSheetDrawingCommand.id, { unitId: this._image.unitId, drawings: [param] });
}
/**
@@ -395,12 +618,20 @@ export class FOverGridImage extends FBase {
* @param width The width of the image
* @param height The height of the image
* @returns success or not
+ * @example
+ * ```ts
+ * // set the size of the image
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Set size state is ', image.setSize(50, 120));
*/
- setSize(width: number, height: number): boolean {
+ async setSizeAsync(width: number, height: number): Promise<boolean> {
const builder = this.toBuilder();
builder.setWidth(width);
builder.setHeight(height);
- return this._commandService.syncExecuteCommand(SetSheetDrawingCommand.id, { unitId: this._image.unitId, drawings: [builder.build()] });
+ const param = await builder.buildAsync();
+ return this._commandService.syncExecuteCommand(SetSheetDrawingCommand.id, { unitId: this._image.unitId, drawings: [param] });
}
/**
@@ -415,8 +646,8 @@ export class FOverGridImage extends FBase {
* // set the crop of the image
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
* const activeSheet = activeSpreadsheet.getActiveSheet();
- * const image = activeSheet.getImages[0];
- * console.log('Set crop state is ', image.setCrop(0, 0, 0, 0));
+ * const image = activeSheet.getImages()[0];
+ * console.log('Set crop state is ', image.setCrop(10, 10, 10, 10));
* ```
*/
setCrop(top?: number, left?: number, bottom?: number, right?: number): boolean {
@@ -457,9 +688,101 @@ export class FOverGridImage extends FBase {
* Set the rotation angle of the image
* @param angle Degree of rotation of the image, for example, 90, 180, 270, etc.
* @returns success or not
+ * @example
+ * ```ts
+ * // set the rotation angle of the image
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Set rotate state is ', image.setRotate(90));
+ * ```
*/
setRotate(angle: number): boolean {
this._image.sheetTransform.angle = angle;
return this._commandService.syncExecuteCommand(SetSheetDrawingCommand.id, { unitId: this._image.unitId, drawings: [this._image] });
}
+
+ /**
+ * Move the image layer forward by one level
+ * @returns success or not
+ * @example
+ * ```ts
+ * // move the image forward
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Move forward state is ', image.setForward());
+ * ```
+ */
+ setForward(): boolean {
+ return this._commandService.syncExecuteCommand(SetDrawingArrangeCommand.id, {
+ unitId: this._image.unitId,
+ subUnitId: this._image.subUnitId,
+ drawingIds: [this._image.drawingId],
+ arrangeType: ArrangeTypeEnum.forward,
+ });
+ }
+
+ /**
+ * Move the image layer backward by one level
+ * @returns success or not
+ * @example
+ * ```ts
+ * // move the image backward
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Move backward state is ', image.setBackward());
+ * ```
+ */
+ setBackward(): boolean {
+ return this._commandService.syncExecuteCommand(SetDrawingArrangeCommand.id, {
+ unitId: this._image.unitId,
+ subUnitId: this._image.subUnitId,
+ drawingIds: [this._image.drawingId],
+ arrangeType: ArrangeTypeEnum.backward,
+ });
+ }
+
+ /**
+ * Move the image layer to the back
+ * @returns success or not
+ * @example
+ * ```ts
+ * // move the image to the back
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Move back state is ', image.setBack());
+ * ```
+ */
+ setBack(): boolean {
+ return this._commandService.syncExecuteCommand(SetDrawingArrangeCommand.id, {
+ unitId: this._image.unitId,
+ subUnitId: this._image.subUnitId,
+ drawingIds: [this._image.drawingId],
+ arrangeType: ArrangeTypeEnum.back,
+ });
+ }
+
+ /**
+ * Move the image layer to the front
+ * @returns success or not
+ * @example
+ * ```ts
+ * // move the image to the front
+ * const activeSpreadsheet = univerAPI.getActiveWorkbook();
+ * const activeSheet = activeSpreadsheet.getActiveSheet();
+ * const image = activeSheet.getImages()[0];
+ * console.log('Move front state is ', image.setFront());
+ * ```
+ */
+ setFront(): boolean {
+ return this._commandService.syncExecuteCommand(SetDrawingArrangeCommand.id, {
+ unitId: this._image.unitId,
+ subUnitId: this._image.subUnitId,
+ drawingIds: [this._image.drawingId],
+ arrangeType: ArrangeTypeEnum.front,
+ });
+ }
}
diff --git a/packages/sheets-drawing-ui/src/facade/f-univer.ts b/packages/sheets-drawing-ui/src/facade/f-univer.ts
new file mode 100644
index 0000000000..7212af7e54
--- /dev/null
+++ b/packages/sheets-drawing-ui/src/facade/f-univer.ts
@@ -0,0 +1,299 @@
+/**
+ * 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 { IDrawingSearch, IEventBase, Injector } from '@univerjs/core';
+import type { ISheetImage } from '@univerjs/sheets-drawing';
+import type { IDeleteDrawingCommandParams, IInsertDrawingCommandParams, ISetDrawingCommandParams } from '@univerjs/sheets-drawing-ui';
+import type { FWorkbook } from '@univerjs/sheets/facade';
+import type { IBeforeOverGridImageChangeParamObject } from './f-event';
+import { FUniver, ICommandService } from '@univerjs/core';
+import { IDrawingManagerService, SetDrawingSelectedOperation } from '@univerjs/drawing';
+import { InsertSheetDrawingCommand, RemoveSheetDrawingCommand, SetSheetDrawingCommand } from '@univerjs/sheets-drawing-ui';
+import { FOverGridImage } from './f-over-grid-image';
+
+interface IBeforeOverGridImageInsertParam extends IEventBase {
+ workbook: FWorkbook;
+ insertImageParams: ISheetImage[];
+}
+
+interface IBeforeOverGridImageRemoveParam extends IEventBase {
+ workbook: FWorkbook;
+ images: FOverGridImage[];
+}
+
+interface IBeforeOverGridImageChangeParam extends IEventBase {
+ workbook: FWorkbook;
+ images: IBeforeOverGridImageChangeParamObject[];
+}
+
+interface IBeforeOverGridImageSelectParam extends IEventBase {
+ workbook: FWorkbook;
+ selectedImages: FOverGridImage[];
+ oldSelectedImages: FOverGridImage[];
+}
+
+export class FUniverDrawingMixin extends FUniver {
+ override _initialize(injector: Injector): void {
+ const commandService = injector.get(ICommandService);
+ this.disposeWithMe(commandService.beforeCommandExecuted((commandInfo) => {
+ switch (commandInfo.id) {
+ case InsertSheetDrawingCommand.id:
+ this._beforeOverGridImageInsert(commandInfo.params as IInsertDrawingCommandParams);
+ break;
+ case RemoveSheetDrawingCommand.id:
+ this._beforeOverGridImageRemove(commandInfo.params as IDeleteDrawingCommandParams);
+ break;
+ case SetSheetDrawingCommand.id:
+ this._beforeOverGridImageChange(commandInfo.params as ISetDrawingCommandParams);
+ break;
+ case SetDrawingSelectedOperation.id:
+ this._beforeOverGridImageSelect(commandInfo.params as IDrawingSearch[]);
+ break;
+ }
+ }));
+ this.disposeWithMe(commandService.onCommandExecuted((commandInfo) => {
+ switch (commandInfo.id) {
+ case InsertSheetDrawingCommand.id:
+ this._overGridImageInserted(commandInfo.params as IInsertDrawingCommandParams);
+ break;
+ case RemoveSheetDrawingCommand.id:
+ this._overGridImageRemoved(commandInfo.params as IDeleteDrawingCommandParams);
+ break;
+ case SetSheetDrawingCommand.id:
+ this._overGridImageChanged(commandInfo.params as ISetDrawingCommandParams);
+ break;
+ case SetDrawingSelectedOperation.id:
+ this._overGridImageSelected(commandInfo.params as IDrawingSearch[]);
+ break;
+ }
+ }));
+ }
+
+ private _beforeOverGridImageInsert(params?: IInsertDrawingCommandParams): void {
+ if (!this.hasEventCallback(this.Event.BeforeOverGridImageInsert)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+
+ const eventParams: IBeforeOverGridImageInsertParam = {
+ workbook,
+ insertImageParams: drawings as ISheetImage[],
+ };
+
+ this.fireEvent(this.Event.BeforeOverGridImageInsert, eventParams);
+
+ if (eventParams.cancel) {
+ throw new Error('Canceled by BeforeOverGridImageInsert event');
+ }
+ }
+
+ private _overGridImageInserted(params?: IInsertDrawingCommandParams): void {
+ if (!this.hasEventCallback(this.Event.OverGridImageInserted)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+
+ this.fireEvent(this.Event.OverGridImageInserted, {
+ workbook,
+ images: this._createFOverGridImage(drawings as ISheetImage[]),
+ });
+ }
+
+ private _beforeOverGridImageRemove(params: IDeleteDrawingCommandParams): void {
+ if (!this.hasEventCallback(this.Event.BeforeOverGridImageRemove)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+
+ const drawingManagerService = this._injector.get(IDrawingManagerService);
+ const willRemoveDrawings = drawings.map((drawing) => {
+ return drawingManagerService.getDrawingByParam(drawing);
+ }) as ISheetImage[];
+
+ const eventParams: IBeforeOverGridImageRemoveParam = {
+ workbook,
+ images: this._createFOverGridImage(willRemoveDrawings),
+ };
+
+ this.fireEvent(this.Event.BeforeOverGridImageRemove, eventParams);
+
+ if (eventParams.cancel) {
+ throw new Error('Canceled by BeforeOverGridImageRemove event');
+ }
+ }
+
+ private _overGridImageRemoved(params: IDeleteDrawingCommandParams): void {
+ if (!this.hasEventCallback(this.Event.OverGridImageRemoved)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+
+ this.fireEvent(this.Event.OverGridImageRemoved, {
+ workbook,
+ removeImageParams: drawings,
+ });
+ }
+
+ private _beforeOverGridImageChange(params: ISetDrawingCommandParams): void {
+ if (!this.hasEventCallback(this.Event.BeforeOverGridImageChange)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+
+ const drawingManagerService = this._injector.get(IDrawingManagerService);
+
+ const images: IBeforeOverGridImageChangeParamObject[] = [];
+ drawings.forEach((drawing) => {
+ const image = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetImage;
+ if (image == null) {
+ return;
+ }
+
+ images.push({
+ changeParam: drawing,
+ image: this._injector.createInstance(FOverGridImage, image),
+ });
+ });
+
+ const eventParams: IBeforeOverGridImageChangeParam = {
+ workbook,
+ images,
+ };
+
+ this.fireEvent(this.Event.BeforeOverGridImageChange, eventParams);
+
+ if (eventParams.cancel) {
+ drawingManagerService.updateNotification(drawings as IDrawingSearch[]);
+ throw new Error('Canceled by BeforeOverGridImageChange event');
+ }
+ }
+
+ private _overGridImageChanged(params: ISetDrawingCommandParams): void {
+ if (!this.hasEventCallback(this.Event.OverGridImageChanged)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+
+ const drawingManagerService = this._injector.get(IDrawingManagerService);
+
+ const images = drawings.map((drawing) => {
+ return this._injector.createInstance(FOverGridImage, drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetImage);
+ });
+
+ this.fireEvent(this.Event.OverGridImageChanged, {
+ workbook,
+ images,
+ });
+ }
+
+ private _beforeOverGridImageSelect(drawings: IDrawingSearch[]): void {
+ if (!this.hasEventCallback(this.Event.BeforeOverGridImageSelect)) {
+ return;
+ }
+
+ const drawingManagerService = this._injector.get(IDrawingManagerService);
+
+ const workbook = this.getActiveUniverSheet();
+
+ if (workbook == null) {
+ return;
+ }
+
+ const oldSelectedDrawings = drawingManagerService.getFocusDrawings() as ISheetImage[];
+
+ const selectedDrawings = drawings.map((drawing) => {
+ return drawingManagerService.getDrawingByParam(drawing);
+ }) as ISheetImage[];
+
+ const eventParams: IBeforeOverGridImageSelectParam = {
+ workbook,
+ selectedImages: this._createFOverGridImage(selectedDrawings),
+ oldSelectedImages: this._createFOverGridImage(oldSelectedDrawings),
+ };
+
+ this.fireEvent(this.Event.BeforeOverGridImageSelect, eventParams);
+
+ if (eventParams.cancel) {
+ throw new Error('Canceled by BeforeOverGridImageSelect event');
+ }
+ }
+
+ private _overGridImageSelected(drawings: IDrawingSearch[]): void {
+ if (!this.hasEventCallback(this.Event.OverGridImageSelected)) {
+ return;
+ }
+
+ const workbook = this.getActiveUniverSheet();
+ const drawingManagerService = this._injector.get(IDrawingManagerService);
+
+ if (workbook == null) {
+ return;
+ }
+
+ const selectedDrawings = drawings.map((drawing) => {
+ return drawingManagerService.getDrawingByParam(drawing);
+ }) as ISheetImage[];
+
+ this.fireEvent(this.Event.OverGridImageSelected, {
+ workbook,
+ selectedImages: this._createFOverGridImage(selectedDrawings as ISheetImage[]),
+ });
+ }
+
+ private _createFOverGridImage(drawings: ISheetImage[]): FOverGridImage[] {
+ return drawings.map((drawing) => {
+ return this._injector.createInstance(FOverGridImage, drawing);
+ });
+ }
+}
+
+FUniver.extend(FUniverDrawingMixin);
diff --git a/packages/sheets-drawing-ui/src/facade/f-worksheet.ts b/packages/sheets-drawing-ui/src/facade/f-worksheet.ts
index c961527ac8..fed3fadb17 100644
--- a/packages/sheets-drawing-ui/src/facade/f-worksheet.ts
+++ b/packages/sheets-drawing-ui/src/facade/f-worksheet.ts
@@ -15,7 +15,7 @@
*/
import type { IDisposable, IFBlobSource, Nullable } from '@univerjs/core';
-import { ImageSourceType, toDisposable } from '@univerjs/core';
+import { DrawingTypeEnum, ImageSourceType, toDisposable } from '@univerjs/core';
import { ISheetDrawingService, type ISheetImage } from '@univerjs/sheets-drawing';
import { type ICanvasFloatDom, InsertSheetDrawingCommand, RemoveSheetDrawingCommand, SetSheetDrawingCommand, SheetCanvasFloatDomManagerService } from '@univerjs/sheets-drawing-ui';
import { type IFComponentKey, transformComponentKey } from '@univerjs/sheets-ui/facade';
@@ -225,7 +225,7 @@ export class FWorksheetLegacy extends FWorksheet implements IFWorksheetLegacy {
imageBuilder.setRowOffset(0);
}
- const param = await imageBuilder.build();
+ const param = await imageBuilder.buildAsync();
return this._commandService.syncExecuteCommand(InsertSheetDrawingCommand.id, { unitId: this._fWorkbook.getId(), drawings: [param] });
}
@@ -261,6 +261,9 @@ export class FWorksheetLegacy extends FWorksheet implements IFWorksheetLegacy {
const images: FOverGridImage[] = [];
for (const drawingId in drawingData) {
const drawing = drawingData[drawingId];
+ if (drawing.drawingType !== DrawingTypeEnum.DRAWING_IMAGE) {
+ continue;
+ }
images.push(this._injector.createInstance(FOverGridImage, drawing as ISheetImage));
}
return images;
@@ -269,7 +272,7 @@ export class FWorksheetLegacy extends FWorksheet implements IFWorksheetLegacy {
override getImageById(id: string): FOverGridImage | null {
const sheetDrawingService = this._injector.get(ISheetDrawingService);
const drawing = sheetDrawingService.getDrawingByParam({ unitId: this._fWorkbook.getId(), subUnitId: this.getSheetId(), drawingId: id });
- if (drawing) {
+ if (drawing && drawing.drawingType === DrawingTypeEnum.DRAWING_IMAGE) {
return this._injector.createInstance(FOverGridImage, drawing as ISheetImage);
}
return null;
diff --git a/packages/sheets-drawing-ui/src/facade/index.ts b/packages/sheets-drawing-ui/src/facade/index.ts
index c5650dbb98..da0846a2ed 100644
--- a/packages/sheets-drawing-ui/src/facade/index.ts
+++ b/packages/sheets-drawing-ui/src/facade/index.ts
@@ -16,6 +16,12 @@
import './f-over-grid-image';
import './f-worksheet';
+import './f-enum';
+import './f-event';
+import './f-univer';
+export type * from './f-enum';
+export type * from './f-event';
export type * from './f-over-grid-image';
+export type * from './f-univer';
export type * from './f-worksheet';
diff --git a/packages/sheets-drawing-ui/src/index.ts b/packages/sheets-drawing-ui/src/index.ts
index 25f6f35546..aa06388462 100644
--- a/packages/sheets-drawing-ui/src/index.ts
+++ b/packages/sheets-drawing-ui/src/index.ts
@@ -36,5 +36,5 @@ export { EditSheetDrawingOperation } from './commands/operations/edit-sheet-draw
export { type IInsertImageCommandParams, InsertFloatImageCommand } from './commands/commands/insert-image.command';
export { SidebarSheetDrawingOperation } from './commands/operations/open-drawing-panel.operation';
-export type { IDeleteDrawingCommandParams, IInsertDrawingCommandParams } from './commands/commands/interfaces';
+export type { IDeleteDrawingCommandParams, IInsertDrawingCommandParams, ISetDrawingCommandParams } from './commands/commands/interfaces';
// #endregion
diff --git a/packages/sheets-drawing/src/services/sheet-drawing.service.ts b/packages/sheets-drawing/src/services/sheet-drawing.service.ts
index 8535164ccb..bb4b126413 100644
--- a/packages/sheets-drawing/src/services/sheet-drawing.service.ts
+++ b/packages/sheets-drawing/src/services/sheet-drawing.service.ts
@@ -20,8 +20,19 @@ import { createIdentifier } from '@univerjs/core';
import { type IImageData, type IUnitDrawingService, UnitDrawingService } from '@univerjs/drawing';
export enum SheetDrawingAnchorType {
+ /**
+ * Only the position of the drawing follows the cell changes. When rows or columns are inserted or deleted, the position of the drawing changes, but the size remains the same.
+ */
Position = '0',
+
+ /**
+ * The size and position of the drawing follow the cell changes. When rows or columns are inserted or deleted, the size and position of the drawing change accordingly.
+ */
Both = '1',
+
+ /**
+ * The size and position of the drawing do not follow the cell changes. When rows or columns are inserted or deleted, the position and size of the drawing remain unchanged.
+ */
None = '2',
}