downcastdispatcher.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/conversion/downcastdispatcher
  7. */
  8. import Consumable from './modelconsumable';
  9. import Range from '../model/range';
  10. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  11. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  12. import { extend } from 'lodash-es';
  13. /**
  14. * `DowncastDispatcher` is a central point of downcasting (conversion from model to view), which is a process of reacting to changes
  15. * in the model and firing a set of events. Callbacks listening to those events are called converters. Those
  16. * converters role is to convert the model changes to changes in view (for example, adding view nodes or
  17. * changing attributes on view elements).
  18. *
  19. * During conversion process, `DowncastDispatcher` fires events, basing on state of the model and prepares
  20. * data for those events. It is important to understand that those events are connected with changes done on model,
  21. * for example: "node has been inserted" or "attribute has changed". This is in a contrary to upcasting (view to model conversion),
  22. * where we convert view state (view nodes) to a model tree.
  23. *
  24. * The events are prepared basing on a diff created by {@link module:engine/model/differ~Differ Differ}, which buffers them
  25. * and then passes to `DowncastDispatcher` as a diff between old model state and new model state.
  26. *
  27. * Note, that because changes are converted there is a need to have a mapping between model structure and view structure.
  28. * To map positions and elements during downcast (model to view conversion) use {@link module:engine/conversion/mapper~Mapper}.
  29. *
  30. * `DowncastDispatcher` fires following events for model tree changes:
  31. *
  32. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert insert}
  33. * if a range of nodes has been inserted to the model tree,
  34. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:remove remove}
  35. * if a range of nodes has been removed from the model tree,
  36. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute attribute}
  37. * if attribute has been added, changed or removed from a model node.
  38. *
  39. * For {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert insert}
  40. * and {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute attribute},
  41. * `DowncastDispatcher` generates {@link module:engine/conversion/modelconsumable~ModelConsumable consumables}.
  42. * These are used to have a control over which changes has been already consumed. It is useful when some converters
  43. * overwrite other or converts multiple changes (for example converts insertion of an element and also converts that
  44. * element's attributes during insertion).
  45. *
  46. * Additionally, `DowncastDispatcher` fires events for {@link module:engine/model/markercollection~Marker marker} changes:
  47. *
  48. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker} if a marker has been added,
  49. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:removeMarker} if a marker has been removed.
  50. *
  51. * Note, that changing a marker is done through removing the marker from the old range, and adding on the new range,
  52. * so both those events are fired.
  53. *
  54. * Finally, `DowncastDispatcher` also handles firing events for {@link module:engine/model/selection model selection}
  55. * conversion:
  56. *
  57. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:selection}
  58. * which converts selection from model to view,
  59. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute}
  60. * which is fired for every selection attribute,
  61. * * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker}
  62. * which is fired for every marker which contains selection.
  63. *
  64. * Unlike model tree and markers, events for selection are not fired for changes but for selection state.
  65. *
  66. * When providing custom listeners for `DowncastDispatcher` remember to check whether given change has not been
  67. * {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed} yet.
  68. *
  69. * When providing custom listeners for `DowncastDispatcher` keep in mind that any callback that had
  70. * {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed} a value from a consumable and
  71. * converted the change should also stop the event (for efficiency purposes).
  72. *
  73. * When providing custom listeners for `DowncastDispatcher` remember to use provided
  74. * {@link module:engine/view/downcastwriter~DowncastWriter view downcast writer} to apply changes to the view document.
  75. *
  76. * Example of a custom converter for `DowncastDispatcher`:
  77. *
  78. * // We will convert inserting "paragraph" model element into the model.
  79. * downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  80. * // Remember to check whether the change has not been consumed yet and consume it.
  81. * if ( conversionApi.consumable.consume( data.item, 'insert' ) ) {
  82. * return;
  83. * }
  84. *
  85. * // Translate position in model to position in view.
  86. * const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  87. *
  88. * // Create <p> element that will be inserted in view at `viewPosition`.
  89. * const viewElement = conversionApi.writer.createContainerElement( 'p' );
  90. *
  91. * // Bind the newly created view element to model element so positions will map accordingly in future.
  92. * conversionApi.mapper.bindElements( data.item, viewElement );
  93. *
  94. * // Add the newly created view element to the view.
  95. * conversionApi.writer.insert( viewPosition, viewElement );
  96. *
  97. * // Remember to stop the event propagation.
  98. * evt.stop();
  99. * } );
  100. */
  101. export default class DowncastDispatcher {
  102. /**
  103. * Creates a `DowncastDispatcher` instance.
  104. *
  105. * @see module:engine/conversion/downcastdispatcher~DowncastConversionApi
  106. * @param {Object} [conversionApi] Additional properties for interface that will be passed to events fired
  107. * by `DowncastDispatcher`.
  108. */
  109. constructor( conversionApi = {} ) {
  110. /**
  111. * Interface passed by dispatcher to the events callbacks.
  112. *
  113. * @member {module:engine/conversion/downcastdispatcher~DowncastConversionApi}
  114. */
  115. this.conversionApi = extend( { dispatcher: this }, conversionApi );
  116. }
  117. /**
  118. * Takes {@link module:engine/model/differ~Differ model differ} object with buffered changes and fires conversion basing on it.
  119. *
  120. * @param {module:engine/model/differ~Differ} differ Differ object with buffered changes.
  121. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  122. */
  123. convertChanges( differ, writer ) {
  124. // Before the view is updated, remove markers which have changed.
  125. for ( const change of differ.getMarkersToRemove() ) {
  126. this.convertMarkerRemove( change.name, change.range, writer );
  127. }
  128. // Convert changes that happened on model tree.
  129. for ( const entry of differ.getChanges() ) {
  130. if ( entry.type == 'insert' ) {
  131. this.convertInsert( Range._createFromPositionAndShift( entry.position, entry.length ), writer );
  132. } else if ( entry.type == 'remove' ) {
  133. this.convertRemove( entry.position, entry.length, entry.name, writer );
  134. } else {
  135. // entry.type == 'attribute'.
  136. this.convertAttribute( entry.range, entry.attributeKey, entry.attributeOldValue, entry.attributeNewValue, writer );
  137. }
  138. }
  139. // After the view is updated, convert markers which have changed.
  140. for ( const change of differ.getMarkersToAdd() ) {
  141. this.convertMarkerAdd( change.name, change.range, writer );
  142. }
  143. }
  144. /**
  145. * Starts conversion of a range insertion.
  146. *
  147. * For each node in the range, {@link #event:insert insert event is fired}. For each attribute on each node,
  148. * {@link #event:attribute attribute event is fired}.
  149. *
  150. * @fires insert
  151. * @fires attribute
  152. * @param {module:engine/model/range~Range} range Inserted range.
  153. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  154. */
  155. convertInsert( range, writer ) {
  156. this.conversionApi.writer = writer;
  157. // Create a list of things that can be consumed, consisting of nodes and their attributes.
  158. this.conversionApi.consumable = this._createInsertConsumable( range );
  159. // Fire a separate insert event for each node and text fragment contained in the range.
  160. for ( const value of range ) {
  161. const item = value.item;
  162. const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
  163. const data = {
  164. item,
  165. range: itemRange
  166. };
  167. this._testAndFire( 'insert', data );
  168. // Fire a separate addAttribute event for each attribute that was set on inserted items.
  169. // This is important because most attributes converters will listen only to add/change/removeAttribute events.
  170. // If we would not add this part, attributes on inserted nodes would not be converted.
  171. for ( const key of item.getAttributeKeys() ) {
  172. data.attributeKey = key;
  173. data.attributeOldValue = null;
  174. data.attributeNewValue = item.getAttribute( key );
  175. this._testAndFire( `attribute:${ key }`, data );
  176. }
  177. }
  178. this._clearConversionApi();
  179. }
  180. /**
  181. * Fires conversion of a single node removal. Fires {@link #event:remove remove event} with provided data.
  182. *
  183. * @param {module:engine/model/position~Position} position Position from which node was removed.
  184. * @param {Number} length Offset size of removed node.
  185. * @param {String} name Name of removed node.
  186. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  187. */
  188. convertRemove( position, length, name, writer ) {
  189. this.conversionApi.writer = writer;
  190. this.fire( 'remove:' + name, { position, length }, this.conversionApi );
  191. this._clearConversionApi();
  192. }
  193. /**
  194. * Starts conversion of attribute change on given `range`.
  195. *
  196. * For each node in the given `range`, {@link #event:attribute attribute event} is fired with the passed data.
  197. *
  198. * @fires attribute
  199. * @param {module:engine/model/range~Range} range Changed range.
  200. * @param {String} key Key of the attribute that has changed.
  201. * @param {*} oldValue Attribute value before the change or `null` if the attribute has not been set before.
  202. * @param {*} newValue New attribute value or `null` if the attribute has been removed.
  203. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  204. */
  205. convertAttribute( range, key, oldValue, newValue, writer ) {
  206. this.conversionApi.writer = writer;
  207. // Create a list with attributes to consume.
  208. this.conversionApi.consumable = this._createConsumableForRange( range, `attribute:${ key }` );
  209. // Create a separate attribute event for each node in the range.
  210. for ( const value of range ) {
  211. const item = value.item;
  212. const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
  213. const data = {
  214. item,
  215. range: itemRange,
  216. attributeKey: key,
  217. attributeOldValue: oldValue,
  218. attributeNewValue: newValue
  219. };
  220. this._testAndFire( `attribute:${ key }`, data );
  221. }
  222. this._clearConversionApi();
  223. }
  224. /**
  225. * Starts model selection conversion.
  226. *
  227. * Fires events for given {@link module:engine/model/selection~Selection selection} to start selection conversion.
  228. *
  229. * @fires selection
  230. * @fires addMarker
  231. * @fires attribute
  232. * @param {module:engine/model/selection~Selection} selection Selection to convert.
  233. * @param {Array.<module:engine/model/markercollection~Marker>} markers Array of markers containing model markers.
  234. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  235. */
  236. convertSelection( selection, markers, writer ) {
  237. const markersAtSelection = Array.from( markers.getMarkersAtPosition( selection.getFirstPosition() ) );
  238. this.conversionApi.writer = writer;
  239. this.conversionApi.consumable = this._createSelectionConsumable( selection, markersAtSelection );
  240. this.fire( 'selection', { selection }, this.conversionApi );
  241. if ( !selection.isCollapsed ) {
  242. return;
  243. }
  244. for ( const marker of markersAtSelection ) {
  245. const markerRange = marker.getRange();
  246. if ( !shouldMarkerChangeBeConverted( selection.getFirstPosition(), marker, this.conversionApi.mapper ) ) {
  247. continue;
  248. }
  249. const data = {
  250. item: selection,
  251. markerName: marker.name,
  252. markerRange
  253. };
  254. if ( this.conversionApi.consumable.test( selection, 'addMarker:' + marker.name ) ) {
  255. this.fire( 'addMarker:' + marker.name, data, this.conversionApi );
  256. }
  257. }
  258. for ( const key of selection.getAttributeKeys() ) {
  259. const data = {
  260. item: selection,
  261. range: selection.getFirstRange(),
  262. attributeKey: key,
  263. attributeOldValue: null,
  264. attributeNewValue: selection.getAttribute( key )
  265. };
  266. // Do not fire event if the attribute has been consumed.
  267. if ( this.conversionApi.consumable.test( selection, 'attribute:' + data.attributeKey ) ) {
  268. this.fire( 'attribute:' + data.attributeKey + ':$text', data, this.conversionApi );
  269. }
  270. }
  271. this._clearConversionApi();
  272. }
  273. /**
  274. * Converts added marker. Fires {@link #event:addMarker addMarker} event for each item
  275. * in marker's range. If range is collapsed single event is dispatched. See event description for more details.
  276. *
  277. * @fires addMarker
  278. * @param {String} markerName Marker name.
  279. * @param {module:engine/model/range~Range} markerRange Marker range.
  280. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  281. */
  282. convertMarkerAdd( markerName, markerRange, writer ) {
  283. // Do not convert if range is in graveyard or not in the document (e.g. in DocumentFragment).
  284. if ( !markerRange.root.document || markerRange.root.rootName == '$graveyard' ) {
  285. return;
  286. }
  287. this.conversionApi.writer = writer;
  288. // In markers' case, event name == consumable name.
  289. const eventName = 'addMarker:' + markerName;
  290. //
  291. // First, fire an event for the whole marker.
  292. //
  293. const consumable = new Consumable();
  294. consumable.add( markerRange, eventName );
  295. this.conversionApi.consumable = consumable;
  296. this.fire( eventName, { markerName, markerRange }, this.conversionApi );
  297. //
  298. // Do not fire events for each item inside the range if the range got consumed.
  299. //
  300. if ( !consumable.test( markerRange, eventName ) ) {
  301. return;
  302. }
  303. //
  304. // Then, fire an event for each item inside the marker range.
  305. //
  306. this.conversionApi.consumable = this._createConsumableForRange( markerRange, eventName );
  307. for ( const item of markerRange.getItems() ) {
  308. // Do not fire event for already consumed items.
  309. if ( !this.conversionApi.consumable.test( item, eventName ) ) {
  310. continue;
  311. }
  312. const data = { item, range: Range._createOn( item ), markerName, markerRange };
  313. this.fire( eventName, data, this.conversionApi );
  314. }
  315. this._clearConversionApi();
  316. }
  317. /**
  318. * Fires conversion of marker removal. Fires {@link #event:removeMarker removeMarker} event with provided data.
  319. *
  320. * @fires removeMarker
  321. * @param {String} markerName Marker name.
  322. * @param {module:engine/model/range~Range} markerRange Marker range.
  323. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  324. */
  325. convertMarkerRemove( markerName, markerRange, writer ) {
  326. // Do not convert if range is in graveyard or not in the document (e.g. in DocumentFragment).
  327. if ( !markerRange.root.document || markerRange.root.rootName == '$graveyard' ) {
  328. return;
  329. }
  330. this.conversionApi.writer = writer;
  331. this.fire( 'removeMarker:' + markerName, { markerName, markerRange }, this.conversionApi );
  332. this._clearConversionApi();
  333. }
  334. /**
  335. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume from given range,
  336. * assuming that the range has just been inserted to the model.
  337. *
  338. * @private
  339. * @param {module:engine/model/range~Range} range Inserted range.
  340. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  341. */
  342. _createInsertConsumable( range ) {
  343. const consumable = new Consumable();
  344. for ( const value of range ) {
  345. const item = value.item;
  346. consumable.add( item, 'insert' );
  347. for ( const key of item.getAttributeKeys() ) {
  348. consumable.add( item, 'attribute:' + key );
  349. }
  350. }
  351. return consumable;
  352. }
  353. /**
  354. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume for given range.
  355. *
  356. * @private
  357. * @param {module:engine/model/range~Range} range Affected range.
  358. * @param {String} type Consumable type.
  359. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  360. */
  361. _createConsumableForRange( range, type ) {
  362. const consumable = new Consumable();
  363. for ( const item of range.getItems() ) {
  364. consumable.add( item, type );
  365. }
  366. return consumable;
  367. }
  368. /**
  369. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with selection consumable values.
  370. *
  371. * @private
  372. * @param {module:engine/model/selection~Selection} selection Selection to create consumable from.
  373. * @param {Iterable.<module:engine/model/markercollection~Marker>} markers Markers which contains selection.
  374. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  375. */
  376. _createSelectionConsumable( selection, markers ) {
  377. const consumable = new Consumable();
  378. consumable.add( selection, 'selection' );
  379. for ( const marker of markers ) {
  380. consumable.add( selection, 'addMarker:' + marker.name );
  381. }
  382. for ( const key of selection.getAttributeKeys() ) {
  383. consumable.add( selection, 'attribute:' + key );
  384. }
  385. return consumable;
  386. }
  387. /**
  388. * Tests passed `consumable` to check whether given event can be fired and if so, fires it.
  389. *
  390. * @private
  391. * @fires insert
  392. * @fires attribute
  393. * @param {String} type Event type.
  394. * @param {Object} data Event data.
  395. */
  396. _testAndFire( type, data ) {
  397. if ( !this.conversionApi.consumable.test( data.item, type ) ) {
  398. // Do not fire event if the item was consumed.
  399. return;
  400. }
  401. const name = data.item.name || '$text';
  402. this.fire( type + ':' + name, data, this.conversionApi );
  403. }
  404. /**
  405. * Clears conversion API object.
  406. *
  407. * @private
  408. */
  409. _clearConversionApi() {
  410. delete this.conversionApi.writer;
  411. delete this.conversionApi.consumable;
  412. }
  413. /**
  414. * Fired for inserted nodes.
  415. *
  416. * `insert` is a namespace for a class of events. Names of actually called events follow this pattern:
  417. * `insert:name`. `name` is either `'$text'`, when {@link module:engine/model/text~Text a text node} has been inserted,
  418. * or {@link module:engine/model/element~Element#name name} of inserted element.
  419. *
  420. * This way listeners can either listen to a general `insert` event or specific event (for example `insert:paragraph`).
  421. *
  422. * @event insert
  423. * @param {Object} data Additional information about the change.
  424. * @param {module:engine/model/item~Item} data.item Inserted item.
  425. * @param {module:engine/model/range~Range} data.range Range spanning over inserted item.
  426. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
  427. * to be used by callback, passed in `DowncastDispatcher` constructor.
  428. */
  429. /**
  430. * Fired for removed nodes.
  431. *
  432. * `remove` is a namespace for a class of events. Names of actually called events follow this pattern:
  433. * `remove:name`. `name` is either `'$text'`, when {@link module:engine/model/text~Text a text node} has been removed,
  434. * or the {@link module:engine/model/element~Element#name name} of removed element.
  435. *
  436. * This way listeners can either listen to a general `remove` event or specific event (for example `remove:paragraph`).
  437. *
  438. * @event remove
  439. * @param {Object} data Additional information about the change.
  440. * @param {module:engine/model/position~Position} data.position Position from which the node has been removed.
  441. * @param {Number} data.length Offset size of the removed node.
  442. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
  443. * to be used by callback, passed in `DowncastDispatcher` constructor.
  444. */
  445. /**
  446. * Fired in the following cases:
  447. *
  448. * * when an attribute has been added, changed, or removed from a node,
  449. * * when a node with an attribute is inserted,
  450. * * when collapsed model selection attribute is converted.
  451. *
  452. * `attribute` is a namespace for a class of events. Names of actually called events follow this pattern:
  453. * `attribute:attributeKey:name`. `attributeKey` is the key of added/changed/removed attribute.
  454. * `name` is either `'$text'` if change was on {@link module:engine/model/text~Text a text node},
  455. * or the {@link module:engine/model/element~Element#name name} of element which attribute has changed.
  456. *
  457. * This way listeners can either listen to a general `attribute:bold` event or specific event (for example `attribute:src:image`).
  458. *
  459. * @event attribute
  460. * @param {Object} data Additional information about the change.
  461. * @param {module:engine/model/item~Item|module:engine/model/documentselection~DocumentSelection} data.item Changed item
  462. * or converted selection.
  463. * @param {module:engine/model/range~Range} data.range Range spanning over changed item or selection range.
  464. * @param {String} data.attributeKey Attribute key.
  465. * @param {*} data.attributeOldValue Attribute value before the change. This is `null` when selection attribute is converted.
  466. * @param {*} data.attributeNewValue New attribute value.
  467. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
  468. * to be used by callback, passed in `DowncastDispatcher` constructor.
  469. */
  470. /**
  471. * Fired for {@link module:engine/model/selection~Selection selection} changes.
  472. *
  473. * @event selection
  474. * @param {module:engine/model/selection~Selection} selection Selection that is converted.
  475. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
  476. * to be used by callback, passed in `DowncastDispatcher` constructor.
  477. */
  478. /**
  479. * Fired when a new marker is added to the model. Also fired when collapsed model selection that is inside marker is converted.
  480. *
  481. * `addMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
  482. * `addMarker:markerName`. By specifying certain marker names, you can make the events even more gradual. For example,
  483. * if markers are named `foo:abc`, `foo:bar`, then it is possible to listen to `addMarker:foo` or `addMarker:foo:abc` and
  484. * `addMarker:foo:bar` events.
  485. *
  486. * If the marker range is not collapsed:
  487. *
  488. * * the event is fired for each item in the marker range one by one,
  489. * * `conversionApi.consumable` includes each item of the marker range and the consumable value is same as event name.
  490. *
  491. * If the marker range is collapsed:
  492. *
  493. * * there is only one event,
  494. * * `conversionApi.consumable` includes marker range with event name.
  495. *
  496. * If selection inside a marker is converted:
  497. *
  498. * * there is only one event,
  499. * * `conversionApi.consumable` includes selection instance with event name.
  500. *
  501. * @event addMarker
  502. * @param {Object} data Additional information about the change.
  503. * @param {module:engine/model/item~Item|module:engine/model/selection~Selection} data.item Item inside the new marker or
  504. * the selection that is being converted.
  505. * @param {module:engine/model/range~Range} [data.range] Range spanning over converted item. Available only in marker conversion, if
  506. * the marker range was not collapsed.
  507. * @param {module:engine/model/range~Range} data.markerRange Marker range.
  508. * @param {String} data.markerName Marker name.
  509. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
  510. * to be used by callback, passed in `DowncastDispatcher` constructor.
  511. */
  512. /**
  513. * Fired when marker is removed from the model.
  514. *
  515. * `removeMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
  516. * `removeMarker:markerName`. By specifying certain marker names, you can make the events even more gradual. For example,
  517. * if markers are named `foo:abc`, `foo:bar`, then it is possible to listen to `removeMarker:foo` or `removeMarker:foo:abc` and
  518. * `removeMarker:foo:bar` events.
  519. *
  520. * @event removeMarker
  521. * @param {Object} data Additional information about the change.
  522. * @param {module:engine/model/range~Range} data.markerRange Marker range.
  523. * @param {String} data.markerName Marker name.
  524. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
  525. * to be used by callback, passed in `DowncastDispatcher` constructor.
  526. */
  527. }
  528. mix( DowncastDispatcher, EmitterMixin );
  529. // Helper function, checks whether change of `marker` at `modelPosition` should be converted. Marker changes are not
  530. // converted if they happen inside an element with custom conversion method.
  531. //
  532. // @param {module:engine/model/position~Position} modelPosition
  533. // @param {module:engine/model/markercollection~Marker} marker
  534. // @param {module:engine/conversion/mapper~Mapper} mapper
  535. // @returns {Boolean}
  536. function shouldMarkerChangeBeConverted( modelPosition, marker, mapper ) {
  537. const range = marker.getRange();
  538. const ancestors = Array.from( modelPosition.getAncestors() );
  539. ancestors.shift(); // Remove root element. It cannot be passed to `model.Range#containsItem`.
  540. ancestors.reverse();
  541. const hasCustomHandling = ancestors.some( element => {
  542. if ( range.containsItem( element ) ) {
  543. const viewElement = mapper.toViewElement( element );
  544. return !!viewElement.getCustomProperty( 'addHighlight' );
  545. }
  546. } );
  547. return !hasCustomHandling;
  548. }
  549. /**
  550. * Conversion interface that is registered for given {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}
  551. * and is passed as one of parameters when {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher dispatcher}
  552. * fires it's events.
  553. *
  554. * @interface module:engine/conversion/downcastdispatcher~DowncastConversionApi
  555. */
  556. /**
  557. * The {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} instance.
  558. *
  559. * @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher} #dispatcher
  560. */
  561. /**
  562. * Stores information about what parts of processed model item are still waiting to be handled. After a piece of model item
  563. * was converted, appropriate consumable value should be {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed}.
  564. *
  565. * @member {module:engine/conversion/modelconsumable~ModelConsumable} #consumable
  566. */
  567. /**
  568. * The {@link module:engine/conversion/mapper~Mapper} instance.
  569. *
  570. * @member {module:engine/conversion/mapper~Mapper} #mapper
  571. */
  572. /**
  573. * The {@link module:engine/view/downcastwriter~DowncastWriter} instance used to manipulate data during conversion.
  574. *
  575. * @member {module:engine/view/downcastwriter~DowncastWriter} #writer
  576. */