@ckeditor/ckeditor5-ckbox 38.1.0 → 38.1.1

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