@hkdigital/lib-sveltekit 0.0.87 → 0.0.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classes/svelte/image/ImageLoader.svelte.js +14 -4
- package/dist/classes/svelte/image/ImageVariantsLoader.svelte.d.ts +1 -1
- package/dist/classes/svelte/image/ImageVariantsLoader.svelte.js +3 -9
- package/dist/classes/svelte/image/index.d.ts +1 -1
- package/dist/classes/svelte/image/index.js +1 -1
- package/dist/classes/svelte/network-loader/NetworkLoader.svelte.d.ts +8 -0
- package/dist/classes/svelte/network-loader/NetworkLoader.svelte.js +31 -4
- package/dist/classes/svelte/network-loader/mocks.d.ts +7 -0
- package/dist/classes/svelte/network-loader/mocks.js +30 -0
- package/dist/components/image/ResponsiveImage.svelte +2 -2
- package/package.json +1 -1
@@ -1,7 +1,7 @@
|
|
1
1
|
import {
|
2
|
-
NetworkLoader
|
3
|
-
ERROR_NOT_LOADED,
|
4
|
-
ERROR_TRANSFERRED
|
2
|
+
NetworkLoader
|
3
|
+
// ERROR_NOT_LOADED,
|
4
|
+
// ERROR_TRANSFERRED
|
5
5
|
} from '../network-loader/index.js';
|
6
6
|
|
7
7
|
/**
|
@@ -9,4 +9,14 @@ import {
|
|
9
9
|
* - Loads image data from network
|
10
10
|
* - The loading process can be monitored
|
11
11
|
*/
|
12
|
-
export default class ImageLoader extends NetworkLoader {
|
12
|
+
export default class ImageLoader extends NetworkLoader {
|
13
|
+
// > Inherited methods
|
14
|
+
//
|
15
|
+
// /**
|
16
|
+
// * Get object URL that can be used as src parameter of an HTML image
|
17
|
+
// *
|
18
|
+
// * @note the objectURL should be revoked when no longer used
|
19
|
+
// */
|
20
|
+
// getObjectUrl() {}
|
21
|
+
//
|
22
|
+
} // end class
|
@@ -6,7 +6,7 @@ import { untrack } from 'svelte';
|
|
6
6
|
|
7
7
|
import ImageLoader from './ImageLoader.svelte.js';
|
8
8
|
|
9
|
-
export default class
|
9
|
+
export default class ImageVariantsLoader {
|
10
10
|
/** @type {number} */
|
11
11
|
#devicePixelRatio;
|
12
12
|
|
@@ -69,11 +69,7 @@ export default class ImageMetasLoader {
|
|
69
69
|
updateOptimalImageMeta(containerWidth) {
|
70
70
|
const newVariant = this.getOptimalImageMeta(containerWidth);
|
71
71
|
|
72
|
-
if (
|
73
|
-
!newVariant ||
|
74
|
-
!this.#imageVariant ||
|
75
|
-
newVariant.width > this.#imageVariant.width
|
76
|
-
) {
|
72
|
+
if (!newVariant || !this.#imageVariant || newVariant.width > this.#imageVariant.width) {
|
77
73
|
// Only update imageVariant is width is larger
|
78
74
|
this.#imageVariant = newVariant;
|
79
75
|
}
|
@@ -141,9 +137,7 @@ export default class ImageMetasLoader {
|
|
141
137
|
|
142
138
|
// Find the smallest image that's larger than our required width
|
143
139
|
|
144
|
-
const optimal = imagesMeta.find(
|
145
|
-
(current) => current.width >= requiredWidth
|
146
|
-
);
|
140
|
+
const optimal = imagesMeta.find((current) => current.width >= requiredWidth);
|
147
141
|
|
148
142
|
// Fall back to the largest image if nothing is big enough
|
149
143
|
return optimal || imagesMeta[imagesMeta.length - 1];
|
@@ -1,2 +1,2 @@
|
|
1
1
|
export { default as ImageLoader } from "./ImageLoader.svelte";
|
2
|
-
export { default as
|
2
|
+
export { default as ImageVariantsLoader } from "./ImageVariantsLoader.svelte";
|
@@ -80,6 +80,14 @@ export default class NetworkLoader {
|
|
80
80
|
* @returns {Blob}
|
81
81
|
*/
|
82
82
|
getBlob(): Blob;
|
83
|
+
/**
|
84
|
+
* Get object URL
|
85
|
+
*
|
86
|
+
* @note the objectURL should be revoked when no longer used
|
87
|
+
*
|
88
|
+
* @returns {string|null}
|
89
|
+
*/
|
90
|
+
getObjectUrl(): string | null;
|
83
91
|
#private;
|
84
92
|
}
|
85
93
|
import { LoadingStateMachine } from '../loading-state-machine/index.js';
|
@@ -18,10 +18,7 @@ import {
|
|
18
18
|
|
19
19
|
import * as expect from '@hkdigital/lib-sveltekit/util/expect/index.js';
|
20
20
|
|
21
|
-
import {
|
22
|
-
httpGet,
|
23
|
-
loadResponseBuffer
|
24
|
-
} from '@hkdigital/lib-sveltekit/util/http/index.js';
|
21
|
+
import { httpGet, loadResponseBuffer } from '@hkdigital/lib-sveltekit/util/http/index.js';
|
25
22
|
|
26
23
|
import { ERROR_NOT_LOADED, ERROR_TRANSFERRED } from './constants.js';
|
27
24
|
|
@@ -214,6 +211,36 @@ export default class NetworkLoader {
|
|
214
211
|
return new Blob([this.getArrayBuffer()], options);
|
215
212
|
}
|
216
213
|
|
214
|
+
/**
|
215
|
+
* Get object URL
|
216
|
+
*
|
217
|
+
* @note the objectURL should be revoked when no longer used
|
218
|
+
*
|
219
|
+
* @returns {string|null}
|
220
|
+
*/
|
221
|
+
getObjectUrl() {
|
222
|
+
//
|
223
|
+
// Example usage:
|
224
|
+
//
|
225
|
+
// $effect(() => {
|
226
|
+
// if (loader.loaded) {
|
227
|
+
// // @ts-ignore
|
228
|
+
// objectUrl = loader.getObjectUrl();
|
229
|
+
// }
|
230
|
+
//
|
231
|
+
// return () => {
|
232
|
+
// if (objectUrl) {
|
233
|
+
// URL.revokeObjectURL(objectUrl);
|
234
|
+
// objectUrl = null;
|
235
|
+
// }
|
236
|
+
// };
|
237
|
+
// });
|
238
|
+
|
239
|
+
const blob = this.getBlob();
|
240
|
+
|
241
|
+
return blob ? URL.createObjectURL(blob) : null;
|
242
|
+
}
|
243
|
+
|
217
244
|
/**
|
218
245
|
* Internal method that initializes the loading process
|
219
246
|
* and transitions to state LOADED when done
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { CONTENT_TYPE, CONTENT_LENGTH } from '@hkdigital/lib-sveltekit/constants/http/index.js';
|
2
|
+
|
3
|
+
import { OCTET_STREAM } from '@hkdigital/lib-sveltekit/constants/mime/application.js';
|
4
|
+
|
5
|
+
const BASE64_DATA =
|
6
|
+
'UklGRnwAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YVgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Create a response value that can be used by a mocked
|
10
|
+
* fetch function
|
11
|
+
*
|
12
|
+
* @returns {Response}
|
13
|
+
*/
|
14
|
+
export function createDataResponse(/* data , options */) {
|
15
|
+
const binaryString = atob(BASE64_DATA);
|
16
|
+
const bytes = new Uint8Array(binaryString.length);
|
17
|
+
|
18
|
+
for (let i = 0; i < binaryString.length; i++) {
|
19
|
+
bytes[i] = binaryString.charCodeAt(i);
|
20
|
+
}
|
21
|
+
|
22
|
+
const response = new Response(bytes, {
|
23
|
+
headers: new Headers({
|
24
|
+
[CONTENT_TYPE]: OCTET_STREAM,
|
25
|
+
[CONTENT_LENGTH]: String(bytes.length)
|
26
|
+
})
|
27
|
+
});
|
28
|
+
|
29
|
+
return response;
|
30
|
+
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
<script>
|
2
2
|
/** @typedef {import('../../types/imagetools.js').ImageMeta} ImageMeta */
|
3
3
|
|
4
|
-
import {
|
4
|
+
import { ImageVariantsLoader } from '../../classes/svelte/image/index.js';
|
5
5
|
|
6
6
|
/**
|
7
7
|
* @type {{
|
@@ -29,7 +29,7 @@
|
|
29
29
|
...attrs
|
30
30
|
} = $props();
|
31
31
|
|
32
|
-
let variantsLoader = new
|
32
|
+
let variantsLoader = new ImageVariantsLoader(images);
|
33
33
|
|
34
34
|
let containerWidth = $state(0);
|
35
35
|
|