@hkdigital/lib-sveltekit 0.0.76 → 0.0.78

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,27 @@
1
+ /**
2
+ * Configures and returns a function that can be used as
3
+ * 'resolveConfigs' parameter in imagetools config
4
+ *
5
+ * @param {object} [options]
6
+ * @param {number[]} [options.widths=DEFAULT_WIDTHS]
7
+ * @returns {(
8
+ * entries: [string, string[]][]
9
+ * ) => { [key: string]: string | string[] }[]}
10
+ */
11
+ export function generateResponseConfigs(options?: {
12
+ widths?: number[];
13
+ }): (entries: [string, string[]][]) => {
14
+ [key: string]: string | string[];
15
+ }[];
16
+ /**
17
+ * Configures and returns a function that can be used as
18
+ * 'defaultDirectives' parameter in imagetools config
19
+ *
20
+ * - This function runs before 'resolveConfigs'
21
+ *
22
+ * @param {object} [options]
23
+ * @param {number[]} [options.presets=DEFAULT_PRESETS]
24
+ */
25
+ export function generateDefaultDirectives(options?: {
26
+ presets?: number[];
27
+ }): (url: URL) => URLSearchParams;
@@ -0,0 +1,169 @@
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
+ * @param {object} [options]
36
+ * @param {number[]} [options.widths=DEFAULT_WIDTHS]
37
+ * @returns {(
38
+ * entries: [string, string[]][]
39
+ * ) => { [key: string]: string | string[] }[]}
40
+ */
41
+ export function generateResponseConfigs(options) {
42
+ return function resolveConfigs(entries /*, outputFormats*/) {
43
+ console.log('resolveConfigs:entries', entries);
44
+
45
+ // @ts-ignore
46
+ const responsiveConfig = entries.find(([key]) => key === 'responsive');
47
+
48
+ if (!responsiveConfig) {
49
+ // Directive 'responsive' was not set => do nothing
50
+ return [];
51
+ }
52
+
53
+ const widths = options?.widths ?? DEFAULT_WIDTHS;
54
+
55
+ const configPairs = {};
56
+
57
+ for (const current of entries) {
58
+ const key = current[0];
59
+ const value = current[1][0];
60
+
61
+ // @ts-ignore
62
+ configPairs[key] = value;
63
+ }
64
+
65
+ /** @type {[key: string]: string | string[] }[]} */
66
+ return widths.map((w) => {
67
+ return { ...configPairs, w: String(w) };
68
+ });
69
+ };
70
+ }
71
+
72
+ /**
73
+ * Configures and returns a function that can be used as
74
+ * 'defaultDirectives' parameter in imagetools config
75
+ *
76
+ * - This function runs before 'resolveConfigs'
77
+ *
78
+ * @param {object} [options]
79
+ * @param {number[]} [options.presets=DEFAULT_PRESETS]
80
+ */
81
+ export function generateDefaultDirectives(options) {
82
+ /**
83
+ * Function that sets a.o. format and quality directives if
84
+ * the 'preset' driective is set
85
+ *
86
+ * @param {URL} url
87
+ */
88
+ return function defaultDirectives(url) {
89
+ // Check the directive in the URL to determine which preset to use
90
+ const params = url.searchParams;
91
+
92
+ let presetName = params.get('preset');
93
+
94
+ // > Return metadata if directive 'responsive' is set
95
+
96
+ if (params.has('responsive')) {
97
+ params.set('as', 'metadata');
98
+ }
99
+ // > Process presets
100
+
101
+ if (presetName) {
102
+ params.delete('preset');
103
+ } else {
104
+ presetName = 'default';
105
+ }
106
+
107
+ const presets = options?.presets || DEFAULT_PRESETS;
108
+
109
+ // @ts-ignore
110
+ let preset = presets[presetName];
111
+
112
+ console.log(`defaultDirectives [url=${url}]`, preset);
113
+
114
+ if (!preset) {
115
+ preset = DEFAULT_PRESETS.default;
116
+ }
117
+
118
+ for (const key in preset) {
119
+ params.set(key, preset[key]);
120
+ }
121
+
122
+ // TODO: process directive 'w''
123
+ // - generate only allowed widths
124
+ // - support width presets such as 'hd'
125
+
126
+ // let width = params.get('w');
127
+
128
+ // if (!width) {
129
+ // // Set max allowed width by default
130
+ // // width = 'hd';
131
+ // }
132
+
133
+ // let presets = {
134
+ // sm: '640',
135
+ // md: '1024',
136
+ // lg: '1536',
137
+ // hd: '1920'
138
+ // };
139
+
140
+ // // @ts-ignore
141
+ // let presetWidth = presets[width];
142
+ // console.log({ width, presetWidth });
143
+
144
+ // if (presetWidth) {
145
+ // params.set('w', presetWidth);
146
+ // } else if (responsive || width === null) {
147
+ // params.set('as', 'metadata');
148
+ // params.delete('w');
149
+ // // } else if (width === null) {
150
+ // // // Set largest size is width parameter is missing
151
+ // // params.set('w', presets.hd);
152
+ // } else {
153
+ // throw new Error(
154
+ // `Directive [w=${width}] is should be a preset value {sm,md,lg,hd,responsive}`
155
+ // );
156
+
157
+ // // let allowedWidths = new Set(['640', '1024', '1536', '1920']);
158
+ // // if (!allowedWidths.has(width)) {
159
+ // // throw new Error(`Width [${width}] is not in list of allowed widths`);
160
+ // // }
161
+ // }
162
+
163
+ // params.set('withoutEnlargement', 'true');
164
+
165
+ console.log('defaultDirectives:output params', params);
166
+
167
+ return params;
168
+ };
169
+ }
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.78",
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
- }, {}, "">;