markeroperation.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/markeroperation
  7. */
  8. import Operation from './operation';
  9. import Range from '../range';
  10. /**
  11. * @extends module:engine/model/operation/operation~Operation
  12. */
  13. export default class MarkerOperation extends Operation {
  14. /**
  15. * @param {String} name Marker name.
  16. * @param {module:engine/model/range~Range} oldRange Marker range before the change.
  17. * @param {module:engine/model/range~Range} newRange Marker range after the change.
  18. * @param {module:engine/model/markercollection~MarkerCollection} markers Marker collection on which change should be executed.
  19. * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which the operation can be applied.
  20. */
  21. constructor( name, oldRange, newRange, markers, baseVersion ) {
  22. super( baseVersion );
  23. /**
  24. * Marker name.
  25. *
  26. * @readonly
  27. * @member {String}
  28. */
  29. this.name = name;
  30. /**
  31. * Marker range before the change.
  32. *
  33. * @readonly
  34. * @member {module:engine/model/range~Range}
  35. */
  36. this.oldRange = oldRange ? Range.createFromRange( oldRange ) : null;
  37. /**
  38. * Marker range after the change.
  39. *
  40. * @readonly
  41. * @member {module:engine/model/range~Range}
  42. */
  43. this.newRange = newRange ? Range.createFromRange( newRange ) : null;
  44. /**
  45. * Marker collection on which change should be executed.
  46. *
  47. * @private
  48. * @member {module:engine/model/markercollection~MarkerCollection}
  49. */
  50. this._markers = markers;
  51. /**
  52. * @inheritDoc
  53. */
  54. this.isDocumentOperation = this._isDocumentOperation();
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. get type() {
  60. return 'marker';
  61. }
  62. /**
  63. * Checks if operation is executed on document or document fragment nodes.
  64. *
  65. * @private
  66. */
  67. _isDocumentOperation() {
  68. if ( this.newRange ) {
  69. return !!this.newRange.root.document;
  70. }
  71. if ( this.oldRange ) {
  72. return !!this.oldRange.root.document;
  73. }
  74. // This is edge and might happen only on data from the server.
  75. return true;
  76. }
  77. /**
  78. * Creates and returns an operation that has the same parameters as this operation.
  79. *
  80. * @returns {module:engine/model/operation/markeroperation~MarkerOperation} Clone of this operation.
  81. */
  82. clone() {
  83. return new MarkerOperation( this.name, this.oldRange, this.newRange, this._markers, this.baseVersion );
  84. }
  85. /**
  86. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  87. *
  88. * @returns {module:engine/model/operation/markeroperation~MarkerOperation}
  89. */
  90. getReversed() {
  91. return new MarkerOperation( this.name, this.newRange, this.oldRange, this._markers, this.baseVersion + 1 );
  92. }
  93. /**
  94. * @inheritDoc
  95. */
  96. _execute() {
  97. const type = this.newRange ? 'set' : 'remove';
  98. this._markers[ type ]( this.name, this.newRange );
  99. return { name: this.name, type };
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. toJSON() {
  105. const json = super.toJSON();
  106. delete json._markers;
  107. return json;
  108. }
  109. /**
  110. * @inheritDoc
  111. */
  112. static get className() {
  113. return 'engine.model.operation.MarkerOperation';
  114. }
  115. /**
  116. * Creates `MarkerOperation` object from deserilized object, i.e. from parsed JSON string.
  117. *
  118. * @param {Object} json Deserialized JSON object.
  119. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  120. * @returns {module:engine/model/operation/markeroperation~MarkerOperation}
  121. */
  122. static fromJSON( json, document ) {
  123. return new MarkerOperation(
  124. json.name,
  125. json.oldRange ? Range.fromJSON( json.oldRange, document ) : null,
  126. json.newRange ? Range.fromJSON( json.newRange, document ) : null,
  127. document.markers,
  128. json.baseVersion
  129. );
  130. }
  131. }