|
@@ -79,8 +79,8 @@ export default class ImageCaptionEditing extends Plugin {
|
|
|
// Hide caption when everything is removed from it.
|
|
// Hide caption when everything is removed from it.
|
|
|
editing.downcastDispatcher.on( 'remove', this._fixCaptionVisibility( data => data.position.parent ), { priority: 'high' } );
|
|
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.
|
|
* and then visible when the image is selected.
|
|
|
*
|
|
*
|
|
|
* @private
|
|
* @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 mapper = this.editor.editing.mapper;
|
|
|
|
|
+ const lastCaption = this._lastSelectedCaption;
|
|
|
let viewCaption;
|
|
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.
|
|
// If whole image is selected.
|
|
|
const modelSelection = this.editor.model.document.selection;
|
|
const modelSelection = this.editor.model.document.selection;
|
|
|
const selectedElement = modelSelection.getSelectedElement();
|
|
const selectedElement = modelSelection.getSelectedElement();
|
|
@@ -115,9 +113,33 @@ export default class ImageCaptionEditing extends Plugin {
|
|
|
viewCaption = mapper.toViewElement( modelCaption );
|
|
viewCaption = mapper.toViewElement( modelCaption );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Is currently any caption selected?
|
|
|
if ( viewCaption ) {
|
|
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;
|
|
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;
|
|
|
|
|
+}
|