screenshot-utils.mjs (2182B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * List of helper functions for screenshot-based images. 7 * 8 * There are two kinds of images: 9 * 1. Remote Image: This is the image from the main process and it refers to 10 * the image in the React props. This can either be an object with the `data` 11 * and `path` properties, if it is a blob, or a string, if it is a normal image. 12 * 2. Local Image: This is the image object in the content process and it refers 13 * to the image *object* in the React component's state. All local image 14 * objects have the `url` property, and an additional property `path`, if they 15 * are blobs. 16 */ 17 export const ScreenshotUtils = { 18 isBlob(isLocal, image) { 19 return !!( 20 image && 21 image.path && 22 ((!isLocal && image.data) || (isLocal && image.url)) 23 ); 24 }, 25 26 // This should always be called with a remote image and not a local image. 27 createLocalImageObject(remoteImage) { 28 if (!remoteImage) { 29 return null; 30 } 31 if (this.isBlob(false, remoteImage)) { 32 return { 33 url: globalThis.URL.createObjectURL(remoteImage.data), 34 path: remoteImage.path, 35 }; 36 } 37 return { url: remoteImage }; 38 }, 39 40 // Revokes the object URL of the image if the local image is a blob. 41 // This should always be called with a local image and not a remote image. 42 maybeRevokeBlobObjectURL(localImage) { 43 if (this.isBlob(true, localImage)) { 44 globalThis.URL.revokeObjectURL(localImage.url); 45 } 46 }, 47 48 // Checks if remoteImage and localImage are the same. 49 isRemoteImageLocal(localImage, remoteImage) { 50 // Both remoteImage and localImage are present. 51 if (remoteImage && localImage) { 52 return this.isBlob(false, remoteImage) 53 ? localImage.path === remoteImage.path 54 : localImage.url === remoteImage; 55 } 56 57 // This will only handle the remaining three possible outcomes. 58 // (i.e. everything except when both image and localImage are present) 59 return !remoteImage && !localImage; 60 }, 61 };