imagecaptionengine.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/imagecaption/imagecaptionengine
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
  10. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  11. import { isImage } from '../image/utils';
  12. import {
  13. captionElementCreator,
  14. getCaptionFromImage,
  15. matchImageCaption
  16. } from './utils';
  17. /**
  18. * The image caption engine plugin.
  19. *
  20. * It registers proper converters. It takes care of adding a caption element if the image without it is inserted
  21. * to the model document.
  22. *
  23. * @extends module:core/plugin~Plugin
  24. */
  25. export default class ImageCaptionEngine extends Plugin {
  26. /**
  27. * @inheritDoc
  28. */
  29. init() {
  30. const editor = this.editor;
  31. const view = editor.editing.view;
  32. const schema = editor.model.schema;
  33. const data = editor.data;
  34. const editing = editor.editing;
  35. const t = editor.t;
  36. /**
  37. * Last selected caption editable.
  38. * It is used for hiding the editable when it is empty and the image widget is no longer selected.
  39. *
  40. * @private
  41. * @member {module:engine/view/editableelement~EditableElement} #_lastSelectedCaption
  42. */
  43. // Schema configuration.
  44. schema.register( 'caption', {
  45. allowIn: 'image',
  46. allowContentOf: '$block',
  47. isLimit: true
  48. } );
  49. // Add caption element to each image inserted without it.
  50. editor.model.document.registerPostFixer( writer => this._insertMissingModelCaptionElement( writer ) );
  51. // View to model converter for the data pipeline.
  52. editor.conversion.for( 'upcast' ).add( upcastElementToElement( {
  53. view: matchImageCaption,
  54. model: 'caption'
  55. } ) );
  56. // Model to view converter for the data pipeline.
  57. const createCaptionForData = writer => writer.createContainerElement( 'figcaption' );
  58. data.downcastDispatcher.on( 'insert:caption', captionModelToView( createCaptionForData, false ) );
  59. // Model to view converter for the editing pipeline.
  60. const createCaptionForEditing = captionElementCreator( view, t( 'Enter image caption' ) );
  61. editing.downcastDispatcher.on( 'insert:caption', captionModelToView( createCaptionForEditing ) );
  62. // Always show caption in view when something is inserted in model.
  63. editing.downcastDispatcher.on(
  64. 'insert',
  65. this._fixCaptionVisibility( data => data.item ),
  66. { priority: 'high' }
  67. );
  68. // Hide caption when everything is removed from it.
  69. editing.downcastDispatcher.on( 'remove', this._fixCaptionVisibility( data => data.position.parent ), { priority: 'high' } );
  70. // Update view before each rendering.
  71. this.listenTo( view, 'render', () => this._updateCaptionVisibility( view ) );
  72. }
  73. /**
  74. * Updates the view before each rendering, making sure that empty captions (so unnecessary ones) are hidden
  75. * and then visible when the image is selected.
  76. *
  77. * @private
  78. */
  79. _updateCaptionVisibility( view ) {
  80. const mapper = this.editor.editing.mapper;
  81. let viewCaption;
  82. // Hide last selected caption if have no child elements.
  83. if ( this._lastSelectedCaption && !this._lastSelectedCaption.childCount ) {
  84. view.change( writer => writer.addClass( 'ck-hidden', this._lastSelectedCaption ) );
  85. }
  86. // If whole image is selected.
  87. const modelSelection = this.editor.model.document.selection;
  88. const selectedElement = modelSelection.getSelectedElement();
  89. if ( selectedElement && selectedElement.is( 'image' ) ) {
  90. const modelCaption = getCaptionFromImage( selectedElement );
  91. viewCaption = mapper.toViewElement( modelCaption );
  92. }
  93. // If selection is placed inside caption.
  94. const position = modelSelection.getFirstPosition();
  95. const modelCaption = getParentCaption( position.parent );
  96. if ( modelCaption ) {
  97. viewCaption = mapper.toViewElement( modelCaption );
  98. }
  99. if ( viewCaption ) {
  100. view.change( writer => writer.removeClass( 'ck-hidden', viewCaption ) );
  101. this._lastSelectedCaption = viewCaption;
  102. }
  103. }
  104. /**
  105. * Returns converter that fixes caption visibility during the model-to-view conversion.
  106. * Checks if the changed node is placed inside the caption element and fixes its visibility in the view.
  107. *
  108. * @private
  109. * @param {Function} nodeFinder
  110. * @returns {Function}
  111. */
  112. _fixCaptionVisibility( nodeFinder ) {
  113. return ( evt, data, consumable, conversionApi ) => {
  114. // There is no consumable on 'remove' event.
  115. conversionApi = conversionApi ? conversionApi : consumable;
  116. const node = nodeFinder( data );
  117. const modelCaption = getParentCaption( node );
  118. const mapper = this.editor.editing.mapper;
  119. const viewWriter = conversionApi.writer;
  120. if ( modelCaption ) {
  121. const viewCaption = mapper.toViewElement( modelCaption );
  122. if ( viewCaption ) {
  123. if ( modelCaption.childCount ) {
  124. viewWriter.removeClass( 'ck-hidden', viewCaption );
  125. } else {
  126. viewWriter.addClass( 'ck-hidden', viewCaption );
  127. }
  128. }
  129. }
  130. };
  131. }
  132. /**
  133. * Checks whether data inserted to the model document have image element that has no caption element inside it.
  134. * If there is none - adds it to the image element.
  135. *
  136. * @private
  137. * @param {module:engine/model/writer~Writer} writer Writer to make changes with.
  138. * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
  139. */
  140. _insertMissingModelCaptionElement( writer ) {
  141. const model = this.editor.model;
  142. const changes = model.document.differ.getChanges();
  143. for ( const entry of changes ) {
  144. if ( entry.type == 'insert' && entry.name == 'image' ) {
  145. const item = entry.position.nodeAfter;
  146. if ( !getCaptionFromImage( item ) ) {
  147. writer.appendElement( 'caption', item );
  148. return true;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. // Creates a converter that converts image caption model element to view element.
  155. //
  156. // @private
  157. // @param {Function} elementCreator
  158. // @param {Boolean} [hide=true] When set to `false` view element will not be inserted when it's empty.
  159. // @return {Function}
  160. function captionModelToView( elementCreator, hide = true ) {
  161. return ( evt, data, conversionApi ) => {
  162. const captionElement = data.item;
  163. // Return if element shouldn't be present when empty.
  164. if ( !captionElement.childCount && !hide ) {
  165. return;
  166. }
  167. if ( isImage( captionElement.parent ) ) {
  168. if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
  169. return;
  170. }
  171. const viewImage = conversionApi.mapper.toViewElement( data.range.start.parent );
  172. const viewCaption = elementCreator( conversionApi.writer );
  173. const viewWriter = conversionApi.writer;
  174. // Hide if empty.
  175. if ( !captionElement.childCount ) {
  176. viewWriter.addClass( 'ck-hidden', viewCaption );
  177. }
  178. insertViewCaptionAndBind( viewCaption, data.item, viewImage, conversionApi );
  179. }
  180. };
  181. }
  182. // Inserts `viewCaption` at the end of `viewImage` and binds it to `modelCaption`.
  183. //
  184. // @private
  185. // @param {module:engine/view/containerelement~ContainerElement} viewCaption
  186. // @param {module:engine/model/element~Element} modelCaption
  187. // @param {module:engine/view/containerelement~ContainerElement} viewImage
  188. // @param {Object} conversionApi
  189. function insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, conversionApi ) {
  190. const viewPosition = ViewPosition.createAt( viewImage, 'end' );
  191. conversionApi.writer.insert( viewPosition, viewCaption );
  192. conversionApi.mapper.bindElements( modelCaption, viewCaption );
  193. }
  194. // Checks if the provided node or one of its ancestors is a caption element, and returns it.
  195. //
  196. // @private
  197. // @param {module:engine/model/node~Node} node
  198. // @returns {module:engine/model/element~Element|null}
  199. function getParentCaption( node ) {
  200. const ancestors = node.getAncestors( { includeSelf: true } );
  201. const caption = ancestors.find( ancestor => ancestor.name == 'caption' );
  202. if ( caption && caption.parent && caption.parent.name == 'image' ) {
  203. return caption;
  204. }
  205. return null;
  206. }