From f026ad47369ef241ecf4e6c0ae79d7deda5b20b5 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Thu, 1 Aug 2024 12:45:22 +0200 Subject: [PATCH] Merge pull request #19547 from overleaf/jpa-move-v2-doc-versions [overleaf-editor-core] keep v2DocVersions in-sync with fileMap GitOrigin-RevId: 23491dfe51b71561837c96b8c550f75b0835a176 --- .../overleaf-editor-core/lib/snapshot.js | 1 + .../lib/v2_doc_versions.js | 17 ++++++++++++++ .../test/move_file_operation.test.js | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/libraries/overleaf-editor-core/lib/snapshot.js b/libraries/overleaf-editor-core/lib/snapshot.js index 2d5be82299..779fdbdbf8 100644 --- a/libraries/overleaf-editor-core/lib/snapshot.js +++ b/libraries/overleaf-editor-core/lib/snapshot.js @@ -133,6 +133,7 @@ class Snapshot { */ moveFile(pathname, newPathname) { this.fileMap.moveFile(pathname, newPathname) + if (this.v2DocVersions) this.v2DocVersions.moveFile(pathname, newPathname) } /** diff --git a/libraries/overleaf-editor-core/lib/v2_doc_versions.js b/libraries/overleaf-editor-core/lib/v2_doc_versions.js index 3911c2e396..4af306babd 100644 --- a/libraries/overleaf-editor-core/lib/v2_doc_versions.js +++ b/libraries/overleaf-editor-core/lib/v2_doc_versions.js @@ -50,6 +50,23 @@ class V2DocVersions { _.assign(snapshot.v2DocVersions.data, this.data) } } + + /** + * Move or remove a doc. + * Must be called after FileMap#moveFile, which validates the paths. + */ + moveFile(pathname, newPathname) { + for (const [id, v] of Object.entries(this.data)) { + if (v.pathname !== pathname) continue + + if (newPathname === '') { + delete this.data[id] + } else { + v.pathname = newPathname + } + break + } + } } module.exports = V2DocVersions diff --git a/libraries/overleaf-editor-core/test/move_file_operation.test.js b/libraries/overleaf-editor-core/test/move_file_operation.test.js index 4a55d122c2..d8bf923762 100644 --- a/libraries/overleaf-editor-core/test/move_file_operation.test.js +++ b/libraries/overleaf-editor-core/test/move_file_operation.test.js @@ -5,6 +5,9 @@ const ot = require('..') const File = ot.File const MoveFileOperation = ot.MoveFileOperation const Snapshot = ot.Snapshot +const Operation = ot.Operation +const V2DocVersions = ot.V2DocVersions +const TextOperation = ot.TextOperation describe('MoveFileOperation', function () { function makeEmptySnapshot() { @@ -39,4 +42,23 @@ describe('MoveFileOperation', function () { expect(snapshot.getFile('a').getContent()).to.equal('test: foo') expect(snapshot.getFile('bar').getContent()).to.equal('test: bar') }) + + it('should keep v2DocVersions in-sync', function () { + const snapshot = makeTwoFileSnapshot() + snapshot.setV2DocVersions( + V2DocVersions.fromRaw({ + id1: { pathname: 'foo', v: 1 }, + id2: { pathname: 'bar', v: 1 }, + }) + ) + Operation.moveFile('foo', 'foo-after').applyTo(snapshot) + Operation.editFile( + 'foo-after', + TextOperation.fromJSON({ textOperation: [9, 'edit'] }) + ).applyTo(snapshot) + Operation.removeFile('bar').applyTo(snapshot) + expect(snapshot.getV2DocVersions().toRaw()).to.deep.equal({ + id1: { pathname: 'foo-after', v: 1 }, + }) + }) })