@ckeditor/ckeditor5-ckbox 38.0.1 → 38.1.1

Sign up to get free protection for your applications and to get access to all the features.
package/src/utils.d.ts CHANGED
@@ -1,31 +1,31 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module ckbox/utils
7
- */
8
- import type { InitializedToken } from '@ckeditor/ckeditor5-cloud-services';
9
- /**
10
- * Creates URLs for the image:
11
- * - responsive URLs for the "webp" image format,
12
- * - one fallback URL for browsers that do not support the "webp" format.
13
- */
14
- export declare function getImageUrls({ token, id, origin, width, extension }: {
15
- token: InitializedToken;
16
- id: string;
17
- origin: string;
18
- width: number;
19
- extension: string;
20
- }): {
21
- imageFallbackUrl: string;
22
- imageSources: Array<{
23
- srcset: string;
24
- sizes: string;
25
- type: string;
26
- }>;
27
- };
28
- /**
29
- * Returns an environment id from a token used for communication with the CKBox service.
30
- */
31
- export declare function getEnvironmentId(token: InitializedToken): string;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module ckbox/utils
7
+ */
8
+ import type { InitializedToken } from '@ckeditor/ckeditor5-cloud-services';
9
+ /**
10
+ * Creates URLs for the image:
11
+ * - responsive URLs for the "webp" image format,
12
+ * - one fallback URL for browsers that do not support the "webp" format.
13
+ */
14
+ export declare function getImageUrls({ token, id, origin, width, extension }: {
15
+ token: InitializedToken;
16
+ id: string;
17
+ origin: string;
18
+ width: number;
19
+ extension: string;
20
+ }): {
21
+ imageFallbackUrl: string;
22
+ imageSources: Array<{
23
+ srcset: string;
24
+ sizes: string;
25
+ type: string;
26
+ }>;
27
+ };
28
+ /**
29
+ * Returns an environment id from a token used for communication with the CKBox service.
30
+ */
31
+ export declare function getEnvironmentId(token: InitializedToken): string;
package/src/utils.js CHANGED
@@ -1,90 +1,90 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- const IMAGE_BREAKPOINT_MAX_WIDTH = 4000;
6
- const IMAGE_BREAKPOINT_PIXELS_THRESHOLD = 80;
7
- const IMAGE_BREAKPOINT_PERCENTAGE_THRESHOLD = 10;
8
- /**
9
- * Creates URLs for the image:
10
- * - responsive URLs for the "webp" image format,
11
- * - one fallback URL for browsers that do not support the "webp" format.
12
- */
13
- export function getImageUrls({ token, id, origin, width, extension }) {
14
- const environmentId = getEnvironmentId(token);
15
- const imageBreakpoints = getImageBreakpoints(width);
16
- const imageFallbackExtension = getImageFallbackExtension(extension);
17
- const imageFallbackUrl = getResponsiveImageUrl({ environmentId, id, origin, width, extension: imageFallbackExtension });
18
- const imageResponsiveUrls = imageBreakpoints.map(imageBreakpoint => {
19
- const responsiveImageUrl = getResponsiveImageUrl({ environmentId, id, origin, width: imageBreakpoint, extension: 'webp' });
20
- return `${responsiveImageUrl} ${imageBreakpoint}w`;
21
- });
22
- // Create just one image source definition containing all calculated URLs for each image breakpoint. Additionally, limit this source
23
- // image width by defining two allowed slot sizes:
24
- // - If the viewport width is not greater than the image width, make the image occupy the whole slot.
25
- // - Otherwise, limit the slot width to be equal to the image width, to avoid enlarging the image beyond its width.
26
- //
27
- // This is a kind of a workaround. In a perfect world we could use `sizes="100vw" width="real image width"` on our single `<source>`
28
- // element, but at the time of writing this code the `width` attribute is not supported in the `<source>` element in Firefox yet.
29
- const imageSources = [{
30
- srcset: imageResponsiveUrls.join(','),
31
- sizes: `(max-width: ${width}px) 100vw, ${width}px`,
32
- type: 'image/webp'
33
- }];
34
- return {
35
- imageFallbackUrl,
36
- imageSources
37
- };
38
- }
39
- /**
40
- * Returns an environment id from a token used for communication with the CKBox service.
41
- */
42
- export function getEnvironmentId(token) {
43
- const [, binaryTokenPayload] = token.value.split('.');
44
- const payload = JSON.parse(atob(binaryTokenPayload));
45
- return payload.aud;
46
- }
47
- /**
48
- * Calculates the image breakpoints for the provided image width in the following way:
49
- *
50
- * 1) The breakpoint threshold (the breakpoint step in the calculations) should be equal to 10% of the image width, but not less than 80
51
- * pixels.
52
- *
53
- * 2) Set the max. allowed image breakpoint (4000px) or the image width (if it is smaller than 4000px) as the first calculated breakpoint.
54
- *
55
- * 3) From the last computed image breakpoint subtract the computed breakpoint threshold, as long as the calculated new breakpoint value is
56
- * greater than the threshold.
57
- */
58
- function getImageBreakpoints(width) {
59
- // Step 1) - calculating the breakpoint threshold.
60
- const imageBreakpointThresholds = [
61
- width * IMAGE_BREAKPOINT_PERCENTAGE_THRESHOLD / 100,
62
- IMAGE_BREAKPOINT_PIXELS_THRESHOLD
63
- ];
64
- const imageBreakpointThreshold = Math.floor(Math.max(...imageBreakpointThresholds));
65
- // Step 2) - set the first breakpoint.
66
- const imageBreakpoints = [Math.min(width, IMAGE_BREAKPOINT_MAX_WIDTH)];
67
- // Step 3) - calculate the next breakpoint as long as it is greater than the breakpoint threshold.
68
- let lastBreakpoint = imageBreakpoints[0];
69
- while (lastBreakpoint - imageBreakpointThreshold >= imageBreakpointThreshold) {
70
- lastBreakpoint -= imageBreakpointThreshold;
71
- imageBreakpoints.unshift(lastBreakpoint);
72
- }
73
- return imageBreakpoints;
74
- }
75
- /**
76
- * Returns the image extension for the fallback URL.
77
- */
78
- function getImageFallbackExtension(extension) {
79
- if (extension === 'bmp' || extension === 'tiff' || extension === 'jpg') {
80
- return 'jpeg';
81
- }
82
- return extension;
83
- }
84
- /**
85
- * Creates the URL for the given image.
86
- */
87
- function getResponsiveImageUrl({ environmentId, id, origin, width, extension }) {
88
- const endpoint = `${environmentId}/assets/${id}/images/${width}.${extension}`;
89
- return new URL(endpoint, origin).toString();
90
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ const IMAGE_BREAKPOINT_MAX_WIDTH = 4000;
6
+ const IMAGE_BREAKPOINT_PIXELS_THRESHOLD = 80;
7
+ const IMAGE_BREAKPOINT_PERCENTAGE_THRESHOLD = 10;
8
+ /**
9
+ * Creates URLs for the image:
10
+ * - responsive URLs for the "webp" image format,
11
+ * - one fallback URL for browsers that do not support the "webp" format.
12
+ */
13
+ export function getImageUrls({ token, id, origin, width, extension }) {
14
+ const environmentId = getEnvironmentId(token);
15
+ const imageBreakpoints = getImageBreakpoints(width);
16
+ const imageFallbackExtension = getImageFallbackExtension(extension);
17
+ const imageFallbackUrl = getResponsiveImageUrl({ environmentId, id, origin, width, extension: imageFallbackExtension });
18
+ const imageResponsiveUrls = imageBreakpoints.map(imageBreakpoint => {
19
+ const responsiveImageUrl = getResponsiveImageUrl({ environmentId, id, origin, width: imageBreakpoint, extension: 'webp' });
20
+ return `${responsiveImageUrl} ${imageBreakpoint}w`;
21
+ });
22
+ // Create just one image source definition containing all calculated URLs for each image breakpoint. Additionally, limit this source
23
+ // image width by defining two allowed slot sizes:
24
+ // - If the viewport width is not greater than the image width, make the image occupy the whole slot.
25
+ // - Otherwise, limit the slot width to be equal to the image width, to avoid enlarging the image beyond its width.
26
+ //
27
+ // This is a kind of a workaround. In a perfect world we could use `sizes="100vw" width="real image width"` on our single `<source>`
28
+ // element, but at the time of writing this code the `width` attribute is not supported in the `<source>` element in Firefox yet.
29
+ const imageSources = [{
30
+ srcset: imageResponsiveUrls.join(','),
31
+ sizes: `(max-width: ${width}px) 100vw, ${width}px`,
32
+ type: 'image/webp'
33
+ }];
34
+ return {
35
+ imageFallbackUrl,
36
+ imageSources
37
+ };
38
+ }
39
+ /**
40
+ * Returns an environment id from a token used for communication with the CKBox service.
41
+ */
42
+ export function getEnvironmentId(token) {
43
+ const [, binaryTokenPayload] = token.value.split('.');
44
+ const payload = JSON.parse(atob(binaryTokenPayload));
45
+ return payload.aud;
46
+ }
47
+ /**
48
+ * Calculates the image breakpoints for the provided image width in the following way:
49
+ *
50
+ * 1) The breakpoint threshold (the breakpoint step in the calculations) should be equal to 10% of the image width, but not less than 80
51
+ * pixels.
52
+ *
53
+ * 2) Set the max. allowed image breakpoint (4000px) or the image width (if it is smaller than 4000px) as the first calculated breakpoint.
54
+ *
55
+ * 3) From the last computed image breakpoint subtract the computed breakpoint threshold, as long as the calculated new breakpoint value is
56
+ * greater than the threshold.
57
+ */
58
+ function getImageBreakpoints(width) {
59
+ // Step 1) - calculating the breakpoint threshold.
60
+ const imageBreakpointThresholds = [
61
+ width * IMAGE_BREAKPOINT_PERCENTAGE_THRESHOLD / 100,
62
+ IMAGE_BREAKPOINT_PIXELS_THRESHOLD
63
+ ];
64
+ const imageBreakpointThreshold = Math.floor(Math.max(...imageBreakpointThresholds));
65
+ // Step 2) - set the first breakpoint.
66
+ const imageBreakpoints = [Math.min(width, IMAGE_BREAKPOINT_MAX_WIDTH)];
67
+ // Step 3) - calculate the next breakpoint as long as it is greater than the breakpoint threshold.
68
+ let lastBreakpoint = imageBreakpoints[0];
69
+ while (lastBreakpoint - imageBreakpointThreshold >= imageBreakpointThreshold) {
70
+ lastBreakpoint -= imageBreakpointThreshold;
71
+ imageBreakpoints.unshift(lastBreakpoint);
72
+ }
73
+ return imageBreakpoints;
74
+ }
75
+ /**
76
+ * Returns the image extension for the fallback URL.
77
+ */
78
+ function getImageFallbackExtension(extension) {
79
+ if (extension === 'bmp' || extension === 'tiff' || extension === 'jpg') {
80
+ return 'jpeg';
81
+ }
82
+ return extension;
83
+ }
84
+ /**
85
+ * Creates the URL for the given image.
86
+ */
87
+ function getResponsiveImageUrl({ environmentId, id, origin, width, extension }) {
88
+ const endpoint = `${environmentId}/assets/${id}/images/${width}.${extension}`;
89
+ return new URL(endpoint, origin).toString();
90
+ }