@ckeditor/ckeditor5-ckbox 36.0.0 → 37.0.0-alpha.0

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