-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
linkimageediting.js
276 lines (229 loc) · 9.61 KB
/
linkimageediting.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module link/linkimageediting
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
import Matcher from '@ckeditor/ckeditor5-engine/src/view/matcher';
import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
import LinkEditing from './linkediting';
import linkIcon from '../theme/icons/link.svg';
/**
* The link image engine feature.
*
* It accepts the `linkHref="url"` attribute in the model for the {@link module:image/image~Image `<image>`} element
* which allows linking images.
*
* @extends module:core/plugin~Plugin
*/
export default class LinkImageEditing extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ ImageEditing, LinkEditing ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'LinkImageEditing';
}
init() {
const editor = this.editor;
editor.model.schema.extend( 'image', { allowAttributes: [ 'linkHref' ] } );
editor.conversion.for( 'upcast' ).add( upcastLink() );
editor.conversion.for( 'editingDowncast' ).add( downcastImageLink( { attachIconIndicator: true } ) );
editor.conversion.for( 'dataDowncast' ).add( downcastImageLink( { attachIconIndicator: false } ) );
// Definitions for decorators are provided by the `link` command and the `LinkEditing` plugin.
this._enableAutomaticDecorators();
this._enableManualDecorators();
}
/**
* Processes {@link module:link/link~LinkDecoratorAutomaticDefinition automatic decorators} definitions and
* attaches proper converters that will work when linking an image.`
*
* @private
*/
_enableAutomaticDecorators() {
const editor = this.editor;
const command = editor.commands.get( 'link' );
const automaticDecorators = command.automaticDecorators;
if ( automaticDecorators.length ) {
editor.conversion.for( 'downcast' ).add( automaticDecorators.getDispatcherForLinkedImage() );
}
}
/**
* Processes transformed {@link module:link/utils~ManualDecorator} instances and attaches proper converters
* that will work when linking an image.
*
* @private
*/
_enableManualDecorators() {
const editor = this.editor;
const command = editor.commands.get( 'link' );
const manualDecorators = command.manualDecorators;
for ( const decorator of command.manualDecorators ) {
editor.model.schema.extend( 'image', { allowAttributes: decorator.id } );
editor.conversion.for( 'downcast' ).add( downcastImageLinkManualDecorator( manualDecorators, decorator ) );
editor.conversion.for( 'upcast' ).add( upcastImageLinkManualDecorator( manualDecorators, decorator ) );
}
}
}
// Returns a converter that consumes the 'href' attribute if a link contains an image.
//
// @private
// @returns {Function}
function upcastLink() {
return dispatcher => {
dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
const viewLink = data.viewItem;
const imageInLink = getFirstImage( viewLink );
if ( !imageInLink ) {
return;
}
// There's an image inside an <a> element - we consume it so it won't be picked up by the Link plugin.
const consumableAttributes = { attributes: [ 'href' ] };
// Consume the `href` attribute so the default one will not convert it to $text attribute.
if ( !conversionApi.consumable.consume( viewLink, consumableAttributes ) ) {
// Might be consumed by something else - i.e. other converter with priority=highest - a standard check.
return;
}
const linkHref = viewLink.getAttribute( 'href' );
// Missing the 'href' attribute.
if ( !linkHref ) {
return;
}
// A full definition of the image feature.
// figure > a > img: parent of the view link element is an image element (figure).
let modelElement = data.modelCursor.parent;
if ( !modelElement.is( 'element', 'image' ) ) {
// a > img: parent of the view link is not the image (figure) element. We need to convert it manually.
const conversionResult = conversionApi.convertItem( imageInLink, data.modelCursor );
// Set image range as conversion result.
data.modelRange = conversionResult.modelRange;
// Continue conversion where image conversion ends.
data.modelCursor = conversionResult.modelCursor;
modelElement = data.modelCursor.nodeBefore;
}
if ( modelElement && modelElement.is( 'element', 'image' ) ) {
// Set the linkHref attribute from link element on model image element.
conversionApi.writer.setAttribute( 'linkHref', linkHref, modelElement );
}
}, { priority: 'high' } );
// Using the same priority that `upcastImageLinkManualDecorator()` converter guarantees
// that manual decorators will decorate the proper element.
};
}
// Return a converter that adds the `<a>` element to data.
//
// @private
// @params {Object} options
// @params {Boolean} options.attachIconIndicator=false If set to `true`, an icon that informs about the linked image will be added.
// @returns {Function}
function downcastImageLink( options ) {
return dispatcher => {
dispatcher.on( 'attribute:linkHref:image', ( evt, data, conversionApi ) => {
// The image will be already converted - so it will be present in the view.
const viewFigure = conversionApi.mapper.toViewElement( data.item );
const writer = conversionApi.writer;
// But we need to check whether the link element exists.
const linkInImage = Array.from( viewFigure.getChildren() ).find( child => child.name === 'a' );
let linkIconIndicator;
if ( options.attachIconIndicator ) {
// Create an icon indicator for a linked image.
linkIconIndicator = writer.createUIElement( 'span', { class: 'ck ck-link-image_icon' }, function( domDocument ) {
const domElement = this.toDomElement( domDocument );
domElement.innerHTML = linkIcon;
return domElement;
} );
}
// If so, update the attribute if it's defined or remove the entire link if the attribute is empty.
if ( linkInImage ) {
if ( data.attributeNewValue ) {
writer.setAttribute( 'href', data.attributeNewValue, linkInImage );
} else {
const viewImage = Array.from( linkInImage.getChildren() ).find( child => child.name === 'img' );
writer.move( writer.createRangeOn( viewImage ), writer.createPositionAt( viewFigure, 0 ) );
writer.remove( linkInImage );
}
} else {
// But if it does not exist. Let's wrap already converted image by newly created link element.
// 1. Create an empty link element.
const linkElement = writer.createContainerElement( 'a', { href: data.attributeNewValue } );
// 2. Insert link inside the associated image.
writer.insert( writer.createPositionAt( viewFigure, 0 ), linkElement );
// 3. Move the image to the link.
writer.move( writer.createRangeOn( viewFigure.getChild( 1 ) ), writer.createPositionAt( linkElement, 0 ) );
// 4. Inset the linked image icon indicator while downcast to editing.
if ( linkIconIndicator ) {
writer.insert( writer.createPositionAt( linkElement, 'end' ), linkIconIndicator );
}
}
} );
};
}
// Returns a converter that decorates the `<a>` element when the image is the link label.
//
// @private
// @returns {Function}
function downcastImageLinkManualDecorator( manualDecorators, decorator ) {
return dispatcher => {
dispatcher.on( `attribute:${ decorator.id }:image`, ( evt, data, conversionApi ) => {
const attributes = manualDecorators.get( decorator.id ).attributes;
const viewFigure = conversionApi.mapper.toViewElement( data.item );
const linkInImage = Array.from( viewFigure.getChildren() ).find( child => child.name === 'a' );
for ( const [ key, val ] of toMap( attributes ) ) {
conversionApi.writer.setAttribute( key, val, linkInImage );
}
} );
};
}
// Returns a converter that checks whether manual decorators should be applied to the link.
//
// @private
// @returns {Function}
function upcastImageLinkManualDecorator( manualDecorators, decorator ) {
return dispatcher => {
dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
const viewLink = data.viewItem;
const imageInLink = getFirstImage( viewLink );
// We need to check whether an image is inside a link because the converter handles
// only manual decorators for linked images. See #7975.
if ( !imageInLink ) {
return;
}
const consumableAttributes = {
attributes: manualDecorators.get( decorator.id ).attributes
};
const matcher = new Matcher( consumableAttributes );
const result = matcher.match( viewLink );
// The link element does not have required attributes or/and proper values.
if ( !result ) {
return;
}
// Check whether we can consume those attributes.
if ( !conversionApi.consumable.consume( viewLink, result.match ) ) {
return;
}
// At this stage we can assume that we have the `<image>` element.
// `nodeBefore` comes after conversion: `<a><img></a>`.
// `parent` comes with full image definition: `<figure><a><img></a></figure>.
// See the body of the `upcastLink()` function.
const modelElement = data.modelCursor.nodeBefore || data.modelCursor.parent;
conversionApi.writer.setAttribute( decorator.id, true, modelElement );
}, { priority: 'high' } );
// Using the same priority that `upcastLink()` converter guarantees that the linked image was properly converted.
};
}
// Returns the first image in a given view element.
//
// @private
// @param {module:engine/view/element~Element}
// @returns {module:engine/view/element~Element|undefined}
function getFirstImage( viewElement ) {
return Array.from( viewElement.getChildren() ).find( child => child.name === 'img' );
}