/
imageutils.js
339 lines (289 loc) · 10.9 KB
/
imageutils.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
/**
* @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
*/
/**
* @module image/imageutils
*/
import { Plugin } from 'ckeditor5/src/core';
import { findOptimalInsertionRange, isWidget, toWidget } from 'ckeditor5/src/widget';
import { determineImageTypeForInsertionAtSelection } from './image/utils';
/**
* A set of helpers related to images.
*
* @extends module:core/plugin~Plugin
*/
export default class ImageUtils extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'ImageUtils';
}
/**
* Checks if the provided model element is an `image` or `imageInline`.
*
* @param {module:engine/model/element~Element} modelElement
* @returns {Boolean}
*/
isImage( modelElement ) {
return this.isInlineImage( modelElement ) || this.isBlockImage( modelElement );
}
/**
* Checks if the provided view element represents an inline image.
*
* Also, see {@link module:image/imageutils~ImageUtils#isImageWidget}.
*
* @param {module:engine/view/element~Element} element
* @returns {Boolean}
*/
isInlineImageView( element ) {
return !!element && element.is( 'element', 'img' );
}
/**
* Checks if the provided view element represents a block image.
*
* Also, see {@link module:image/imageutils~ImageUtils#isImageWidget}.
*
* @param {module:engine/view/element~Element} element
* @returns {Boolean}
*/
isBlockImageView( element ) {
return !!element && element.is( 'element', 'figure' ) && element.hasClass( 'image' );
}
/**
* Handles inserting single file. This method unifies image insertion using {@link module:widget/utils~findOptimalInsertionRange}
* method.
*
* const imageUtils = editor.plugins.get( 'ImageUtils' );
*
* imageUtils.insertImage( { src: 'path/to/image.jpg' } );
*
* @param {Object} [attributes={}] Attributes of the inserted image.
* This method filters out the attributes which are disallowed by the {@link module:engine/model/schema~Schema}.
* @param {module:engine/model/selection~Selectable} [selectable] Place to insert the image. If not specified,
* the {@link module:widget/utils~findOptimalInsertionRange} logic will be applied for the block images
* and `model.document.selection` for the inline images.
*
* **Note**: If `selectable` is passed, this helper will not be able to set selection attributes (such as `linkHref`)
* and apply them to the new image. In this case, make sure all selection attributes are passed in `attributes`.
*
* @param {'imageBlock'|'imageInline'} [imageType] Image type of inserted image. If not specified,
* it will be determined automatically depending of editor config or place of the insertion.
* @return {module:engine/view/element~Element|null} The inserted model image element.
*/
insertImage( attributes = {}, selectable = null, imageType = null ) {
const editor = this.editor;
const model = editor.model;
const selection = model.document.selection;
imageType = determineImageTypeForInsertion( editor, selectable || selection, imageType );
// Mix declarative attributes with selection attributes because the new image should "inherit"
// the latter for best UX. For instance, inline images inserted into existing links
// should not split them. To do that, they need to have "linkHref" inherited from the selection.
attributes = {
...Object.fromEntries( selection.getAttributes() ),
...attributes
};
for ( const attributeName in attributes ) {
if ( !model.schema.checkAttribute( imageType, attributeName ) ) {
delete attributes[ attributeName ];
}
}
return model.change( writer => {
const imageElement = writer.createElement( imageType, attributes );
model.insertObject( imageElement, selectable, null, {
setSelection: 'on',
// If we want to insert a block image (for whatever reason) then we don't want to split text blocks.
// This applies only when we don't have the selectable specified (i.e., we insert multiple block images at once).
findOptimalPosition: !selectable && imageType != 'imageInline'
} );
// Inserting an image might've failed due to schema regulations.
if ( imageElement.parent ) {
return imageElement;
}
return null;
} );
}
/**
* Returns an image widget editing view element if one is selected or is among the selection's ancestors.
*
* @protected
* @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
* @returns {module:engine/view/element~Element|null}
*/
getClosestSelectedImageWidget( selection ) {
const viewElement = selection.getSelectedElement();
if ( viewElement && this.isImageWidget( viewElement ) ) {
return viewElement;
}
let parent = selection.getFirstPosition().parent;
while ( parent ) {
if ( parent.is( 'element' ) && this.isImageWidget( parent ) ) {
return parent;
}
parent = parent.parent;
}
return null;
}
/**
* Returns a image model element if one is selected or is among the selection's ancestors.
*
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
* @returns {module:engine/model/element~Element|null}
*/
getClosestSelectedImageElement( selection ) {
const selectedElement = selection.getSelectedElement();
return this.isImage( selectedElement ) ? selectedElement : selection.getFirstPosition().findAncestor( 'imageBlock' );
}
/**
* Checks if image can be inserted at current model selection.
*
* @protected
* @returns {Boolean}
*/
isImageAllowed() {
const model = this.editor.model;
const selection = model.document.selection;
return isImageAllowedInParent( this.editor, selection ) && isNotInsideImage( selection );
}
/**
* Converts a given {@link module:engine/view/element~Element} to an image widget:
* * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the image widget
* element.
* * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
*
* @protected
* @param {module:engine/view/element~Element} viewElement
* @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
* @param {String} label The element's label. It will be concatenated with the image `alt` attribute if one is present.
* @returns {module:engine/view/element~Element}
*/
toImageWidget( viewElement, writer, label ) {
writer.setCustomProperty( 'image', true, viewElement );
const labelCreator = () => {
const imgElement = this.findViewImgElement( viewElement );
const altText = imgElement.getAttribute( 'alt' );
return altText ? `${ altText } ${ label }` : label;
};
return toWidget( viewElement, writer, { label: labelCreator } );
}
/**
* Checks if a given view element is an image widget.
*
* @protected
* @param {module:engine/view/element~Element} viewElement
* @returns {Boolean}
*/
isImageWidget( viewElement ) {
return !!viewElement.getCustomProperty( 'image' ) && isWidget( viewElement );
}
/**
* Checks if the provided model element is an `image`.
*
* @param {module:engine/model/element~Element} modelElement
* @returns {Boolean}
*/
isBlockImage( modelElement ) {
return !!modelElement && modelElement.is( 'element', 'imageBlock' );
}
/**
* Checks if the provided model element is an `imageInline`.
*
* @param {module:engine/model/element~Element} modelElement
* @returns {Boolean}
*/
isInlineImage( modelElement ) {
return !!modelElement && modelElement.is( 'element', 'imageInline' );
}
/**
* Get the view `<img>` from another view element, e.g. a widget (`<figure class="image">`), a link (`<a>`).
*
* The `<img>` can be located deep in other elements, so this helper performs a deep tree search.
*
* @param {module:engine/view/element~Element} figureView
* @returns {module:engine/view/element~Element}
*/
findViewImgElement( figureView ) {
if ( this.isInlineImageView( figureView ) ) {
return figureView;
}
const editingView = this.editor.editing.view;
for ( const { item } of editingView.createRangeIn( figureView ) ) {
if ( this.isInlineImageView( item ) ) {
return item;
}
}
}
}
// Checks if image is allowed by schema in optimal insertion parent.
//
// @private
// @param {module:core/editor/editor~Editor} editor
// @param {module:engine/model/selection~Selection} selection
// @returns {Boolean}
function isImageAllowedInParent( editor, selection ) {
const imageType = determineImageTypeForInsertion( editor, selection );
if ( imageType == 'imageBlock' ) {
const parent = getInsertImageParent( selection, editor.model );
if ( editor.model.schema.checkChild( parent, 'imageBlock' ) ) {
return true;
}
} else if ( editor.model.schema.checkChild( selection.focus, 'imageInline' ) ) {
return true;
}
return false;
}
// Checks if selection is not placed inside an image (e.g. its caption).
//
// @private
// @param {module:engine/model/selection~Selectable} selection
// @returns {Boolean}
function isNotInsideImage( selection ) {
return [ ...selection.focus.getAncestors() ].every( ancestor => !ancestor.is( 'element', 'imageBlock' ) );
}
// Returns a node that will be used to insert image with `model.insertContent`.
//
// @private
// @param {module:engine/model/selection~Selection} selection
// @param {module:engine/model/model~Model} model
// @returns {module:engine/model/element~Element}
function getInsertImageParent( selection, model ) {
const insertionRange = findOptimalInsertionRange( selection, model );
const parent = insertionRange.start.parent;
if ( parent.isEmpty && !parent.is( 'element', '$root' ) ) {
return parent.parent;
}
return parent;
}
// Determine image element type name depending on editor config or place of insertion.
//
// @private
// @param {module:core/editor/editor~Editor} editor
// @param {module:engine/model/selection~Selectable} selectable
// @param {'imageBlock'|'imageInline'} [imageType] Image element type name. Used to force return of provided element name,
// but only if there is proper plugin enabled.
// @returns {'imageBlock'|'imageInline'} imageType
function determineImageTypeForInsertion( editor, selectable, imageType ) {
const schema = editor.model.schema;
const configImageInsertType = editor.config.get( 'image.insert.type' );
if ( !editor.plugins.has( 'ImageBlockEditing' ) ) {
return 'imageInline';
}
if ( !editor.plugins.has( 'ImageInlineEditing' ) ) {
return 'imageBlock';
}
if ( imageType ) {
return imageType;
}
if ( configImageInsertType === 'inline' ) {
return 'imageInline';
}
if ( configImageInsertType === 'block' ) {
return 'imageBlock';
}
// Try to replace the selected widget (e.g. another image).
if ( selectable.is( 'selection' ) ) {
return determineImageTypeForInsertionAtSelection( schema, selectable );
}
return schema.checkChild( selectable, 'imageInline' ) ? 'imageInline' : 'imageBlock';
}