8
0

modelconversiondispatcher.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Consumable from './modelconsumable.js';
  7. import TextFragment from '../treemodel/textfragment.js';
  8. // TODO: Add docs that if listener is called it means that element is consumable, so every listener need to stop the event if
  9. // it was consumed.
  10. export default class ModelConversionDispatcher {
  11. constructor( controller ) {
  12. this.controller = controller;
  13. }
  14. convertChange( type, changeInfo ) {
  15. if ( type == 'insert' || type == 'reinsert' ) {
  16. this.convertInsert( changeInfo.range );
  17. } else if ( type == 'move' ) {
  18. this.convertMove( changeInfo.range, changeInfo.sourcePosition );
  19. } else if ( type == 'remove' ) {
  20. this.convertRemove( changeInfo.range, changeInfo.sourcePosition );
  21. } else if ( type == 'addAttribute' || type == 'removeAttribute' || type == 'changeAttribute' ) {
  22. this.convertAttribute( type, changeInfo.range, changeInfo.key, changeInfo.oldValue, changeInfo.newValue );
  23. }
  24. }
  25. convertInsert( range ) {
  26. this.controller.consumable = this._createInsertConsumable();
  27. for ( let value of range ) {
  28. const item = value.item;
  29. const range = Range.createFromPositionAndShift( value.previousPosition, value.length );
  30. const model = { item, range };
  31. this._testAndFire( 'insert', model, this.controller );
  32. for ( let key of item.getAttributes() ) {
  33. model.attributeKey = key;
  34. this._testAndFire( 'addAttribute:' + key, model, this.controller );
  35. }
  36. }
  37. this.controller.consumable = undefined;
  38. }
  39. convertMove( range, sourcePosition ) {
  40. const model = {
  41. range: range,
  42. sourcePosition: sourcePosition,
  43. };
  44. this.fire( 'move', model, this.controller );
  45. }
  46. convertRemove( range, sourcePosition ) {
  47. const model = {
  48. range: range,
  49. sourcePosition: sourcePosition,
  50. };
  51. this.fire( 'remove', model, this.controller );
  52. }
  53. convertAttribute( type, range, key, oldValue, newValue ) {
  54. this.controller.consumable = this._createAttributeConsumable( type, range, key );
  55. for ( let value of range ) {
  56. const item = value.item;
  57. const range = Range.createFromPositionAndShift( value.previousPosition, value.length );
  58. const model = {
  59. item: item,
  60. range: range,
  61. attributeKey: key,
  62. attributeOldValue: oldValue,
  63. attributeNewValue: newValue
  64. };
  65. this._testAndFire( type + ':' + key, model, this.controller );
  66. }
  67. this.controller.consumable = undefined;
  68. }
  69. _createInsertConsumable( range ) {
  70. const consumable = new Consumable();
  71. for ( let value of range ) {
  72. const item = value.item;
  73. consumable.add( item, 'insert' );
  74. for ( let key of item.getAttributes() ) {
  75. consumable.add( item, 'addAttribute:' + key );
  76. }
  77. }
  78. return consumable;
  79. }
  80. _createAttributeConsumable( type, range, key ) {
  81. const consumable = new Consumable();
  82. for ( let value of range ) {
  83. const item = value.item;
  84. consumable.add( item, type + ':' + key );
  85. }
  86. return consumable;
  87. }
  88. _testAndFire( type, model, controller ) {
  89. if ( !model.consumable.test( model.item, type ) ) {
  90. // Do not fire event if the item was consumed.
  91. return;
  92. }
  93. if ( type === 'insert' ) {
  94. if ( model.item instanceof TextFragment ) {
  95. // e.g. insert:text
  96. this.fire( type + ':text', model, controller );
  97. } else {
  98. // e.g. insert:element:p
  99. this.fire( type + ':element:' + model.item.name, model, controller );
  100. }
  101. } else {
  102. // e.g. addAttribute:alt:img
  103. this.fire( type + ':' + model.item.name, model, controller );
  104. }
  105. }
  106. }