downcastdispatcher.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. * @param {Object} [conversionApi] Interface passed by dispatcher to the events calls.
  106. */
  107. constructor( conversionApi = {} ) {
  108. /**
  109. * Interface passed by dispatcher to the events callbacks.
  110. *
  111. * @member {Object}
  112. */
  113. this.conversionApi = extend( { dispatcher: this }, conversionApi );
  114. }
  115. /**
  116. * Takes {@link module:engine/model/differ~Differ model differ} object with buffered changes and fires conversion basing on it.
  117. *
  118. * @param {module:engine/model/differ~Differ} differ Differ object with buffered changes.
  119. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  120. */
  121. convertChanges( differ, writer ) {
  122. // Before the view is updated, remove markers which have changed.
  123. for ( const change of differ.getMarkersToRemove() ) {
  124. this.convertMarkerRemove( change.name, change.range, writer );
  125. }
  126. // Convert changes that happened on model tree.
  127. for ( const entry of differ.getChanges() ) {
  128. if ( entry.type == 'insert' ) {
  129. this.convertInsert( Range._createFromPositionAndShift( entry.position, entry.length ), writer );
  130. } else if ( entry.type == 'remove' ) {
  131. this.convertRemove( entry.position, entry.length, entry.name, writer );
  132. } else {
  133. // entry.type == 'attribute'.
  134. this.convertAttribute( entry.range, entry.attributeKey, entry.attributeOldValue, entry.attributeNewValue, writer );
  135. }
  136. }
  137. // After the view is updated, convert markers which have changed.
  138. for ( const change of differ.getMarkersToAdd() ) {
  139. this.convertMarkerAdd( change.name, change.range, writer );
  140. }
  141. }
  142. /**
  143. * Starts conversion of a range insertion.
  144. *
  145. * For each node in the range, {@link #event:insert insert event is fired}. For each attribute on each node,
  146. * {@link #event:attribute attribute event is fired}.
  147. *
  148. * @fires insert
  149. * @fires attribute
  150. * @param {module:engine/model/range~Range} range Inserted range.
  151. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  152. */
  153. convertInsert( range, writer ) {
  154. this.conversionApi.writer = writer;
  155. // Create a list of things that can be consumed, consisting of nodes and their attributes.
  156. this.conversionApi.consumable = this._createInsertConsumable( range );
  157. // Fire a separate insert event for each node and text fragment contained in the range.
  158. for ( const value of range ) {
  159. const item = value.item;
  160. const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
  161. const data = {
  162. item,
  163. range: itemRange
  164. };
  165. this._testAndFire( 'insert', data );
  166. // Fire a separate addAttribute event for each attribute that was set on inserted items.
  167. // This is important because most attributes converters will listen only to add/change/removeAttribute events.
  168. // If we would not add this part, attributes on inserted nodes would not be converted.
  169. for ( const key of item.getAttributeKeys() ) {
  170. data.attributeKey = key;
  171. data.attributeOldValue = null;
  172. data.attributeNewValue = item.getAttribute( key );
  173. this._testAndFire( `attribute:${ key }`, data );
  174. }
  175. }
  176. this._clearConversionApi();
  177. }
  178. /**
  179. * Fires conversion of a single node removal. Fires {@link #event:remove remove event} with provided data.
  180. *
  181. * @param {module:engine/model/position~Position} position Position from which node was removed.
  182. * @param {Number} length Offset size of removed node.
  183. * @param {String} name Name of removed node.
  184. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  185. */
  186. convertRemove( position, length, name, writer ) {
  187. this.conversionApi.writer = writer;
  188. this.fire( 'remove:' + name, { position, length }, this.conversionApi );
  189. this._clearConversionApi();
  190. }
  191. /**
  192. * Starts conversion of attribute change on given `range`.
  193. *
  194. * For each node in the given `range`, {@link #event:attribute attribute event} is fired with the passed data.
  195. *
  196. * @fires attribute
  197. * @param {module:engine/model/range~Range} range Changed range.
  198. * @param {String} key Key of the attribute that has changed.
  199. * @param {*} oldValue Attribute value before the change or `null` if the attribute has not been set before.
  200. * @param {*} newValue New attribute value or `null` if the attribute has been removed.
  201. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  202. */
  203. convertAttribute( range, key, oldValue, newValue, writer ) {
  204. this.conversionApi.writer = writer;
  205. // Create a list with attributes to consume.
  206. this.conversionApi.consumable = this._createConsumableForRange( range, `attribute:${ key }` );
  207. // Create a separate attribute event for each node in the range.
  208. for ( const value of range ) {
  209. const item = value.item;
  210. const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
  211. const data = {
  212. item,
  213. range: itemRange,
  214. attributeKey: key,
  215. attributeOldValue: oldValue,
  216. attributeNewValue: newValue
  217. };
  218. this._testAndFire( `attribute:${ key }`, data );
  219. }
  220. this._clearConversionApi();
  221. }
  222. /**
  223. * Starts model selection conversion.
  224. *
  225. * Fires events for given {@link module:engine/model/selection~Selection selection} to start selection conversion.
  226. *
  227. * @fires selection
  228. * @fires addMarker
  229. * @fires attribute
  230. * @param {module:engine/model/selection~Selection} selection Selection to convert.
  231. * @param {Array.<module:engine/model/markercollection~Marker>} markers Array of markers containing model markers.
  232. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  233. */
  234. convertSelection( selection, markers, writer ) {
  235. const markersAtSelection = Array.from( markers.getMarkersAtPosition( selection.getFirstPosition() ) );
  236. this.conversionApi.writer = writer;
  237. this.conversionApi.consumable = this._createSelectionConsumable( selection, markersAtSelection );
  238. this.fire( 'selection', { selection }, this.conversionApi );
  239. if ( !selection.isCollapsed ) {
  240. return;
  241. }
  242. for ( const marker of markersAtSelection ) {
  243. const markerRange = marker.getRange();
  244. if ( !shouldMarkerChangeBeConverted( selection.getFirstPosition(), marker, this.conversionApi.mapper ) ) {
  245. continue;
  246. }
  247. const data = {
  248. item: selection,
  249. markerName: marker.name,
  250. markerRange
  251. };
  252. if ( this.conversionApi.consumable.test( selection, 'addMarker:' + marker.name ) ) {
  253. this.fire( 'addMarker:' + marker.name, data, this.conversionApi );
  254. }
  255. }
  256. for ( const key of selection.getAttributeKeys() ) {
  257. const data = {
  258. item: selection,
  259. range: selection.getFirstRange(),
  260. attributeKey: key,
  261. attributeOldValue: null,
  262. attributeNewValue: selection.getAttribute( key )
  263. };
  264. // Do not fire event if the attribute has been consumed.
  265. if ( this.conversionApi.consumable.test( selection, 'attribute:' + data.attributeKey ) ) {
  266. this.fire( 'attribute:' + data.attributeKey + ':$text', data, this.conversionApi );
  267. }
  268. }
  269. this._clearConversionApi();
  270. }
  271. /**
  272. * Converts added marker. Fires {@link #event:addMarker addMarker} event for each item
  273. * in marker's range. If range is collapsed single event is dispatched. See event description for more details.
  274. *
  275. * @fires addMarker
  276. * @param {String} markerName Marker name.
  277. * @param {module:engine/model/range~Range} markerRange Marker range.
  278. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  279. */
  280. convertMarkerAdd( markerName, markerRange, writer ) {
  281. // Do not convert if range is in graveyard or not in the document (e.g. in DocumentFragment).
  282. if ( !markerRange.root.document || markerRange.root.rootName == '$graveyard' ) {
  283. return;
  284. }
  285. this.conversionApi.writer = writer;
  286. // In markers' case, event name == consumable name.
  287. const eventName = 'addMarker:' + markerName;
  288. // When range is collapsed - fire single event with collapsed range in consumable.
  289. if ( markerRange.isCollapsed ) {
  290. const consumable = new Consumable();
  291. consumable.add( markerRange, eventName );
  292. this.conversionApi.consumable = consumable;
  293. this.fire( eventName, { markerName, markerRange }, this.conversionApi );
  294. return;
  295. }
  296. // Create consumable for each item in range.
  297. this.conversionApi.consumable = this._createConsumableForRange( markerRange, eventName );
  298. // Create separate event for each node in the range.
  299. for ( const item of markerRange.getItems() ) {
  300. // Do not fire event for already consumed items.
  301. if ( !this.conversionApi.consumable.test( item, eventName ) ) {
  302. continue;
  303. }
  304. const data = { item, range: Range._createOn( item ), markerName, markerRange };
  305. this.fire( eventName, data, this.conversionApi );
  306. }
  307. this._clearConversionApi();
  308. }
  309. /**
  310. * Fires conversion of marker removal. Fires {@link #event:removeMarker removeMarker} event with provided data.
  311. *
  312. * @fires removeMarker
  313. * @param {String} markerName Marker name.
  314. * @param {module:engine/model/range~Range} markerRange Marker range.
  315. * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
  316. */
  317. convertMarkerRemove( markerName, markerRange, writer ) {
  318. // Do not convert if range is in graveyard or not in the document (e.g. in DocumentFragment).
  319. if ( !markerRange.root.document || markerRange.root.rootName == '$graveyard' ) {
  320. return;
  321. }
  322. this.conversionApi.writer = writer;
  323. this.fire( 'removeMarker:' + markerName, { markerName, markerRange }, this.conversionApi );
  324. this._clearConversionApi();
  325. }
  326. /**
  327. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume from given range,
  328. * assuming that the range has just been inserted to the model.
  329. *
  330. * @private
  331. * @param {module:engine/model/range~Range} range Inserted range.
  332. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  333. */
  334. _createInsertConsumable( range ) {
  335. const consumable = new Consumable();
  336. for ( const value of range ) {
  337. const item = value.item;
  338. consumable.add( item, 'insert' );
  339. for ( const key of item.getAttributeKeys() ) {
  340. consumable.add( item, 'attribute:' + key );
  341. }
  342. }
  343. return consumable;
  344. }
  345. /**
  346. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume for given range.
  347. *
  348. * @private
  349. * @param {module:engine/model/range~Range} range Affected range.
  350. * @param {String} type Consumable type.
  351. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  352. */
  353. _createConsumableForRange( range, type ) {
  354. const consumable = new Consumable();
  355. for ( const item of range.getItems() ) {
  356. consumable.add( item, type );
  357. }
  358. return consumable;
  359. }
  360. /**
  361. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with selection consumable values.
  362. *
  363. * @private
  364. * @param {module:engine/model/selection~Selection} selection Selection to create consumable from.
  365. * @param {Iterable.<module:engine/model/markercollection~Marker>} markers Markers which contains selection.
  366. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  367. */
  368. _createSelectionConsumable( selection, markers ) {
  369. const consumable = new Consumable();
  370. consumable.add( selection, 'selection' );
  371. for ( const marker of markers ) {
  372. consumable.add( selection, 'addMarker:' + marker.name );
  373. }
  374. for ( const key of selection.getAttributeKeys() ) {
  375. consumable.add( selection, 'attribute:' + key );
  376. }
  377. return consumable;
  378. }
  379. /**
  380. * Tests passed `consumable` to check whether given event can be fired and if so, fires it.
  381. *
  382. * @private
  383. * @fires insert
  384. * @fires attribute
  385. * @param {String} type Event type.
  386. * @param {Object} data Event data.
  387. */
  388. _testAndFire( type, data ) {
  389. if ( !this.conversionApi.consumable.test( data.item, type ) ) {
  390. // Do not fire event if the item was consumed.
  391. return;
  392. }
  393. const name = data.item.name || '$text';
  394. this.fire( type + ':' + name, data, this.conversionApi );
  395. }
  396. /**
  397. * Clears conversion API object.
  398. *
  399. * @private
  400. */
  401. _clearConversionApi() {
  402. delete this.conversionApi.writer;
  403. delete this.conversionApi.consumable;
  404. }
  405. /**
  406. * Fired for inserted nodes.
  407. *
  408. * `insert` is a namespace for a class of events. Names of actually called events follow this pattern:
  409. * `insert:name`. `name` is either `'$text'`, when {@link module:engine/model/text~Text a text node} has been inserted,
  410. * or {@link module:engine/model/element~Element#name name} of inserted element.
  411. *
  412. * This way listeners can either listen to a general `insert` event or specific event (for example `insert:paragraph`).
  413. *
  414. * @event insert
  415. * @param {Object} data Additional information about the change.
  416. * @param {module:engine/model/item~Item} data.item Inserted item.
  417. * @param {module:engine/model/range~Range} data.range Range spanning over inserted item.
  418. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `DowncastDispatcher` constructor.
  419. */
  420. /**
  421. * Fired for removed nodes.
  422. *
  423. * `remove` is a namespace for a class of events. Names of actually called events follow this pattern:
  424. * `remove:name`. `name` is either `'$text'`, when {@link module:engine/model/text~Text a text node} has been removed,
  425. * or the {@link module:engine/model/element~Element#name name} of removed element.
  426. *
  427. * This way listeners can either listen to a general `remove` event or specific event (for example `remove:paragraph`).
  428. *
  429. * @event remove
  430. * @param {Object} data Additional information about the change.
  431. * @param {module:engine/model/position~Position} data.position Position from which the node has been removed.
  432. * @param {Number} data.length Offset size of the removed node.
  433. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `DowncastDispatcher` constructor.
  434. */
  435. /**
  436. * Fired in the following cases:
  437. *
  438. * * when an attribute has been added, changed, or removed from a node,
  439. * * when a node with an attribute is inserted,
  440. * * when collapsed model selection attribute is converted.
  441. *
  442. * `attribute` is a namespace for a class of events. Names of actually called events follow this pattern:
  443. * `attribute:attributeKey:name`. `attributeKey` is the key of added/changed/removed attribute.
  444. * `name` is either `'$text'` if change was on {@link module:engine/model/text~Text a text node},
  445. * or the {@link module:engine/model/element~Element#name name} of element which attribute has changed.
  446. *
  447. * This way listeners can either listen to a general `attribute:bold` event or specific event (for example `attribute:src:image`).
  448. *
  449. * @event attribute
  450. * @param {Object} data Additional information about the change.
  451. * @param {module:engine/model/item~Item|module:engine/model/documentselection~DocumentSelection} data.item Changed item
  452. * or converted selection.
  453. * @param {module:engine/model/range~Range} data.range Range spanning over changed item or selection range.
  454. * @param {String} data.attributeKey Attribute key.
  455. * @param {*} data.attributeOldValue Attribute value before the change. This is `null` when selection attribute is converted.
  456. * @param {*} data.attributeNewValue New attribute value.
  457. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  458. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `DowncastDispatcher` constructor.
  459. */
  460. /**
  461. * Fired for {@link module:engine/model/selection~Selection selection} changes.
  462. *
  463. * @event selection
  464. * @param {module:engine/model/selection~Selection} selection Selection that is converted.
  465. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  466. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `DowncastDispatcher` constructor.
  467. */
  468. /**
  469. * Fired when a new marker is added to the model. Also fired when collapsed model selection that is inside marker is converted.
  470. *
  471. * `addMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
  472. * `addMarker:markerName`. By specifying certain marker names, you can make the events even more gradual. For example,
  473. * if markers are named `foo:abc`, `foo:bar`, then it is possible to listen to `addMarker:foo` or `addMarker:foo:abc` and
  474. * `addMarker:foo:bar` events.
  475. *
  476. * If the marker range is not collapsed:
  477. *
  478. * * the event is fired for each item in the marker range one by one,
  479. * * consumables object includes each item of the marker range and the consumable value is same as event name.
  480. *
  481. * If the marker range is collapsed:
  482. *
  483. * * there is only one event,
  484. * * consumables object includes marker range with event name.
  485. *
  486. * If selection inside a marker is converted:
  487. *
  488. * * there is only one event,
  489. * * consumables object includes selection instance with event name.
  490. *
  491. * @event addMarker
  492. * @param {Object} data Additional information about the change.
  493. * @param {module:engine/model/item~Item|module:engine/model/selection~Selection} data.item Item inside the new marker or
  494. * the selection that is being converted.
  495. * @param {module:engine/model/range~Range} [data.range] Range spanning over converted item. Available only in marker conversion, if
  496. * the marker range was not collapsed.
  497. * @param {module:engine/model/range~Range} data.markerRange Marker range.
  498. * @param {String} data.markerName Marker name.
  499. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  500. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `DowncastDispatcher` constructor.
  501. */
  502. /**
  503. * Fired when marker is removed from the model.
  504. *
  505. * `removeMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
  506. * `removeMarker:markerName`. By specifying certain marker names, you can make the events even more gradual. For example,
  507. * if markers are named `foo:abc`, `foo:bar`, then it is possible to listen to `removeMarker:foo` or `removeMarker:foo:abc` and
  508. * `removeMarker:foo:bar` events.
  509. *
  510. * @event removeMarker
  511. * @param {Object} data Additional information about the change.
  512. * @param {module:engine/model/range~Range} data.markerRange Marker range.
  513. * @param {String} data.markerName Marker name.
  514. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `DowncastDispatcher` constructor.
  515. */
  516. }
  517. mix( DowncastDispatcher, EmitterMixin );
  518. // Helper function, checks whether change of `marker` at `modelPosition` should be converted. Marker changes are not
  519. // converted if they happen inside an element with custom conversion method.
  520. //
  521. // @param {module:engine/model/position~Position} modelPosition
  522. // @param {module:engine/model/markercollection~Marker} marker
  523. // @param {module:engine/conversion/mapper~Mapper} mapper
  524. // @returns {Boolean}
  525. function shouldMarkerChangeBeConverted( modelPosition, marker, mapper ) {
  526. const range = marker.getRange();
  527. const ancestors = Array.from( modelPosition.getAncestors() );
  528. ancestors.shift(); // Remove root element. It cannot be passed to `model.Range#containsItem`.
  529. ancestors.reverse();
  530. const hasCustomHandling = ancestors.some( element => {
  531. if ( range.containsItem( element ) ) {
  532. const viewElement = mapper.toViewElement( element );
  533. return !!viewElement.getCustomProperty( 'addHighlight' );
  534. }
  535. } );
  536. return !hasCustomHandling;
  537. }