@ckeditor/ckeditor5-ckbox 36.0.0 → 37.0.0-alpha.0
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.
- 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/ckboxcommand.js
CHANGED
@@ -2,22 +2,13 @@
|
|
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 document, window, setTimeout, URL */
|
7
|
-
|
8
|
-
/**
|
9
|
-
* @module ckbox/ckboxcommand
|
10
|
-
*/
|
11
|
-
|
12
5
|
import { Command } from 'ckeditor5/src/core';
|
13
6
|
import { createElement, toMap } from 'ckeditor5/src/utils';
|
14
7
|
import { getEnvironmentId, getImageUrls } from './utils';
|
15
|
-
|
16
8
|
// Defines the waiting time (in milliseconds) for inserting the chosen asset into the model. The chosen asset is temporarily stored in the
|
17
9
|
// `CKBoxCommand#_chosenAssets` and it is removed from there automatically after this time. See `CKBoxCommand#_chosenAssets` for more
|
18
10
|
// details.
|
19
11
|
const ASSET_INSERTION_WAIT_TIMEOUT = 1000;
|
20
|
-
|
21
12
|
/**
|
22
13
|
* The CKBox command. It is used by the {@link module:ckbox/ckboxediting~CKBoxEditing CKBox editing feature} to open the CKBox file manager.
|
23
14
|
* The file manager allows inserting an image or a link to a file into the editor content.
|
@@ -29,378 +20,294 @@ const ASSET_INSERTION_WAIT_TIMEOUT = 1000;
|
|
29
20
|
* {@link module:image/image~Image Image feature}.
|
30
21
|
* - To insert links to other files it uses the {@link module:link/linkcommand~LinkCommand 'link'} command from the
|
31
22
|
* {@link module:link/link~Link Link feature}.
|
32
|
-
*
|
33
|
-
* @extends module:core/command~Command
|
34
23
|
*/
|
35
24
|
export default class CKBoxCommand extends Command {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
// Except for the last chosen asset, move the selection to the end of the current range to avoid overwriting other, already
|
257
|
-
// inserted assets.
|
258
|
-
if ( !isLastAsset ) {
|
259
|
-
writer.setSelection( selection.getLastPosition() );
|
260
|
-
}
|
261
|
-
}
|
262
|
-
|
263
|
-
/**
|
264
|
-
* Inserts the image by calling the `insertImage` command.
|
265
|
-
*
|
266
|
-
* @protected
|
267
|
-
* @param {module:ckbox/ckbox~CKBoxAssetDefinition} asset The asset to be inserted.
|
268
|
-
*/
|
269
|
-
_insertImage( asset ) {
|
270
|
-
const editor = this.editor;
|
271
|
-
const { imageFallbackUrl, imageSources, imageTextAlternative } = asset.attributes;
|
272
|
-
|
273
|
-
editor.execute( 'insertImage', {
|
274
|
-
source: {
|
275
|
-
src: imageFallbackUrl,
|
276
|
-
sources: imageSources,
|
277
|
-
alt: imageTextAlternative
|
278
|
-
}
|
279
|
-
} );
|
280
|
-
}
|
281
|
-
|
282
|
-
/**
|
283
|
-
* Inserts the link to the asset by calling the `link` command.
|
284
|
-
*
|
285
|
-
* @protected
|
286
|
-
* @param {module:ckbox/ckbox~CKBoxAssetDefinition} asset The asset to be inserted.
|
287
|
-
* @param {module:engine/model/writer~Writer} writer An instance of the model writer.
|
288
|
-
*/
|
289
|
-
_insertLink( asset, writer ) {
|
290
|
-
const editor = this.editor;
|
291
|
-
const model = editor.model;
|
292
|
-
const selection = model.document.selection;
|
293
|
-
const { linkName, linkHref } = asset.attributes;
|
294
|
-
|
295
|
-
// If the selection is collapsed, insert the asset name as the link label and select it.
|
296
|
-
if ( selection.isCollapsed ) {
|
297
|
-
const selectionAttributes = toMap( selection.getAttributes() );
|
298
|
-
const textNode = writer.createText( linkName, selectionAttributes );
|
299
|
-
const range = model.insertContent( textNode );
|
300
|
-
|
301
|
-
writer.setSelection( range );
|
302
|
-
}
|
303
|
-
|
304
|
-
editor.execute( 'link', linkHref );
|
305
|
-
}
|
306
|
-
}
|
307
|
-
|
308
|
-
// Parses the chosen assets into the internal data format. Filters out chosen assets that are not allowed.
|
309
|
-
//
|
310
|
-
// @private
|
311
|
-
// @param {Object} data
|
312
|
-
// @param {Array.<module:ckbox/ckbox~CKBoxRawAssetDefinition>} data.assets
|
313
|
-
// @param {String} data.origin The base URL for assets inserted into the editor.
|
314
|
-
// @param {module:cloud-services/token~Token} data.token
|
315
|
-
// @param {Boolean} data.isImageAllowed
|
316
|
-
// @param {Boolean} data.isLinkAllowed
|
317
|
-
// @returns {Array.<module:ckbox/ckbox~CKBoxAssetDefinition>}
|
318
|
-
function prepareAssets( { assets, origin, token, isImageAllowed, isLinkAllowed } ) {
|
319
|
-
return assets
|
320
|
-
.map( asset => ( {
|
321
|
-
id: asset.data.id,
|
322
|
-
type: isImage( asset ) ? 'image' : 'link',
|
323
|
-
attributes: prepareAssetAttributes( asset, token, origin )
|
324
|
-
} ) )
|
325
|
-
.filter( asset => asset.type === 'image' ? isImageAllowed : isLinkAllowed );
|
25
|
+
/**
|
26
|
+
* @inheritDoc
|
27
|
+
*/
|
28
|
+
constructor(editor) {
|
29
|
+
super(editor);
|
30
|
+
/**
|
31
|
+
* A set of all chosen assets. They are stored temporarily and they are automatically removed 1 second after being chosen.
|
32
|
+
* Chosen assets have to be "remembered" for a while to be able to map the given asset with the element inserted into the model.
|
33
|
+
* This association map is then used to set the ID on the model element.
|
34
|
+
*
|
35
|
+
* All chosen assets are automatically removed after the timeout, because (theoretically) it may happen that they will never be
|
36
|
+
* inserted into the model, even if the {@link module:link/linkcommand~LinkCommand `'link'`} command or the
|
37
|
+
* {@link module:image/image/insertimagecommand~InsertImageCommand `'insertImage'`} command is enabled. Such a case may arise when
|
38
|
+
* another plugin blocks the command execution. Then, in order not to keep the chosen (but not inserted) assets forever, we delete
|
39
|
+
* them automatically to prevent memory leakage. The 1 second timeout is enough to insert the asset into the model and extract the
|
40
|
+
* ID from the chosen asset.
|
41
|
+
*
|
42
|
+
* The assets are stored only if
|
43
|
+
* the {@link module:ckbox/ckboxconfig~CKBoxConfig#ignoreDataId `config.ckbox.ignoreDataId`} option is set to `false` (by default).
|
44
|
+
*
|
45
|
+
* @internal
|
46
|
+
*/
|
47
|
+
this._chosenAssets = new Set();
|
48
|
+
/**
|
49
|
+
* The DOM element that acts as a mounting point for the CKBox dialog.
|
50
|
+
*/
|
51
|
+
this._wrapper = null;
|
52
|
+
this._initListeners();
|
53
|
+
}
|
54
|
+
/**
|
55
|
+
* @inheritDoc
|
56
|
+
*/
|
57
|
+
refresh() {
|
58
|
+
this.value = this._getValue();
|
59
|
+
this.isEnabled = this._checkEnabled();
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* @inheritDoc
|
63
|
+
*/
|
64
|
+
execute() {
|
65
|
+
this.fire('ckbox:open');
|
66
|
+
}
|
67
|
+
/**
|
68
|
+
* Indicates if the CKBox dialog is already opened.
|
69
|
+
*
|
70
|
+
* @protected
|
71
|
+
* @returns {Boolean}
|
72
|
+
*/
|
73
|
+
_getValue() {
|
74
|
+
return this._wrapper !== null;
|
75
|
+
}
|
76
|
+
/**
|
77
|
+
* Checks whether the command can be enabled in the current context.
|
78
|
+
*/
|
79
|
+
_checkEnabled() {
|
80
|
+
const imageCommand = this.editor.commands.get('insertImage');
|
81
|
+
const linkCommand = this.editor.commands.get('link');
|
82
|
+
if (!imageCommand.isEnabled && !linkCommand.isEnabled) {
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
return true;
|
86
|
+
}
|
87
|
+
/**
|
88
|
+
* Creates the options object for the CKBox dialog.
|
89
|
+
*
|
90
|
+
* @returns The object with properties:
|
91
|
+
* - theme The theme for CKBox dialog.
|
92
|
+
* - language The language for CKBox dialog.
|
93
|
+
* - tokenUrl The token endpoint URL.
|
94
|
+
* - serviceOrigin The base URL of the API service.
|
95
|
+
* - assetsOrigin The base URL for assets inserted into the editor.
|
96
|
+
* - dialog.onClose The callback function invoked after closing the CKBox dialog.
|
97
|
+
* - assets.onChoose The callback function invoked after choosing the assets.
|
98
|
+
*/
|
99
|
+
_prepareOptions() {
|
100
|
+
const editor = this.editor;
|
101
|
+
const ckboxConfig = editor.config.get('ckbox');
|
102
|
+
return {
|
103
|
+
theme: ckboxConfig.theme,
|
104
|
+
language: ckboxConfig.language,
|
105
|
+
tokenUrl: ckboxConfig.tokenUrl,
|
106
|
+
serviceOrigin: ckboxConfig.serviceOrigin,
|
107
|
+
assetsOrigin: ckboxConfig.assetsOrigin,
|
108
|
+
dialog: {
|
109
|
+
onClose: () => this.fire('ckbox:close')
|
110
|
+
},
|
111
|
+
assets: {
|
112
|
+
onChoose: (assets) => this.fire('ckbox:choose', assets)
|
113
|
+
}
|
114
|
+
};
|
115
|
+
}
|
116
|
+
/**
|
117
|
+
* Initializes various event listeners for the `ckbox:*` events, because all functionality of the `ckbox` command is event-based.
|
118
|
+
*/
|
119
|
+
_initListeners() {
|
120
|
+
const editor = this.editor;
|
121
|
+
const model = editor.model;
|
122
|
+
const shouldInsertDataId = !editor.config.get('ckbox.ignoreDataId');
|
123
|
+
// Refresh the command after firing the `ckbox:*` event.
|
124
|
+
this.on('ckbox', () => {
|
125
|
+
this.refresh();
|
126
|
+
}, { priority: 'low' });
|
127
|
+
// Handle opening of the CKBox dialog.
|
128
|
+
this.on('ckbox:open', () => {
|
129
|
+
if (!this.isEnabled || this.value) {
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
this._wrapper = createElement(document, 'div', { class: 'ck ckbox-wrapper' });
|
133
|
+
document.body.appendChild(this._wrapper);
|
134
|
+
window.CKBox.mount(this._wrapper, this._prepareOptions());
|
135
|
+
});
|
136
|
+
// Handle closing of the CKBox dialog.
|
137
|
+
this.on('ckbox:close', () => {
|
138
|
+
if (!this.value) {
|
139
|
+
return;
|
140
|
+
}
|
141
|
+
this._wrapper.remove();
|
142
|
+
this._wrapper = null;
|
143
|
+
});
|
144
|
+
// Handle choosing the assets.
|
145
|
+
this.on('ckbox:choose', (evt, assets) => {
|
146
|
+
if (!this.isEnabled) {
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
const imageCommand = editor.commands.get('insertImage');
|
150
|
+
const linkCommand = editor.commands.get('link');
|
151
|
+
const ckboxEditing = editor.plugins.get('CKBoxEditing');
|
152
|
+
const assetsOrigin = editor.config.get('ckbox.assetsOrigin');
|
153
|
+
const assetsToProcess = prepareAssets({
|
154
|
+
assets,
|
155
|
+
origin: assetsOrigin,
|
156
|
+
token: ckboxEditing.getToken(),
|
157
|
+
isImageAllowed: imageCommand.isEnabled,
|
158
|
+
isLinkAllowed: linkCommand.isEnabled
|
159
|
+
});
|
160
|
+
if (assetsToProcess.length === 0) {
|
161
|
+
return;
|
162
|
+
}
|
163
|
+
// All assets are inserted in one undo step.
|
164
|
+
model.change(writer => {
|
165
|
+
for (const asset of assetsToProcess) {
|
166
|
+
const isLastAsset = asset === assetsToProcess[assetsToProcess.length - 1];
|
167
|
+
this._insertAsset(asset, isLastAsset, writer);
|
168
|
+
// If asset ID must be set for the inserted model element, store the asset temporarily and remove it automatically
|
169
|
+
// after the timeout.
|
170
|
+
if (shouldInsertDataId) {
|
171
|
+
setTimeout(() => this._chosenAssets.delete(asset), ASSET_INSERTION_WAIT_TIMEOUT);
|
172
|
+
this._chosenAssets.add(asset);
|
173
|
+
}
|
174
|
+
}
|
175
|
+
});
|
176
|
+
});
|
177
|
+
// Clean up after the editor is destroyed.
|
178
|
+
this.listenTo(editor, 'destroy', () => {
|
179
|
+
this.fire('ckbox:close');
|
180
|
+
this._chosenAssets.clear();
|
181
|
+
});
|
182
|
+
}
|
183
|
+
/**
|
184
|
+
* Inserts the asset into the model.
|
185
|
+
*
|
186
|
+
* @param asset The asset to be inserted.
|
187
|
+
* @param isLastAsset Indicates if the current asset is the last one from the chosen set.
|
188
|
+
* @param writer An instance of the model writer.
|
189
|
+
*/
|
190
|
+
_insertAsset(asset, isLastAsset, writer) {
|
191
|
+
const editor = this.editor;
|
192
|
+
const model = editor.model;
|
193
|
+
const selection = model.document.selection;
|
194
|
+
// Remove the `linkHref` attribute to not affect the asset to be inserted.
|
195
|
+
writer.removeSelectionAttribute('linkHref');
|
196
|
+
if (asset.type === 'image') {
|
197
|
+
this._insertImage(asset);
|
198
|
+
}
|
199
|
+
else {
|
200
|
+
this._insertLink(asset, writer);
|
201
|
+
}
|
202
|
+
// Except for the last chosen asset, move the selection to the end of the current range to avoid overwriting other, already
|
203
|
+
// inserted assets.
|
204
|
+
if (!isLastAsset) {
|
205
|
+
writer.setSelection(selection.getLastPosition());
|
206
|
+
}
|
207
|
+
}
|
208
|
+
/**
|
209
|
+
* Inserts the image by calling the `insertImage` command.
|
210
|
+
*
|
211
|
+
* @param asset The asset to be inserted.
|
212
|
+
*/
|
213
|
+
_insertImage(asset) {
|
214
|
+
const editor = this.editor;
|
215
|
+
const { imageFallbackUrl, imageSources, imageTextAlternative } = asset.attributes;
|
216
|
+
editor.execute('insertImage', {
|
217
|
+
source: {
|
218
|
+
src: imageFallbackUrl,
|
219
|
+
sources: imageSources,
|
220
|
+
alt: imageTextAlternative
|
221
|
+
}
|
222
|
+
});
|
223
|
+
}
|
224
|
+
/**
|
225
|
+
* Inserts the link to the asset by calling the `link` command.
|
226
|
+
*
|
227
|
+
* @param asset The asset to be inserted.
|
228
|
+
* @param writer An instance of the model writer.
|
229
|
+
*/
|
230
|
+
_insertLink(asset, writer) {
|
231
|
+
const editor = this.editor;
|
232
|
+
const model = editor.model;
|
233
|
+
const selection = model.document.selection;
|
234
|
+
const { linkName, linkHref } = asset.attributes;
|
235
|
+
// If the selection is collapsed, insert the asset name as the link label and select it.
|
236
|
+
if (selection.isCollapsed) {
|
237
|
+
const selectionAttributes = toMap(selection.getAttributes());
|
238
|
+
const textNode = writer.createText(linkName, selectionAttributes);
|
239
|
+
const range = model.insertContent(textNode);
|
240
|
+
writer.setSelection(range);
|
241
|
+
}
|
242
|
+
editor.execute('link', linkHref);
|
243
|
+
}
|
326
244
|
}
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
return {
|
346
|
-
imageFallbackUrl,
|
347
|
-
imageSources,
|
348
|
-
imageTextAlternative: asset.data.metadata.description || ''
|
349
|
-
};
|
350
|
-
}
|
351
|
-
|
352
|
-
return {
|
353
|
-
linkName: asset.data.name,
|
354
|
-
linkHref: getAssetUrl( asset, token, origin )
|
355
|
-
};
|
356
|
-
}
|
357
|
-
|
358
|
-
// Checks whether the asset is an image.
|
359
|
-
//
|
360
|
-
// @private
|
361
|
-
// @param {module:ckbox/ckbox~CKBoxRawAssetDefinition} asset
|
362
|
-
// @returns {Boolean}
|
363
|
-
function isImage( asset ) {
|
364
|
-
const metadata = asset.data.metadata;
|
365
|
-
|
366
|
-
if ( !metadata ) {
|
367
|
-
return false;
|
368
|
-
}
|
369
|
-
|
370
|
-
return metadata.width && metadata.height;
|
371
|
-
}
|
372
|
-
|
373
|
-
// Creates the URL for the asset.
|
374
|
-
//
|
375
|
-
// @private
|
376
|
-
// @param {module:ckbox/ckbox~CKBoxRawAssetDefinition} asset
|
377
|
-
// @param {module:cloud-services/token~Token} token
|
378
|
-
// @param {String} origin The base URL for assets inserted into the editor.
|
379
|
-
// @returns {String}
|
380
|
-
function getAssetUrl( asset, token, origin ) {
|
381
|
-
const environmentId = getEnvironmentId( token );
|
382
|
-
const url = new URL( `${ environmentId }/assets/${ asset.data.id }/file`, origin );
|
383
|
-
|
384
|
-
url.searchParams.set( 'download', 'true' );
|
385
|
-
|
386
|
-
return url.toString();
|
245
|
+
/**
|
246
|
+
* Parses the chosen assets into the internal data format. Filters out chosen assets that are not allowed.
|
247
|
+
*/
|
248
|
+
function prepareAssets({ assets, origin, token, isImageAllowed, isLinkAllowed }) {
|
249
|
+
return assets
|
250
|
+
.map(asset => isImage(asset) ?
|
251
|
+
{
|
252
|
+
id: asset.data.id,
|
253
|
+
type: 'image',
|
254
|
+
attributes: prepareImageAssetAttributes(asset, token, origin)
|
255
|
+
} :
|
256
|
+
{
|
257
|
+
id: asset.data.id,
|
258
|
+
type: 'link',
|
259
|
+
attributes: prepareLinkAssetAttributes(asset, token, origin)
|
260
|
+
})
|
261
|
+
.filter(asset => asset.type === 'image' ? isImageAllowed : isLinkAllowed);
|
387
262
|
}
|
388
|
-
|
389
263
|
/**
|
390
|
-
*
|
264
|
+
* Parses the assets attributes into the internal data format.
|
391
265
|
*
|
392
|
-
* @
|
266
|
+
* @param origin The base URL for assets inserted into the editor.
|
393
267
|
*/
|
394
|
-
|
268
|
+
function prepareImageAssetAttributes(asset, token, origin) {
|
269
|
+
const { imageFallbackUrl, imageSources } = getImageUrls({
|
270
|
+
token,
|
271
|
+
origin,
|
272
|
+
id: asset.data.id,
|
273
|
+
width: asset.data.metadata.width,
|
274
|
+
extension: asset.data.extension
|
275
|
+
});
|
276
|
+
return {
|
277
|
+
imageFallbackUrl,
|
278
|
+
imageSources,
|
279
|
+
imageTextAlternative: asset.data.metadata.description || ''
|
280
|
+
};
|
281
|
+
}
|
395
282
|
/**
|
396
|
-
*
|
283
|
+
* Parses the assets attributes into the internal data format.
|
397
284
|
*
|
398
|
-
* @
|
285
|
+
* @param origin The base URL for assets inserted into the editor.
|
399
286
|
*/
|
400
|
-
|
287
|
+
function prepareLinkAssetAttributes(asset, token, origin) {
|
288
|
+
return {
|
289
|
+
linkName: asset.data.name,
|
290
|
+
linkHref: getAssetUrl(asset, token, origin)
|
291
|
+
};
|
292
|
+
}
|
401
293
|
/**
|
402
|
-
*
|
294
|
+
* Checks whether the asset is an image.
|
295
|
+
*/
|
296
|
+
function isImage(asset) {
|
297
|
+
const metadata = asset.data.metadata;
|
298
|
+
if (!metadata) {
|
299
|
+
return false;
|
300
|
+
}
|
301
|
+
return metadata.width && metadata.height;
|
302
|
+
}
|
303
|
+
/**
|
304
|
+
* Creates the URL for the asset.
|
403
305
|
*
|
404
|
-
* @
|
405
|
-
* @param {Array.<module:ckbox/ckbox~CKBoxRawAssetDefinition>} assets Chosen assets.
|
306
|
+
* @param origin The base URL for assets inserted into the editor.
|
406
307
|
*/
|
308
|
+
function getAssetUrl(asset, token, origin) {
|
309
|
+
const environmentId = getEnvironmentId(token);
|
310
|
+
const url = new URL(`${environmentId}/assets/${asset.data.id}/file`, origin);
|
311
|
+
url.searchParams.set('download', 'true');
|
312
|
+
return url.toString();
|
313
|
+
}
|