operation.js 3.5 KB

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