8
0

markeroperation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @license Copyright (c) 2003-2019, 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/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 {Boolean} affectsData Specifies whether the marker operation affects the data produced by the data pipeline
  20. * (is persisted in the editor's data).
  21. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  22. * can be applied or `null` if the operation operates on detached (non-document) tree.
  23. */
  24. constructor( name, oldRange, newRange, markers, affectsData, baseVersion ) {
  25. super( baseVersion );
  26. /**
  27. * Marker name.
  28. *
  29. * @readonly
  30. * @member {String}
  31. */
  32. this.name = name;
  33. /**
  34. * Marker range before the change.
  35. *
  36. * @readonly
  37. * @member {module:engine/model/range~Range}
  38. */
  39. this.oldRange = oldRange ? oldRange.clone() : null;
  40. /**
  41. * Marker range after the change.
  42. *
  43. * @readonly
  44. * @member {module:engine/model/range~Range}
  45. */
  46. this.newRange = newRange ? newRange.clone() : null;
  47. /**
  48. * Specifies whether the marker operation affects the data produced by the data pipeline
  49. * (is persisted in the editor's data).
  50. *
  51. * @readonly
  52. * @member {Boolean}
  53. */
  54. this.affectsData = affectsData;
  55. /**
  56. * Marker collection on which change should be executed.
  57. *
  58. * @private
  59. * @member {module:engine/model/markercollection~MarkerCollection}
  60. */
  61. this._markers = markers;
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. get type() {
  67. return 'marker';
  68. }
  69. /**
  70. * Creates and returns an operation that has the same parameters as this operation.
  71. *
  72. * @returns {module:engine/model/operation/markeroperation~MarkerOperation} Clone of this operation.
  73. */
  74. clone() {
  75. return new MarkerOperation( this.name, this.oldRange, this.newRange, this._markers, this.affectsData, this.baseVersion );
  76. }
  77. /**
  78. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  79. *
  80. * @returns {module:engine/model/operation/markeroperation~MarkerOperation}
  81. */
  82. getReversed() {
  83. return new MarkerOperation( this.name, this.newRange, this.oldRange, this._markers, this.affectsData, this.baseVersion + 1 );
  84. }
  85. /**
  86. * @inheritDoc
  87. */
  88. _execute() {
  89. const type = this.newRange ? '_set' : '_remove';
  90. this._markers[ type ]( this.name, this.newRange, true, this.affectsData );
  91. }
  92. /**
  93. * @inheritDoc
  94. */
  95. toJSON() {
  96. const json = super.toJSON();
  97. if ( this.oldRange ) {
  98. json.oldRange = this.oldRange.toJSON();
  99. }
  100. if ( this.newRange ) {
  101. json.newRange = this.newRange.toJSON();
  102. }
  103. delete json._markers;
  104. return json;
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. static get className() {
  110. return 'MarkerOperation';
  111. }
  112. /**
  113. * Creates `MarkerOperation` object from deserialized object, i.e. from parsed JSON string.
  114. *
  115. * @param {Object} json Deserialized JSON object.
  116. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  117. * @returns {module:engine/model/operation/markeroperation~MarkerOperation}
  118. */
  119. static fromJSON( json, document ) {
  120. return new MarkerOperation(
  121. json.name,
  122. json.oldRange ? Range.fromJSON( json.oldRange, document ) : null,
  123. json.newRange ? Range.fromJSON( json.newRange, document ) : null,
  124. document.model.markers,
  125. json.affectsData,
  126. json.baseVersion
  127. );
  128. }
  129. // @if CK_DEBUG_ENGINE // toString() {
  130. // @if CK_DEBUG_ENGINE // return `MarkerOperation( ${ this.baseVersion } ): ` +
  131. // @if CK_DEBUG_ENGINE // `"${ this.name }": ${ this.oldRange } -> ${ this.newRange }`;
  132. // @if CK_DEBUG_ENGINE // }
  133. }