Просмотр исходного кода

Merge pull request #18305 from overleaf/mj-add-comment-invert

[overleaf-editor-core] Make AddCommentOperation inversion restore state

GitOrigin-RevId: 5a47a2463e3650ba7a6b0a0f53c895fa6ec1b54c
Mathias Jakobsen 2 лет назад
Родитель
Сommit
478a6157cc

+ 14 - 4
libraries/overleaf-editor-core/lib/operation/add_comment_operation.js

@@ -55,11 +55,21 @@ class AddCommentOperation extends EditOperation {
   }
 
   /**
-   *
-   * @returns {DeleteCommentOperation}
+   * @inheritdoc
+   * @param {StringFileData} previousState
+   * @returns {EditOperation}
    */
-  invert() {
-    return new core.DeleteCommentOperation(this.commentId)
+  invert(previousState) {
+    const comment = previousState.comments.getComment(this.commentId)
+    if (!comment) {
+      return new core.DeleteCommentOperation(this.commentId)
+    }
+
+    return new core.AddCommentOperation(
+      comment.id,
+      comment.ranges.slice(),
+      comment.resolved
+    )
   }
 
   /**

+ 75 - 14
libraries/overleaf-editor-core/test/add_comment_operation.test.js

@@ -44,21 +44,82 @@ describe('AddCommentOperation', function () {
     ])
   })
 
-  it('should invert operation', function () {
-    const fileData = new StringFileData('abc')
-    const op = new AddCommentOperation('123', [new Range(0, 1)])
-    op.apply(fileData)
-    expect(fileData.getComments().toRaw()).to.eql([
-      {
-        id: '123',
-        ranges: [{ pos: 0, length: 1 }],
-        resolved: false,
-      },
-    ])
+  describe('invert', function () {
+    it('should delete added comment', function () {
+      const initialFileData = new StringFileData('abc')
+      const fileData = StringFileData.fromRaw(initialFileData.toRaw())
+      const op = new AddCommentOperation('123', [new Range(0, 1)])
+      op.apply(fileData)
+      expect(fileData.getComments().toRaw()).to.eql([
+        {
+          id: '123',
+          ranges: [{ pos: 0, length: 1 }],
+          resolved: false,
+        },
+      ])
 
-    const invertedOp = op.invert()
-    invertedOp.apply(fileData)
-    expect(fileData.getComments().toRaw()).to.eql([])
+      const invertedOp = op.invert(initialFileData)
+      invertedOp.apply(fileData)
+      expect(fileData.getComments().toRaw()).to.eql([])
+    })
+
+    it('should restore previous comment ranges', function () {
+      const initialComments = [
+        {
+          id: '123',
+          ranges: [{ pos: 0, length: 1 }],
+          resolved: false,
+        },
+      ]
+
+      const initialFileData = new StringFileData(
+        'the quick brown fox jumps over the lazy dog',
+        initialComments
+      )
+      const fileData = StringFileData.fromRaw(initialFileData.toRaw())
+      const op = new AddCommentOperation('123', [new Range(12, 7)], true)
+      op.apply(fileData)
+      expect(fileData.getComments().toRaw()).to.eql([
+        {
+          id: '123',
+          ranges: [{ pos: 12, length: 7 }],
+          resolved: true,
+        },
+      ])
+
+      const invertedOp = op.invert(initialFileData)
+      invertedOp.apply(fileData)
+      expect(fileData.getComments().toRaw()).to.deep.equal(initialComments)
+    })
+
+    it('should restore previous comment resolution status', function () {
+      const initialComments = [
+        {
+          id: '123',
+          ranges: [{ pos: 0, length: 1 }],
+          resolved: false,
+        },
+      ]
+
+      const initialFileData = new StringFileData(
+        'the quick brown fox jumps over the lazy dog',
+        initialComments
+      )
+      const fileData = StringFileData.fromRaw(initialFileData.toRaw())
+      const op = new AddCommentOperation('123', [new Range(0, 1)], true)
+      op.apply(fileData)
+      expect(fileData.getComments().toRaw()).to.eql([
+        {
+          id: '123',
+          ranges: [{ pos: 0, length: 1 }],
+          resolved: true,
+        },
+      ])
+
+      const invertedOp = op.invert(initialFileData)
+      invertedOp.apply(fileData)
+      expect(fileData.getComments().toRaw()).to.deep.equal(initialComments)
+    })
   })
 
   it('should compose with DeleteCommentOperation', function () {