Skip to content

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

fix(docs-drawing): skip intrinsic loading for explicit image size (#7320)

#FF0000ac8201a47cec
diff --git a/packages/docs-drawing/src/facade/__tests__/f-document-image.spec.ts b/packages/docs-drawing/src/facade/__tests__/f-document-image.spec.ts
index 0b68bd3c38..b34a34bfc7 100644
--- a/packages/docs-drawing/src/facade/__tests__/f-document-image.spec.ts
+++ b/packages/docs-drawing/src/facade/__tests__/f-document-image.spec.ts
@@ -136,6 +136,25 @@ describe('FDocument image facade', () => {
expect(image?.getSize()).toEqual({ width: 500, height: 250 });
});
+ it('uses explicit dimensions without loading the image source', async () => {
+ vi.stubGlobal('Image', undefined);
+
+ const image = await testBed.document.insertImage({
+ source: 'data:image/png;base64,image',
+ imageSourceType: ImageSourceType.BASE64,
+ width: 160,
+ height: 90,
+ textRange: {
+ startOffset: 0,
+ endOffset: 0,
+ collapsed: true,
+ segmentId: '',
+ },
+ });
+
+ expect(image?.getSize()).toEqual({ width: 160, height: 90 });
+ });
+
it('inserts an image with a non-inline wrapping style', async () => {
const image = await testBed.document.insertImage({
source: 'data:image/png;base64,image',
diff --git a/packages/docs-drawing/src/facade/f-document.ts b/packages/docs-drawing/src/facade/f-document.ts
index 57eb65a7f8..9c77fbeabd 100644
--- a/packages/docs-drawing/src/facade/f-document.ts
+++ b/packages/docs-drawing/src/facade/f-document.ts
@@ -134,8 +134,12 @@ export class FDocumentImageMixin extends FDocument implements IFDocumentImageMix
override async insertImage(options: IFDocumentInsertImageOptions): Promise<FDocumentImage | null> {
const unitId = this.getId();
const imageId = generateRandomId(6);
- const intrinsicSize = await this._getIntrinsicSize(options.source, options.imageSourceType);
- const size = resolveImageSize(intrinsicSize, options);
+ const size = options.width != null && options.height != null
+ ? { width: options.width, height: options.height }
+ : resolveImageSize(
+ await this._getIntrinsicSize(options.source, options.imageSourceType),
+ options
+ );
const defaultTransform = buildDocTransform(size.width, size.height);
const wrappingStyle = options.wrappingStyle ?? TextWrappingStyle.INLINE;
const docTransform = {