operation.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/operation
  7. */
  8. import clone from '../../../utils/lib/lodash/clone.js';
  9. /**
  10. * Abstract base operation class.
  11. *
  12. * @abstract
  13. */
  14. export default class Operation {
  15. /**
  16. * Base operation constructor.
  17. * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which the operation can be applied.
  18. */
  19. constructor( baseVersion ) {
  20. /**
  21. * {@link module:engine/model/document~Document#version} on which operation can be applied. If you try to
  22. * {@link module:engine/model/document~Document#applyOperation apply} operation with different base version than the
  23. * {@link module:engine/model/document~Document#version document version} the
  24. * {@link module:utils/ckeditorerror~CKEditorError model-document-applyOperation-wrong-version} error is thrown.
  25. *
  26. * @member {Number}
  27. */
  28. this.baseVersion = baseVersion;
  29. /**
  30. * Operation type.
  31. *
  32. * @member {String} #type
  33. */
  34. /**
  35. * {@link module:engine/model/delta/delta~Delta Delta} which the operation is a part of. This property is set by the
  36. * {@link module:engine/model/delta/delta~Delta delta} when the operations is added to it by the
  37. * {@link module:engine/model/delta/delta~Delta#addOperation} method.
  38. *
  39. * @member {module:engine/model/delta/delta~Delta} #delta
  40. */
  41. /**
  42. * Creates and returns an operation that has the same parameters as this operation.
  43. *
  44. * @method #clone
  45. * @returns {module:engine/model/operation/operation~Operation} Clone of this operation.
  46. */
  47. /**
  48. * Creates and returns a reverse operation. Reverse operation when executed right after
  49. * the original operation will bring back tree model state to the point before the original
  50. * operation execution. In other words, it reverses changes done by the original operation.
  51. *
  52. * Keep in mind that tree model state may change since executing the original operation,
  53. * so reverse operation will be "outdated". In that case you will need to
  54. * {@link module:engine/model/operation/transform~transform} it by all operations that were executed after the original operation.
  55. *
  56. * @method #getReversed
  57. * @returns {module:engine/model/operation/operation~Operation} Reversed operation.
  58. */
  59. /**
  60. * Executes the operation - modifications described by the operation attributes
  61. * will be applied to the tree model.
  62. *
  63. * @protected
  64. * @method #_execute
  65. * @returns {Object} Object with additional information about the applied changes. It properties depends on the
  66. * operation type.
  67. */
  68. }
  69. /**
  70. * Custom toJSON method to solve child-parent circular dependencies.
  71. *
  72. * @method #toJSON
  73. * @returns {Object} Clone of this object with the delta property replaced with string.
  74. */
  75. toJSON() {
  76. const json = clone( this, true );
  77. json.__className = this.constructor.className;
  78. // Remove parent delta to avoid circular dependencies.
  79. delete json.delta;
  80. return json;
  81. }
  82. /**
  83. * Name of the operation class used for serialization.
  84. *
  85. * @type {String}
  86. */
  87. static get className() {
  88. return 'engine.model.operation.Operation';
  89. }
  90. /**
  91. * Creates Operation object from deserilized object, i.e. from parsed JSON string.
  92. *
  93. * @param {Object} json Deserialized JSON object.
  94. * @param {module:engine/model/document~Document} doc Document on which this operation will be applied.
  95. * @returns {module:engine/model/operation/operation~Operation}
  96. */
  97. static fromJSON( json ) {
  98. return new this( json.baseVersion );
  99. }
  100. }