mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
c4437c69bc
* added getHistoryOpForAcceptedChange in RangesManager * rename adjustHistoryUpdatesMetadata to be treated as public * handle retain op in UpdateTranslator and updateCompressor * send op to project-history in acceptChanges * use promises.queueOps * use ranges in getHistoryOpForAcceptedChange * using rangesWithChangeRemoved * acceptChanges acceptance test * using change.op.hpos * Revert "using change.op.hpos" This reverts commit f53333b5099c840ab8fb8bb08df198ad6cfa2d84. * use getHistoryOpForAcceptedChanges * fix historyDocLength * Revert "rename adjustHistoryUpdatesMetadata to be treated as public" This reverts commit 2ba9443fd040a5c953828584285887c00dc40ea6. * fix typescript issues * sort changes before creating history updates * fix tests * sinon spy RangesManager.getHistoryUpdatesForAcceptedChanges * added unit tests * sort deletes before inserts * use getDocLength function * fix docLength calculation * fix typo * allow all retains * fix lint error * refactor RangesTests * fix ts error * fix history_doc_length calculation in RangesManager * remove retain tracking check from UpdateCompressor * use makeRanges() properly in tests * refactor acceptance tests GitOrigin-RevId: ab12ec53c5f52c20d44827c6037335e048f2edb0
92 lines
2.1 KiB
JavaScript
92 lines
2.1 KiB
JavaScript
// @ts-check
|
|
const _ = require('lodash')
|
|
|
|
/**
|
|
* @typedef {import('./types').CommentOp} CommentOp
|
|
* @typedef {import('./types').DeleteOp} DeleteOp
|
|
* @typedef {import('./types').InsertOp} InsertOp
|
|
* @typedef {import('./types').Op} Op
|
|
* @typedef {import('./types').TrackedChange} TrackedChange
|
|
*/
|
|
|
|
/**
|
|
* Returns true if the op is an insert
|
|
*
|
|
* @param {Op} op
|
|
* @returns {op is InsertOp}
|
|
*/
|
|
function isInsert(op) {
|
|
return 'i' in op && op.i != null
|
|
}
|
|
|
|
/**
|
|
* Returns true if the op is an insert
|
|
*
|
|
* @param {Op} op
|
|
* @returns {op is DeleteOp}
|
|
*/
|
|
function isDelete(op) {
|
|
return 'd' in op && op.d != null
|
|
}
|
|
|
|
/**
|
|
* Returns true if the op is a comment
|
|
*
|
|
* @param {Op} op
|
|
* @returns {op is CommentOp}
|
|
*/
|
|
function isComment(op) {
|
|
return 'c' in op && op.c != null
|
|
}
|
|
|
|
/**
|
|
* Get the length of a document from its lines
|
|
*
|
|
* @param {string[]} lines
|
|
* @returns {number}
|
|
*/
|
|
function getDocLength(lines) {
|
|
let docLength = _.reduce(lines, (chars, line) => chars + line.length, 0)
|
|
// 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)
|
|
|
|
return docLength
|
|
}
|
|
|
|
/**
|
|
* Adds given tracked deletes to the given content.
|
|
*
|
|
* The history system includes tracked deletes in the document content.
|
|
*
|
|
* @param {string} content
|
|
* @param {TrackedChange[]} trackedChanges
|
|
* @return {string} content for the history service
|
|
*/
|
|
function addTrackedDeletesToContent(content, trackedChanges) {
|
|
let cursor = 0
|
|
let result = ''
|
|
for (const change of trackedChanges) {
|
|
if (isDelete(change.op)) {
|
|
// Add the content before the tracked delete
|
|
result += content.slice(cursor, change.op.p)
|
|
cursor = change.op.p
|
|
// Add the content of the tracked delete
|
|
result += change.op.d
|
|
}
|
|
}
|
|
|
|
// Add the content after all tracked deletes
|
|
result += content.slice(cursor)
|
|
|
|
return result
|
|
}
|
|
|
|
module.exports = {
|
|
isInsert,
|
|
isDelete,
|
|
isComment,
|
|
addTrackedDeletesToContent,
|
|
getDocLength,
|
|
}
|