@hkdigital/lib-sveltekit 0.0.76 → 0.0.77

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.
@@ -4,7 +4,7 @@
4
4
  /**
5
5
  * @example
6
6
  * import { EnhancedImage }
7
- * from '../EnhancedImage/index.js';
7
+ * from '$lib/components/EnhancedImage/index.js';
8
8
  *
9
9
  * <EnhancedImage
10
10
  * classes="aspect-video max-h-svh w-full border-8 border-pink-500"
@@ -24,7 +24,7 @@
24
24
  * aspect?: string,
25
25
  * fit?: string,
26
26
  * position?: string,
27
- * src: string | import('../../typedef/image.js').Picture,
27
+ * src: string | import('$lib/typedef/image.js').Picture,
28
28
  * alt?: string,
29
29
  * onload?: ( e: (Event|{ type: string, target: HTMLImageElement }) ) => void,
30
30
  * onerror?: ( e: (Event|{ type: string, target: HTMLImageElement }) ) => void,
@@ -61,7 +61,7 @@
61
61
  /** @type {HTMLImageElement|undefined} */
62
62
  let imgElem = $state();
63
63
 
64
- /** @type {string | import('../../typedef/image.js').Picture} */
64
+ /** @type {string | import('$lib/typedef/image.js').Picture} */
65
65
  let src_;
66
66
 
