Merge pull request #17655 from overleaf/em-doc-length-fix

Fix doc length reporting in document-updater

GitOrigin-RevId: 37a9c35e3510f7b5be233fff817c89eeff1cb296
This commit is contained in:
Eric Mc Sween 2024-03-27 09:51:05 -04:00 committed by Copybot
parent 205cfbe816
commit 19937e3e7e
2 changed files with 27 additions and 1 deletions

View file

@ -320,7 +320,10 @@ const UpdateManager = {
historyRangesSupport
) {
let docLength = _.reduce(lines, (chars, line) => chars + line.length, 0)
docLength += lines.length - 1 // count newline characters
// Add newline characters. Lines are joined by newlines, but the last line
// doesn't include a newline. We must make a special case for an empty list
// so that it doesn't report a doc length of -1.
docLength += Math.max(lines.length - 1, 0)
let historyDocLength = docLength
for (const change of ranges.changes ?? []) {
if ('d' in change.op) {

View file

@ -678,6 +678,29 @@ describe('UpdateManager', function () {
},
])
})
it('should calculate the right doc length for an empty document', function () {
this.historyUpdates = [{ v: 42, op: [{ i: 'foobar', p: 0 }] }]
this.UpdateManager._adjustHistoryUpdatesMetadata(
this.historyUpdates,
this.pathname,
this.projectHistoryId,
[],
{},
false
)
this.historyUpdates.should.deep.equal([
{
projectHistoryId: this.projectHistoryId,
v: 42,
op: [{ i: 'foobar', p: 0 }],
meta: {
pathname: this.pathname,
doc_length: 0,
},
},
])
})
})
describe('lockUpdatesAndDo', function () {