/
ckboxediting.js
466 lines (388 loc) · 14.4 KB
/
ckboxediting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals window */
/**
* @module ckbox/ckboxediting
*/
import { Plugin } from 'ckeditor5/src/core';
import { Range } from 'ckeditor5/src/engine';
import { CKEditorError, logError } from 'ckeditor5/src/utils';
import CKBoxCommand from './ckboxcommand';
import CKBoxUploadAdapter from './ckboxuploadadapter';
/**
* The CKBox editing feature. It introduces the {@link module:ckbox/ckboxcommand~CKBoxCommand CKBox command} and
* {@link module:ckbox/ckboxuploadadapter~CKBoxUploadAdapter CKBox upload adapter}.
*
* @extends module:core/plugin~Plugin
*/
export default class CKBoxEditing extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'CKBoxEditing';
}
/**
* @inheritDoc
*/
static get requires() {
return [ 'CloudServices', 'LinkEditing', 'PictureEditing', CKBoxUploadAdapter ];
}
/**
* @inheritDoc
*/
async init() {
const editor = this.editor;
const hasConfiguration = !!editor.config.get( 'ckbox' );
const isLibraryLoaded = !!window.CKBox;
// Proceed with plugin initialization only when the integrator intentionally wants to use it, i.e. when the `config.ckbox` exists or
// the CKBox JavaScript library is loaded.
if ( !hasConfiguration && !isLibraryLoaded ) {
return;
}
this._initConfig();
const cloudServicesCore = editor.plugins.get( 'CloudServicesCore' );
const ckboxTokenUrl = editor.config.get( 'ckbox.tokenUrl' );
const cloudServicesTokenUrl = editor.config.get( 'cloudServices.tokenUrl' );
// To avoid fetching the same token twice we need to compare the `ckbox.tokenUrl` and `cloudServices.tokenUrl` values.
// If they are equal, it's enough to take the token generated by the `CloudServices` plugin.
if ( ckboxTokenUrl === cloudServicesTokenUrl ) {
/**
* CKEditor Cloud Services access token.
*
* @protected
* @member {module:cloud-services/token~Token} #_token
*/
this._token = editor.plugins.get( 'CloudServices' ).token;
}
// Otherwise, create a new token manually.
else {
this._token = await cloudServicesCore.createToken( ckboxTokenUrl ).init();
}
// Extending the schema, registering converters and applying fixers only make sense if the configuration option to assign
// the assets ID with the model elements is enabled.
if ( !editor.config.get( 'ckbox.ignoreDataId' ) ) {
this._initSchema();
this._initConversion();
this._initFixers();
}
// Registering the `ckbox` command makes sense only if the CKBox library is loaded, as the `ckbox` command opens the CKBox dialog.
if ( isLibraryLoaded ) {
editor.commands.add( 'ckbox', new CKBoxCommand( editor ) );
}
}
/**
* Returns a token used by the CKBox plugin for communication with the CKBox service.
*
* @returns {module:cloud-services/token~Token}
*/
getToken() {
return this._token;
}
/**
* Initializes the `ckbox` editor configuration.
*
* @private
*/
_initConfig() {
const editor = this.editor;
editor.config.define( 'ckbox', {
serviceOrigin: 'https://api.ckbox.io',
assetsOrigin: 'https://ckbox.cloud',
defaultUploadCategories: null,
ignoreDataId: false,
language: editor.locale.uiLanguage,
theme: 'default',
tokenUrl: editor.config.get( 'cloudServices.tokenUrl' )
} );
const tokenUrl = editor.config.get( 'ckbox.tokenUrl' );
if ( !tokenUrl ) {
/**
* The {@link module:ckbox/ckbox~CKBoxConfig#tokenUrl `config.ckbox.tokenUrl`} or the
* {@link module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl `config.cloudServices.tokenUrl`}
* configuration is required for the CKBox plugin.
*
* ClassicEditor.create( document.createElement( 'div' ), {
* ckbox: {
* tokenUrl: "YOUR_TOKEN_URL"
* // ...
* }
* // ...
* } );
*
* @error ckbox-plugin-missing-token-url
*/
throw new CKEditorError( 'ckbox-plugin-missing-token-url', this );
}
if ( !editor.plugins.has( 'ImageBlockEditing' ) && !editor.plugins.has( 'ImageInlineEditing' ) ) {
/**
* The CKBox feature requires one of the following plugins to be loaded to work correctly:
*
* * {@link module:image/imageblock~ImageBlock},
* * {@link module:image/imageinline~ImageInline},
* * {@link module:image/image~Image} (loads both `ImageBlock` and `ImageInline`)
*
* Please make sure your editor configuration is correct.
*
* @error ckbox-plugin-image-feature-missing
* @param {module:core/editor/editor~Editor} editor
*/
logError( 'ckbox-plugin-image-feature-missing', editor );
}
}
/**
* Extends the schema to allow the `ckboxImageId` and `ckboxLinkId` attributes for links and images.
*
* @private
*/
_initSchema() {
const editor = this.editor;
const schema = editor.model.schema;
schema.extend( '$text', { allowAttributes: 'ckboxLinkId' } );
if ( schema.isRegistered( 'imageBlock' ) ) {
schema.extend( 'imageBlock', { allowAttributes: [ 'ckboxImageId', 'ckboxLinkId' ] } );
}
if ( schema.isRegistered( 'imageInline' ) ) {
schema.extend( 'imageInline', { allowAttributes: [ 'ckboxImageId', 'ckboxLinkId' ] } );
}
schema.addAttributeCheck( ( context, attributeName ) => {
const isLink = !!context.last.getAttribute( 'linkHref' );
if ( !isLink && attributeName === 'ckboxLinkId' ) {
return false;
}
} );
}
/**
* Configures the upcast and downcast conversions for the `ckboxImageId` and `ckboxLinkId` attributes.
*
* @private
*/
_initConversion() {
const editor = this.editor;
// Convert `ckboxLinkId` => `data-ckbox-resource-id`.
editor.conversion.for( 'downcast' ).add( dispatcher => {
// Due to custom converters for linked block images, handle the `ckboxLinkId` attribute manually.
dispatcher.on( 'attribute:ckboxLinkId:imageBlock', ( evt, data, conversionApi ) => {
const { writer, mapper, consumable } = conversionApi;
if ( !consumable.consume( data.item, evt.name ) ) {
return;
}
const viewFigure = mapper.toViewElement( data.item );
const linkInImage = [ ...viewFigure.getChildren() ].find( child => child.name === 'a' );
// No link inside an image - no conversion needed.
if ( !linkInImage ) {
return;
}
if ( data.item.hasAttribute( 'ckboxLinkId' ) ) {
writer.setAttribute( 'data-ckbox-resource-id', data.item.getAttribute( 'ckboxLinkId' ), linkInImage );
} else {
writer.removeAttribute( 'data-ckbox-resource-id', linkInImage );
}
}, { priority: 'low' } );
dispatcher.on( 'attribute:ckboxLinkId', ( evt, data, conversionApi ) => {
const { writer, mapper, consumable } = conversionApi;
if ( !consumable.consume( data.item, evt.name ) ) {
return;
}
// Remove the previous attribute value if it was applied.
if ( data.attributeOldValue ) {
const viewElement = createLinkElement( writer, data.attributeOldValue );
writer.unwrap( mapper.toViewRange( data.range ), viewElement );
}
// Add the new attribute value if specified in a model element.
if ( data.attributeNewValue ) {
const viewElement = createLinkElement( writer, data.attributeNewValue );
if ( data.item.is( 'selection' ) ) {
const viewSelection = writer.document.selection;
writer.wrap( viewSelection.getFirstRange(), viewElement );
} else {
writer.wrap( mapper.toViewRange( data.range ), viewElement );
}
}
}, { priority: 'low' } );
} );
// Convert `data-ckbox-resource-id` => `ckboxLinkId`.
//
// The helper conversion does not handle all cases, so take care of the `data-ckbox-resource-id` attribute manually for images
// and links.
editor.conversion.for( 'upcast' ).add( dispatcher => {
dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
const { writer, consumable } = conversionApi;
// Upcast the `data-ckbox-resource-id` attribute only for valid link elements.
if ( !data.viewItem.getAttribute( 'href' ) ) {
return;
}
const consumableAttributes = { attributes: [ 'data-ckbox-resource-id' ] };
if ( !consumable.consume( data.viewItem, consumableAttributes ) ) {
return;
}
const attributeValue = data.viewItem.getAttribute( 'data-ckbox-resource-id' );
// Missing the `data-ckbox-resource-id` attribute.
if ( !attributeValue ) {
return;
}
if ( data.modelRange ) {
// If the `<a>` element contains more than single children (e.g. a linked image), set the `ckboxLinkId` for each
// allowed child.
for ( let item of data.modelRange.getItems() ) {
if ( item.is( '$textProxy' ) ) {
item = item.textNode;
}
// Do not copy the `ckboxLinkId` attribute when wrapping an element in a block element, e.g. when
// auto-paragraphing.
if ( shouldUpcastAttributeForNode( item ) ) {
writer.setAttribute( 'ckboxLinkId', attributeValue, item );
}
}
} else {
// Otherwise, just set the `ckboxLinkId` for the model element.
const modelElement = data.modelCursor.nodeBefore || data.modelCursor.parent;
writer.setAttribute( 'ckboxLinkId', attributeValue, modelElement );
}
}, { priority: 'low' } );
} );
// Convert `ckboxImageId` => `data-ckbox-resource-id`.
editor.conversion.for( 'downcast' ).attributeToAttribute( {
model: 'ckboxImageId',
view: 'data-ckbox-resource-id'
} );
// Convert `data-ckbox-resource-id` => `ckboxImageId`.
editor.conversion.for( 'upcast' ).elementToAttribute( {
model: {
key: 'ckboxImageId',
value: viewElement => viewElement.getAttribute( 'data-ckbox-resource-id' )
},
view: {
attributes: {
'data-ckbox-resource-id': /[\s\S]+/
}
}
} );
}
/**
* Registers post-fixers that add or remove the `ckboxLinkId` and `ckboxImageId` attributes.
*
* @private
*/
_initFixers() {
const editor = this.editor;
const model = editor.model;
const selection = model.document.selection;
// Registers the post-fixer to sync the asset ID with the model elements.
model.document.registerPostFixer( syncDataIdPostFixer( editor ) );
// Registers the post-fixer to remove the `ckboxLinkId` attribute from the model selection.
model.document.registerPostFixer( injectSelectionPostFixer( selection ) );
}
}
// A post-fixer that synchronizes the asset ID with the model element.
//
// @private
// @param {module:core/editor/editor~Editor} editor
// @returns {Function}
function syncDataIdPostFixer( editor ) {
return writer => {
let changed = false;
const model = editor.model;
const ckboxCommand = editor.commands.get( 'ckbox' );
// The ID from chosen assets are stored in the `CKBoxCommand#_chosenAssets`. If there is no command, it makes no sense to check
// for changes in the model.
if ( !ckboxCommand ) {
return changed;
}
for ( const entry of model.document.differ.getChanges() ) {
if ( entry.type !== 'insert' && entry.type !== 'attribute' ) {
continue;
}
const range = entry.type === 'insert' ?
new Range( entry.position, entry.position.getShiftedBy( entry.length ) ) :
entry.range;
const isLinkHrefAttributeRemoval = entry.type === 'attribute' &&
entry.attributeKey === 'linkHref' &&
entry.attributeNewValue === null;
for ( const item of range.getItems() ) {
// If the `linkHref` attribute has been removed, sync the change with the `ckboxLinkId` attribute.
if ( isLinkHrefAttributeRemoval && item.hasAttribute( 'ckboxLinkId' ) ) {
writer.removeAttribute( 'ckboxLinkId', item );
changed = true;
continue;
}
// Otherwise, the change concerns either a new model element or an attribute change. Try to find the assets for the modified
// model element.
const assets = findAssetsForItem( item, ckboxCommand._chosenAssets );
for ( const asset of assets ) {
const attributeName = asset.type === 'image' ? 'ckboxImageId' : 'ckboxLinkId';
if ( asset.id === item.getAttribute( attributeName ) ) {
continue;
}
writer.setAttribute( attributeName, asset.id, item );
changed = true;
}
}
}
return changed;
};
}
// A post-fixer that removes the `ckboxLinkId` from the selection if it does not represent a link anymore.
//
// @private
// @param {module:engine/model/selection~Selection} selection
// @returns {Function}
function injectSelectionPostFixer( selection ) {
return writer => {
const shouldRemoveLinkIdAttribute = !selection.hasAttribute( 'linkHref' ) && selection.hasAttribute( 'ckboxLinkId' );
if ( shouldRemoveLinkIdAttribute ) {
writer.removeSelectionAttribute( 'ckboxLinkId' );
}
};
}
// Tries to find the asset that is associated with the model element by comparing the attributes:
// - the image fallback URL with the `src` attribute for images,
// - the link URL with the `href` attribute for links.
//
// For any model element, zero, one or more than one asset can be found (e.g. a linked image may be associated with the link asset and the
// image asset).
//
// @private
// @param {module:engine/model/item~Item} item
// @param {Set.<module:ckbox/ckbox~CKBoxAssetDefinition>} assets
// @returns {Array.<module:ckbox/ckbox~CKBoxAssetDefinition>}
function findAssetsForItem( item, assets ) {
const isImageElement = item.is( 'element', 'imageInline' ) || item.is( 'element', 'imageBlock' );
const isLinkElement = item.hasAttribute( 'linkHref' );
return [ ...assets ].filter( asset => {
if ( asset.type === 'image' && isImageElement ) {
return asset.attributes.imageFallbackUrl === item.getAttribute( 'src' );
}
if ( asset.type === 'link' && isLinkElement ) {
return asset.attributes.linkHref === item.getAttribute( 'linkHref' );
}
} );
}
// Creates view link element with the requested ID.
//
// @private
// @param {module:engine/view/downcastwriter~DowncastWriter} writer
// @param {String} id
// @returns {module:engine/view/attributeelement~AttributeElement}
function createLinkElement( writer, id ) {
// Priority equal 5 is needed to merge adjacent `<a>` elements together.
const viewElement = writer.createAttributeElement( 'a', { 'data-ckbox-resource-id': id }, { priority: 5 } );
writer.setCustomProperty( 'link', true, viewElement );
return viewElement;
}
// Checks if the model element may have the `ckboxLinkId` attribute.
//
// @private
// @param {module:engine/model/node~Node} node
// @returns {Boolean}
function shouldUpcastAttributeForNode( node ) {
if ( node.is( '$text' ) ) {
return true;
}
if ( node.is( 'element', 'imageInline' ) || node.is( 'element', 'imageBlock' ) ) {
return true;
}
return false;
}