-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexports.ts
59 lines (50 loc) · 2.13 KB
/
exports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// istanbul ignore file: Covered by other scripts.
import { logFailure } from '@lib/util/async';
import type { ImageInfo } from './image-info';
import type { InfoCache } from './info-cache';
import { CAAImageWithFullSizeURL, displayInfoWhenInView } from './displayed-image';
import { CAAImage } from './image';
interface LegacyImageInfo {
url: string;
width: number;
height: number;
size?: number;
format?: string;
}
declare global {
interface Window {
ROpdebee_getDimensionsWhenInView: (imageElement: HTMLImageElement) => void;
ROpdebee_getCAAImageInfo: (imageUrl: string) => Promise<ImageInfo>;
ROpdebee_loadImageDimensions: (imageUrl: string) => Promise<LegacyImageInfo>;
}
}
export function setupExports(cachePromise: Promise<InfoCache>): void {
async function getCAAImageInfo(imageUrl: string): Promise<ImageInfo> {
if (new URL(imageUrl).hostname !== 'archive.org') {
throw new Error('Unsupported URL: Need direct image URL');
}
const cache = await cachePromise;
const image = new CAAImage(imageUrl, cache);
return image.getImageInfo();
}
function getDimensionsWhenInView(imageElement: HTMLImageElement): void {
cachePromise.then((cache) => {
const image = new CAAImageWithFullSizeURL(imageElement, cache);
displayInfoWhenInView(image);
}).catch(logFailure(`Something went wrong when attempting to load image info for ${imageElement.src}`));
}
async function loadImageDimensions(imageUrl: string): Promise<LegacyImageInfo> {
const imageInfo = await getCAAImageInfo(imageUrl);
return {
url: imageUrl,
...imageInfo.dimensions ?? { width: 0, height: 0 },
size: imageInfo.size,
format: imageInfo.fileType,
};
}
// Expose the function for use in other scripts that may load images.
window.ROpdebee_getDimensionsWhenInView = getDimensionsWhenInView;
// Deprecated, use `ROpdebee_getImageInfo` instead.
window.ROpdebee_loadImageDimensions = loadImageDimensions;
window.ROpdebee_getCAAImageInfo = getCAAImageInfo;
}