8
0
Просмотр исходного кода

Merge branch 'master' into t/ckeditor5/488

Szymon Cofalik 8 лет назад
Родитель
Сommit
f20e6a5c21

+ 62 - 10
packages/ckeditor5-image/src/imagecaption/imagecaptionediting.js

@@ -79,8 +79,8 @@ export default class ImageCaptionEditing extends Plugin {
 		// Hide caption when everything is removed from it.
 		editing.downcastDispatcher.on( 'remove', this._fixCaptionVisibility( data => data.position.parent ), { priority: 'high' } );
 
-		// Update view before each rendering.
-		this.listenTo( view, 'render', () => this._updateCaptionVisibility( view ) );
+		// Update caption visibility on view in post fixer.
+		view.document.registerPostFixer( writer => this._updateCaptionVisibility( writer ) );
 	}
 
 	/**
@@ -88,16 +88,14 @@ export default class ImageCaptionEditing extends Plugin {
 	 * and then visible when the image is selected.
 	 *
 	 * @private
+	 * @param {module:engine/view/writer~Writer} viewWriter
+	 * @returns {Boolean} Returns `true` when view is updated.
 	 */
-	_updateCaptionVisibility( view ) {
+	_updateCaptionVisibility( viewWriter ) {
 		const mapper = this.editor.editing.mapper;
+		const lastCaption = this._lastSelectedCaption;
 		let viewCaption;
 
-		// Hide last selected caption if have no child elements.
-		if ( this._lastSelectedCaption && !this._lastSelectedCaption.childCount ) {
-			view.change( writer => writer.addClass( 'ck-hidden', this._lastSelectedCaption ) );
-		}
-
 		// If whole image is selected.
 		const modelSelection = this.editor.model.document.selection;
 		const selectedElement = modelSelection.getSelectedElement();
@@ -115,9 +113,33 @@ export default class ImageCaptionEditing extends Plugin {
 			viewCaption = mapper.toViewElement( modelCaption );
 		}
 
+		// Is currently any caption selected?
 		if ( viewCaption ) {
-			view.change( writer => writer.removeClass( 'ck-hidden', viewCaption ) );
-			this._lastSelectedCaption = viewCaption;
+			// Was any caption selected before?
+			if ( lastCaption ) {
+				// Same caption as before?
+				if ( lastCaption === viewCaption ) {
+					return showCaption( viewCaption, viewWriter );
+				} else {
+					hideCaptionIfEmpty( lastCaption, viewWriter );
+					this._lastSelectedCaption = viewCaption;
+
+					return showCaption( viewCaption, viewWriter );
+				}
+			} else {
+				this._lastSelectedCaption = viewCaption;
+				return showCaption( viewCaption, viewWriter );
+			}
+		} else {
+			// Was any caption selected before?
+			if ( lastCaption ) {
+				const viewModified = hideCaptionIfEmpty( lastCaption, viewWriter );
+				this._lastSelectedCaption = null;
+
+				return viewModified;
+			} else {
+				return false;
+			}
 		}
 	}
 
@@ -239,3 +261,33 @@ function getParentCaption( node ) {
 
 	return null;
 }
+
+// Hides given caption in the view if it's empty.
+//
+// @private
+// @param {module:engine/view/containerelement~ContainerElement} caption
+// @param {module:engine/view/writer~Writer} viewWriter
+// @returns {Boolean} Returns `true` if view was modified.
+function hideCaptionIfEmpty( caption, viewWriter ) {
+	if ( !caption.childCount && !caption.hasClass( 'ck-hidden' ) ) {
+		viewWriter.addClass( 'ck-hidden', caption );
+		return true;
+	}
+
+	return false;
+}
+
+// Shows the caption
+//
+// @private
+// @param {module:engine/view/containerelement~ContainerElement} caption
+// @param {module:engine/view/writer~Writer} viewWriter
+// @returns {Boolean} Returns `true` if view was modified.
+function showCaption( caption, viewWriter ) {
+	if ( caption.hasClass( 'ck-hidden' ) ) {
+		viewWriter.removeClass( 'ck-hidden', caption );
+		return true;
+	}
+
+	return false;
+}

+ 1 - 1
packages/ckeditor5-image/src/imagetoolbar.js

@@ -94,7 +94,7 @@ export default class ImageToolbar extends Plugin {
 		// Show balloon panel each time image widget is selected.
 		this.listenTo( editor.editing.view, 'render', () => {
 			this._checkIsVisible();
-		}, { priority: 'low' } );
+		} );
 
 		// There is no render method after focus is back in editor, we need to check if balloon panel should be visible.
 		this.listenTo( editor.ui.focusTracker, 'change:isFocused', () => {