Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(engine-render): render fallback image when cell image renderer-error (#5449)
WEI ZHANGcca2971fefdd
diff --git a/packages/core/src/shared/cache/image-cache.ts b/packages/core/src/shared/cache/image-cache.tsindex 2f18ef393f..4f84e4a52c 100644--- a/packages/core/src/shared/cache/image-cache.ts+++ b/packages/core/src/shared/cache/image-cache.ts@@ -34,32 +34,34 @@ export class ImageCacheMap {getImage(imageSourceType: ImageSourceType, source: string, onLoad?: () => void, onError?: () => void) {const imageCacheKey = this._getImageCacheKey(imageSourceType, source);- let imageElement = this._imageCacheMap.get(imageCacheKey);+ const imageElement = this._imageCacheMap.get(imageCacheKey);if (imageElement) {return imageElement;} else {(async () => {- imageElement = new Image();+ const newImageElement = new Image();const imageIoService = this._injector.has(IImageIoService) ? this._injector.get(IImageIoService) : null;if (imageSourceType === ImageSourceType.UUID) {try {- imageElement.src = await imageIoService?.getImage(source) || '';+ newImageElement.src = await imageIoService?.getImage(source) || '';} catch (error) {console.error(error);}} else {- imageElement.src = source;+ newImageElement.src = source;}- imageElement.onload = () => {+ newImageElement.onload = () => {+ newImageElement.removeAttribute('data-error');onLoad?.();};- imageElement.onerror = () => {+ newImageElement.onerror = () => {+ newImageElement.setAttribute('data-error', 'true');onError?.();};- this._imageCacheMap.set(imageCacheKey, imageElement);+ this._imageCacheMap.set(imageCacheKey, newImageElement);})();return null;diff --git a/packages/engine-render/src/components/sheets/extensions/font.ts b/packages/engine-render/src/components/sheets/extensions/font.tsindex 9e526add05..b991594b88 100644--- a/packages/engine-render/src/components/sheets/extensions/font.ts+++ b/packages/engine-render/src/components/sheets/extensions/font.ts@@ -37,6 +37,11 @@ import { SheetExtension } from './sheet-extension';const UNIQUE_KEY = 'DefaultFontExtension';+const IMAGE_FALLBACK_SRC = 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2NC0xNzYuNjR6TTMwNC42NCA3OTIuMDY0SDE2NS44ODhjLTE2LjM4NCAwLTMwLjIwOC0xMy4zMTItMzAuMjA4LTMwLjIwOHYtOS43MjhsMTM4Ljc1Mi0xNjQuMzUyIDEwNC45NiAxMjQuNDE2LTc0Ljc1MiA7OS44NzJ6bTgxLjkyLTM1NS44NGwzNy4zNzYgMjI4Ljg2NC0uNTEyLjUxMi0xNDIuODQ4LTE2OS45ODRjLTMuMDcyLTMuNTg0LTkuMjE2LTMuNTg0LTEyLjI4OCAwTDEzNS42OCA2NTIuOFYyNjIuMTQ0YzAtMTYuMzg0IDEzLjMxMi0zMC4yMDggMzAuMjA4LTMwLjIwOGg0NzQuNjI0TDM4Ni41NiA0MzYuMjI0em01MDEuMjQ4IDMyNS42MzJjMCAxNi44OTYtMTMuMzEyIDMwLjIwOC0yOS42OTYgMzAuMjA4SDY4MC45Nmw1Ny4zNDQtOTMuMTg0LTg3LjU1Mi0yMDIuMjQgNy4xNjgtNy42OCAyMjkuODg4IDI3Mi44OTZ6IiBmaWxsPSIjZTZlNmU2Ci8+PC9zdmc+';++const IMAGE_FALLBACK = new Image();+IMAGE_FALLBACK.src = IMAGE_FALLBACK_SRC;+function rotatedBoundingBox(width: number, height: number, angleDegrees: number) {const angle = angleDegrees * Math.PI / 180; // 将角度转换为弧度const rotatedWidth = Math.abs(width * Math.cos(angle)) + Math.abs(height * Math.sin(angle));@@ -330,10 +335,16 @@ export class Font extends SheetExtension {ctx.save();ctx.translate(x + rotatedWidth / 2, y + rotatedHeight / 2);ctx.rotate(angleRadians);- try {- ctx.drawImage(image, -rotatedWidth / 2, -rotatedHeight / 2, width, height);- } catch (e) {- console.error(e);+ const isError = image.getAttribute('data-error') === 'true';+ if (isError) {+ ctx.drawImage(IMAGE_FALLBACK, -rotatedWidth / 2, -rotatedHeight / 2, width, height);+ } else {+ try {+ ctx.drawImage(image, -rotatedWidth / 2, -rotatedHeight / 2, width, height);+ } catch (e) {+ console.error(e);+ ctx.drawImage(IMAGE_FALLBACK, -rotatedWidth / 2, -rotatedHeight / 2, width, height);+ }}ctx.restore();}