edit_operation.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // @ts-check
  2. /**
  3. * @typedef {import('../file_data')} FileData
  4. */
  5. class EditOperation {
  6. constructor() {
  7. if (this.constructor === EditOperation) {
  8. throw new Error('Cannot instantiate abstract class')
  9. }
  10. }
  11. /**
  12. * Converts operation into a JSON value.
  13. * @returns {object}
  14. */
  15. toJSON() {
  16. throw new Error('Abstract method not implemented')
  17. }
  18. /**
  19. * @abstract
  20. * @param {FileData} fileData
  21. */
  22. apply(fileData) {
  23. throw new Error('Abstract method not implemented')
  24. }
  25. /**
  26. * Determine the effect of this operation on the length of the text.
  27. *
  28. * NB: This is an Overleaf addition to the original OT system.
  29. *
  30. * @param {number} length of the original string; non-negative
  31. * @return {number} length of the new string; non-negative
  32. */
  33. applyToLength(length) {
  34. return length
  35. }
  36. /**
  37. * Computes the inverse of an operation. The inverse of an operation is the
  38. * operation that reverts the effects of the operation, e.g. when you have an
  39. * operation 'insert("hello "); skip(6);' then the inverse is 'remove("hello ");
  40. * skip(6);'. The inverse should be used for implementing undo.
  41. * @param {FileData} previousState
  42. * @returns {EditOperation}
  43. */
  44. invert(previousState) {
  45. throw new Error('Abstract method not implemented')
  46. }
  47. /**
  48. *
  49. * @param {EditOperation} other
  50. * @returns {boolean}
  51. */
  52. canBeComposedWith(other) {
  53. return false
  54. }
  55. /**
  56. * When you use ctrl-z to undo your latest changes, you expect the program not
  57. * to undo every single keystroke but to undo your last sentence you wrote at
  58. * a stretch or the deletion you did by holding the backspace key down. This
  59. * This can be implemented by composing operations on the undo stack. This
  60. * method can help decide whether two operations should be composed. It
  61. * returns true if the operations are consecutive insert operations or both
  62. * operations delete text at the same position. You may want to include other
  63. * factors like the time since the last change in your decision.
  64. * @param {EditOperation} other
  65. */
  66. canBeComposedWithForUndo(other) {
  67. return false
  68. }
  69. /**
  70. * Compose merges two consecutive operations into one operation, that
  71. * preserves the changes of both. Or, in other words, for each input string S
  72. * and a pair of consecutive operations A and B,
  73. * apply(apply(S, A), B) = apply(S, compose(A, B)) must hold.
  74. * @param {EditOperation} other
  75. * @returns {EditOperation}
  76. */
  77. compose(other) {
  78. throw new Error('Abstract method not implemented')
  79. }
  80. }
  81. module.exports = EditOperation