-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
imagekit_loader.ts
51 lines (46 loc) 路 1.54 KB
/
imagekit_loader.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {createImageLoader, ImageLoaderConfig, ImageLoaderInfo} from './image_loader';
/**
* Name and URL tester for ImageKit.
*/
export const imageKitLoaderInfo: ImageLoaderInfo = {
name: 'ImageKit',
testUrl: isImageKitUrl
};
const IMAGE_KIT_LOADER_REGEX = /https?\:\/\/[^\/]+\.imagekit\.io\/.+/;
/**
* Tests whether a URL is from ImageKit CDN.
*/
function isImageKitUrl(url: string): boolean {
return IMAGE_KIT_LOADER_REGEX.test(url);
}
/**
* Function that generates an ImageLoader for ImageKit and turns it into an Angular provider.
*
* @param path Base URL of your ImageKit images
* This URL should match one of the following formats:
* https://ik.imagekit.io/myaccount
* https://subdomain.mysite.com
* @returns Set of providers to configure the ImageKit loader.
*
* @publicApi
* @developerPreview
*/
export const provideImageKitLoader = createImageLoader(
createImagekitUrl,
ngDevMode ? ['https://ik.imagekit.io/mysite', 'https://subdomain.mysite.com'] : undefined);
export function createImagekitUrl(path: string, config: ImageLoaderConfig) {
// Example of an ImageKit image URL:
// https://ik.imagekit.io/demo/tr:w-300,h-300/medium_cafe_B1iTdD0C.jpg
let params = `tr:q-auto`; // applies the "auto quality" transformation
if (config.width) {
params += `,w-${config.width}`;
}
return `${path}/${params}/${config.src}`;
}