@ckeditor/ckeditor5-image 28.0.0 → 30.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/README.md +3 -3
- package/build/image.js +1 -1
- package/build/translations/ar.js +1 -0
- package/build/translations/ast.js +1 -0
- package/build/translations/az.js +1 -0
- package/build/translations/bg.js +1 -0
- package/build/translations/cs.js +1 -0
- package/build/translations/da.js +1 -0
- package/build/translations/de-ch.js +1 -0
- package/build/translations/de.js +1 -0
- package/build/translations/el.js +1 -0
- package/build/translations/en-au.js +1 -0
- package/build/translations/en-gb.js +1 -0
- package/build/translations/eo.js +1 -0
- package/build/translations/es.js +1 -0
- package/build/translations/et.js +1 -0
- package/build/translations/eu.js +1 -0
- package/build/translations/fa.js +1 -0
- package/build/translations/fi.js +1 -0
- package/build/translations/fr.js +1 -0
- package/build/translations/gl.js +1 -0
- package/build/translations/he.js +1 -0
- package/build/translations/hi.js +1 -0
- package/build/translations/hr.js +1 -0
- package/build/translations/hu.js +1 -0
- package/build/translations/id.js +1 -0
- package/build/translations/it.js +1 -0
- package/build/translations/ja.js +1 -0
- package/build/translations/km.js +1 -0
- package/build/translations/kn.js +1 -0
- package/build/translations/ko.js +1 -0
- package/build/translations/ku.js +1 -0
- package/build/translations/lt.js +1 -0
- package/build/translations/lv.js +1 -0
- package/build/translations/nb.js +1 -0
- package/build/translations/ne.js +1 -0
- package/build/translations/nl.js +1 -0
- package/build/translations/no.js +1 -0
- package/build/translations/pl.js +1 -0
- package/build/translations/pt-br.js +1 -0
- package/build/translations/pt.js +1 -0
- package/build/translations/ro.js +1 -0
- package/build/translations/ru.js +1 -0
- package/build/translations/si.js +1 -0
- package/build/translations/sk.js +1 -0
- package/build/translations/sq.js +1 -0
- package/build/translations/sr-latn.js +1 -0
- package/build/translations/sr.js +1 -0
- package/build/translations/sv.js +1 -0
- package/build/translations/th.js +1 -0
- package/build/translations/tk.js +1 -0
- package/build/translations/tr.js +1 -0
- package/build/translations/ug.js +1 -0
- package/build/translations/uk.js +1 -0
- package/build/translations/vi.js +1 -0
- package/build/translations/zh-cn.js +1 -0
- package/build/translations/zh.js +1 -0
- package/ckeditor5-metadata.json +233 -0
- package/lang/contexts.json +3 -0
- package/lang/translations/ar.po +12 -0
- package/lang/translations/ast.po +12 -0
- package/lang/translations/az.po +12 -0
- package/lang/translations/bg.po +12 -0
- package/lang/translations/cs.po +12 -0
- package/lang/translations/da.po +12 -0
- package/lang/translations/de-ch.po +12 -0
- package/lang/translations/de.po +15 -3
- package/lang/translations/el.po +12 -0
- package/lang/translations/en-au.po +12 -0
- package/lang/translations/en-gb.po +12 -0
- package/lang/translations/en.po +12 -0
- package/lang/translations/eo.po +12 -0
- package/lang/translations/es.po +12 -0
- package/lang/translations/et.po +12 -0
- package/lang/translations/eu.po +12 -0
- package/lang/translations/fa.po +12 -0
- package/lang/translations/fi.po +12 -0
- package/lang/translations/fr.po +12 -0
- package/lang/translations/gl.po +12 -0
- package/lang/translations/he.po +12 -0
- package/lang/translations/hi.po +12 -0
- package/lang/translations/hr.po +12 -0
- package/lang/translations/hu.po +13 -1
- package/lang/translations/id.po +21 -9
- package/lang/translations/it.po +12 -0
- package/lang/translations/ja.po +12 -0
- package/lang/translations/km.po +12 -0
- package/lang/translations/kn.po +12 -0
- package/lang/translations/ko.po +12 -0
- package/lang/translations/ku.po +12 -0
- package/lang/translations/lt.po +12 -0
- package/lang/translations/lv.po +12 -0
- package/lang/translations/nb.po +12 -0
- package/lang/translations/ne.po +12 -0
- package/lang/translations/nl.po +14 -2
- package/lang/translations/no.po +12 -0
- package/lang/translations/pl.po +20 -8
- package/lang/translations/pt-br.po +12 -0
- package/lang/translations/pt.po +12 -0
- package/lang/translations/ro.po +21 -9
- package/lang/translations/ru.po +12 -0
- package/lang/translations/si.po +12 -0
- package/lang/translations/sk.po +12 -0
- package/lang/translations/sq.po +12 -0
- package/lang/translations/sr-latn.po +12 -0
- package/lang/translations/sr.po +12 -0
- package/lang/translations/sv.po +12 -0
- package/lang/translations/th.po +12 -0
- package/lang/translations/tk.po +12 -0
- package/lang/translations/tr.po +12 -0
- package/lang/translations/ug.po +12 -0
- package/lang/translations/uk.po +12 -0
- package/lang/translations/vi.po +12 -0
- package/lang/translations/zh-cn.po +12 -0
- package/lang/translations/zh.po +12 -0
- package/package.json +36 -29
- package/src/autoimage.js +9 -4
- package/src/image/converters.js +191 -15
- package/src/image/imageblockediting.js +182 -0
- package/src/image/imageediting.js +13 -70
- package/src/image/imageinlineediting.js +207 -0
- package/src/image/imagetypecommand.js +105 -0
- package/src/image/insertimagecommand.js +77 -10
- package/src/image/ui/utils.js +5 -4
- package/src/image/utils.js +65 -121
- package/src/image.js +7 -19
- package/src/imageblock.js +46 -0
- package/src/imagecaption/imagecaptionediting.js +183 -227
- package/src/imagecaption/imagecaptionui.js +78 -0
- package/src/imagecaption/toggleimagecaptioncommand.js +165 -0
- package/src/imagecaption/utils.js +25 -40
- package/src/imagecaption.js +3 -2
- package/src/imageinline.js +46 -0
- package/src/imageinsert/imageinsertui.js +5 -6
- package/src/imageinsert.js +16 -4
- package/src/imageresize/imageresizebuttons.js +1 -1
- package/src/imageresize/imageresizeediting.js +21 -8
- package/src/imageresize/imageresizehandles.js +30 -8
- package/src/imageresize/resizeimagecommand.js +8 -5
- package/src/imagestyle/converters.js +26 -17
- package/src/imagestyle/imagestylecommand.js +73 -33
- package/src/imagestyle/imagestyleediting.js +113 -52
- package/src/imagestyle/imagestyleui.js +197 -31
- package/src/imagestyle/utils.js +300 -85
- package/src/imagestyle.js +218 -47
- package/src/imagetextalternative/imagetextalternativecommand.js +10 -7
- package/src/imagetextalternative/imagetextalternativeediting.js +9 -1
- package/src/imagetextalternative/imagetextalternativeui.js +2 -2
- package/src/imagetextalternative.js +1 -1
- package/src/imagetoolbar.js +33 -11
- package/src/imageupload/imageuploadediting.js +90 -30
- package/src/imageupload/imageuploadprogress.js +17 -9
- package/src/imageupload/imageuploadui.js +1 -1
- package/src/imageupload/uploadimagecommand.js +50 -24
- package/src/imageupload/utils.js +3 -2
- package/src/imageupload.js +1 -1
- package/src/imageutils.js +342 -0
- package/src/pictureediting.js +149 -0
- package/theme/image.css +101 -21
- package/theme/imagecaption.css +24 -2
- package/theme/imageresize.css +11 -0
- package/theme/imagestyle.css +76 -0
- package/theme/imageuploadicon.css +8 -2
- package/theme/imageuploadprogress.css +12 -8
- package/CHANGELOG.md +0 -423
- package/build/image.js.map +0 -1
|
@@ -8,18 +8,31 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { Element, enablePlaceholder } from 'ckeditor5/src/engine';
|
|
12
|
+
import { toWidgetEditable } from 'ckeditor5/src/widget';
|
|
13
|
+
|
|
14
|
+
import ToggleImageCaptionCommand from './toggleimagecaptioncommand';
|
|
15
|
+
|
|
16
|
+
import ImageUtils from '../imageutils';
|
|
17
|
+
import { getCaptionFromImageModelElement, matchImageCaptionViewElement } from './utils';
|
|
13
18
|
|
|
14
19
|
/**
|
|
15
|
-
* The image caption engine plugin.
|
|
20
|
+
* The image caption engine plugin. It is responsible for:
|
|
16
21
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
22
|
+
* * registering converters for the caption element,
|
|
23
|
+
* * registering converters for the caption model attribute,
|
|
24
|
+
* * registering the {@link module:image/imagecaption/toggleimagecaptioncommand~ToggleImageCaptionCommand `toggleImageCaption`} command.
|
|
19
25
|
*
|
|
20
26
|
* @extends module:core/plugin~Plugin
|
|
21
27
|
*/
|
|
22
28
|
export default class ImageCaptionEditing extends Plugin {
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get requires() {
|
|
33
|
+
return [ ImageUtils ];
|
|
34
|
+
}
|
|
35
|
+
|
|
23
36
|
/**
|
|
24
37
|
* @inheritDoc
|
|
25
38
|
*/
|
|
@@ -30,286 +43,229 @@ export default class ImageCaptionEditing extends Plugin {
|
|
|
30
43
|
/**
|
|
31
44
|
* @inheritDoc
|
|
32
45
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const view = editor.editing.view;
|
|
36
|
-
const schema = editor.model.schema;
|
|
37
|
-
const data = editor.data;
|
|
38
|
-
const editing = editor.editing;
|
|
39
|
-
const t = editor.t;
|
|
46
|
+
constructor( editor ) {
|
|
47
|
+
super( editor );
|
|
40
48
|
|
|
41
49
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
50
|
+
* A map that keeps saved JSONified image captions and image model elements they are
|
|
51
|
+
* associated with.
|
|
52
|
+
*
|
|
53
|
+
* To learn more about this system, see {@link #_saveCaption}.
|
|
44
54
|
*
|
|
45
|
-
* @
|
|
46
|
-
* @member {module:engine/view/editableelement~EditableElement} #_lastSelectedCaption
|
|
55
|
+
* @member {WeakMap.<module:engine/model/element~Element,Object>}
|
|
47
56
|
*/
|
|
57
|
+
this._savedCaptionsMap = new WeakMap();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @inheritDoc
|
|
62
|
+
*/
|
|
63
|
+
init() {
|
|
64
|
+
const editor = this.editor;
|
|
65
|
+
const schema = editor.model.schema;
|
|
48
66
|
|
|
49
67
|
// Schema configuration.
|
|
50
68
|
if ( !schema.isRegistered( 'caption' ) ) {
|
|
51
69
|
schema.register( 'caption', {
|
|
52
|
-
allowIn: '
|
|
70
|
+
allowIn: 'imageBlock',
|
|
53
71
|
allowContentOf: '$block',
|
|
54
72
|
isLimit: true
|
|
55
73
|
} );
|
|
56
74
|
} else {
|
|
57
75
|
schema.extend( 'caption', {
|
|
58
|
-
allowIn: '
|
|
76
|
+
allowIn: 'imageBlock'
|
|
59
77
|
} );
|
|
60
78
|
}
|
|
61
79
|
|
|
62
|
-
|
|
63
|
-
editor.model.document.registerPostFixer( writer => this._insertMissingModelCaptionElement( writer ) );
|
|
80
|
+
editor.commands.add( 'toggleImageCaption', new ToggleImageCaptionCommand( this.editor ) );
|
|
64
81
|
|
|
65
|
-
|
|
82
|
+
this._setupConversion();
|
|
83
|
+
this._setupImageTypeCommandsIntegration();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Configures conversion pipelines to support upcasting and downcasting
|
|
88
|
+
* image captions.
|
|
89
|
+
*
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
_setupConversion() {
|
|
93
|
+
const editor = this.editor;
|
|
94
|
+
const view = editor.editing.view;
|
|
95
|
+
const imageUtils = editor.plugins.get( 'ImageUtils' );
|
|
96
|
+
const t = editor.t;
|
|
97
|
+
|
|
98
|
+
// View -> model converter for the data pipeline.
|
|
66
99
|
editor.conversion.for( 'upcast' ).elementToElement( {
|
|
67
|
-
view:
|
|
100
|
+
view: element => matchImageCaptionViewElement( imageUtils, element ),
|
|
68
101
|
model: 'caption'
|
|
69
102
|
} );
|
|
70
103
|
|
|
71
|
-
// Model
|
|
72
|
-
|
|
73
|
-
|
|
104
|
+
// Model -> view converter for the data pipeline.
|
|
105
|
+
editor.conversion.for( 'dataDowncast' ).elementToElement( {
|
|
106
|
+
model: 'caption',
|
|
107
|
+
view: ( modelElement, { writer } ) => {
|
|
108
|
+
if ( !imageUtils.isBlockImage( modelElement.parent ) ) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
74
111
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
112
|
+
return writer.createContainerElement( 'figcaption' );
|
|
113
|
+
}
|
|
114
|
+
} );
|
|
78
115
|
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
'
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
116
|
+
// Model -> view converter for the editing pipeline.
|
|
117
|
+
editor.conversion.for( 'editingDowncast' ).elementToElement( {
|
|
118
|
+
model: 'caption',
|
|
119
|
+
view: ( modelElement, { writer } ) => {
|
|
120
|
+
if ( !imageUtils.isBlockImage( modelElement.parent ) ) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const figcaptionElement = writer.createEditableElement( 'figcaption' );
|
|
125
|
+
writer.setCustomProperty( 'imageCaption', true, figcaptionElement );
|
|
126
|
+
|
|
127
|
+
enablePlaceholder( {
|
|
128
|
+
view,
|
|
129
|
+
element: figcaptionElement,
|
|
130
|
+
text: t( 'Enter image caption' ),
|
|
131
|
+
keepOnFocus: true
|
|
132
|
+
} );
|
|
85
133
|
|
|
86
|
-
|
|
87
|
-
|
|
134
|
+
return toWidgetEditable( figcaptionElement, writer );
|
|
135
|
+
}
|
|
136
|
+
} );
|
|
88
137
|
|
|
89
|
-
|
|
90
|
-
|
|
138
|
+
editor.editing.mapper.on( 'modelToViewPosition', mapModelPositionToView( view ) );
|
|
139
|
+
editor.data.mapper.on( 'modelToViewPosition', mapModelPositionToView( view ) );
|
|
91
140
|
}
|
|
92
141
|
|
|
93
142
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
143
|
+
* Integrates with {@link module:image/image/imagetypecommand~ImageTypeCommand image type commands}
|
|
144
|
+
* to make sure the caption is preserved when the type of an image changes so it can be restored
|
|
145
|
+
* in the future if the user decides they want their caption back.
|
|
96
146
|
*
|
|
97
147
|
* @private
|
|
98
|
-
* @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter
|
|
99
|
-
* @returns {Boolean} Returns `true` when the view is updated.
|
|
100
148
|
*/
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// If whole image is selected.
|
|
107
|
-
const modelSelection = this.editor.model.document.selection;
|
|
108
|
-
const selectedElement = modelSelection.getSelectedElement();
|
|
109
|
-
|
|
110
|
-
if ( selectedElement && selectedElement.is( 'element', 'image' ) ) {
|
|
111
|
-
const modelCaption = getCaptionFromImage( selectedElement );
|
|
112
|
-
viewCaption = mapper.toViewElement( modelCaption );
|
|
113
|
-
}
|
|
149
|
+
_setupImageTypeCommandsIntegration() {
|
|
150
|
+
const editor = this.editor;
|
|
151
|
+
const imageUtils = editor.plugins.get( 'ImageUtils' );
|
|
152
|
+
const imageTypeInlineCommand = editor.commands.get( 'imageTypeInline' );
|
|
153
|
+
const imageTypeBlockCommand = editor.commands.get( 'imageTypeBlock' );
|
|
114
154
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
155
|
+
const handleImageTypeChange = evt => {
|
|
156
|
+
// The image type command execution can be unsuccessful.
|
|
157
|
+
if ( !evt.return ) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
118
160
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
161
|
+
const { oldElement, newElement } = evt.return;
|
|
162
|
+
|
|
163
|
+
/* istanbul ignore if: paranoid check */
|
|
164
|
+
if ( !oldElement ) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
122
167
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
//
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
this._lastSelectedCaption = viewCaption;
|
|
133
|
-
|
|
134
|
-
return showCaption( viewCaption, viewWriter );
|
|
168
|
+
if ( imageUtils.isBlockImage( oldElement ) ) {
|
|
169
|
+
const oldCaptionElement = getCaptionFromImageModelElement( oldElement );
|
|
170
|
+
|
|
171
|
+
// If the old element was a captioned block image (the caption was visible),
|
|
172
|
+
// simply save it so it can be restored.
|
|
173
|
+
if ( oldCaptionElement ) {
|
|
174
|
+
this._saveCaption( newElement, oldCaptionElement );
|
|
175
|
+
|
|
176
|
+
return;
|
|
135
177
|
}
|
|
136
|
-
} else {
|
|
137
|
-
this._lastSelectedCaption = viewCaption;
|
|
138
|
-
return showCaption( viewCaption, viewWriter );
|
|
139
178
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
179
|
+
|
|
180
|
+
const savedOldElementCaption = this._getSavedCaption( oldElement );
|
|
181
|
+
|
|
182
|
+
// If either:
|
|
183
|
+
//
|
|
184
|
+
// * the block image didn't have a visible caption,
|
|
185
|
+
// * the block image caption was hidden (and already saved),
|
|
186
|
+
// * the inline image was passed
|
|
187
|
+
//
|
|
188
|
+
// just try to "pass" the saved caption from the old image to the new image
|
|
189
|
+
// so it can be retrieved in the future if the user wants it back.
|
|
190
|
+
if ( savedOldElementCaption ) {
|
|
191
|
+
// Note: Since we're writing to a WeakMap, we don't bother with removing the
|
|
192
|
+
// [ oldElement, savedOldElementCaption ] pair from it.
|
|
193
|
+
this._saveCaption( newElement, savedOldElementCaption );
|
|
149
194
|
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Presence of the commands depends on the Image(Inline|Block)Editing plugins loaded in the editor.
|
|
198
|
+
if ( imageTypeInlineCommand ) {
|
|
199
|
+
this.listenTo( imageTypeInlineCommand, 'execute', handleImageTypeChange, { priority: 'low' } );
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if ( imageTypeBlockCommand ) {
|
|
203
|
+
this.listenTo( imageTypeBlockCommand, 'execute', handleImageTypeChange, { priority: 'low' } );
|
|
150
204
|
}
|
|
151
205
|
}
|
|
152
206
|
|
|
153
207
|
/**
|
|
154
|
-
* Returns
|
|
155
|
-
*
|
|
208
|
+
* Returns the saved {@link module:engine/model/element~Element#toJSON JSONified} caption
|
|
209
|
+
* of an image model element.
|
|
156
210
|
*
|
|
157
|
-
* @
|
|
158
|
-
*
|
|
159
|
-
* @
|
|
211
|
+
* See {@link #_saveCaption}.
|
|
212
|
+
*
|
|
213
|
+
* @protected
|
|
214
|
+
* @param {module:engine/model/element~Element} imageModelElement The model element the
|
|
215
|
+
* caption should be returned for.
|
|
216
|
+
* @returns {module:engine/model/element~Element|null} The model caption element or `null` if there is none.
|
|
160
217
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const mapper = this.editor.editing.mapper;
|
|
166
|
-
const viewWriter = conversionApi.writer;
|
|
167
|
-
|
|
168
|
-
if ( modelCaption ) {
|
|
169
|
-
const viewCaption = mapper.toViewElement( modelCaption );
|
|
170
|
-
|
|
171
|
-
if ( viewCaption ) {
|
|
172
|
-
if ( modelCaption.childCount ) {
|
|
173
|
-
viewWriter.removeClass( 'ck-hidden', viewCaption );
|
|
174
|
-
} else {
|
|
175
|
-
viewWriter.addClass( 'ck-hidden', viewCaption );
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
};
|
|
218
|
+
_getSavedCaption( imageModelElement ) {
|
|
219
|
+
const jsonObject = this._savedCaptionsMap.get( imageModelElement );
|
|
220
|
+
|
|
221
|
+
return jsonObject ? Element.fromJSON( jsonObject ) : null;
|
|
180
222
|
}
|
|
181
223
|
|
|
182
224
|
/**
|
|
183
|
-
*
|
|
184
|
-
*
|
|
225
|
+
* Saves a {@link module:engine/model/element~Element#toJSON JSONified} caption for
|
|
226
|
+
* an image element to allow restoring it in the future.
|
|
185
227
|
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
228
|
+
* A caption is saved every time it gets hidden and/or the type of an image changes. The
|
|
229
|
+
* user should be able to restore it on demand.
|
|
230
|
+
*
|
|
231
|
+
* **Note**: The caption cannot be stored in the image model element attribute because,
|
|
232
|
+
* for instance, when the model state propagates to collaborators, the attribute would get
|
|
233
|
+
* lost (mainly because it does not convert to anything when the caption is hidden) and
|
|
234
|
+
* the states of collaborators' models would de-synchronize causing numerous issues.
|
|
235
|
+
*
|
|
236
|
+
* See {@link #_getSavedCaption}.
|
|
237
|
+
*
|
|
238
|
+
* @protected
|
|
239
|
+
* @param {module:engine/model/element~Element} imageModelElement The model element the
|
|
240
|
+
* caption is saved for.
|
|
241
|
+
* @param {module:engine/model/element~Element} caption The caption model element to be saved.
|
|
189
242
|
*/
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const changes = model.document.differ.getChanges();
|
|
193
|
-
|
|
194
|
-
const imagesWithoutCaption = [];
|
|
195
|
-
|
|
196
|
-
for ( const entry of changes ) {
|
|
197
|
-
if ( entry.type == 'insert' && entry.name != '$text' ) {
|
|
198
|
-
const item = entry.position.nodeAfter;
|
|
199
|
-
|
|
200
|
-
if ( item.is( 'element', 'image' ) && !getCaptionFromImage( item ) ) {
|
|
201
|
-
imagesWithoutCaption.push( item );
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Check elements with children for nested images.
|
|
205
|
-
if ( !item.is( 'element', 'image' ) && item.childCount ) {
|
|
206
|
-
for ( const nestedItem of model.createRangeIn( item ).getItems() ) {
|
|
207
|
-
if ( nestedItem.is( 'element', 'image' ) && !getCaptionFromImage( nestedItem ) ) {
|
|
208
|
-
imagesWithoutCaption.push( nestedItem );
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
for ( const image of imagesWithoutCaption ) {
|
|
216
|
-
writer.appendElement( 'caption', image );
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return !!imagesWithoutCaption.length;
|
|
243
|
+
_saveCaption( imageModelElement, caption ) {
|
|
244
|
+
this._savedCaptionsMap.set( imageModelElement, caption.toJSON() );
|
|
220
245
|
}
|
|
221
246
|
}
|
|
222
247
|
|
|
223
|
-
// Creates a
|
|
248
|
+
// Creates a mapper callback that reverses the order of `<img>` and `<figcaption>` in the image.
|
|
249
|
+
// Without it, `<figcaption>` would precede the `<img>` in the conversion.
|
|
250
|
+
//
|
|
251
|
+
// <imageBlock>^</imageBlock> -> <figure><img>^<caption></caption></figure>
|
|
224
252
|
//
|
|
225
253
|
// @private
|
|
226
|
-
// @param {
|
|
227
|
-
// @param {Boolean} [hide=true] When set to `false` view element will not be inserted when it's empty.
|
|
254
|
+
// @param {module:engine/view/view~View} editingView
|
|
228
255
|
// @returns {Function}
|
|
229
|
-
function
|
|
230
|
-
return ( evt, data
|
|
231
|
-
const
|
|
256
|
+
function mapModelPositionToView( editingView ) {
|
|
257
|
+
return ( evt, data ) => {
|
|
258
|
+
const modelPosition = data.modelPosition;
|
|
259
|
+
const parent = modelPosition.parent;
|
|
232
260
|
|
|
233
|
-
|
|
234
|
-
if ( !captionElement.childCount && !hide ) {
|
|
261
|
+
if ( !parent.is( 'element', 'imageBlock' ) ) {
|
|
235
262
|
return;
|
|
236
263
|
}
|
|
237
264
|
|
|
238
|
-
|
|
239
|
-
if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const viewImage = conversionApi.mapper.toViewElement( data.range.start.parent );
|
|
244
|
-
const viewCaption = elementCreator( conversionApi.writer );
|
|
245
|
-
const viewWriter = conversionApi.writer;
|
|
246
|
-
|
|
247
|
-
// Hide if empty.
|
|
248
|
-
if ( !captionElement.childCount ) {
|
|
249
|
-
viewWriter.addClass( 'ck-hidden', viewCaption );
|
|
250
|
-
}
|
|
265
|
+
const viewElement = data.mapper.toViewElement( parent );
|
|
251
266
|
|
|
252
|
-
|
|
253
|
-
|
|
267
|
+
// The "img" element is inserted by ImageBlockEditing during the downcast conversion via
|
|
268
|
+
// an explicit view position so the "0" position does not need any mapping.
|
|
269
|
+
data.viewPosition = editingView.createPositionAt( viewElement, modelPosition.offset + 1 );
|
|
254
270
|
};
|
|
255
271
|
}
|
|
256
|
-
|
|
257
|
-
// Inserts `viewCaption` at the end of `viewImage` and binds it to `modelCaption`.
|
|
258
|
-
//
|
|
259
|
-
// @private
|
|
260
|
-
// @param {module:engine/view/containerelement~ContainerElement} viewCaption
|
|
261
|
-
// @param {module:engine/model/element~Element} modelCaption
|
|
262
|
-
// @param {module:engine/view/containerelement~ContainerElement} viewImage
|
|
263
|
-
// @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi
|
|
264
|
-
function insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, conversionApi ) {
|
|
265
|
-
const viewPosition = conversionApi.writer.createPositionAt( viewImage, 'end' );
|
|
266
|
-
|
|
267
|
-
conversionApi.writer.insert( viewPosition, viewCaption );
|
|
268
|
-
conversionApi.mapper.bindElements( modelCaption, viewCaption );
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// Checks if the provided node or one of its ancestors is a caption element, and returns it.
|
|
272
|
-
//
|
|
273
|
-
// @private
|
|
274
|
-
// @param {module:engine/model/node~Node} node
|
|
275
|
-
// @returns {module:engine/model/element~Element|null}
|
|
276
|
-
function getParentCaption( node ) {
|
|
277
|
-
const ancestors = node.getAncestors( { includeSelf: true } );
|
|
278
|
-
const caption = ancestors.find( ancestor => ancestor.name == 'caption' );
|
|
279
|
-
|
|
280
|
-
if ( caption && caption.parent && caption.parent.name == 'image' ) {
|
|
281
|
-
return caption;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return null;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// Hides a given caption in the view if it is empty.
|
|
288
|
-
//
|
|
289
|
-
// @private
|
|
290
|
-
// @param {module:engine/view/containerelement~ContainerElement} caption
|
|
291
|
-
// @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter
|
|
292
|
-
// @returns {Boolean} Returns `true` if the view was modified.
|
|
293
|
-
function hideCaptionIfEmpty( caption, viewWriter ) {
|
|
294
|
-
if ( !caption.childCount && !caption.hasClass( 'ck-hidden' ) ) {
|
|
295
|
-
viewWriter.addClass( 'ck-hidden', caption );
|
|
296
|
-
return true;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
return false;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Shows the caption.
|
|
303
|
-
//
|
|
304
|
-
// @private
|
|
305
|
-
// @param {module:engine/view/containerelement~ContainerElement} caption
|
|
306
|
-
// @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter
|
|
307
|
-
// @returns {Boolean} Returns `true` if the view was modified.
|
|
308
|
-
function showCaption( caption, viewWriter ) {
|
|
309
|
-
if ( caption.hasClass( 'ck-hidden' ) ) {
|
|
310
|
-
viewWriter.removeClass( 'ck-hidden', caption );
|
|
311
|
-
return true;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module image/imagecaption/imagecaptionui
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Plugin, icons } from 'ckeditor5/src/core';
|
|
11
|
+
import { ButtonView } from 'ckeditor5/src/ui';
|
|
12
|
+
import ImageUtils from '../imageutils';
|
|
13
|
+
|
|
14
|
+
import { getCaptionFromModelSelection } from './utils';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The image caption UI plugin. It introduces the `'toggleImageCaption'` UI button.
|
|
18
|
+
*
|
|
19
|
+
* @extends module:core/plugin~Plugin
|
|
20
|
+
*/
|
|
21
|
+
export default class ImageCaptionUI extends Plugin {
|
|
22
|
+
/**
|
|
23
|
+
* @inheritDoc
|
|
24
|
+
*/
|
|
25
|
+
static get requires() {
|
|
26
|
+
return [ ImageUtils ];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get pluginName() {
|
|
33
|
+
return 'ImageCaptionUI';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @inheritDoc
|
|
38
|
+
*/
|
|
39
|
+
init() {
|
|
40
|
+
const editor = this.editor;
|
|
41
|
+
const editingView = editor.editing.view;
|
|
42
|
+
const imageUtils = editor.plugins.get( 'ImageUtils' );
|
|
43
|
+
const t = editor.t;
|
|
44
|
+
|
|
45
|
+
editor.ui.componentFactory.add( 'toggleImageCaption', locale => {
|
|
46
|
+
const command = editor.commands.get( 'toggleImageCaption' );
|
|
47
|
+
const view = new ButtonView( locale );
|
|
48
|
+
|
|
49
|
+
view.set( {
|
|
50
|
+
icon: icons.caption,
|
|
51
|
+
tooltip: true,
|
|
52
|
+
isToggleable: true
|
|
53
|
+
} );
|
|
54
|
+
|
|
55
|
+
view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
|
|
56
|
+
view.bind( 'label' ).to( command, 'value', value => value ? t( 'Toggle caption off' ) : t( 'Toggle caption on' ) );
|
|
57
|
+
|
|
58
|
+
this.listenTo( view, 'execute', () => {
|
|
59
|
+
editor.execute( 'toggleImageCaption', { focusCaptionOnShow: true } );
|
|
60
|
+
|
|
61
|
+
// Scroll to the selection and highlight the caption if the caption showed up.
|
|
62
|
+
const modelCaptionElement = getCaptionFromModelSelection( imageUtils, editor.model.document.selection );
|
|
63
|
+
|
|
64
|
+
if ( modelCaptionElement ) {
|
|
65
|
+
const figcaptionElement = editor.editing.mapper.toViewElement( modelCaptionElement );
|
|
66
|
+
|
|
67
|
+
editingView.scrollToTheSelection();
|
|
68
|
+
|
|
69
|
+
editingView.change( writer => {
|
|
70
|
+
writer.addClass( 'image__caption_highlighted', figcaptionElement );
|
|
71
|
+
} );
|
|
72
|
+
}
|
|
73
|
+
} );
|
|
74
|
+
|
|
75
|
+
return view;
|
|
76
|
+
} );
|
|
77
|
+
}
|
|
78
|
+
}
|