@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/build/ckbox.js +1 -1
- package/ckeditor5-metadata.json +1 -1
- package/package.json +24 -19
- package/src/ckbox.d.ts +36 -0
- package/src/ckbox.js +12 -218
- package/src/ckboxcommand.d.ts +114 -0
- package/src/ckboxcommand.js +278 -371
- package/src/ckboxconfig.d.ts +282 -0
- package/src/ckboxconfig.js +5 -0
- package/src/ckboxediting.d.ts +56 -0
- package/src/ckboxediting.js +343 -446
- package/src/ckboxui.d.ts +21 -0
- package/src/ckboxui.js +32 -47
- package/src/ckboxuploadadapter.d.ts +37 -0
- package/src/ckboxuploadadapter.js +248 -365
- package/src/index.d.ts +10 -0
- package/src/index.js +0 -2
- package/src/utils.d.ts +31 -0
- package/src/utils.js +70 -114
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(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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(
|
66
|
-
|
67
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
//
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
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
|
}
|