modelconsumable.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import TextProxy from '../treemodel/textproxy.js';
  7. /**
  8. * Manages a list of consumable values for {@link engine.treeModel.Item model items}.
  9. *
  10. * Consumables are various aspects of the model items. A model item can be broken down into singular properties
  11. * that might be taken into consideration when converting that item.
  12. *
  13. * `ModelConsumable` is used by {@link engine.treeController.ModelConversionDispatcher} while analyzing changed
  14. * parts of {@link engine.treeModel.Document the document}. The added / changed / removed model items are broken down
  15. * into singular properties (the item itself and it's attributes). All those parts are saved in `ModelConsumable`. Then,
  16. * during conversion, when given part of model item is converted (i.e. the view element has been inserted into the view,
  17. * but without attributes), consumable value is removed from `ModelConsumable`.
  18. *
  19. * Although `ModelConsumable` might store consumable values of any type (see {@link engine.treeController.ModelConsumable#add}),
  20. * the commonly used ones are similar to events names fired by {@link engine.treeController.ModelConversionDispatcher}:
  21. * `insert`, `addAttribute:<attributeKey>`, `changeAttribute:<attributeKey>`, `removeAttribute:<attributeKey>`.
  22. *
  23. * Most commonly, {@link engine.treeController.ModelConsumable#add add method} will be used only by
  24. * {@link engine.treeController.ModelConversionDispatcher} to gather consumable values. However, it is important to
  25. * understand how consumable values can be {@link engine.treeController.ModelConsumable#consume consumed}. See also:
  26. * {@link engine.treeController.modelToView}.
  27. *
  28. * Keep in mind, that one conversion event may have multiple callbacks (converters) attached to it. Each of those is
  29. * able to convert one or more parts of the model. However, when one of those callbacks actually converts
  30. * something, other should not, because they would duplicate the results. Using `ModelConsumable` helps avoiding
  31. * this situation, because callbacks should only convert those values, which were not yet consumed from `ModelConsumable`.
  32. *
  33. * Consuming multiple values in a single callback:
  34. *
  35. * // Converter for custom `image` element that might have a `caption` element inside which changes
  36. * // how the image is displayed in the view:
  37. * //
  38. * // Model:
  39. * //
  40. * // [image]
  41. * // └─ [caption]
  42. * // ├─ f
  43. * // ├─ o
  44. * // └─ o
  45. * //
  46. * // View:
  47. * //
  48. * // <figure>
  49. * // ├─ <img />
  50. * // └─ <caption>
  51. * // └─ foo
  52. * modelConversionDispatcher.on( 'insert:image', ( evt, data, consumable, conversionApi ) => {
  53. * // First, consume the `image` element.
  54. * consumable.consume( data.item, 'insert' );
  55. *
  56. * // Just create normal image element for the view.
  57. * // Maybe it will be "decorated" later.
  58. * const viewImage = new ViewElement( 'img' );
  59. * const insertPosition = conversionApi.mapper.toViewPosition( data.range.start );
  60. *
  61. * // Check if the `image` element has children.
  62. * if ( data.item.getChildCount() > 0 ) {
  63. * const modelCaption = data.item.getChild( 0 );
  64. *
  65. * // `modelCaption` insertion change is consumed from consumable values.
  66. * // It will not be converted by other converters, but it's children (probably some text) will be.
  67. * // Through mapping, converters for text will know where to insert contents of `modelCaption`.
  68. * if ( consumable.consume( modelCaption, 'insert' ) ) {
  69. * const viewCaption = new ViewElement( 'figcaption' );
  70. *
  71. * const viewImageHolder = new ViewElement( 'figure', null, [ viewImage, viewCaption ] );
  72. *
  73. * conversionApi.mapper.bindElements( modelCaption, viewCaption );
  74. * conversionApi.mapper.bindElements( data.item, viewImageHolder );
  75. * conversionApi.writer.insert( insertPosition, viewImageHolder );
  76. * }
  77. * } else {
  78. * conversionApi.mapper.bindElements( data.item, viewImage );
  79. * conversionApi.writer.insert( insertPosition, viewImage );
  80. * }
  81. *
  82. * evt.stop();
  83. * } );
  84. *
  85. * @memberOf engine.treeController
  86. */
  87. export default class ModelConsumable {
  88. /**
  89. * Creates an empty consumables list.
  90. */
  91. constructor() {
  92. /**
  93. * Contains list of consumable values.
  94. *
  95. * @private
  96. * @member {Map} engine.treeController.ModelConsumable#_consumable
  97. */
  98. this._consumable = new Map();
  99. /**
  100. * For each {@link engine.treeModel.TextProxy} added to `ModelConsumable`, this registry holds parent
  101. * of that `TextProxy` and start and end indices of that `TextProxy`. This allows identification of `TextProxy`
  102. * instances that points to the same part of the model but are different instances. Each distinct `TextProxy`
  103. * is given unique `Symbol` which is then registered as consumable. This process is transparent for `ModelConsumable`
  104. * API user because whenever `TextProxy` is added, tested, consumed or reverted, internal mechanisms of
  105. * `ModelConsumable` translates `TextProxy` to that unique `Symbol`.
  106. *
  107. * @private
  108. * @member {Map} engine.treeController.ModelConsumable#_textParentsRegistry
  109. */
  110. this._textProxyRegistry = new Map();
  111. }
  112. /**
  113. * Adds a consumable value to the consumables list and links it with given model item.
  114. *
  115. * modelConsumable.add( modelElement, 'insert' ); // Add `modelElement` insertion change to consumable values.
  116. * modelConsumable.add( modelElement, 'addAttribute:bold' ); // Add `bold` attribute insertion on `modelElement` change.
  117. * modelConsumable.add( modelElement, 'removeAttribute:bold' ); // Add `bold` attribute removal on `modelElement` change.
  118. *
  119. * @param {engine.treeModel.Item} item Model item that has the consumable.
  120. * @param {String} type Consumable type.
  121. */
  122. add( item, type ) {
  123. if ( item instanceof TextProxy ) {
  124. item = this._getSymbolForTextProxy( item );
  125. }
  126. if ( !this._consumable.has( item ) ) {
  127. this._consumable.set( item, new Map() );
  128. }
  129. this._consumable.get( item ).set( type, true );
  130. }
  131. /**
  132. * Removes given consumable value from given model item.
  133. *
  134. * modelConsumable.consume( modelElement, 'insert' ); // Remove `modelElement` insertion change from consumable values.
  135. * modelConsumable.consume( modelElement, 'addAttribute:bold' ); // Remove `bold` attribute insertion on `modelElement` change.
  136. * modelConsumable.consume( modelElement, 'removeAttribute:bold' ); // Remove `bold` attribute removal on `modelElement` change.
  137. *
  138. * @param {engine.treeModel.Item} item Model item from which consumable will be consumed.
  139. * @param {String} type Consumable type.
  140. * @returns {Boolean} `true` if consumable value was available and was consumed, `false` otherwise.
  141. */
  142. consume( item, type ) {
  143. if ( item instanceof TextProxy ) {
  144. item = this._getSymbolForTextProxy( item );
  145. }
  146. if ( this.test( item, type ) ) {
  147. this._consumable.get( item ).set( type, false );
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. /**
  154. * Tests whether there is a consumable value of given type connected with given model item.
  155. *
  156. * modelConsumable.test( modelElement, 'insert' ); // Check for `modelElement` insertion change.
  157. * modelConsumable.test( modelElement, 'addAttribute:bold' ); // Check for `bold` attribute insertion on `modelElement` change.
  158. * modelConsumable.test( modelElement, 'removeAttribute:bold' ); // Check for `bold` attribute removal on `modelElement` change.
  159. *
  160. * @param {engine.treeModel.Item} item Model item that will be tested.
  161. * @param {String} type Consumable type.
  162. * @returns {null|Boolean} `null` if such consumable was never added, `false` if the consumable values was
  163. * already consumed or `true` if it was added and not consumed yet.
  164. */
  165. test( item, type ) {
  166. if ( item instanceof TextProxy ) {
  167. item = this._getSymbolForTextProxy( item );
  168. }
  169. const itemConsumables = this._consumable.get( item );
  170. if ( itemConsumables === undefined ) {
  171. return null;
  172. }
  173. const value = itemConsumables.get( type );
  174. if ( value === undefined ) {
  175. return null;
  176. }
  177. return value;
  178. }
  179. /**
  180. * Reverts consuming of consumable value.
  181. *
  182. * modelConsumable.revert( modelElement, 'insert' ); // Revert consuming `modelElement` insertion change.
  183. * modelConsumable.revert( modelElement, 'addAttribute:bold' ); // Revert consuming `bold` attribute insert from `modelElement`.
  184. * modelConsumable.revert( modelElement, 'removeAttribute:bold' ); // Revert consuming `bold` attribute remove from `modelElement`.
  185. *
  186. * @param {engine.treeModel.Item} item Model item that will be tested.
  187. * @param {String} type Consumable type.
  188. * @returns {null|Boolean} `true` if consumable has been reversed, `false` otherwise. `null` if the consumable has
  189. * never been added.
  190. */
  191. revert( item, type ) {
  192. if ( item instanceof TextProxy ) {
  193. item = this._getSymbolForTextProxy( item );
  194. }
  195. const test = this.test( item, type );
  196. if ( test === false ) {
  197. this._consumable.get( item ).set( type, true );
  198. return true;
  199. } else if ( test === true ) {
  200. return false;
  201. }
  202. return null;
  203. }
  204. /**
  205. * Gets a unique symbol for passed {@link engine.treeModel.TextProxy} instance. All `TextProxy` instances that
  206. * have same parent, same start index and same end index will get the same symbol.
  207. *
  208. * Used internally to correctly consume `TextProxy` instances.
  209. *
  210. * @private
  211. * @param {engine.treeModel.TextProxy} textProxy `TextProxy` instance to get a symbol for.
  212. * @returns {Symbol} Symbol representing all equal instances of `TextProxy`.
  213. */
  214. _getSymbolForTextProxy( textProxy ) {
  215. let symbol = null;
  216. const startIndex = textProxy.first.getIndex();
  217. const endIndex = startIndex + textProxy.text.length;
  218. const parent = textProxy.commonParent;
  219. const startIndexMap = this._textProxyRegistry.get( startIndex );
  220. if ( startIndexMap ) {
  221. const endIndexMap = startIndexMap.get( endIndex );
  222. if ( endIndexMap ) {
  223. symbol = endIndexMap.get( parent );
  224. }
  225. }
  226. if ( !symbol ) {
  227. symbol = this._addSymbolForTextProxy( startIndex, endIndex, parent );
  228. }
  229. return symbol;
  230. }
  231. /**
  232. * Adds a symbol for given properties that characterizes a {@link engine.treeModel.TextProxy} instance.
  233. *
  234. * Used internally to correctly consume `TextProxy` instances.
  235. *
  236. * @private
  237. * @param {Number} startIndex Text proxy start index in it's parent.
  238. * @param {Number} endIndex Text proxy end index in it's parent.
  239. * @param {engine.treeModel.Element} parent Text proxy parent.
  240. * @returns {Symbol} Symbol generated for given properties.
  241. */
  242. _addSymbolForTextProxy( startIndex, endIndex, parent ) {
  243. const symbol = Symbol();
  244. let startIndexMap, endIndexMap;
  245. startIndexMap = this._textProxyRegistry.get( startIndex );
  246. if ( !startIndexMap ) {
  247. startIndexMap = new Map();
  248. this._textProxyRegistry.set( startIndex, startIndexMap );
  249. }
  250. endIndexMap = startIndexMap.get( endIndex );
  251. if ( !endIndexMap ) {
  252. endIndexMap = new Map();
  253. startIndexMap.set( endIndex, endIndexMap );
  254. }
  255. endIndexMap.set( parent, symbol );
  256. return symbol;
  257. }
  258. }