downcastdispatcher.js 28 KB

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