From 6a7a4e6cad90b3b15967ee2c6d7f874e4570e909 Mon Sep 17 00:00:00 2001 From: zhirongwang Date: Tue, 24 Nov 2026 14:01:02 -0800 Subject: [PATCH 1/6] feat(image): show the 320 first on every image open Show the 320 placeholder whenever imageConversionPlaceholder is enabled, not only while the image rep converts, and accept a host-provided placeholder through viewers.Image.placeholder. --- src/lib/viewers/image/ImageViewer.js | 95 ++++++++++++++----- .../image/__tests__/ImageViewer-test.js | 72 +++++++++++++- 2 files changed, 138 insertions(+), 29 deletions(-) diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index bb8372861..46384d4fb 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -11,8 +11,6 @@ import { CLASS_INVISIBLE, DISCOVERABILITY_ATTRIBUTE, IMAGE_FTUX_CURSOR_SEEN_KEY, - STATUS_NONE, - STATUS_PENDING, } from '../../constants'; import { LOAD_METRIC, FIRST_RENDER_METRIC, VIEWER_EVENT } from '../../events'; import Timer from '../../Timer'; @@ -24,7 +22,6 @@ const IMAGE_ZOOM_SCALE = 1.2; const PLACEHOLDER_DISPLAY_SIZE = 920; const PLACEHOLDER_FADE_MS = 500; const PLACEHOLDER_REP = 'jpg_320x320'; -const CONVERTING_STATES = [STATUS_NONE, STATUS_PENDING]; class ImageViewer extends ImageBaseViewer { /** @inheritdoc */ @@ -134,10 +131,7 @@ class ImageViewer extends ImageBaseViewer { this.bindDOMListeners(); const contentUrl = this.createContentUrlV2(template, viewer.ASSET); - if ( - this.featureEnabled('imageConversionPlaceholder.enabled') && - CONVERTING_STATES.includes(representation.status?.state) - ) { + if (this.featureEnabled('imageConversionPlaceholder.enabled')) { return this.loadWithPlaceholder(contentUrl); } @@ -169,8 +163,8 @@ class ImageViewer extends ImageBaseViewer { } /** - * Shows the 320 thumbnail while the image rep converts, zoomed to PLACEHOLDER_DISPLAY_SIZE, - * then swaps in the image rep once it is ready. Falls back to waiting for the image rep, with + * Shows the 320 thumbnail first, zoomed to PLACEHOLDER_DISPLAY_SIZE, then swaps in the image + * rep once it is ready. Falls back to waiting for the image rep, with * the host's loading indicator, when the 320 is not available either. * * @param {string} contentUrl - Content URL of the image rep @@ -195,7 +189,7 @@ class ImageViewer extends ImageBaseViewer { () => undefined, ); - return this.fetchDecodedImage(placeholderUrl) + return this.getPlaceholderImage(placeholderUrl) .then( placeholder => { if (this.isDestroyed()) { @@ -244,20 +238,73 @@ class ImageViewer extends ImageBaseViewer { * @return {Promise} blobUrl, width and height of the decoded image */ fetchDecodedImage(url) { - return this.fetchContentAsBlobUrl(url).then(blobUrl => { - const decoder = new Image(); - decoder.src = blobUrl; - return decoder - .decode() - .catch(() => undefined) - .then(() => { - if (!decoder.naturalWidth) { - URL.revokeObjectURL(blobUrl); - throw new Error('Response is not a decodable image'); - } - return { blobUrl, height: decoder.naturalHeight, width: decoder.naturalWidth }; - }); - }); + return this.fetchContentAsBlobUrl(url).then(blobUrl => this.decodeBlobUrl(blobUrl)); + } + + /** + * Whether the placeholder viewer option is for the file and version being previewed. Without + * a version, it is taken to be the current version's, so it is only used when no other + * version was requested. + * + * @param {Object} [hostPlaceholder] - fileId, optional fileVersionId, and url + * @return {boolean} + */ + isHostPlaceholderForFile(hostPlaceholder) { + const fileId = getProp(this.options, 'file.id'); + if (!hostPlaceholder?.url || !fileId || String(hostPlaceholder.fileId) !== String(fileId)) { + return false; + } + if (hostPlaceholder.fileVersionId) { + return String(hostPlaceholder.fileVersionId) === String(getProp(this.options, 'file.file_version.id')); + } + return !getProp(this.options, ['fileOptions', fileId, 'fileVersionId']); + } + + /** + * Gets the placeholder shown before the image rep. Uses the image the host passed in the + * placeholder viewer option, such as a thumbnail it already loaded, when it is for the file + * being previewed, and fetches the 320 otherwise. + * + * @param {string} placeholderUrl - Content URL of the 320 + * @return {Promise} blobUrl, width and height of the decoded placeholder + */ + getPlaceholderImage(placeholderUrl) { + const hostPlaceholder = this.getViewerOption('placeholder'); + if (!this.isHostPlaceholderForFile(hostPlaceholder)) { + return this.fetchDecodedImage(placeholderUrl); + } + + // The host owns its URL and may revoke it, so decode a copy that this viewer can revoke. + return fetch(hostPlaceholder.url) + .then(response => { + if (!response.ok) { + throw new Error(`Host placeholder fetch failed: ${response.status}`); + } + return response.blob(); + }) + .then(blob => this.decodeBlobUrl(URL.createObjectURL(blob))) + .catch(() => this.fetchDecodedImage(placeholderUrl)); + } + + /** + * Decodes an image blob URL. Rejects, and revokes the URL, when it is not an image. + * + * @param {string} blobUrl - Blob URL of the image + * @return {Promise} blobUrl, width and height of the decoded image + */ + decodeBlobUrl(blobUrl) { + const decoder = new Image(); + decoder.src = blobUrl; + return decoder + .decode() + .catch(() => undefined) + .then(() => { + if (!decoder.naturalWidth) { + URL.revokeObjectURL(blobUrl); + throw new Error('Response is not a decodable image'); + } + return { blobUrl, height: decoder.naturalHeight, width: decoder.naturalWidth }; + }); } /** diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index 967c7bdd1..c2ad32566 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -208,13 +208,14 @@ describe('lib/viewers/image/ImageViewer', () => { expect(image.fetchDecodedImage).not.toHaveBeenCalled(); }); - test.each(['success', 'error'])('should wait for the image rep as before when its state is %s', state => { - image.options.representation.status.state = state; - jest.spyOn(image, 'fetchDecodedImage'); + test('should show the 320 first when the image rep is already ready', async () => { + image.options.representation.status.state = 'success'; + jest.spyOn(image, 'fetchDecodedImage').mockResolvedValue({ blobUrl: 'blob:320', height: 240, width: 320 }); - image.load(); + await image.load(); - expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + expect(image.fetchDecodedImage).toHaveBeenCalledWith(placeholderUrl); + expect(image.imageEl.src).toBe('blob:320'); }); test('should show the 320 without telling the host the rep is converting', async () => { @@ -305,6 +306,67 @@ describe('lib/viewers/image/ImageViewer', () => { }); }); + describe('getPlaceholderImage()', () => { + const placeholderUrl = 'https://dl.example.com/representations/jpg_320x320/content/'; + const decoded = { blobUrl: 'blob:copy', height: 768, width: 1024 }; + let originalFetch; + + beforeEach(() => { + originalFetch = global.fetch; + global.fetch = jest.fn().mockResolvedValue({ blob: () => Promise.resolve(new Blob(['img'])), ok: true }); + jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:copy'); + jest.spyOn(image, 'decodeBlobUrl').mockResolvedValue(decoded); + jest.spyOn(image, 'fetchDecodedImage').mockResolvedValue({ blobUrl: 'blob:320', height: 240, width: 320 }); + }); + + afterEach(() => { + global.fetch = originalFetch; + }); + + test.each([ + ['the previewed version', { fileId: '1', fileVersionId: 1, url: 'blob:host' }], + ['the file, with no version, while previewing the current version', { fileId: 1, url: 'blob:host' }], + ])('should decode a copy of the host placeholder for %s', async (_, placeholder) => { + image.options.viewers = { Image: { placeholder } }; + + await expect(image.getPlaceholderImage(placeholderUrl)).resolves.toBe(decoded); + expect(global.fetch).toHaveBeenCalledWith('blob:host'); + expect(image.decodeBlobUrl).toHaveBeenCalledWith('blob:copy'); + expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + }); + + test.each([ + ['there is no host placeholder', undefined, undefined], + ['the host placeholder is for another file', { fileId: '2', url: 'blob:host' }, undefined], + [ + 'the host placeholder is for another version', + { fileId: '1', fileVersionId: '2', url: 'blob:host' }, + undefined, + ], + ['another version is requested', { fileId: '1', url: 'blob:host' }, { 1: { fileVersionId: '3' } }], + ])('should fetch the 320 when %s', async (_, placeholder, fileOptions) => { + image.options.viewers = { Image: { placeholder } }; + image.options.fileOptions = fileOptions; + + await image.getPlaceholderImage(placeholderUrl); + + expect(global.fetch).not.toHaveBeenCalled(); + expect(image.fetchDecodedImage).toHaveBeenCalledWith(placeholderUrl); + }); + + test.each([ + ['cannot be read', () => Promise.reject(new TypeError('Failed to fetch'))], + ['returns an error status', () => Promise.resolve({ ok: false, status: 404 })], + ])('should fetch the 320 when the host placeholder %s', async (_, fetchResult) => { + image.options.viewers = { Image: { placeholder: { fileId: '1', url: 'blob:revoked' } } }; + global.fetch.mockImplementation(fetchResult); + + await image.getPlaceholderImage(placeholderUrl); + + expect(image.fetchDecodedImage).toHaveBeenCalledWith(placeholderUrl); + }); + }); + describe('waitForViewerLoad()', () => { test('should resolve immediately if the viewer has loaded', async () => { image.loaded = true; From 703e834067a0a77ec8ae0f6b41136e05f6c2480b Mon Sep 17 00:00:00 2001 From: zhirongwang Date: Tue, 24 Nov 2026 14:22:24 -0800 Subject: [PATCH 2/6] feat(image): gate the 320-first load behind imageProgressiveLoad imageConversionPlaceholder keeps showing the 320 only while the image rep converts. imageProgressiveLoad shows it on every open and uses the host placeholder. --- src/lib/viewers/image/ImageViewer.js | 21 ++++++++++------ .../image/__tests__/ImageViewer-test.js | 25 ++++++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index 46384d4fb..d8992d3fe 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -11,6 +11,8 @@ import { CLASS_INVISIBLE, DISCOVERABILITY_ATTRIBUTE, IMAGE_FTUX_CURSOR_SEEN_KEY, + STATUS_NONE, + STATUS_PENDING, } from '../../constants'; import { LOAD_METRIC, FIRST_RENDER_METRIC, VIEWER_EVENT } from '../../events'; import Timer from '../../Timer'; @@ -22,6 +24,7 @@ const IMAGE_ZOOM_SCALE = 1.2; const PLACEHOLDER_DISPLAY_SIZE = 920; const PLACEHOLDER_FADE_MS = 500; const PLACEHOLDER_REP = 'jpg_320x320'; +const CONVERTING_STATES = [STATUS_NONE, STATUS_PENDING]; class ImageViewer extends ImageBaseViewer { /** @inheritdoc */ @@ -131,7 +134,11 @@ class ImageViewer extends ImageBaseViewer { this.bindDOMListeners(); const contentUrl = this.createContentUrlV2(template, viewer.ASSET); - if (this.featureEnabled('imageConversionPlaceholder.enabled')) { + if ( + this.featureEnabled('imageProgressiveLoad.enabled') || + (this.featureEnabled('imageConversionPlaceholder.enabled') && + CONVERTING_STATES.includes(representation.status?.state)) + ) { return this.loadWithPlaceholder(contentUrl); } @@ -163,9 +170,9 @@ class ImageViewer extends ImageBaseViewer { } /** - * Shows the 320 thumbnail first, zoomed to PLACEHOLDER_DISPLAY_SIZE, then swaps in the image - * rep once it is ready. Falls back to waiting for the image rep, with - * the host's loading indicator, when the 320 is not available either. + * Shows the 320 thumbnail, zoomed to PLACEHOLDER_DISPLAY_SIZE, then swaps in the image rep + * once it is ready. Falls back to waiting for the image rep, with the host's loading + * indicator, when the 320 is not available either. * * @param {string} contentUrl - Content URL of the image rep * @return {Promise} @@ -262,15 +269,15 @@ class ImageViewer extends ImageBaseViewer { /** * Gets the placeholder shown before the image rep. Uses the image the host passed in the - * placeholder viewer option, such as a thumbnail it already loaded, when it is for the file - * being previewed, and fetches the 320 otherwise. + * placeholder viewer option, such as a thumbnail it already loaded, when imageProgressiveLoad + * is on and it is for the file being previewed, and fetches the 320 otherwise. * * @param {string} placeholderUrl - Content URL of the 320 * @return {Promise} blobUrl, width and height of the decoded placeholder */ getPlaceholderImage(placeholderUrl) { const hostPlaceholder = this.getViewerOption('placeholder'); - if (!this.isHostPlaceholderForFile(hostPlaceholder)) { + if (!this.featureEnabled('imageProgressiveLoad.enabled') || !this.isHostPlaceholderForFile(hostPlaceholder)) { return this.fetchDecodedImage(placeholderUrl); } diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index c2ad32566..06f3f70d5 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -208,8 +208,18 @@ describe('lib/viewers/image/ImageViewer', () => { expect(image.fetchDecodedImage).not.toHaveBeenCalled(); }); - test('should show the 320 first when the image rep is already ready', async () => { + test.each(['success', 'error'])('should wait for the image rep as before when its state is %s', state => { + image.options.representation.status.state = state; + jest.spyOn(image, 'fetchDecodedImage'); + + image.load(); + + expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + }); + + test('should show the 320 first when the image rep is ready and imageProgressiveLoad is on', async () => { image.options.representation.status.state = 'success'; + image.featureEnabled.mockImplementation(feature => feature === 'imageProgressiveLoad.enabled'); jest.spyOn(image, 'fetchDecodedImage').mockResolvedValue({ blobUrl: 'blob:320', height: 240, width: 320 }); await image.load(); @@ -315,6 +325,9 @@ describe('lib/viewers/image/ImageViewer', () => { originalFetch = global.fetch; global.fetch = jest.fn().mockResolvedValue({ blob: () => Promise.resolve(new Blob(['img'])), ok: true }); jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:copy'); + jest.spyOn(image, 'featureEnabled').mockImplementation( + feature => feature === 'imageProgressiveLoad.enabled', + ); jest.spyOn(image, 'decodeBlobUrl').mockResolvedValue(decoded); jest.spyOn(image, 'fetchDecodedImage').mockResolvedValue({ blobUrl: 'blob:320', height: 240, width: 320 }); }); @@ -354,6 +367,16 @@ describe('lib/viewers/image/ImageViewer', () => { expect(image.fetchDecodedImage).toHaveBeenCalledWith(placeholderUrl); }); + test('should fetch the 320 when imageProgressiveLoad is off', async () => { + image.featureEnabled.mockReturnValue(false); + image.options.viewers = { Image: { placeholder: { fileId: '1', url: 'blob:host' } } }; + + await image.getPlaceholderImage(placeholderUrl); + + expect(global.fetch).not.toHaveBeenCalled(); + expect(image.fetchDecodedImage).toHaveBeenCalledWith(placeholderUrl); + }); + test.each([ ['cannot be read', () => Promise.reject(new TypeError('Failed to fetch'))], ['returns an error status', () => Promise.resolve({ ok: false, status: 404 })], From 219e2737eb76ee47b082d11754f874123c06194f Mon Sep 17 00:00:00 2001 From: zhirongwang Date: Tue, 24 Nov 2026 15:15:38 -0800 Subject: [PATCH 3/6] fix(image): skip the placeholder fetch for the ORIGINAL rep The ORIGINAL rep (SVG, GIF) has no representations/ path, so the 320 URL rewrite left it unchanged and the original was downloaded twice. Without a host placeholder, load the original directly. --- src/lib/viewers/image/ImageViewer.js | 21 +++--- .../image/__tests__/ImageViewer-test.js | 65 +++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index d8992d3fe..819b5de5b 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -25,6 +25,7 @@ const PLACEHOLDER_DISPLAY_SIZE = 920; const PLACEHOLDER_FADE_MS = 500; const PLACEHOLDER_REP = 'jpg_320x320'; const CONVERTING_STATES = [STATUS_NONE, STATUS_PENDING]; +const REP_PATH_PATTERN = /representations\/[^/]+/; class ImageViewer extends ImageBaseViewer { /** @inheritdoc */ @@ -179,11 +180,11 @@ class ImageViewer extends ImageBaseViewer { */ loadWithPlaceholder(contentUrl) { const { representation } = this.options; - const placeholderTemplate = representation.content.url_template.replace( - /representations\/[^/]+/, - `representations/${PLACEHOLDER_REP}`, - ); - const placeholderUrl = this.createContentUrlV2(placeholderTemplate); + const repTemplate = representation.content.url_template; + // The ORIGINAL rep (SVG, GIF) downloads the file itself, so it has no 320 to switch to. + const placeholderUrl = REP_PATH_PATTERN.test(repTemplate) + ? this.createContentUrlV2(repTemplate.replace(REP_PATH_PATTERN, `representations/${PLACEHOLDER_REP}`)) + : null; // Passing the representation keeps conversionpending from reaching the host, which would // show its loading indicator over the placeholder. @@ -272,13 +273,17 @@ class ImageViewer extends ImageBaseViewer { * placeholder viewer option, such as a thumbnail it already loaded, when imageProgressiveLoad * is on and it is for the file being previewed, and fetches the 320 otherwise. * - * @param {string} placeholderUrl - Content URL of the 320 + * @param {string|null} placeholderUrl - Content URL of the 320, or null when the rep has none * @return {Promise} blobUrl, width and height of the decoded placeholder */ getPlaceholderImage(placeholderUrl) { + const fetchRepPlaceholder = () => + placeholderUrl + ? this.fetchDecodedImage(placeholderUrl) + : Promise.reject(new Error('Representation has no 320')); const hostPlaceholder = this.getViewerOption('placeholder'); if (!this.featureEnabled('imageProgressiveLoad.enabled') || !this.isHostPlaceholderForFile(hostPlaceholder)) { - return this.fetchDecodedImage(placeholderUrl); + return fetchRepPlaceholder(); } // The host owns its URL and may revoke it, so decode a copy that this viewer can revoke. @@ -290,7 +295,7 @@ class ImageViewer extends ImageBaseViewer { return response.blob(); }) .then(blob => this.decodeBlobUrl(URL.createObjectURL(blob))) - .catch(() => this.fetchDecodedImage(placeholderUrl)); + .catch(fetchRepPlaceholder); } /** diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index 06f3f70d5..0e6be4690 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -388,6 +388,71 @@ describe('lib/viewers/image/ImageViewer', () => { expect(image.fetchDecodedImage).toHaveBeenCalledWith(placeholderUrl); }); + + test('should reject when the host placeholder fails and the rep has no 320', async () => { + image.options.viewers = { Image: { placeholder: { fileId: '1', url: 'blob:revoked' } } }; + global.fetch.mockRejectedValue(new TypeError('Failed to fetch')); + + await expect(image.getPlaceholderImage(null)).rejects.toThrow('Representation has no 320'); + expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + }); + }); + + describe('load() for the ORIGINAL rep', () => { + const originalTemplate = 'https://api.box.com/2.0/files/1/content?preview=true'; + const fullImage = { blobUrl: 'blob:original', height: 600, width: 800 }; + let originalFetch; + + beforeEach(() => { + originalFetch = global.fetch; + global.fetch = jest.fn().mockResolvedValue({ blob: () => Promise.resolve(new Blob(['img'])), ok: true }); + image.loadTimeout = 60000; + image.options.representation = { + content: { url_template: originalTemplate }, + status: { state: 'success' }, + }; + jest.spyOn(image, 'featureEnabled').mockImplementation( + feature => feature === 'imageProgressiveLoad.enabled', + ); + jest.spyOn(image, 'createContentUrlV2').mockImplementation(template => template); + jest.spyOn(image, 'getRepStatus').mockReturnValue({ getPromise: () => Promise.resolve() }); + jest.spyOn(image, 'fetchDecodedImage').mockResolvedValue(fullImage); + jest.spyOn(image, 'loadContent').mockResolvedValue(); + jest.spyOn(image, 'emit'); + }); + + afterEach(() => { + global.fetch = originalFetch; + }); + + test('should load the original once, without a placeholder, when the host passes none', async () => { + await image.load(); + + expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + expect(image.loadContent).toHaveBeenCalledTimes(1); + expect(image.loadContent).toHaveBeenCalledWith(originalTemplate); + expect(image.emit).not.toHaveBeenCalledWith(VIEWER_EVENT.conversionPending); + expect(image.displaySizeCap).toBeUndefined(); + }); + + test('should show the host placeholder, then swap in the original', async () => { + image.options.viewers = { Image: { placeholder: { fileId: '1', url: 'blob:host' } } }; + jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:copy'); + jest.spyOn(image, 'decodeBlobUrl').mockResolvedValue({ blobUrl: 'blob:copy', height: 240, width: 320 }); + jest.spyOn(image, 'waitForViewerLoad').mockResolvedValue(); + jest.spyOn(image, 'swapInFullImage').mockImplementation(); + + await image.load(); + await new Promise(resolve => { + setTimeout(resolve, 0); + }); + + expect(global.fetch).toHaveBeenCalledWith('blob:host'); + expect(image.fetchDecodedImage).toHaveBeenCalledTimes(1); + expect(image.fetchDecodedImage).toHaveBeenCalledWith(originalTemplate); + expect(image.swapInFullImage).toHaveBeenCalledWith(fullImage); + expect(image.loadContent).not.toHaveBeenCalled(); + }); }); describe('waitForViewerLoad()', () => { From b5946ea3ffed7b4b496cfa6a4d449864aadd32e9 Mon Sep 17 00:00:00 2001 From: zhirongwang Date: Tue, 24 Nov 2026 15:32:23 -0800 Subject: [PATCH 4/6] fix(image): re-center the image rep when the swap changes its size Only one of width and height is set, so an image rep with a different aspect ratio than the placeholder renders at a different size. Re-center it after the swap when that happens, behind imageProgressiveLoad. --- src/lib/viewers/image/ImageViewer.js | 19 ++++++ .../image/__tests__/ImageViewer-test.js | 64 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index 819b5de5b..5e74a3a39 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -370,11 +370,30 @@ class ImageViewer extends ImageBaseViewer { // Keep the new source from re-running the load sequence, which would reset the zoom. imageEl.removeEventListener('load', this.finishLoading); + const { clientHeight: placeholderHeight, clientWidth: placeholderWidth } = imageEl; imageEl.src = blobUrl; imageEl.setAttribute('originalWidth', width); imageEl.setAttribute('originalHeight', height); this.setScale(parseFloat(imageEl.style.width) || undefined, parseFloat(imageEl.style.height) || undefined); + // Only one of width and height is set, so an image rep with a different aspect ratio + // renders at a different size than the placeholder. Re-center it then. + if (this.featureEnabled('imageProgressiveLoad.enabled')) { + imageEl + .decode() + .catch(() => undefined) + .then(() => { + if ( + this.isDestroyed() || + (imageEl.clientWidth === placeholderWidth && imageEl.clientHeight === placeholderHeight) + ) { + return; + } + this.adjustImageZoomPadding(); + this.updatePannability(); + }); + } + // eslint-disable-next-line no-unused-expressions placeholderEl.offsetWidth; placeholderEl.style.opacity = '0'; diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index 0e6be4690..7cc2d330e 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -486,12 +486,76 @@ describe('lib/viewers/image/ImageViewer', () => { image.imageEl.style.width = '920px'; image.imageEl.setAttribute('originalWidth', 320); image.imageEl.setAttribute('originalHeight', 240); + image.imageEl.decode = jest.fn().mockResolvedValue(); + jest.spyOn(image, 'featureEnabled').mockImplementation( + feature => feature === 'imageProgressiveLoad.enabled', + ); }); afterEach(() => { jest.useRealTimers(); }); + const flushMicrotasks = async () => { + for (let i = 0; i < 5; i += 1) { + await Promise.resolve(); // eslint-disable-line no-await-in-loop + } + }; + + const mockRenderedHeight = heightBySrc => { + Object.defineProperty(image.imageEl, 'clientWidth', { configurable: true, value: 920 }); + Object.defineProperty(image.imageEl, 'clientHeight', { + configurable: true, + get: () => heightBySrc[image.imageEl.src], + }); + }; + + test('should re-center the image rep when it renders at a different size', async () => { + mockRenderedHeight({ 'blob:320': 690, 'blob:2048': 518 }); + jest.spyOn(image, 'adjustImageZoomPadding').mockImplementation(); + jest.spyOn(image, 'updatePannability').mockImplementation(); + + image.swapInFullImage({ blobUrl: 'blob:2048', height: 1152, width: 2048 }); + await flushMicrotasks(); + + expect(image.imageEl.decode).toHaveBeenCalled(); + expect(image.adjustImageZoomPadding).toHaveBeenCalled(); + expect(image.updatePannability).toHaveBeenCalled(); + }); + + test('should keep the position when the image rep renders at the same size', async () => { + mockRenderedHeight({ 'blob:320': 690, 'blob:2048': 690 }); + jest.spyOn(image, 'adjustImageZoomPadding').mockImplementation(); + + image.swapInFullImage({ blobUrl: 'blob:2048', height: 1536, width: 2048 }); + await flushMicrotasks(); + + expect(image.adjustImageZoomPadding).not.toHaveBeenCalled(); + }); + + test('should not re-center when imageProgressiveLoad is off', async () => { + image.featureEnabled.mockReturnValue(false); + mockRenderedHeight({ 'blob:320': 690, 'blob:2048': 518 }); + jest.spyOn(image, 'adjustImageZoomPadding').mockImplementation(); + + image.swapInFullImage({ blobUrl: 'blob:2048', height: 1152, width: 2048 }); + await flushMicrotasks(); + + expect(image.imageEl.decode).not.toHaveBeenCalled(); + expect(image.adjustImageZoomPadding).not.toHaveBeenCalled(); + }); + + test('should not re-center if destroyed before the image rep decodes', async () => { + mockRenderedHeight({ 'blob:320': 690, 'blob:2048': 518 }); + jest.spyOn(image, 'adjustImageZoomPadding').mockImplementation(); + + image.swapInFullImage({ blobUrl: 'blob:2048', height: 1152, width: 2048 }); + image.destroy(); + await flushMicrotasks(); + + expect(image.adjustImageZoomPadding).not.toHaveBeenCalled(); + }); + test('should keep the displayed size and rescale against the image rep', () => { image.swapInFullImage({ blobUrl: 'blob:2048', height: 1536, width: 2048 }); From bb7ad92831dcafc1f993c8988f5aefd546ed88a1 Mon Sep 17 00:00:00 2001 From: zhirongwang Date: Tue, 24 Nov 2026 15:40:11 -0800 Subject: [PATCH 5/6] fix(image): show rep errors without a placeholder With imageProgressiveLoad on, a rep already in the error state went through the placeholder path and could leave the 320 on screen with no error. Load it the normal way so the error is shown. --- src/lib/viewers/image/ImageViewer.js | 7 ++++--- .../viewers/image/__tests__/ImageViewer-test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index 5e74a3a39..8d301caf7 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -11,6 +11,7 @@ import { CLASS_INVISIBLE, DISCOVERABILITY_ATTRIBUTE, IMAGE_FTUX_CURSOR_SEEN_KEY, + STATUS_ERROR, STATUS_NONE, STATUS_PENDING, } from '../../constants'; @@ -135,10 +136,10 @@ class ImageViewer extends ImageBaseViewer { this.bindDOMListeners(); const contentUrl = this.createContentUrlV2(template, viewer.ASSET); + const state = representation.status?.state; if ( - this.featureEnabled('imageProgressiveLoad.enabled') || - (this.featureEnabled('imageConversionPlaceholder.enabled') && - CONVERTING_STATES.includes(representation.status?.state)) + (this.featureEnabled('imageProgressiveLoad.enabled') && state !== STATUS_ERROR) || + (this.featureEnabled('imageConversionPlaceholder.enabled') && CONVERTING_STATES.includes(state)) ) { return this.loadWithPlaceholder(contentUrl); } diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index 7cc2d330e..e3fc0715e 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -228,6 +228,22 @@ describe('lib/viewers/image/ImageViewer', () => { expect(image.imageEl.src).toBe('blob:320'); }); + test('should show the rep error without a placeholder when imageProgressiveLoad is on', async () => { + const error = new Error('conversion failed'); + image.options.representation.status.state = 'error'; + image.featureEnabled.mockImplementation(feature => feature === 'imageProgressiveLoad.enabled'); + image.getRepStatus.mockReturnValue({ getPromise: () => Promise.reject(error) }); + jest.spyOn(image, 'fetchDecodedImage'); + jest.spyOn(image, 'handleAssetError').mockImplementation(); + + await image.load(); + + expect(image.getRepStatus).toHaveBeenCalledWith(); + expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + expect(image.emit).not.toHaveBeenCalledWith(VIEWER_EVENT.conversionPending); + expect(image.handleAssetError).toHaveBeenCalledWith(error); + }); + test('should show the 320 without telling the host the rep is converting', async () => { jest.spyOn(image, 'fetchDecodedImage').mockResolvedValue({ blobUrl: 'blob:320', height: 240, width: 320 }); From df2dda5f357bf5304130cd4f62c8fabe567e0f29 Mon Sep 17 00:00:00 2001 From: zhirongwang Date: Tue, 24 Nov 2026 15:43:44 -0800 Subject: [PATCH 6/6] fix(image): load SVGs without a placeholder An SVG with only a viewBox decodes to a zero size, so the image rep never replaced the placeholder. With imageProgressiveLoad on, load SVGs the normal way, which reads the size from the viewBox. --- src/lib/viewers/image/ImageViewer.js | 4 +++- .../viewers/image/__tests__/ImageViewer-test.js | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index 8d301caf7..1ba63d2b9 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -137,8 +137,10 @@ class ImageViewer extends ImageBaseViewer { const contentUrl = this.createContentUrlV2(template, viewer.ASSET); const state = representation.status?.state; + // An SVG with only a viewBox decodes to a zero size, which the placeholder swap cannot use. + const isSvg = this.options.file.extension === 'svg'; if ( - (this.featureEnabled('imageProgressiveLoad.enabled') && state !== STATUS_ERROR) || + (this.featureEnabled('imageProgressiveLoad.enabled') && state !== STATUS_ERROR && !isSvg) || (this.featureEnabled('imageConversionPlaceholder.enabled') && CONVERTING_STATES.includes(state)) ) { return this.loadWithPlaceholder(contentUrl); diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index e3fc0715e..c0d76e2c6 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -451,7 +451,21 @@ describe('lib/viewers/image/ImageViewer', () => { expect(image.displaySizeCap).toBeUndefined(); }); - test('should show the host placeholder, then swap in the original', async () => { + test('should load an SVG the normal way, ignoring the host placeholder', async () => { + image.options.file.extension = 'svg'; + image.options.viewers = { Image: { placeholder: { fileId: '1', url: 'blob:host' } } }; + + await image.load(); + + expect(image.getRepStatus).toHaveBeenCalledWith(); + expect(global.fetch).not.toHaveBeenCalled(); + expect(image.fetchDecodedImage).not.toHaveBeenCalled(); + expect(image.loadContent).toHaveBeenCalledWith(originalTemplate); + expect(image.displaySizeCap).toBeUndefined(); + }); + + test('should show the host placeholder for a GIF, then swap in the original', async () => { + image.options.file.extension = 'gif'; image.options.viewers = { Image: { placeholder: { fileId: '1', url: 'blob:host' } } }; jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:copy'); jest.spyOn(image, 'decodeBlobUrl').mockResolvedValue({ blobUrl: 'blob:copy', height: 240, width: 320 });