@ckeditor/ckeditor5-ckbox 40.1.0 → 41.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE.md +1 -1
- package/build/ckbox.js +3 -3
- package/build/translations/ar.js +1 -1
- package/build/translations/az.js +1 -1
- package/build/translations/bg.js +1 -1
- package/build/translations/bn.js +1 -1
- package/build/translations/ca.js +1 -1
- package/build/translations/cs.js +1 -1
- package/build/translations/da.js +1 -1
- package/build/translations/de.js +1 -1
- package/build/translations/el.js +1 -1
- package/build/translations/en-au.js +1 -1
- package/build/translations/es-co.js +1 -1
- package/build/translations/es.js +1 -1
- package/build/translations/et.js +1 -1
- package/build/translations/fa.js +1 -1
- package/build/translations/fi.js +1 -1
- package/build/translations/fr.js +1 -1
- package/build/translations/gl.js +1 -1
- package/build/translations/he.js +1 -1
- package/build/translations/hi.js +1 -1
- package/build/translations/hr.js +1 -1
- package/build/translations/hu.js +1 -1
- package/build/translations/id.js +1 -1
- package/build/translations/it.js +1 -1
- package/build/translations/ja.js +1 -1
- package/build/translations/ko.js +1 -1
- package/build/translations/lt.js +1 -1
- package/build/translations/lv.js +1 -1
- package/build/translations/ms.js +1 -1
- package/build/translations/nl.js +1 -1
- package/build/translations/no.js +1 -1
- package/build/translations/pl.js +1 -1
- package/build/translations/pt-br.js +1 -1
- package/build/translations/pt.js +1 -1
- package/build/translations/ro.js +1 -1
- package/build/translations/ru.js +1 -1
- package/build/translations/sk.js +1 -1
- package/build/translations/sq.js +1 -1
- package/build/translations/sr-latn.js +1 -1
- package/build/translations/sr.js +1 -1
- package/build/translations/sv.js +1 -1
- package/build/translations/th.js +1 -1
- package/build/translations/tr.js +1 -1
- package/build/translations/ug.js +1 -1
- package/build/translations/uk.js +1 -1
- package/build/translations/ur.js +1 -1
- package/build/translations/uz.js +1 -1
- package/build/translations/vi.js +1 -1
- package/build/translations/zh-cn.js +1 -1
- package/build/translations/zh.js +1 -1
- package/ckeditor5-metadata.json +18 -1
- package/lang/contexts.json +6 -2
- package/lang/translations/ar.po +19 -3
- package/lang/translations/az.po +19 -3
- package/lang/translations/bg.po +19 -3
- package/lang/translations/bn.po +19 -3
- package/lang/translations/ca.po +19 -3
- package/lang/translations/cs.po +19 -3
- package/lang/translations/da.po +19 -3
- package/lang/translations/de.po +19 -3
- package/lang/translations/el.po +19 -3
- package/lang/translations/en-au.po +19 -3
- package/lang/translations/en.po +19 -3
- package/lang/translations/es-co.po +19 -3
- package/lang/translations/es.po +19 -3
- package/lang/translations/et.po +19 -3
- package/lang/translations/fa.po +19 -3
- package/lang/translations/fi.po +19 -3
- package/lang/translations/fr.po +19 -3
- package/lang/translations/gl.po +19 -3
- package/lang/translations/he.po +19 -3
- package/lang/translations/hi.po +19 -3
- package/lang/translations/hr.po +19 -3
- package/lang/translations/hu.po +19 -3
- package/lang/translations/id.po +19 -3
- package/lang/translations/it.po +19 -3
- package/lang/translations/ja.po +19 -3
- package/lang/translations/ko.po +19 -3
- package/lang/translations/lt.po +19 -3
- package/lang/translations/lv.po +19 -3
- package/lang/translations/ms.po +19 -3
- package/lang/translations/nl.po +19 -3
- package/lang/translations/no.po +19 -3
- package/lang/translations/pl.po +19 -3
- package/lang/translations/pt-br.po +19 -3
- package/lang/translations/pt.po +19 -3
- package/lang/translations/ro.po +19 -3
- package/lang/translations/ru.po +19 -3
- package/lang/translations/sk.po +19 -3
- package/lang/translations/sq.po +19 -3
- package/lang/translations/sr-latn.po +19 -3
- package/lang/translations/sr.po +19 -3
- package/lang/translations/sv.po +19 -3
- package/lang/translations/th.po +19 -3
- package/lang/translations/tr.po +19 -3
- package/lang/translations/ug.po +19 -3
- package/lang/translations/uk.po +19 -3
- package/lang/translations/ur.po +19 -3
- package/lang/translations/uz.po +19 -3
- package/lang/translations/vi.po +19 -3
- package/lang/translations/zh-cn.po +19 -3
- package/lang/translations/zh.po +19 -3
- package/package.json +5 -3
- package/src/augmentation.d.ts +12 -2
- package/src/augmentation.js +1 -1
- package/src/ckbox.d.ts +4 -4
- package/src/ckbox.js +4 -4
- package/src/ckboxcommand.d.ts +10 -8
- package/src/ckboxcommand.js +9 -5
- package/src/ckboxconfig.d.ts +27 -1
- package/src/ckboxconfig.js +1 -1
- package/src/ckboxediting.d.ts +8 -15
- package/src/ckboxediting.js +12 -60
- package/src/ckboximageedit/ckboximageeditcommand.d.ts +97 -0
- package/src/ckboximageedit/ckboximageeditcommand.js +299 -0
- package/src/ckboximageedit/ckboximageeditediting.d.ts +28 -0
- package/src/ckboximageedit/ckboximageeditediting.js +36 -0
- package/src/ckboximageedit/ckboximageeditui.d.ts +24 -0
- package/src/ckboximageedit/ckboximageeditui.js +48 -0
- package/src/ckboximageedit/utils.d.ts +10 -0
- package/src/ckboximageedit/utils.js +48 -0
- package/src/ckboximageedit.d.ts +24 -0
- package/src/ckboximageedit.js +28 -0
- package/src/ckboxui.d.ts +2 -2
- package/src/ckboxui.js +31 -5
- package/src/ckboxuploadadapter.d.ts +4 -9
- package/src/ckboxuploadadapter.js +19 -164
- package/src/ckboxutils.d.ts +50 -0
- package/src/ckboxutils.js +183 -0
- package/src/index.d.ts +11 -7
- package/src/index.js +8 -5
- package/src/utils.d.ts +33 -2
- package/src/utils.js +94 -1
- package/theme/ckboximageedit.css +53 -0
- package/theme/icons/ckbox-image-edit.svg +1 -0
- package/theme/icons/browse-files.svg +0 -1
@@ -1,16 +1,16 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2024, 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
|
-
/* globals AbortController, FormData, URL,
|
5
|
+
/* globals AbortController, FormData, URL, window */
|
6
6
|
/**
|
7
7
|
* @module ckbox/ckboxuploadadapter
|
8
8
|
*/
|
9
|
-
import { Plugin } from 'ckeditor5/src/core';
|
10
|
-
import { FileRepository } from 'ckeditor5/src/upload';
|
11
|
-
import
|
12
|
-
import
|
13
|
-
import
|
9
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
10
|
+
import { FileRepository } from 'ckeditor5/src/upload.js';
|
11
|
+
import CKBoxEditing from './ckboxediting.js';
|
12
|
+
import { getImageUrls, sendHttpRequest } from './utils.js';
|
13
|
+
import CKBoxUtils from './ckboxutils.js';
|
14
14
|
/**
|
15
15
|
* A plugin that enables file uploads in CKEditor 5 using the CKBox server–side connector.
|
16
16
|
* See the {@glink features/file-management/ckbox CKBox file manager integration} guide to learn how to configure
|
@@ -47,10 +47,8 @@ export default class CKBoxUploadAdapter extends Plugin {
|
|
47
47
|
return;
|
48
48
|
}
|
49
49
|
const fileRepository = editor.plugins.get(FileRepository);
|
50
|
-
const
|
51
|
-
fileRepository.createUploadAdapter = loader =>
|
52
|
-
return new Adapter(loader, ckboxEditing.getToken(), editor);
|
53
|
-
};
|
50
|
+
const ckboxUtils = editor.plugins.get(CKBoxUtils);
|
51
|
+
fileRepository.createUploadAdapter = loader => new Adapter(loader, editor, ckboxUtils);
|
54
52
|
const shouldInsertDataId = !editor.config.get('ckbox.ignoreDataId');
|
55
53
|
const imageUploadEditing = editor.plugins.get('ImageUploadEditing');
|
56
54
|
// Mark uploaded assets with the `ckboxImageId` attribute. Its value represents an ID in CKBox.
|
@@ -70,114 +68,27 @@ class Adapter {
|
|
70
68
|
/**
|
71
69
|
* Creates a new adapter instance.
|
72
70
|
*/
|
73
|
-
constructor(loader,
|
71
|
+
constructor(loader, editor, ckboxUtils) {
|
74
72
|
this.loader = loader;
|
75
|
-
this.token =
|
73
|
+
this.token = ckboxUtils.getToken();
|
74
|
+
this.ckboxUtils = ckboxUtils;
|
76
75
|
this.editor = editor;
|
77
76
|
this.controller = new AbortController();
|
78
77
|
this.serviceOrigin = editor.config.get('ckbox.serviceOrigin');
|
79
78
|
}
|
80
|
-
/**
|
81
|
-
* The ID of workspace to use.
|
82
|
-
*/
|
83
|
-
getWorkspaceId() {
|
84
|
-
const t = this.editor.t;
|
85
|
-
const cannotAccessDefaultWorkspaceError = t('Cannot access default workspace.');
|
86
|
-
const defaultWorkspaceId = this.editor.config.get('ckbox.defaultUploadWorkspaceId');
|
87
|
-
const workspaceId = getWorkspaceId(this.token, defaultWorkspaceId);
|
88
|
-
if (workspaceId == null) {
|
89
|
-
/**
|
90
|
-
* The user is not authorized to access the workspace defined in the`ckbox.defaultUploadWorkspaceId` configuration.
|
91
|
-
*
|
92
|
-
* @error ckbox-access-default-workspace-error
|
93
|
-
*/
|
94
|
-
logError('ckbox-access-default-workspace-error');
|
95
|
-
throw cannotAccessDefaultWorkspaceError;
|
96
|
-
}
|
97
|
-
return workspaceId;
|
98
|
-
}
|
99
|
-
/**
|
100
|
-
* Resolves a promise with an array containing available categories with which the uploaded file can be associated.
|
101
|
-
*
|
102
|
-
* If the API returns limited results, the method will collect all items.
|
103
|
-
*/
|
104
|
-
async getAvailableCategories(offset = 0) {
|
105
|
-
const ITEMS_PER_REQUEST = 50;
|
106
|
-
const categoryUrl = new URL('categories', this.serviceOrigin);
|
107
|
-
categoryUrl.searchParams.set('limit', ITEMS_PER_REQUEST.toString());
|
108
|
-
categoryUrl.searchParams.set('offset', offset.toString());
|
109
|
-
categoryUrl.searchParams.set('workspaceId', this.getWorkspaceId());
|
110
|
-
return this._sendHttpRequest({ url: categoryUrl })
|
111
|
-
.then(async (data) => {
|
112
|
-
const remainingItems = data.totalCount - (offset + ITEMS_PER_REQUEST);
|
113
|
-
if (remainingItems > 0) {
|
114
|
-
const offsetItems = await this.getAvailableCategories(offset + ITEMS_PER_REQUEST);
|
115
|
-
return [
|
116
|
-
...data.items,
|
117
|
-
...offsetItems
|
118
|
-
];
|
119
|
-
}
|
120
|
-
return data.items;
|
121
|
-
})
|
122
|
-
.catch(() => {
|
123
|
-
this.controller.signal.throwIfAborted();
|
124
|
-
/**
|
125
|
-
* Fetching a list of available categories with which an uploaded file can be associated failed.
|
126
|
-
*
|
127
|
-
* @error ckbox-fetch-category-http-error
|
128
|
-
*/
|
129
|
-
logError('ckbox-fetch-category-http-error');
|
130
|
-
});
|
131
|
-
}
|
132
|
-
/**
|
133
|
-
* Resolves a promise with an object containing a category with which the uploaded file is associated or an error code.
|
134
|
-
*/
|
135
|
-
async getCategoryIdForFile(file) {
|
136
|
-
const extension = getFileExtension(file.name);
|
137
|
-
const allCategories = await this.getAvailableCategories();
|
138
|
-
// Couldn't fetch all categories. Perhaps the authorization token is invalid.
|
139
|
-
if (!allCategories) {
|
140
|
-
return null;
|
141
|
-
}
|
142
|
-
// The plugin allows defining to which category the uploaded file should be assigned.
|
143
|
-
const defaultCategories = this.editor.config.get('ckbox.defaultUploadCategories');
|
144
|
-
// If a user specifies the plugin configuration, find the first category that accepts the uploaded file.
|
145
|
-
if (defaultCategories) {
|
146
|
-
const userCategory = Object.keys(defaultCategories).find(category => {
|
147
|
-
return defaultCategories[category].find(e => e.toLowerCase() == extension);
|
148
|
-
});
|
149
|
-
// If found, return its ID if the category exists on the server side.
|
150
|
-
if (userCategory) {
|
151
|
-
const serverCategory = allCategories.find(category => category.id === userCategory || category.name === userCategory);
|
152
|
-
if (!serverCategory) {
|
153
|
-
return null;
|
154
|
-
}
|
155
|
-
return serverCategory.id;
|
156
|
-
}
|
157
|
-
}
|
158
|
-
// Otherwise, find the first category that accepts the uploaded file and returns its ID.
|
159
|
-
const category = allCategories.find(category => category.extensions.find(e => e.toLowerCase() == extension));
|
160
|
-
if (!category) {
|
161
|
-
return null;
|
162
|
-
}
|
163
|
-
return category.id;
|
164
|
-
}
|
165
79
|
/**
|
166
80
|
* Starts the upload process.
|
167
81
|
*
|
168
82
|
* @see module:upload/filerepository~UploadAdapter#upload
|
169
83
|
*/
|
170
84
|
async upload() {
|
85
|
+
const ckboxUtils = this.ckboxUtils;
|
171
86
|
const t = this.editor.t;
|
172
|
-
const cannotFindCategoryError = t('Cannot determine a category for the uploaded file.');
|
173
87
|
const file = (await this.loader.file);
|
174
|
-
const category = await
|
175
|
-
if (!category) {
|
176
|
-
return Promise.reject(cannotFindCategoryError);
|
177
|
-
}
|
88
|
+
const category = await ckboxUtils.getCategoryIdForFile(file, { signal: this.controller.signal });
|
178
89
|
const uploadUrl = new URL('assets', this.serviceOrigin);
|
179
90
|
const formData = new FormData();
|
180
|
-
uploadUrl.searchParams.set('workspaceId',
|
91
|
+
uploadUrl.searchParams.set('workspaceId', ckboxUtils.getWorkspaceId());
|
181
92
|
formData.append('categoryId', category);
|
182
93
|
formData.append('file', file);
|
183
94
|
const requestConfig = {
|
@@ -190,9 +101,11 @@ class Adapter {
|
|
190
101
|
this.loader.uploadTotal = evt.total;
|
191
102
|
this.loader.uploaded = evt.loaded;
|
192
103
|
}
|
193
|
-
}
|
104
|
+
},
|
105
|
+
signal: this.controller.signal,
|
106
|
+
authorization: this.token.value
|
194
107
|
};
|
195
|
-
return
|
108
|
+
return sendHttpRequest(requestConfig)
|
196
109
|
.then(async (data) => {
|
197
110
|
const imageUrls = getImageUrls(data.imageUrls);
|
198
111
|
return {
|
@@ -214,62 +127,4 @@ class Adapter {
|
|
214
127
|
abort() {
|
215
128
|
this.controller.abort();
|
216
129
|
}
|
217
|
-
/**
|
218
|
-
* Sends the HTTP request.
|
219
|
-
*
|
220
|
-
* @param config.url the URL where the request will be sent.
|
221
|
-
* @param config.method The HTTP method.
|
222
|
-
* @param config.data Additional data to send.
|
223
|
-
* @param config.onUploadProgress A callback informing about the upload progress.
|
224
|
-
*/
|
225
|
-
_sendHttpRequest({ url, method = 'GET', data, onUploadProgress }) {
|
226
|
-
const signal = this.controller.signal;
|
227
|
-
const xhr = new XMLHttpRequest();
|
228
|
-
xhr.open(method, url.toString(), true);
|
229
|
-
xhr.setRequestHeader('Authorization', this.token.value);
|
230
|
-
xhr.setRequestHeader('CKBox-Version', 'CKEditor 5');
|
231
|
-
xhr.responseType = 'json';
|
232
|
-
// The callback is attached to the `signal#abort` event.
|
233
|
-
const abortCallback = () => {
|
234
|
-
xhr.abort();
|
235
|
-
};
|
236
|
-
return new Promise((resolve, reject) => {
|
237
|
-
signal.addEventListener('abort', abortCallback);
|
238
|
-
xhr.addEventListener('loadstart', () => {
|
239
|
-
signal.addEventListener('abort', abortCallback);
|
240
|
-
});
|
241
|
-
xhr.addEventListener('loadend', () => {
|
242
|
-
signal.removeEventListener('abort', abortCallback);
|
243
|
-
});
|
244
|
-
xhr.addEventListener('error', () => {
|
245
|
-
reject();
|
246
|
-
});
|
247
|
-
xhr.addEventListener('abort', () => {
|
248
|
-
reject();
|
249
|
-
});
|
250
|
-
xhr.addEventListener('load', async () => {
|
251
|
-
const response = xhr.response;
|
252
|
-
if (!response || response.statusCode >= 400) {
|
253
|
-
return reject(response && response.message);
|
254
|
-
}
|
255
|
-
return resolve(response);
|
256
|
-
});
|
257
|
-
/* istanbul ignore else -- @preserve */
|
258
|
-
if (onUploadProgress) {
|
259
|
-
xhr.upload.addEventListener('progress', evt => {
|
260
|
-
onUploadProgress(evt);
|
261
|
-
});
|
262
|
-
}
|
263
|
-
// Send the request.
|
264
|
-
xhr.send(data);
|
265
|
-
});
|
266
|
-
}
|
267
|
-
}
|
268
|
-
/**
|
269
|
-
* Returns an extension from the given value.
|
270
|
-
*/
|
271
|
-
function getFileExtension(value) {
|
272
|
-
const extensionRegExp = /\.(?<ext>[^.]+)$/;
|
273
|
-
const match = value.match(extensionRegExp);
|
274
|
-
return match.groups.ext.toLowerCase();
|
275
130
|
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2024, 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/ckboxutils
|
7
|
+
*/
|
8
|
+
import type { InitializedToken } from '@ckeditor/ckeditor5-cloud-services';
|
9
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
10
|
+
/**
|
11
|
+
* The CKBox utilities plugin.
|
12
|
+
*/
|
13
|
+
export default class CKBoxUtils extends Plugin {
|
14
|
+
/**
|
15
|
+
* CKEditor Cloud Services access token.
|
16
|
+
*/
|
17
|
+
private _token;
|
18
|
+
/**
|
19
|
+
* @inheritDoc
|
20
|
+
*/
|
21
|
+
static get pluginName(): "CKBoxUtils";
|
22
|
+
/**
|
23
|
+
* @inheritDoc
|
24
|
+
*/
|
25
|
+
static get requires(): readonly ["CloudServices"];
|
26
|
+
/**
|
27
|
+
* @inheritDoc
|
28
|
+
*/
|
29
|
+
init(): Promise<void>;
|
30
|
+
/**
|
31
|
+
* Returns a token used by the CKBox plugin for communication with the CKBox service.
|
32
|
+
*/
|
33
|
+
getToken(): InitializedToken;
|
34
|
+
/**
|
35
|
+
* The ID of workspace to use when uploading an image.
|
36
|
+
*/
|
37
|
+
getWorkspaceId(): string;
|
38
|
+
/**
|
39
|
+
* Resolves a promise with an object containing a category with which the uploaded file is associated or an error code.
|
40
|
+
*/
|
41
|
+
getCategoryIdForFile(fileOrUrl: File | string, options: {
|
42
|
+
signal: AbortSignal;
|
43
|
+
}): Promise<string>;
|
44
|
+
/**
|
45
|
+
* Resolves a promise with an array containing available categories with which the uploaded file can be associated.
|
46
|
+
*
|
47
|
+
* If the API returns limited results, the method will collect all items.
|
48
|
+
*/
|
49
|
+
private _getAvailableCategories;
|
50
|
+
}
|
@@ -0,0 +1,183 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2024, 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
|
+
import { CKEditorError, logError } from 'ckeditor5/src/utils.js';
|
6
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
7
|
+
import { convertMimeTypeToExtension, getContentTypeOfUrl, getFileExtension, getWorkspaceId, sendHttpRequest } from './utils.js';
|
8
|
+
const DEFAULT_CKBOX_THEME_NAME = 'lark';
|
9
|
+
/**
|
10
|
+
* The CKBox utilities plugin.
|
11
|
+
*/
|
12
|
+
export default class CKBoxUtils extends Plugin {
|
13
|
+
/**
|
14
|
+
* @inheritDoc
|
15
|
+
*/
|
16
|
+
static get pluginName() {
|
17
|
+
return 'CKBoxUtils';
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* @inheritDoc
|
21
|
+
*/
|
22
|
+
static get requires() {
|
23
|
+
return ['CloudServices'];
|
24
|
+
}
|
25
|
+
/**
|
26
|
+
* @inheritDoc
|
27
|
+
*/
|
28
|
+
async init() {
|
29
|
+
const editor = this.editor;
|
30
|
+
const hasConfiguration = !!editor.config.get('ckbox');
|
31
|
+
const isLibraryLoaded = !!window.CKBox;
|
32
|
+
// Proceed with plugin initialization only when the integrator intentionally wants to use it, i.e. when the `config.ckbox` exists or
|
33
|
+
// the CKBox JavaScript library is loaded.
|
34
|
+
if (!hasConfiguration && !isLibraryLoaded) {
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
editor.config.define('ckbox', {
|
38
|
+
serviceOrigin: 'https://api.ckbox.io',
|
39
|
+
defaultUploadCategories: null,
|
40
|
+
ignoreDataId: false,
|
41
|
+
language: editor.locale.uiLanguage,
|
42
|
+
theme: DEFAULT_CKBOX_THEME_NAME,
|
43
|
+
tokenUrl: editor.config.get('cloudServices.tokenUrl')
|
44
|
+
});
|
45
|
+
const cloudServices = editor.plugins.get('CloudServices');
|
46
|
+
const cloudServicesTokenUrl = editor.config.get('cloudServices.tokenUrl');
|
47
|
+
const ckboxTokenUrl = editor.config.get('ckbox.tokenUrl');
|
48
|
+
if (!ckboxTokenUrl) {
|
49
|
+
/**
|
50
|
+
* The {@link module:ckbox/ckboxconfig~CKBoxConfig#tokenUrl `config.ckbox.tokenUrl`} or the
|
51
|
+
* {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl `config.cloudServices.tokenUrl`}
|
52
|
+
* configuration is required for the CKBox plugin.
|
53
|
+
*
|
54
|
+
* ```ts
|
55
|
+
* ClassicEditor.create( document.createElement( 'div' ), {
|
56
|
+
* ckbox: {
|
57
|
+
* tokenUrl: "YOUR_TOKEN_URL"
|
58
|
+
* // ...
|
59
|
+
* }
|
60
|
+
* // ...
|
61
|
+
* } );
|
62
|
+
* ```
|
63
|
+
*
|
64
|
+
* @error ckbox-plugin-missing-token-url
|
65
|
+
*/
|
66
|
+
throw new CKEditorError('ckbox-plugin-missing-token-url', this);
|
67
|
+
}
|
68
|
+
if (ckboxTokenUrl == cloudServicesTokenUrl) {
|
69
|
+
this._token = cloudServices.token;
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
this._token = await cloudServices.registerTokenUrl(ckboxTokenUrl);
|
73
|
+
}
|
74
|
+
}
|
75
|
+
/**
|
76
|
+
* Returns a token used by the CKBox plugin for communication with the CKBox service.
|
77
|
+
*/
|
78
|
+
getToken() {
|
79
|
+
return this._token;
|
80
|
+
}
|
81
|
+
/**
|
82
|
+
* The ID of workspace to use when uploading an image.
|
83
|
+
*/
|
84
|
+
getWorkspaceId() {
|
85
|
+
const t = this.editor.t;
|
86
|
+
const cannotAccessDefaultWorkspaceError = t('Cannot access default workspace.');
|
87
|
+
const defaultWorkspaceId = this.editor.config.get('ckbox.defaultUploadWorkspaceId');
|
88
|
+
const workspaceId = getWorkspaceId(this._token, defaultWorkspaceId);
|
89
|
+
if (workspaceId == null) {
|
90
|
+
/**
|
91
|
+
* The user is not authorized to access the workspace defined in the`ckbox.defaultUploadWorkspaceId` configuration.
|
92
|
+
*
|
93
|
+
* @error ckbox-access-default-workspace-error
|
94
|
+
*/
|
95
|
+
logError('ckbox-access-default-workspace-error');
|
96
|
+
throw cannotAccessDefaultWorkspaceError;
|
97
|
+
}
|
98
|
+
return workspaceId;
|
99
|
+
}
|
100
|
+
/**
|
101
|
+
* Resolves a promise with an object containing a category with which the uploaded file is associated or an error code.
|
102
|
+
*/
|
103
|
+
async getCategoryIdForFile(fileOrUrl, options) {
|
104
|
+
const t = this.editor.t;
|
105
|
+
const cannotFindCategoryError = t('Cannot determine a category for the uploaded file.');
|
106
|
+
const defaultCategories = this.editor.config.get('ckbox.defaultUploadCategories');
|
107
|
+
const allCategoriesPromise = this._getAvailableCategories(options);
|
108
|
+
const extension = typeof fileOrUrl == 'string' ?
|
109
|
+
convertMimeTypeToExtension(await getContentTypeOfUrl(fileOrUrl, options)) :
|
110
|
+
getFileExtension(fileOrUrl);
|
111
|
+
const allCategories = await allCategoriesPromise;
|
112
|
+
// Couldn't fetch all categories. Perhaps the authorization token is invalid.
|
113
|
+
if (!allCategories) {
|
114
|
+
throw cannotFindCategoryError;
|
115
|
+
}
|
116
|
+
// If a user specifies the plugin configuration, find the first category that accepts the uploaded file.
|
117
|
+
if (defaultCategories) {
|
118
|
+
const userCategory = Object.keys(defaultCategories).find(category => {
|
119
|
+
return defaultCategories[category].find(e => e.toLowerCase() == extension);
|
120
|
+
});
|
121
|
+
// If found, return its ID if the category exists on the server side.
|
122
|
+
if (userCategory) {
|
123
|
+
const serverCategory = allCategories.find(category => category.id === userCategory || category.name === userCategory);
|
124
|
+
if (!serverCategory) {
|
125
|
+
throw cannotFindCategoryError;
|
126
|
+
}
|
127
|
+
return serverCategory.id;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
// Otherwise, find the first category that accepts the uploaded file and returns its ID.
|
131
|
+
const category = allCategories.find(category => category.extensions.find(e => e.toLowerCase() == extension));
|
132
|
+
if (!category) {
|
133
|
+
throw cannotFindCategoryError;
|
134
|
+
}
|
135
|
+
return category.id;
|
136
|
+
}
|
137
|
+
/**
|
138
|
+
* Resolves a promise with an array containing available categories with which the uploaded file can be associated.
|
139
|
+
*
|
140
|
+
* If the API returns limited results, the method will collect all items.
|
141
|
+
*/
|
142
|
+
async _getAvailableCategories(options) {
|
143
|
+
const ITEMS_PER_REQUEST = 50;
|
144
|
+
const editor = this.editor;
|
145
|
+
const token = this._token;
|
146
|
+
const { signal } = options;
|
147
|
+
const serviceOrigin = editor.config.get('ckbox.serviceOrigin');
|
148
|
+
const workspaceId = this.getWorkspaceId();
|
149
|
+
try {
|
150
|
+
const result = [];
|
151
|
+
let offset = 0;
|
152
|
+
let remainingItems;
|
153
|
+
do {
|
154
|
+
const data = await fetchCategories(offset);
|
155
|
+
result.push(...data.items);
|
156
|
+
remainingItems = data.totalCount - (offset + ITEMS_PER_REQUEST);
|
157
|
+
offset += ITEMS_PER_REQUEST;
|
158
|
+
} while (remainingItems > 0);
|
159
|
+
return result;
|
160
|
+
}
|
161
|
+
catch {
|
162
|
+
signal.throwIfAborted();
|
163
|
+
/**
|
164
|
+
* Fetching a list of available categories with which an uploaded file can be associated failed.
|
165
|
+
*
|
166
|
+
* @error ckbox-fetch-category-http-error
|
167
|
+
*/
|
168
|
+
logError('ckbox-fetch-category-http-error');
|
169
|
+
return undefined;
|
170
|
+
}
|
171
|
+
function fetchCategories(offset) {
|
172
|
+
const categoryUrl = new URL('categories', serviceOrigin);
|
173
|
+
categoryUrl.searchParams.set('limit', ITEMS_PER_REQUEST.toString());
|
174
|
+
categoryUrl.searchParams.set('offset', offset.toString());
|
175
|
+
categoryUrl.searchParams.set('workspaceId', workspaceId);
|
176
|
+
return sendHttpRequest({
|
177
|
+
url: categoryUrl,
|
178
|
+
signal,
|
179
|
+
authorization: token.value
|
180
|
+
});
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
package/src/index.d.ts
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2024, 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
5
|
/**
|
6
6
|
* @module ckbox
|
7
7
|
*/
|
8
|
-
export { default as CKBox } from './ckbox';
|
9
|
-
export { default as CKBoxEditing } from './ckboxediting';
|
10
|
-
export { default as CKBoxUI } from './ckboxui';
|
11
|
-
export
|
12
|
-
export
|
13
|
-
|
8
|
+
export { default as CKBox } from './ckbox.js';
|
9
|
+
export { default as CKBoxEditing } from './ckboxediting.js';
|
10
|
+
export { default as CKBoxUI } from './ckboxui.js';
|
11
|
+
export { default as CKBoxImageEditEditing } from './ckboximageedit/ckboximageeditediting.js';
|
12
|
+
export { default as CKBoxImageEditUI } from './ckboximageedit/ckboximageeditui.js';
|
13
|
+
export { default as CKBoxImageEdit } from './ckboximageedit.js';
|
14
|
+
export type { default as CKBoxCommand } from './ckboxcommand.js';
|
15
|
+
export type { default as CKBoxImageEditCommand } from './ckboximageedit/ckboximageeditcommand.js';
|
16
|
+
export type { CKBoxConfig } from './ckboxconfig.js';
|
17
|
+
import './augmentation.js';
|
package/src/index.js
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2024, 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
5
|
/**
|
6
6
|
* @module ckbox
|
7
7
|
*/
|
8
|
-
export { default as CKBox } from './ckbox';
|
9
|
-
export { default as CKBoxEditing } from './ckboxediting';
|
10
|
-
export { default as CKBoxUI } from './ckboxui';
|
11
|
-
|
8
|
+
export { default as CKBox } from './ckbox.js';
|
9
|
+
export { default as CKBoxEditing } from './ckboxediting.js';
|
10
|
+
export { default as CKBoxUI } from './ckboxui.js';
|
11
|
+
export { default as CKBoxImageEditEditing } from './ckboximageedit/ckboximageeditediting.js';
|
12
|
+
export { default as CKBoxImageEditUI } from './ckboximageedit/ckboximageeditui.js';
|
13
|
+
export { default as CKBoxImageEdit } from './ckboximageedit.js';
|
14
|
+
import './augmentation.js';
|
package/src/utils.d.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2024, 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
5
|
/**
|
6
6
|
* @module ckbox/utils
|
7
7
|
*/
|
8
8
|
import type { InitializedToken } from '@ckeditor/ckeditor5-cloud-services';
|
9
|
-
import type { CKBoxImageUrls } from './ckboxconfig';
|
9
|
+
import type { CKBoxImageUrls } from './ckboxconfig.js';
|
10
10
|
/**
|
11
11
|
* Converts image source set provided by the CKBox into an object containing:
|
12
12
|
* - responsive URLs for the "webp" image format,
|
@@ -30,3 +30,34 @@ export declare function getWorkspaceId(token: InitializedToken, defaultWorkspace
|
|
30
30
|
* Generates an image data URL from its `blurhash` representation.
|
31
31
|
*/
|
32
32
|
export declare function blurHashToDataUrl(hash?: string): string | undefined;
|
33
|
+
/**
|
34
|
+
* Sends the HTTP request.
|
35
|
+
*
|
36
|
+
* @internal
|
37
|
+
* @param config.url the URL where the request will be sent.
|
38
|
+
* @param config.method The HTTP method.
|
39
|
+
* @param config.data Additional data to send.
|
40
|
+
* @param config.onUploadProgress A callback informing about the upload progress.
|
41
|
+
*/
|
42
|
+
export declare function sendHttpRequest({ url, method, data, onUploadProgress, signal, authorization }: {
|
43
|
+
url: URL;
|
44
|
+
signal: AbortSignal;
|
45
|
+
authorization: string;
|
46
|
+
method?: 'GET' | 'POST';
|
47
|
+
data?: FormData | null;
|
48
|
+
onUploadProgress?: (evt: ProgressEvent) => void;
|
49
|
+
}): Promise<any>;
|
50
|
+
/**
|
51
|
+
* Returns an extension a typical file in the specified `mimeType` format would have.
|
52
|
+
*/
|
53
|
+
export declare function convertMimeTypeToExtension(mimeType: string): string;
|
54
|
+
/**
|
55
|
+
* Tries to fetch the given `url` and returns 'content-type' of the response.
|
56
|
+
*/
|
57
|
+
export declare function getContentTypeOfUrl(url: string, options: {
|
58
|
+
signal: AbortSignal;
|
59
|
+
}): Promise<string>;
|
60
|
+
/**
|
61
|
+
* Returns an extension from the given value.
|
62
|
+
*/
|
63
|
+
export declare function getFileExtension(file: File): string;
|
package/src/utils.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2024, 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
5
|
import { decode } from 'blurhash';
|
@@ -80,3 +80,96 @@ export function blurHashToDataUrl(hash) {
|
|
80
80
|
return undefined;
|
81
81
|
}
|
82
82
|
}
|
83
|
+
/**
|
84
|
+
* Sends the HTTP request.
|
85
|
+
*
|
86
|
+
* @internal
|
87
|
+
* @param config.url the URL where the request will be sent.
|
88
|
+
* @param config.method The HTTP method.
|
89
|
+
* @param config.data Additional data to send.
|
90
|
+
* @param config.onUploadProgress A callback informing about the upload progress.
|
91
|
+
*/
|
92
|
+
export function sendHttpRequest({ url, method = 'GET', data, onUploadProgress, signal, authorization }) {
|
93
|
+
const xhr = new XMLHttpRequest();
|
94
|
+
xhr.open(method, url.toString());
|
95
|
+
xhr.setRequestHeader('Authorization', authorization);
|
96
|
+
xhr.setRequestHeader('CKBox-Version', 'CKEditor 5');
|
97
|
+
xhr.responseType = 'json';
|
98
|
+
// The callback is attached to the `signal#abort` event.
|
99
|
+
const abortCallback = () => {
|
100
|
+
xhr.abort();
|
101
|
+
};
|
102
|
+
return new Promise((resolve, reject) => {
|
103
|
+
signal.throwIfAborted();
|
104
|
+
signal.addEventListener('abort', abortCallback);
|
105
|
+
xhr.addEventListener('loadstart', () => {
|
106
|
+
signal.addEventListener('abort', abortCallback);
|
107
|
+
});
|
108
|
+
xhr.addEventListener('loadend', () => {
|
109
|
+
signal.removeEventListener('abort', abortCallback);
|
110
|
+
});
|
111
|
+
xhr.addEventListener('error', () => {
|
112
|
+
reject();
|
113
|
+
});
|
114
|
+
xhr.addEventListener('abort', () => {
|
115
|
+
reject();
|
116
|
+
});
|
117
|
+
xhr.addEventListener('load', () => {
|
118
|
+
const response = xhr.response;
|
119
|
+
if (!response || response.statusCode >= 400) {
|
120
|
+
return reject(response && response.message);
|
121
|
+
}
|
122
|
+
resolve(response);
|
123
|
+
});
|
124
|
+
/* istanbul ignore else -- @preserve */
|
125
|
+
if (onUploadProgress) {
|
126
|
+
xhr.upload.addEventListener('progress', evt => {
|
127
|
+
onUploadProgress(evt);
|
128
|
+
});
|
129
|
+
}
|
130
|
+
// Send the request.
|
131
|
+
xhr.send(data);
|
132
|
+
});
|
133
|
+
}
|
134
|
+
const MIME_TO_EXTENSION = {
|
135
|
+
'image/gif': 'gif',
|
136
|
+
'image/jpeg': 'jpg',
|
137
|
+
'image/png': 'png',
|
138
|
+
'image/webp': 'webp',
|
139
|
+
'image/bmp': 'bmp',
|
140
|
+
'image/tiff': 'tiff'
|
141
|
+
};
|
142
|
+
/**
|
143
|
+
* Returns an extension a typical file in the specified `mimeType` format would have.
|
144
|
+
*/
|
145
|
+
export function convertMimeTypeToExtension(mimeType) {
|
146
|
+
return MIME_TO_EXTENSION[mimeType];
|
147
|
+
}
|
148
|
+
/**
|
149
|
+
* Tries to fetch the given `url` and returns 'content-type' of the response.
|
150
|
+
*/
|
151
|
+
export async function getContentTypeOfUrl(url, options) {
|
152
|
+
try {
|
153
|
+
const response = await fetch(url, {
|
154
|
+
method: 'HEAD',
|
155
|
+
cache: 'force-cache',
|
156
|
+
...options
|
157
|
+
});
|
158
|
+
if (!response.ok) {
|
159
|
+
return '';
|
160
|
+
}
|
161
|
+
return response.headers.get('content-type') || '';
|
162
|
+
}
|
163
|
+
catch {
|
164
|
+
return '';
|
165
|
+
}
|
166
|
+
}
|
167
|
+
/**
|
168
|
+
* Returns an extension from the given value.
|
169
|
+
*/
|
170
|
+
export function getFileExtension(file) {
|
171
|
+
const fileName = file.name;
|
172
|
+
const extensionRegExp = /\.(?<ext>[^.]+)$/;
|
173
|
+
const match = fileName.match(extensionRegExp);
|
174
|
+
return match.groups.ext.toLowerCase();
|
175
|
+
}
|