modelconversiondispatcher.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/conversion/modelconversiondispatcher
  7. */
  8. import Consumable from './modelconsumable';
  9. import Range from '../model/range';
  10. import Position from '../model/position';
  11. import DocumentFragment from '../model/documentfragment';
  12. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  13. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  14. import extend from '@ckeditor/ckeditor5-utils/src/lib/lodash/extend';
  15. /**
  16. * `ModelConversionDispatcher` is a central point of {@link module:engine/model/model model} conversion, which is
  17. * a process of reacting to changes in the model and reflecting them by listeners that listen to those changes.
  18. * In default application, {@link module:engine/model/model model} is converted to {@link module:engine/view/view view}. This means
  19. * that changes in the model are reflected by changing the view (i.e. adding view nodes or changing attributes on view elements).
  20. *
  21. * During conversion process, `ModelConversionDispatcher` fires data-manipulation events, basing on state of the model and prepares
  22. * data for those events. It is important to note that the events are connected with "change actions" like "inserting"
  23. * or "removing" so one might say that we are converting "changes". This is in contrary to view to model conversion,
  24. * where we convert view nodes (the structure, not "changes" to the view). Note, that because changes are converted
  25. * and not the structure itself, there is a need to have a mapping between model and the structure on which changes are
  26. * reflected. To map elements during model to view conversion use {@link module:engine/conversion/mapper~Mapper}.
  27. *
  28. * The main use for this class is to listen to {@link module:engine/model/document~Document#event:change Document change event}, process it
  29. * and then fire specific events telling what exactly has changed. For those events, `ModelConversionDispatcher`
  30. * creates {@link module:engine/conversion/modelconsumable~ModelConsumable list of consumable values} that should be handled by event
  31. * callbacks. Those events are listened to by model-to-view converters which convert changes done in the
  32. * {@link module:engine/model/model model} to changes in the {@link module:engine/view/view view}. `ModelConversionController` also checks
  33. * the current state of consumables, so it won't fire events for parts of model that were already consumed. This is
  34. * especially important in callbacks that consume multiple values. See {@link module:engine/conversion/modelconsumable~ModelConsumable}
  35. * for an example of such callback.
  36. *
  37. * Although the primary usage for this class is the model-to-view conversion, `ModelConversionDispatcher` can be used
  38. * to build custom data processing pipelines that converts model to anything that is needed. Existing model structure can
  39. * be used to generate events (listening to {@link module:engine/model/document~Document#event:change Document change event} is not
  40. * required)
  41. * and custom callbacks can be added to the events (these does not have to be limited to changes in the view).
  42. *
  43. * When providing your own event listeners for `ModelConversionDispatcher` keep in mind that any callback that had
  44. * {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed} a value from consumable (and did some changes, i.e. to
  45. * the view) should also stop the event. This is because whenever a callback is fired it is assumed that there is something
  46. * to be consumed. Thanks to that approach, you do not have to test whether there is anything to consume at the beginning
  47. * of your listener callback.
  48. *
  49. * Example of providing a converter for `ModelConversionDispatcher`:
  50. *
  51. * // We will convert inserting "paragraph" model element into the model.
  52. * modelDispatcher.on( 'insert:paragraph', ( evt, data, consumable, conversionApi ) => {
  53. * // Remember to consume the part of consumable.
  54. * consumable.consume( data.item, 'insert' );
  55. *
  56. * // Translate position in model to position in the view.
  57. * const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  58. *
  59. * // Create a P element (note that this converter is for inserting P elements -> 'insert:paragraph').
  60. * const viewElement = new ViewElement( 'p' );
  61. *
  62. * // Bind the newly created view element to model element so positions will map accordingly in future.
  63. * conversionApi.mapper.bindElements( data.item, viewElement );
  64. *
  65. * // Add the newly created view element to the view.
  66. * viewWriter.insert( viewPosition, viewElement );
  67. *
  68. * // Remember to stop the event propagation if the data.item was consumed.
  69. * evt.stop();
  70. * } );
  71. *
  72. * Callback that "overrides" other callback:
  73. *
  74. * // Special converter for `linkHref` attribute added on custom `quote` element. Note, that this
  75. * // attribute may be the same as the attribute added by other features (link feature in this case).
  76. * // It might be even added by that feature! It makes sense that a part of content that is a quote is linked
  77. * // to an external source so it makes sense that link feature works on the custom quote element.
  78. * // However, we have to make sure that the attributes added by link feature are correctly converted.
  79. * // To block default `linkHref` conversion we have to:
  80. * // 1) add this callback with higher priority than link feature callback,
  81. * // 2) consume `linkHref` attribute add change.
  82. * modelConversionDispatcher.on( 'addAttribute:linkHref:quote', ( evt, data, consumable, conversionApi ) => {
  83. * consumable.consume( data.item, 'addAttribute:linkHref' );
  84. *
  85. * // Create a button that will represent the `linkHref` attribute.
  86. * let viewSourceBtn = new ViewElement( 'a', {
  87. * href: data.item.getAttribute( 'linkHref' ),
  88. * title: 'source'
  89. * } );
  90. *
  91. * // Add a class for the button.
  92. * viewSourceBtn.addClass( 'source' );
  93. *
  94. * // Insert the button using writer API.
  95. * // If `addAttribute` event is fired by
  96. * // `module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#convertInsert` it is fired
  97. * // after `data.item` insert conversion was done. If the event is fired due to attribute insertion coming from
  98. * // different source, `data.item` already existed. This means we are safe to get `viewQuote` from mapper.
  99. * const viewQuote = conversionApi.mapper.toViewElement( data.item );
  100. * const position = new ViewPosition( viewQuote, viewQuote.childCount );
  101. * viewWriter.insert( position, viewSourceBtn );
  102. *
  103. * evt.stop();
  104. * }, { priority: 'high' } );
  105. */
  106. export default class ModelConversionDispatcher {
  107. /**
  108. * Creates a `ModelConversionDispatcher` that operates using passed API.
  109. *
  110. * @param {module:engine/model/document~Document} modelDocument Model document instance bound with this dispatcher.
  111. * @param {Object} [conversionApi] Interface passed by dispatcher to the events callbacks.
  112. */
  113. constructor( modelDocument, conversionApi = {} ) {
  114. /**
  115. * Model document instance bound with this dispatcher.
  116. *
  117. * @private
  118. * @member {module:engine/model/document~Document}
  119. */
  120. this._modelDocument = modelDocument;
  121. /**
  122. * Interface passed by dispatcher to the events callbacks.
  123. *
  124. * @member {Object}
  125. */
  126. this.conversionApi = extend( { dispatcher: this }, conversionApi );
  127. }
  128. /**
  129. * Prepares data and fires a proper event.
  130. *
  131. * The method is crafted to take use of parameters passed in {@link module:engine/model/document~Document#event:change Document change
  132. * event}.
  133. *
  134. * @see module:engine/model/document~Document#event:change
  135. * @fires insert
  136. * @fires remove
  137. * @fires addAttribute
  138. * @fires removeAttribute
  139. * @fires changeAttribute
  140. * @fires addMarker
  141. * @param {String} type Change type.
  142. * @param {Object} data Additional information about the change.
  143. */
  144. convertChange( type, data ) {
  145. // Do not convert changes if they happen in graveyard.
  146. // Graveyard is a special root that has no view / no other representation and changes done in it should not be converted.
  147. if ( type !== 'remove' && data.range && data.range.root.rootName == '$graveyard' ) {
  148. return;
  149. }
  150. if ( type == 'remove' && data.sourcePosition.root.rootName == '$graveyard' ) {
  151. return;
  152. }
  153. if ( type == 'rename' && data.element.root.rootName == '$graveyard' ) {
  154. return;
  155. }
  156. // We can safely dispatch changes.
  157. if ( type == 'insert' || type == 'reinsert' ) {
  158. this.convertInsertion( data.range );
  159. } else if ( type == 'move' ) {
  160. this.convertMove( data.sourcePosition, data.range );
  161. } else if ( type == 'remove' ) {
  162. this.convertRemove( data.sourcePosition, data.range );
  163. } else if ( type == 'addAttribute' || type == 'removeAttribute' || type == 'changeAttribute' ) {
  164. this.convertAttribute( type, data.range, data.key, data.oldValue, data.newValue );
  165. } else if ( type == 'rename' ) {
  166. this.convertRename( data.element, data.oldName );
  167. }
  168. }
  169. /**
  170. * Starts conversion of insertion-change on given `range`.
  171. *
  172. * Analyzes given range and fires insertion-connected events with data based on that range.
  173. *
  174. * **Note**: This method will fire separate events for node insertion and attributes insertion. All
  175. * attributes that are set on inserted nodes are treated like they were added just after node insertion.
  176. *
  177. * @fires insert
  178. * @fires addAttribute
  179. * @fires addMarker
  180. * @param {module:engine/model/range~Range} range Inserted range.
  181. */
  182. convertInsertion( range ) {
  183. // Create a list of things that can be consumed, consisting of nodes and their attributes.
  184. const consumable = this._createInsertConsumable( range );
  185. // Fire a separate insert event for each node and text fragment contained in the range.
  186. for ( let value of range ) {
  187. const item = value.item;
  188. const itemRange = Range.createFromPositionAndShift( value.previousPosition, value.length );
  189. const data = {
  190. item: item,
  191. range: itemRange
  192. };
  193. this._testAndFire( 'insert', data, consumable );
  194. // Fire a separate addAttribute event for each attribute that was set on inserted items.
  195. // This is important because most attributes converters will listen only to add/change/removeAttribute events.
  196. // If we would not add this part, attributes on inserted nodes would not be converted.
  197. for ( let key of item.getAttributeKeys() ) {
  198. data.attributeKey = key;
  199. data.attributeOldValue = null;
  200. data.attributeNewValue = item.getAttribute( key );
  201. this._testAndFire( `addAttribute:${ key }`, data, consumable );
  202. }
  203. }
  204. for ( let marker of this._modelDocument.markers ) {
  205. const markerRange = marker.getRange();
  206. // Check if inserted content is inserted into a marker.
  207. if ( markerRange.containsPosition( range.start ) ) {
  208. this.convertMarker( 'addMarker', marker.name, markerRange.getIntersection( range ) );
  209. }
  210. // Check if inserted content contains a marker.
  211. if ( range.containsRange( markerRange ) || range.isEqual( markerRange ) ) {
  212. this.convertMarker( 'addMarker', marker.name, markerRange );
  213. }
  214. }
  215. }
  216. /**
  217. * Starts conversion of move-change of given `range`, that was moved from given `sourcePosition`.
  218. *
  219. * Fires {@link ~#event:remove remove event} and {@link ~#event:insert insert event} based on passed parameters.
  220. *
  221. * @fires remove
  222. * @fires insert
  223. * @param {module:engine/model/position~Position} sourcePosition Position from where the range has been removed.
  224. * @param {module:engine/model/range~Range} range Removed range (after remove, in
  225. * {@link module:engine/model/document~Document#graveyard graveyard root}).
  226. */
  227. convertMove( sourcePosition, range ) {
  228. this.convertRemove( sourcePosition, range );
  229. this.convertInsertion( range );
  230. }
  231. /**
  232. * Starts conversion of remove-change of given `range`, that was removed from given `sourcePosition`.
  233. *
  234. * Fires {@link ~#event:remove remove event} with data based on passed values.
  235. *
  236. * @fires remove
  237. * @param {module:engine/model/position~Position} sourcePosition Position from where the range has been removed.
  238. * @param {module:engine/model/range~Range} range Removed range (after remove, in
  239. * {@link module:engine/model/document~Document#graveyard graveyard root}).
  240. */
  241. convertRemove( sourcePosition, range ) {
  242. const consumable = this._createConsumableForRange( range, 'remove' );
  243. for ( let item of range.getItems( { shallow: true } ) ) {
  244. const data = {
  245. sourcePosition: sourcePosition,
  246. item: item
  247. };
  248. this._testAndFire( `remove`, data, consumable );
  249. }
  250. }
  251. /**
  252. * Starts conversion of attribute-change on given `range`.
  253. *
  254. * Analyzes given attribute change and fires attributes-connected events with data based on passed values.
  255. *
  256. * @fires addAttribute
  257. * @fires removeAttribute
  258. * @fires changeAttribute
  259. * @param {String} type Change type. Possible values: `addAttribute`, `removeAttribute`, `changeAttribute`.
  260. * @param {module:engine/model/range~Range} range Changed range.
  261. * @param {String} key Attribute key.
  262. * @param {*} oldValue Attribute value before the change or `null` if attribute has not been set.
  263. * @param {*} newValue New attribute value or `null` if attribute has been removed.
  264. */
  265. convertAttribute( type, range, key, oldValue, newValue ) {
  266. // Create a list with attributes to consume.
  267. const consumable = this._createConsumableForRange( range, type + ':' + key );
  268. // Create a separate attribute event for each node in the range.
  269. for ( let value of range ) {
  270. const item = value.item;
  271. const itemRange = Range.createFromPositionAndShift( value.previousPosition, value.length );
  272. const data = {
  273. item: item,
  274. range: itemRange,
  275. attributeKey: key,
  276. attributeOldValue: oldValue,
  277. attributeNewValue: newValue
  278. };
  279. this._testAndFire( `${ type }:${ key }`, data, consumable );
  280. }
  281. }
  282. /**
  283. * Starts conversion of rename-change of given `element` that had given `oldName`.
  284. *
  285. * Fires {@link ~#event:remove remove event} and {@link ~#event:insert insert event} based on passed values.
  286. *
  287. * @fires remove
  288. * @fires insert
  289. * @param {module:engine/model/element~Element} element Renamed element.
  290. * @param {String} oldName Name of the renamed element before it was renamed.
  291. */
  292. convertRename( element, oldName ) {
  293. // Create fake element that will be used to fire remove event. The fake element will have the old element name.
  294. const fakeElement = element.clone( true );
  295. fakeElement.name = oldName;
  296. // Bind fake element with original view element so the view element will be removed.
  297. this.conversionApi.mapper.bindElements(
  298. fakeElement,
  299. this.conversionApi.mapper.toViewElement( element )
  300. );
  301. // Create fake document fragment so a range can be created on fake element.
  302. const fakeDocumentFragment = new DocumentFragment();
  303. fakeDocumentFragment.appendChildren( fakeElement );
  304. this.convertRemove( Position.createBefore( element ), Range.createOn( fakeElement ) );
  305. this.convertInsertion( Range.createOn( element ) );
  306. }
  307. /**
  308. * Starts selection conversion.
  309. *
  310. * Fires events for given {@link module:engine/model/selection~Selection selection} to start selection conversion.
  311. *
  312. * @fires selection
  313. * @fires selectionAttribute
  314. * @param {module:engine/model/selection~Selection} selection Selection to convert.
  315. */
  316. convertSelection( selection ) {
  317. const markers = Array.from( this._modelDocument.markers.getMarkersAtPosition( selection.getFirstPosition() ) );
  318. const consumable = this._createSelectionConsumable( selection, markers );
  319. this.fire( 'selection', { selection }, consumable, this.conversionApi );
  320. for ( let marker of markers ) {
  321. const data = {
  322. selection: selection,
  323. name: marker.name
  324. };
  325. if ( consumable.test( selection, 'selectionMarker:' + marker.name ) ) {
  326. this.fire( 'selectionMarker:' + marker.name, data, consumable, this.conversionApi );
  327. }
  328. }
  329. for ( let key of selection.getAttributeKeys() ) {
  330. const data = {
  331. selection: selection,
  332. key: key,
  333. value: selection.getAttribute( key )
  334. };
  335. // Do not fire event if the attribute has been consumed.
  336. if ( consumable.test( selection, 'selectionAttribute:' + data.key ) ) {
  337. this.fire( 'selectionAttribute:' + data.key, data, consumable, this.conversionApi );
  338. }
  339. }
  340. }
  341. /**
  342. * Starts marker-conversion.
  343. *
  344. * Fires {@link ~#event:addMarker addMarker event} or {@link ~#event:removeMarker removeMarker event} based on
  345. * given `type` with data based on passed parameters.
  346. *
  347. * @fires addMarker
  348. * @fires removeMarker
  349. * @param {'addMarker'|'removeMarker'} type Change type.
  350. * @param {String} name Marker name.
  351. * @param {module:engine/model/range~Range} range Marker range.
  352. */
  353. convertMarker( type, name, range ) {
  354. // Do not convert if range is in graveyard or not in the document (e.g. in DocumentFragment).
  355. if ( !range.root.document || range.root.rootName == '$graveyard' ) {
  356. return;
  357. }
  358. const consumable = this._createMarkerConsumable( type, range );
  359. const data = { name, range };
  360. this.fire( type + ':' + name, data, consumable, this.conversionApi );
  361. }
  362. /**
  363. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume from given range, assuming that
  364. * given range has just been inserted to the model.
  365. *
  366. * @private
  367. * @param {module:engine/model/range~Range} range Inserted range.
  368. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  369. */
  370. _createInsertConsumable( range ) {
  371. const consumable = new Consumable();
  372. for ( let value of range ) {
  373. const item = value.item;
  374. consumable.add( item, 'insert' );
  375. for ( let key of item.getAttributeKeys() ) {
  376. consumable.add( item, 'addAttribute:' + key );
  377. }
  378. }
  379. return consumable;
  380. }
  381. /**
  382. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values of given `type`
  383. * for each item from given `range`.
  384. *
  385. * @private
  386. * @param {module:engine/model/range~Range} range Affected range.
  387. * @param {String} type Consumable type.
  388. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  389. */
  390. _createConsumableForRange( range, type ) {
  391. const consumable = new Consumable();
  392. for ( let item of range.getItems() ) {
  393. consumable.add( item, type );
  394. }
  395. return consumable;
  396. }
  397. /**
  398. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with selection consumable values.
  399. *
  400. * @private
  401. * @param {module:engine/model/selection~Selection} selection Selection to create consumable from.
  402. * @param {Iterable.<module:engine/model/markercollection~Marker>} markers Markers which contains selection.
  403. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  404. */
  405. _createSelectionConsumable( selection, markers ) {
  406. const consumable = new Consumable();
  407. consumable.add( selection, 'selection' );
  408. for ( let marker of markers ) {
  409. consumable.add( selection, 'selectionMarker:' + marker.name );
  410. }
  411. for ( let key of selection.getAttributeKeys() ) {
  412. consumable.add( selection, 'selectionAttribute:' + key );
  413. }
  414. return consumable;
  415. }
  416. /**
  417. * Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} for adding or removing marker on given `range`.
  418. *
  419. * @private
  420. * @param {'addMarker'|'removeMarker'} type Change type.
  421. * @param {module:engine/model/range~Range} range Range on which marker was added or removed.
  422. * @returns {module:engine/conversion/modelconsumable~ModelConsumable} Values to consume.
  423. */
  424. _createMarkerConsumable( type, range ) {
  425. const consumable = new Consumable();
  426. consumable.add( range, type );
  427. return consumable;
  428. }
  429. /**
  430. * Tests passed `consumable` to check whether given event can be fired and if so, fires it.
  431. *
  432. * @private
  433. * @fires insert
  434. * @fires remove
  435. * @fires addAttribute
  436. * @fires removeAttribute
  437. * @fires changeAttribute
  438. * @param {String} type Event type.
  439. * @param {Object} data Event data.
  440. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  441. */
  442. _testAndFire( type, data, consumable ) {
  443. if ( !consumable.test( data.item, type ) ) {
  444. // Do not fire event if the item was consumed.
  445. return;
  446. }
  447. const name = data.item.name || '$text';
  448. this.fire( type + ':' + name, data, consumable, this.conversionApi );
  449. }
  450. /**
  451. * Fired for inserted nodes.
  452. *
  453. * `insert` is a namespace for a class of events. Names of actually called events follow this pattern:
  454. * `insert:<name>`. `name` is either `'$text'` when one or more characters has been inserted or
  455. * {@link module:engine/model/element~Element#name name} of inserted element.
  456. *
  457. * This way listeners can either listen to a general `insert` event or specific event (for example `insert:paragraph`).
  458. *
  459. * @event insert
  460. * @param {Object} data Additional information about the change.
  461. * @param {module:engine/model/item~Item} data.item Inserted item.
  462. * @param {module:engine/model/range~Range} data.range Range spanning over inserted item.
  463. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  464. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  465. */
  466. /**
  467. * Fired for removed nodes.
  468. *
  469. * `remove` is a namespace for a class of events. Names of actually called events follow this pattern:
  470. * `remove:<name>`. `name` is either `'$text'` when one or more characters has been removed or the
  471. * {@link module:engine/model/element~Element#name name} of removed element.
  472. *
  473. * This way listeners can either listen to a general `remove` event or specific event (for example `remove:paragraph`).
  474. *
  475. * @event remove
  476. * @param {Object} data Additional information about the change.
  477. * @param {module:engine/model/position~Position} data.sourcePosition Position from where the range has been removed.
  478. * @param {module:engine/model/range~Range} data.range Removed range (in {@link module:engine/model/document~Document#graveyard
  479. * graveyard root}).
  480. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  481. */
  482. /**
  483. * Fired when attribute has been added on a node.
  484. *
  485. * `addAttribute` is a namespace for a class of events. Names of actually called events follow this pattern:
  486. * `addAttribute:<attributeKey>:<name>`. `attributeKey` is the key of added attribute. `name` is either `'$text'`
  487. * if attribute was added on one or more characters, or the {@link module:engine/model/element~Element#name name} of
  488. * the element on which attribute was added.
  489. *
  490. * This way listeners can either listen to a general `addAttribute:bold` event or specific event
  491. * (for example `addAttribute:link:image`).
  492. *
  493. * @event addAttribute
  494. * @param {Object} data Additional information about the change.
  495. * @param {module:engine/model/item~Item} data.item Changed item.
  496. * @param {module:engine/model/range~Range} data.range Range spanning over changed item.
  497. * @param {String} data.attributeKey Attribute key.
  498. * @param {null} data.attributeOldValue Attribute value before the change - always `null`. Kept for the sake of unifying events.
  499. * @param {*} data.attributeNewValue New attribute value.
  500. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  501. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  502. */
  503. /**
  504. * Fired when attribute has been removed from a node.
  505. *
  506. * `removeAttribute` is a namespace for a class of events. Names of actually called events follow this pattern:
  507. * `removeAttribute:<attributeKey>:<name>`. `attributeKey` is the key of removed attribute. `name` is either `'$text'`
  508. * if attribute was removed from one or more characters, or the {@link module:engine/model/element~Element#name name} of
  509. * the element from which attribute was removed.
  510. *
  511. * This way listeners can either listen to a general `removeAttribute:bold` event or specific event
  512. * (for example `removeAttribute:link:image`).
  513. *
  514. * @event removeAttribute
  515. * @param {Object} data Additional information about the change.
  516. * @param {module:engine/model/item~Item} data.item Changed item.
  517. * @param {module:engine/model/range~Range} data.range Range spanning over changed item.
  518. * @param {String} data.attributeKey Attribute key.
  519. * @param {*} data.attributeOldValue Attribute value before it was removed.
  520. * @param {null} data.attributeNewValue New attribute value - always `null`. Kept for the sake of unifying events.
  521. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  522. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  523. */
  524. /**
  525. * Fired when attribute of a node has been changed.
  526. *
  527. * `changeAttribute` is a namespace for a class of events. Names of actually called events follow this pattern:
  528. * `changeAttribute:<attributeKey>:<name>`. `attributeKey` is the key of changed attribute. `name` is either `'$text'`
  529. * if attribute was changed on one or more characters, or the {@link module:engine/model/element~Element#name name} of
  530. * the element on which attribute was changed.
  531. *
  532. * This way listeners can either listen to a general `changeAttribute:link` event or specific event
  533. * (for example `changeAttribute:link:image`).
  534. *
  535. * @event changeAttribute
  536. * @param {Object} data Additional information about the change.
  537. * @param {module:engine/model/item~Item} data.item Changed item.
  538. * @param {module:engine/model/range~Range} data.range Range spanning over changed item.
  539. * @param {String} data.attributeKey Attribute key.
  540. * @param {*} data.attributeOldValue Attribute value before the change.
  541. * @param {*} data.attributeNewValue New attribute value.
  542. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  543. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  544. */
  545. /**
  546. * Fired for {@link module:engine/model/selection~Selection selection} changes.
  547. *
  548. * @event selection
  549. * @param {module:engine/model/selection~Selection} selection `Selection` instance that is converted.
  550. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  551. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  552. */
  553. /**
  554. * Fired for {@link module:engine/model/selection~Selection selection} attributes changes.
  555. *
  556. * `selectionAttribute` is a namespace for a class of events. Names of actually called events follow this pattern:
  557. * `selectionAttribute:<attributeKey>`. `attributeKey` is the key of selection attribute. This way listen can listen to
  558. * certain attribute, i.e. `addAttribute:bold`.
  559. *
  560. * @event selectionAttribute
  561. * @param {Object} data Additional information about the change.
  562. * @param {module:engine/model/selection~Selection} data.selection Selection that is converted.
  563. * @param {String} data.attributeKey Key of changed attribute.
  564. * @param {*} data.attributeValue Value of changed attribute.
  565. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  566. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  567. */
  568. /**
  569. * Fired when a new marker is added to the model.
  570. *
  571. * `addMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
  572. * `addMarker:<markerName>`. By specifying certain marker names, you can make the events even more gradual. For example,
  573. * markers can be named `foo:abc`, `foo:bar`, then it is possible to listen to `addMarker:foo` or `addMarker:foo:abc` and
  574. * `addMarker:foo:bar` events.
  575. *
  576. * @event addMarker
  577. * @param {Object} data Additional information about the change.
  578. * @param {String} data.name Marker name.
  579. * @param {module:engine/model/range~Range} data.range Marker range.
  580. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  581. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  582. */
  583. /**
  584. * Fired when marker is removed from the model.
  585. *
  586. * `removeMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
  587. * `removeMarker:<markerName>`. By specifying certain marker names, you can make the events even more gradual. For example,
  588. * markers can be named `foo:abc`, `foo:bar`, then it is possible to listen to `removeMarker:foo` or `removeMarker:foo:abc` and
  589. * `removeMarker:foo:bar` events.
  590. *
  591. * @event removeMarker
  592. * @param {Object} data Additional information about the change.
  593. * @param {String} data.name Marker name.
  594. * @param {module:engine/model/range~Range} data.range Marker range.
  595. * @param {module:engine/conversion/modelconsumable~ModelConsumable} consumable Values to consume.
  596. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ModelConversionDispatcher` constructor.
  597. */
  598. }
  599. mix( ModelConversionDispatcher, EmitterMixin );