67
67
  $effect(() => {
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Configures and returns a function that can be used as
3
+ * 'resolveConfigs' parameter in imagetools config
4
+ *
5
+ * - This function runs after 'defaultDirectives'
6
+ *
7
+ * @param {object} [options]
8
+ * @param {number[]} [options.widths=DEFAULT_WIDTHS]
9
+ *
10
+ * @returns {(config: [ key:string, [ value: string] ][]) => Record<string, string>[]|undefined}
11
+ */
12
+ export function generateResponseConfigs(options?: {
13
+ widths?: number[];
14
+ }): (config: [key: string, [value: string]][]) => Record<string, string>[] | undefined;
15
+ /**
16
+ * Configures and returns a function that can be used as
17
+ * 'defaultDirectives' parameter in imagetools config
18
+ *
19
+ * - This function runs before 'resolveConfigs'
20
+ *
21
+ * @param {object} [options]
22
+ * @param {number[]} [options.presets=DEFAULT_PRESETS]
23
+ */
24
+ export function generateDefaultDirectives(options?: {
25
+ presets?: number[];
26
+ }): (url: URL) => URLSearchParams;
@@ -0,0 +1,178 @@
1
+ const DEFAULT_WIDTHS = [640, 1024, 1536, 1920];
2
+
3
+ const DEFAULT_PRESETS = {
4
+ default: {
5
+ format: 'avif',
6
+ quality: '90'
7
+ },
8
+ gradient: {
9
+ format: 'jpg',
10
+ quality: '95'
11
+ },
12
+ photo: {
13
+ format: 'jpg',
14
+ quality: '95'
15
+ },
16
+ drawing: {
17
+ format: 'avif',
18
+ quality: '90'
19
+ },
20
+ savedata: {
21
+ format: 'avif',
22
+ quality: '85'
23
+ },
24
+ blur: {
25
+ format: 'avif',
26
+ quality: '50',
27
+ blur: '75'
28
+ }
29
+ };
30
+
31
+ /**
32
+ * Configures and returns a function that can be used as
33
+ * 'resolveConfigs' parameter in imagetools config
34
+ *
35
+ * - This function runs after 'defaultDirectives'
36
+ *
37
+ * @param {object} [options]
38
+ * @param {number[]} [options.widths=DEFAULT_WIDTHS]
39
+ *
40
+ * @returns {(config: [ key:string, [ value: string] ][]) => Record<string, string>[]|undefined}
41
+ */
42
+ export function generateResponseConfigs(options) {
43
+ /**
44
+ * Function that returns a list that specifies the image
45
+ * widths for the image variants that should be generated
46
+ * when the directive 'responsive' is set.
47
+ *
48
+ * @param {[ key:string, [ value: string] ][]} config
49
+ *
50
+ * @returns {Record<string, string>[]|undefined}
51
+ */
52
+ return function resolveConfigs(config) {
53
+ console.log('resolveConfigs:config', config);
54
+
55
+ // @ts-ignore
56
+ const responsiveConfig = config.find(([key]) => key === 'responsive');
57
+
58
+ if (!responsiveConfig) {
59
+ // Driective 'responsive' was not set => do nothing
60
+ return undefined;
61
+ }
62
+
63
+ const widths = options?.widths ?? DEFAULT_WIDTHS;
64
+
65
+ const configPairs = {};
66
+
67
+ for (const current of config) {
68
+ const key = current[0];
69
+ const value = current[1][0];
70
+
71
+ // @ts-ignore
72
+ configPairs[key] = value;
73
+ }
74
+
75
+ return widths.map((w) => {
76
+ return { ...configPairs, w: String(w) };
77
+ });
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Configures and returns a function that can be used as
83
+ * 'defaultDirectives' parameter in imagetools config
84
+ *
85
+ * - This function runs before 'resolveConfigs'
86
+ *
87
+ * @param {object} [options]
88
+ * @param {number[]} [options.presets=DEFAULT_PRESETS]
89
+ */
90
+ export function generateDefaultDirectives(options) {
91
+ /**
92
+ * Function that sets a.o. format and quality directives if
93
+ * the 'preset' driective is set
94
+ *
95
+ * @param {URL} url
96
+ */
97
+ return function defaultDirectives(url) {
98
+ // Check the directive in the URL to determine which preset to use
99
+ const params = url.searchParams;
100
+
101
+ let presetName = params.get('preset');
102
+
103
+ // > Return metadata if directive 'responsive' is set
104
+
105
+ if (params.has('responsive')) {
106
+ params.set('as', 'metadata');
107
+ }
108
+ // > Process presets
109
+
110
+ if (presetName) {
111
+ params.delete('preset');
112
+ } else {
113
+ presetName = 'default';
114
+ }
115
+
116
+ const presets = options?.presets || DEFAULT_PRESETS;
117
+
118
+ // @ts-ignore
119
+ let preset = presets[presetName];
120
+
121
+ console.log(`defaultDirectives [url=${url}]`, preset);
122
+
123
+ if (!preset) {
124
+ preset = DEFAULT_PRESETS.default;
125
+ }
126
+
127
+ for (const key in preset) {
128
+ params.set(key, preset[key]);
129
+ }
130
+
131
+ // TODO: process directive 'w''
132
+ // - generate only allowed widths
133
+ // - support width presets such as 'hd'
134
+
135
+ // let width = params.get('w');
136
+
137
+ // if (!width) {
138
+ // // Set max allowed width by default
139
+ // // width = 'hd';
140
+ // }
141
+
142
+ // let presets = {
143
+ // sm: '640',
144
+ // md: '1024',
145
+ // lg: '1536',
146
+ // hd: '1920'
147
+ // };
148
+
149
+ // // @ts-ignore
150
+ // let presetWidth = presets[width];
151
+ // console.log({ width, presetWidth });
152
+
153
+ // if (presetWidth) {
154
+ // params.set('w', presetWidth);
155
+ // } else if (responsive || width === null) {
156
+ // params.set('as', 'metadata');
157
+ // params.delete('w');
158
+ // // } else if (width === null) {
159
+ // // // Set largest size is width parameter is missing
160
+ // // params.set('w', presets.hd);
161
+ // } else {
162
+ // throw new Error(
163
+ // `Directive [w=${width}] is should be a preset value {sm,md,lg,hd,responsive}`
164
+ // );
165
+
166
+ // // let allowedWidths = new Set(['640', '1024', '1536', '1920']);
167
+ // // if (!allowedWidths.has(width)) {
168
+ // // throw new Error(`Width [${width}] is not in list of allowed widths`);
169
+ // // }
170
+ // }
171
+
172
+ // params.set('withoutEnlargement', 'true');
173
+
174
+ console.log('defaultDirectives:output params', params);
175
+
176
+ return params;
177
+ };
178
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.0.76",
3
+ "version": "0.0.77",
4
4
  "author": "Jens Kleinhout, HKdigital (https://hkdigital.nl)",
5
5
  "license": "ISC",
6
6
  "repository": {
@@ -1,23 +0,0 @@
1
- export default EnhancedImage;
2
- declare const EnhancedImage: import("svelte").Component<{
3
- base?: string;
4
- bg?: string;
5
- classes?: string;
6
- overflow?: string;
7
- aspect?: string;
8
- fit?: string;
9
- position?: string;
10
- src: string | any;
11
- alt?: string;
12
- onload?: (e: (Event | {
13
- type: string;
14
- target: HTMLImageElement;
15
- })) => void;
16
- onerror?: (e: (Event | {
17
- type: string;
18
- target: HTMLImageElement;
19
- })) => void;
20
- loading?: string;
21
- } & {
22
- [attr: string]: any;
23
- }, {}, "">;