|
1 | 1 | import { Provider } from '@angular/core'; |
2 | 2 | import { IMAGE_LOADER, ImageLoaderConfig } from '@angular/common'; |
| 3 | + |
3 | 4 | import imageUrlBuilder from '@sanity/image-url'; |
4 | | -import type { |
5 | | - SanityImageSource, |
6 | | - SanityImageObject, |
7 | | - SanityReference, |
8 | | - SanityAsset, |
9 | | -} from '@sanity/image-url/lib/types/types'; |
10 | 5 |
|
11 | | -interface SanityConfig { |
12 | | - projectId: string; |
13 | | - dataset: string; |
14 | | -} |
| 6 | +import { SANITY_CONFIG, SanityConfig } from '@limitless-angular/sanity/shared'; |
15 | 7 |
|
16 | 8 | const DEFAULT_IMAGE_QUALITY = 75; |
17 | 9 |
|
18 | | -function getSanityRefId(image: SanityImageSource): string { |
19 | | - if (typeof image === 'string') { |
20 | | - return image; |
21 | | - } |
22 | | - |
23 | | - const obj = image as SanityImageObject; |
24 | | - const ref = image as SanityReference; |
25 | | - const img = image as SanityAsset; |
26 | | - |
27 | | - if (obj.asset) { |
28 | | - return obj.asset._ref || (obj.asset as SanityAsset)._id; |
29 | | - } |
30 | | - |
31 | | - return ref._ref || img._id || ''; |
32 | | -} |
33 | | - |
34 | 10 | export function provideSanityLoader(config: SanityConfig): Provider { |
35 | | - return { |
36 | | - provide: IMAGE_LOADER, |
37 | | - useValue: (loaderConfig: ImageLoaderConfig) => { |
38 | | - const { |
39 | | - src, |
40 | | - loaderParams = {}, |
41 | | - width = loaderParams['width'], |
42 | | - isPlaceholder, |
43 | | - } = loaderConfig; |
44 | | - const image = src as SanityImageSource; |
45 | | - const id = getSanityRefId(image); |
46 | | - if (!id) { |
47 | | - throw new Error('Invalid Sanity image source'); |
48 | | - } |
49 | | - |
50 | | - const builder = imageUrlBuilder(config); |
51 | | - let imageBuilder = builder |
52 | | - .image(image) |
53 | | - .auto('format') |
54 | | - .fit((loaderParams['fit'] ?? loaderParams['height']) ? 'min' : 'max'); |
55 | | - |
56 | | - if (width && loaderParams['height'] && loaderParams['width']) { |
57 | | - imageBuilder = imageBuilder.height( |
58 | | - Math.round((loaderParams['height'] / loaderParams['width']) * width), |
59 | | - ); |
60 | | - } |
61 | | - |
62 | | - if (width) { |
63 | | - imageBuilder = imageBuilder.width(width); |
64 | | - } |
65 | | - |
66 | | - // Use loaderParams for additional configuration |
67 | | - if (loaderParams['quality']) { |
68 | | - imageBuilder = imageBuilder.quality(loaderParams['quality']); |
69 | | - } else { |
70 | | - imageBuilder = imageBuilder.quality(DEFAULT_IMAGE_QUALITY); |
71 | | - } |
72 | | - |
73 | | - if (loaderParams['blur']) { |
74 | | - imageBuilder = imageBuilder.blur(loaderParams['blur']); |
75 | | - } |
76 | | - |
77 | | - if (isPlaceholder) { |
78 | | - imageBuilder = imageBuilder.blur(50).quality(20); |
79 | | - } |
80 | | - |
81 | | - return imageBuilder.url() || ''; |
| 11 | + return [ |
| 12 | + { provide: SANITY_CONFIG, useValue: config }, |
| 13 | + { |
| 14 | + provide: IMAGE_LOADER, |
| 15 | + useValue: (loaderConfig: ImageLoaderConfig) => { |
| 16 | + const { |
| 17 | + src, |
| 18 | + loaderParams = {}, |
| 19 | + width = loaderParams['width'], |
| 20 | + isPlaceholder, |
| 21 | + } = loaderConfig; |
| 22 | + const builder = imageUrlBuilder(config); |
| 23 | + let imageBuilder = builder |
| 24 | + .image(src) |
| 25 | + .auto('format') |
| 26 | + .fit((loaderParams['fit'] ?? loaderParams['height']) ? 'min' : 'max'); |
| 27 | + |
| 28 | + if (width && loaderParams['height'] && loaderParams['width']) { |
| 29 | + imageBuilder = imageBuilder.height( |
| 30 | + Math.round( |
| 31 | + (loaderParams['height'] / loaderParams['width']) * width, |
| 32 | + ), |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + if (width) { |
| 37 | + imageBuilder = imageBuilder.width(width); |
| 38 | + } |
| 39 | + |
| 40 | + // Use loaderParams for additional configuration |
| 41 | + if (loaderParams['quality']) { |
| 42 | + imageBuilder = imageBuilder.quality(loaderParams['quality']); |
| 43 | + } else { |
| 44 | + imageBuilder = imageBuilder.quality(DEFAULT_IMAGE_QUALITY); |
| 45 | + } |
| 46 | + |
| 47 | + if (loaderParams['blur']) { |
| 48 | + imageBuilder = imageBuilder.blur(loaderParams['blur']); |
| 49 | + } |
| 50 | + |
| 51 | + if (isPlaceholder) { |
| 52 | + imageBuilder = imageBuilder.blur(50).quality(20); |
| 53 | + } |
| 54 | + |
| 55 | + return imageBuilder.url() || ''; |
| 56 | + }, |
82 | 57 | }, |
83 | | - }; |
| 58 | + ]; |
84 | 59 | } |
0 commit comments