@ckeditor/ckeditor5-ckbox 40.0.0 → 40.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE.md +6 -2
- package/build/ckbox.js +2 -2
- 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 +17 -0
- package/lang/contexts.json +6 -2
- package/lang/translations/ar.po +18 -2
- package/lang/translations/az.po +18 -2
- package/lang/translations/bg.po +18 -2
- package/lang/translations/bn.po +18 -2
- package/lang/translations/ca.po +18 -2
- package/lang/translations/cs.po +18 -2
- package/lang/translations/da.po +18 -2
- package/lang/translations/de.po +18 -2
- package/lang/translations/el.po +18 -2
- package/lang/translations/en-au.po +18 -2
- package/lang/translations/en.po +18 -2
- package/lang/translations/es-co.po +18 -2
- package/lang/translations/es.po +18 -2
- package/lang/translations/et.po +18 -2
- package/lang/translations/fa.po +18 -2
- package/lang/translations/fi.po +18 -2
- package/lang/translations/fr.po +18 -2
- package/lang/translations/gl.po +18 -2
- package/lang/translations/he.po +18 -2
- package/lang/translations/hi.po +18 -2
- package/lang/translations/hr.po +18 -2
- package/lang/translations/hu.po +18 -2
- package/lang/translations/id.po +18 -2
- package/lang/translations/it.po +18 -2
- package/lang/translations/ja.po +18 -2
- package/lang/translations/ko.po +18 -2
- package/lang/translations/lt.po +18 -2
- package/lang/translations/lv.po +18 -2
- package/lang/translations/ms.po +18 -2
- package/lang/translations/nl.po +18 -2
- package/lang/translations/no.po +18 -2
- package/lang/translations/pl.po +18 -2
- package/lang/translations/pt-br.po +18 -2
- package/lang/translations/pt.po +18 -2
- package/lang/translations/ro.po +18 -2
- package/lang/translations/ru.po +18 -2
- package/lang/translations/sk.po +18 -2
- package/lang/translations/sq.po +18 -2
- package/lang/translations/sr-latn.po +18 -2
- package/lang/translations/sr.po +18 -2
- package/lang/translations/sv.po +18 -2
- package/lang/translations/th.po +18 -2
- package/lang/translations/tr.po +18 -2
- package/lang/translations/ug.po +18 -2
- package/lang/translations/uk.po +18 -2
- package/lang/translations/ur.po +18 -2
- package/lang/translations/uz.po +18 -2
- package/lang/translations/vi.po +18 -2
- package/lang/translations/zh-cn.po +18 -2
- package/lang/translations/zh.po +18 -2
- package/package.json +4 -2
- package/src/augmentation.d.ts +32 -22
- package/src/augmentation.js +5 -5
- package/src/ckbox.d.ts +33 -33
- package/src/ckbox.js +37 -37
- package/src/ckboxcommand.d.ts +114 -110
- package/src/ckboxcommand.js +332 -302
- package/src/ckboxconfig.d.ts +325 -283
- package/src/ckboxconfig.js +5 -5
- package/src/ckboxediting.d.ts +45 -52
- package/src/ckboxediting.js +321 -362
- package/src/ckboximageedit/ckboximageeditcommand.d.ts +97 -0
- package/src/ckboximageedit/ckboximageeditcommand.js +298 -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 +21 -21
- package/src/ckboxui.js +74 -47
- package/src/ckboxuploadadapter.d.ts +33 -38
- package/src/ckboxuploadadapter.js +130 -275
- package/src/ckboxutils.d.ts +50 -0
- package/src/ckboxutils.js +183 -0
- package/src/index.d.ts +17 -13
- package/src/index.js +14 -11
- package/src/utils.d.ts +63 -28
- package/src/utils.js +175 -49
- package/theme/ckboximageedit.css +53 -0
- package/theme/icons/ckbox-image-edit.svg +1 -0
- package/build/ckbox.js.map +0 -1
package/src/ckboxediting.js
CHANGED
@@ -1,362 +1,321 @@
|
|
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 { Plugin } from 'ckeditor5/src/core';
|
6
|
-
import { Range } from 'ckeditor5/src/engine';
|
7
|
-
import {
|
8
|
-
import CKBoxCommand from './ckboxcommand';
|
9
|
-
import CKBoxUploadAdapter from './ckboxuploadadapter';
|
10
|
-
|
11
|
-
|
12
|
-
* {@link module:ckbox/
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
const
|
33
|
-
const
|
34
|
-
|
35
|
-
// the
|
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
|
-
dispatcher.on('
|
150
|
-
const { writer,
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
const
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
if (
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
|
180
|
-
const
|
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
|
-
* Tries to find the asset that is associated with the model element by comparing the attributes:
|
324
|
-
* - the image fallback URL with the `src` attribute for images,
|
325
|
-
* - the link URL with the `href` attribute for links.
|
326
|
-
*
|
327
|
-
* 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
|
328
|
-
* image asset).
|
329
|
-
*/
|
330
|
-
function findAssetsForItem(item, assets) {
|
331
|
-
const isImageElement = item.is('element', 'imageInline') || item.is('element', 'imageBlock');
|
332
|
-
const isLinkElement = item.hasAttribute('linkHref');
|
333
|
-
return [...assets].filter(asset => {
|
334
|
-
if (asset.type === 'image' && isImageElement) {
|
335
|
-
return asset.attributes.imageFallbackUrl === item.getAttribute('src');
|
336
|
-
}
|
337
|
-
if (asset.type === 'link' && isLinkElement) {
|
338
|
-
return asset.attributes.linkHref === item.getAttribute('linkHref');
|
339
|
-
}
|
340
|
-
});
|
341
|
-
}
|
342
|
-
/**
|
343
|
-
* Creates view link element with the requested ID.
|
344
|
-
*/
|
345
|
-
function createLinkElement(writer, id) {
|
346
|
-
// Priority equal 5 is needed to merge adjacent `<a>` elements together.
|
347
|
-
const viewElement = writer.createAttributeElement('a', { 'data-ckbox-resource-id': id }, { priority: 5 });
|
348
|
-
writer.setCustomProperty('link', true, viewElement);
|
349
|
-
return viewElement;
|
350
|
-
}
|
351
|
-
/**
|
352
|
-
* Checks if the model element may have the `ckboxLinkId` attribute.
|
353
|
-
*/
|
354
|
-
function shouldUpcastAttributeForNode(node) {
|
355
|
-
if (node.is('$text')) {
|
356
|
-
return true;
|
357
|
-
}
|
358
|
-
if (node.is('element', 'imageInline') || node.is('element', 'imageBlock')) {
|
359
|
-
return true;
|
360
|
-
}
|
361
|
-
return false;
|
362
|
-
}
|
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 { Plugin } from 'ckeditor5/src/core';
|
6
|
+
import { Range } from 'ckeditor5/src/engine';
|
7
|
+
import { logError } from 'ckeditor5/src/utils';
|
8
|
+
import CKBoxCommand from './ckboxcommand';
|
9
|
+
import CKBoxUploadAdapter from './ckboxuploadadapter';
|
10
|
+
import CKBoxUtils from './ckboxutils';
|
11
|
+
/**
|
12
|
+
* The CKBox editing feature. It introduces the {@link module:ckbox/ckboxcommand~CKBoxCommand CKBox command} and
|
13
|
+
* {@link module:ckbox/ckboxuploadadapter~CKBoxUploadAdapter CKBox upload adapter}.
|
14
|
+
*/
|
15
|
+
export default class CKBoxEditing extends Plugin {
|
16
|
+
/**
|
17
|
+
* @inheritDoc
|
18
|
+
*/
|
19
|
+
static get pluginName() {
|
20
|
+
return 'CKBoxEditing';
|
21
|
+
}
|
22
|
+
/**
|
23
|
+
* @inheritDoc
|
24
|
+
*/
|
25
|
+
static get requires() {
|
26
|
+
return ['LinkEditing', 'PictureEditing', CKBoxUploadAdapter, CKBoxUtils];
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* @inheritDoc
|
30
|
+
*/
|
31
|
+
init() {
|
32
|
+
const editor = this.editor;
|
33
|
+
const hasConfiguration = !!editor.config.get('ckbox');
|
34
|
+
const isLibraryLoaded = !!window.CKBox;
|
35
|
+
// Proceed with plugin initialization only when the integrator intentionally wants to use it, i.e. when the `config.ckbox` exists or
|
36
|
+
// the CKBox JavaScript library is loaded.
|
37
|
+
if (!hasConfiguration && !isLibraryLoaded) {
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
this._checkImagePlugins();
|
41
|
+
// Extending the schema, registering converters and applying fixers only make sense if the configuration option to assign
|
42
|
+
// the assets ID with the model elements is enabled.
|
43
|
+
if (!editor.config.get('ckbox.ignoreDataId')) {
|
44
|
+
this._initSchema();
|
45
|
+
this._initConversion();
|
46
|
+
this._initFixers();
|
47
|
+
}
|
48
|
+
// Registering the `ckbox` command makes sense only if the CKBox library is loaded, as the `ckbox` command opens the CKBox dialog.
|
49
|
+
if (isLibraryLoaded) {
|
50
|
+
editor.commands.add('ckbox', new CKBoxCommand(editor));
|
51
|
+
}
|
52
|
+
}
|
53
|
+
/**
|
54
|
+
* Checks if at least one image plugin is loaded.
|
55
|
+
*/
|
56
|
+
_checkImagePlugins() {
|
57
|
+
const editor = this.editor;
|
58
|
+
if (!editor.plugins.has('ImageBlockEditing') && !editor.plugins.has('ImageInlineEditing')) {
|
59
|
+
/**
|
60
|
+
* The CKBox feature requires one of the following plugins to be loaded to work correctly:
|
61
|
+
*
|
62
|
+
* * {@link module:image/imageblock~ImageBlock},
|
63
|
+
* * {@link module:image/imageinline~ImageInline},
|
64
|
+
* * {@link module:image/image~Image} (loads both `ImageBlock` and `ImageInline`)
|
65
|
+
*
|
66
|
+
* Please make sure your editor configuration is correct.
|
67
|
+
*
|
68
|
+
* @error ckbox-plugin-image-feature-missing
|
69
|
+
* @param {module:core/editor/editor~Editor} editor
|
70
|
+
*/
|
71
|
+
logError('ckbox-plugin-image-feature-missing', editor);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* Extends the schema to allow the `ckboxImageId` and `ckboxLinkId` attributes for links and images.
|
76
|
+
*/
|
77
|
+
_initSchema() {
|
78
|
+
const editor = this.editor;
|
79
|
+
const schema = editor.model.schema;
|
80
|
+
schema.extend('$text', { allowAttributes: 'ckboxLinkId' });
|
81
|
+
if (schema.isRegistered('imageBlock')) {
|
82
|
+
schema.extend('imageBlock', { allowAttributes: ['ckboxImageId', 'ckboxLinkId'] });
|
83
|
+
}
|
84
|
+
if (schema.isRegistered('imageInline')) {
|
85
|
+
schema.extend('imageInline', { allowAttributes: ['ckboxImageId', 'ckboxLinkId'] });
|
86
|
+
}
|
87
|
+
schema.addAttributeCheck((context, attributeName) => {
|
88
|
+
const isLink = !!context.last.getAttribute('linkHref');
|
89
|
+
if (!isLink && attributeName === 'ckboxLinkId') {
|
90
|
+
return false;
|
91
|
+
}
|
92
|
+
});
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* Configures the upcast and downcast conversions for the `ckboxImageId` and `ckboxLinkId` attributes.
|
96
|
+
*/
|
97
|
+
_initConversion() {
|
98
|
+
const editor = this.editor;
|
99
|
+
// Convert `ckboxLinkId` => `data-ckbox-resource-id`.
|
100
|
+
editor.conversion.for('downcast').add(dispatcher => {
|
101
|
+
// Due to custom converters for linked block images, handle the `ckboxLinkId` attribute manually.
|
102
|
+
dispatcher.on('attribute:ckboxLinkId:imageBlock', (evt, data, conversionApi) => {
|
103
|
+
const { writer, mapper, consumable } = conversionApi;
|
104
|
+
if (!consumable.consume(data.item, evt.name)) {
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
const viewFigure = mapper.toViewElement(data.item);
|
108
|
+
const linkInImage = [...viewFigure.getChildren()]
|
109
|
+
.find((child) => child.name === 'a');
|
110
|
+
// No link inside an image - no conversion needed.
|
111
|
+
if (!linkInImage) {
|
112
|
+
return;
|
113
|
+
}
|
114
|
+
if (data.item.hasAttribute('ckboxLinkId')) {
|
115
|
+
writer.setAttribute('data-ckbox-resource-id', data.item.getAttribute('ckboxLinkId'), linkInImage);
|
116
|
+
}
|
117
|
+
else {
|
118
|
+
writer.removeAttribute('data-ckbox-resource-id', linkInImage);
|
119
|
+
}
|
120
|
+
}, { priority: 'low' });
|
121
|
+
dispatcher.on('attribute:ckboxLinkId', (evt, data, conversionApi) => {
|
122
|
+
const { writer, mapper, consumable } = conversionApi;
|
123
|
+
if (!consumable.consume(data.item, evt.name)) {
|
124
|
+
return;
|
125
|
+
}
|
126
|
+
// Remove the previous attribute value if it was applied.
|
127
|
+
if (data.attributeOldValue) {
|
128
|
+
const viewElement = createLinkElement(writer, data.attributeOldValue);
|
129
|
+
writer.unwrap(mapper.toViewRange(data.range), viewElement);
|
130
|
+
}
|
131
|
+
// Add the new attribute value if specified in a model element.
|
132
|
+
if (data.attributeNewValue) {
|
133
|
+
const viewElement = createLinkElement(writer, data.attributeNewValue);
|
134
|
+
if (data.item.is('selection')) {
|
135
|
+
const viewSelection = writer.document.selection;
|
136
|
+
writer.wrap(viewSelection.getFirstRange(), viewElement);
|
137
|
+
}
|
138
|
+
else {
|
139
|
+
writer.wrap(mapper.toViewRange(data.range), viewElement);
|
140
|
+
}
|
141
|
+
}
|
142
|
+
}, { priority: 'low' });
|
143
|
+
});
|
144
|
+
// Convert `data-ckbox-resource-id` => `ckboxLinkId`.
|
145
|
+
//
|
146
|
+
// The helper conversion does not handle all cases, so take care of the `data-ckbox-resource-id` attribute manually for images
|
147
|
+
// and links.
|
148
|
+
editor.conversion.for('upcast').add(dispatcher => {
|
149
|
+
dispatcher.on('element:a', (evt, data, conversionApi) => {
|
150
|
+
const { writer, consumable } = conversionApi;
|
151
|
+
// Upcast the `data-ckbox-resource-id` attribute only for valid link elements.
|
152
|
+
if (!data.viewItem.getAttribute('href')) {
|
153
|
+
return;
|
154
|
+
}
|
155
|
+
const consumableAttributes = { attributes: ['data-ckbox-resource-id'] };
|
156
|
+
if (!consumable.consume(data.viewItem, consumableAttributes)) {
|
157
|
+
return;
|
158
|
+
}
|
159
|
+
const attributeValue = data.viewItem.getAttribute('data-ckbox-resource-id');
|
160
|
+
// Missing the `data-ckbox-resource-id` attribute.
|
161
|
+
if (!attributeValue) {
|
162
|
+
return;
|
163
|
+
}
|
164
|
+
if (data.modelRange) {
|
165
|
+
// If the `<a>` element contains more than single children (e.g. a linked image), set the `ckboxLinkId` for each
|
166
|
+
// allowed child.
|
167
|
+
for (let item of data.modelRange.getItems()) {
|
168
|
+
if (item.is('$textProxy')) {
|
169
|
+
item = item.textNode;
|
170
|
+
}
|
171
|
+
// Do not copy the `ckboxLinkId` attribute when wrapping an element in a block element, e.g. when
|
172
|
+
// auto-paragraphing.
|
173
|
+
if (shouldUpcastAttributeForNode(item)) {
|
174
|
+
writer.setAttribute('ckboxLinkId', attributeValue, item);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
else {
|
179
|
+
// Otherwise, just set the `ckboxLinkId` for the model element.
|
180
|
+
const modelElement = data.modelCursor.nodeBefore || data.modelCursor.parent;
|
181
|
+
writer.setAttribute('ckboxLinkId', attributeValue, modelElement);
|
182
|
+
}
|
183
|
+
}, { priority: 'low' });
|
184
|
+
});
|
185
|
+
// Convert `ckboxImageId` => `data-ckbox-resource-id`.
|
186
|
+
editor.conversion.for('downcast').attributeToAttribute({
|
187
|
+
model: 'ckboxImageId',
|
188
|
+
view: 'data-ckbox-resource-id'
|
189
|
+
});
|
190
|
+
// Convert `data-ckbox-resource-id` => `ckboxImageId`.
|
191
|
+
editor.conversion.for('upcast').elementToAttribute({
|
192
|
+
model: {
|
193
|
+
key: 'ckboxImageId',
|
194
|
+
value: (viewElement) => viewElement.getAttribute('data-ckbox-resource-id')
|
195
|
+
},
|
196
|
+
view: {
|
197
|
+
attributes: {
|
198
|
+
'data-ckbox-resource-id': /[\s\S]+/
|
199
|
+
}
|
200
|
+
}
|
201
|
+
});
|
202
|
+
const replaceImageSourceCommand = editor.commands.get('replaceImageSource');
|
203
|
+
if (replaceImageSourceCommand) {
|
204
|
+
this.listenTo(replaceImageSourceCommand, 'cleanupImage', (_, [writer, image]) => {
|
205
|
+
writer.removeAttribute('ckboxImageId', image);
|
206
|
+
});
|
207
|
+
}
|
208
|
+
}
|
209
|
+
/**
|
210
|
+
* Registers post-fixers that add or remove the `ckboxLinkId` and `ckboxImageId` attributes.
|
211
|
+
*/
|
212
|
+
_initFixers() {
|
213
|
+
const editor = this.editor;
|
214
|
+
const model = editor.model;
|
215
|
+
const selection = model.document.selection;
|
216
|
+
// Registers the post-fixer to sync the asset ID with the model elements.
|
217
|
+
model.document.registerPostFixer(syncDataIdPostFixer(editor));
|
218
|
+
// Registers the post-fixer to remove the `ckboxLinkId` attribute from the model selection.
|
219
|
+
model.document.registerPostFixer(injectSelectionPostFixer(selection));
|
220
|
+
}
|
221
|
+
}
|
222
|
+
/**
|
223
|
+
* A post-fixer that synchronizes the asset ID with the model element.
|
224
|
+
*/
|
225
|
+
function syncDataIdPostFixer(editor) {
|
226
|
+
return (writer) => {
|
227
|
+
let changed = false;
|
228
|
+
const model = editor.model;
|
229
|
+
const ckboxCommand = editor.commands.get('ckbox');
|
230
|
+
// The ID from chosen assets are stored in the `CKBoxCommand#_chosenAssets`. If there is no command, it makes no sense to check
|
231
|
+
// for changes in the model.
|
232
|
+
if (!ckboxCommand) {
|
233
|
+
return changed;
|
234
|
+
}
|
235
|
+
for (const entry of model.document.differ.getChanges()) {
|
236
|
+
if (entry.type !== 'insert' && entry.type !== 'attribute') {
|
237
|
+
continue;
|
238
|
+
}
|
239
|
+
const range = entry.type === 'insert' ?
|
240
|
+
new Range(entry.position, entry.position.getShiftedBy(entry.length)) :
|
241
|
+
entry.range;
|
242
|
+
const isLinkHrefAttributeRemoval = entry.type === 'attribute' &&
|
243
|
+
entry.attributeKey === 'linkHref' &&
|
244
|
+
entry.attributeNewValue === null;
|
245
|
+
for (const item of range.getItems()) {
|
246
|
+
// If the `linkHref` attribute has been removed, sync the change with the `ckboxLinkId` attribute.
|
247
|
+
if (isLinkHrefAttributeRemoval && item.hasAttribute('ckboxLinkId')) {
|
248
|
+
writer.removeAttribute('ckboxLinkId', item);
|
249
|
+
changed = true;
|
250
|
+
continue;
|
251
|
+
}
|
252
|
+
// Otherwise, the change concerns either a new model element or an attribute change. Try to find the assets for the modified
|
253
|
+
// model element.
|
254
|
+
const assets = findAssetsForItem(item, ckboxCommand._chosenAssets);
|
255
|
+
for (const asset of assets) {
|
256
|
+
const attributeName = asset.type === 'image' ? 'ckboxImageId' : 'ckboxLinkId';
|
257
|
+
if (asset.id === item.getAttribute(attributeName)) {
|
258
|
+
continue;
|
259
|
+
}
|
260
|
+
writer.setAttribute(attributeName, asset.id, item);
|
261
|
+
changed = true;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
return changed;
|
266
|
+
};
|
267
|
+
}
|
268
|
+
/**
|
269
|
+
* A post-fixer that removes the `ckboxLinkId` from the selection if it does not represent a link anymore.
|
270
|
+
*/
|
271
|
+
function injectSelectionPostFixer(selection) {
|
272
|
+
return (writer) => {
|
273
|
+
const shouldRemoveLinkIdAttribute = !selection.hasAttribute('linkHref') && selection.hasAttribute('ckboxLinkId');
|
274
|
+
if (shouldRemoveLinkIdAttribute) {
|
275
|
+
writer.removeSelectionAttribute('ckboxLinkId');
|
276
|
+
return true;
|
277
|
+
}
|
278
|
+
return false;
|
279
|
+
};
|
280
|
+
}
|
281
|
+
/**
|
282
|
+
* Tries to find the asset that is associated with the model element by comparing the attributes:
|
283
|
+
* - the image fallback URL with the `src` attribute for images,
|
284
|
+
* - the link URL with the `href` attribute for links.
|
285
|
+
*
|
286
|
+
* 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
|
287
|
+
* image asset).
|
288
|
+
*/
|
289
|
+
function findAssetsForItem(item, assets) {
|
290
|
+
const isImageElement = item.is('element', 'imageInline') || item.is('element', 'imageBlock');
|
291
|
+
const isLinkElement = item.hasAttribute('linkHref');
|
292
|
+
return [...assets].filter(asset => {
|
293
|
+
if (asset.type === 'image' && isImageElement) {
|
294
|
+
return asset.attributes.imageFallbackUrl === item.getAttribute('src');
|
295
|
+
}
|
296
|
+
if (asset.type === 'link' && isLinkElement) {
|
297
|
+
return asset.attributes.linkHref === item.getAttribute('linkHref');
|
298
|
+
}
|
299
|
+
});
|
300
|
+
}
|
301
|
+
/**
|
302
|
+
* Creates view link element with the requested ID.
|
303
|
+
*/
|
304
|
+
function createLinkElement(writer, id) {
|
305
|
+
// Priority equal 5 is needed to merge adjacent `<a>` elements together.
|
306
|
+
const viewElement = writer.createAttributeElement('a', { 'data-ckbox-resource-id': id }, { priority: 5 });
|
307
|
+
writer.setCustomProperty('link', true, viewElement);
|
308
|
+
return viewElement;
|
309
|
+
}
|
310
|
+
/**
|
311
|
+
* Checks if the model element may have the `ckboxLinkId` attribute.
|
312
|
+
*/
|
313
|
+
function shouldUpcastAttributeForNode(node) {
|
314
|
+
if (node.is('$text')) {
|
315
|
+
return true;
|
316
|
+
}
|
317
|
+
if (node.is('element', 'imageInline') || node.is('element', 'imageBlock')) {
|
318
|
+
return true;
|
319
|
+
}
|
320
|
+
return false;
|
321
|
+
}
|