8
0

modelconsumable.js 12 KB

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