2024-02-13 08:15:43 -05:00
|
|
|
// @ts-check
|
|
|
|
|
2022-10-05 08:17:32 -04:00
|
|
|
const RangesTracker = require('@overleaf/ranges-tracker')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2024-02-13 08:15:43 -05:00
|
|
|
const OError = require('@overleaf/o-error')
|
2022-04-07 05:02:19 -04:00
|
|
|
const Metrics = require('./Metrics')
|
2020-05-06 06:09:33 -04:00
|
|
|
const _ = require('lodash')
|
2024-04-16 04:55:56 -04:00
|
|
|
const { isInsert, isDelete, isComment, getDocLength } = require('./Utils')
|
2024-02-13 08:15:43 -05:00
|
|
|
|
|
|
|
/**
|
2024-02-14 07:40:51 -05:00
|
|
|
* @typedef {import('./types').Comment} Comment
|
2024-02-13 08:15:43 -05:00
|
|
|
* @typedef {import('./types').CommentOp} CommentOp
|
|
|
|
* @typedef {import('./types').DeleteOp} DeleteOp
|
|
|
|
* @typedef {import('./types').HistoryCommentOp} HistoryCommentOp
|
|
|
|
* @typedef {import('./types').HistoryDeleteOp} HistoryDeleteOp
|
2024-03-26 07:28:14 -04:00
|
|
|
* @typedef {import('./types').HistoryDeleteTrackedChange} HistoryDeleteTrackedChange
|
2024-02-13 08:15:43 -05:00
|
|
|
* @typedef {import('./types').HistoryInsertOp} HistoryInsertOp
|
2024-04-16 04:55:56 -04:00
|
|
|
* @typedef {import('./types').HistoryRetainOp} HistoryRetainOp
|
2024-02-13 08:15:43 -05:00
|
|
|
* @typedef {import('./types').HistoryOp} HistoryOp
|
|
|
|
* @typedef {import('./types').HistoryUpdate} HistoryUpdate
|
|
|
|
* @typedef {import('./types').InsertOp} InsertOp
|
|
|
|
* @typedef {import('./types').Op} Op
|
|
|
|
* @typedef {import('./types').Ranges} Ranges
|
|
|
|
* @typedef {import('./types').TrackedChange} TrackedChange
|
|
|
|
* @typedef {import('./types').Update} Update
|
|
|
|
*/
|
2016-12-08 07:31:43 -05:00
|
|
|
|
2022-04-07 05:02:19 -04:00
|
|
|
const RANGE_DELTA_BUCKETS = [0, 1, 2, 3, 4, 5, 10, 20, 50]
|
|
|
|
|
2024-01-30 10:35:54 -05:00
|
|
|
const RangesManager = {
|
2020-05-06 06:09:33 -04:00
|
|
|
MAX_COMMENTS: 500,
|
|
|
|
MAX_CHANGES: 2000,
|
2017-01-10 10:58:11 -05:00
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
/**
|
|
|
|
* Apply an update to the given doc (lines and ranges) and return new ranges
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {string} docId
|
|
|
|
* @param {Ranges} ranges - ranges before the updates were applied
|
|
|
|
* @param {Update[]} updates
|
|
|
|
* @param {string[]} newDocLines - the document lines after the updates were applied
|
2024-02-16 07:40:13 -05:00
|
|
|
* @param {object} opts
|
|
|
|
* @param {boolean} [opts.historyRangesSupport] - whether history ranges support is enabled
|
2024-02-13 08:15:43 -05:00
|
|
|
* @returns {{ newRanges: Ranges, rangesWereCollapsed: boolean, historyUpdates: HistoryUpdate[] }}
|
|
|
|
*/
|
2024-02-16 07:40:13 -05:00
|
|
|
applyUpdate(projectId, docId, ranges, updates, newDocLines, opts = {}) {
|
2024-02-13 08:15:43 -05:00
|
|
|
if (ranges == null) {
|
|
|
|
ranges = {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
if (updates == null) {
|
|
|
|
updates = []
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
const { changes, comments } = _.cloneDeep(ranges)
|
2020-05-06 06:09:33 -04:00
|
|
|
const rangesTracker = new RangesTracker(changes, comments)
|
2022-04-07 05:02:19 -04:00
|
|
|
const [emptyRangeCountBefore, totalRangeCountBefore] =
|
|
|
|
RangesManager._emptyRangesCount(rangesTracker)
|
2024-02-13 08:15:43 -05:00
|
|
|
const historyUpdates = []
|
2024-02-06 08:08:59 -05:00
|
|
|
for (const update of updates) {
|
2024-02-13 08:15:43 -05:00
|
|
|
rangesTracker.track_changes = Boolean(update.meta?.tc)
|
|
|
|
if (update.meta?.tc) {
|
2020-05-06 06:09:33 -04:00
|
|
|
rangesTracker.setIdSeed(update.meta.tc)
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
const historyOps = []
|
2024-02-06 08:08:59 -05:00
|
|
|
for (const op of update.op) {
|
2024-02-16 07:40:13 -05:00
|
|
|
if (opts.historyRangesSupport) {
|
|
|
|
historyOps.push(
|
|
|
|
getHistoryOp(op, rangesTracker.comments, rangesTracker.changes)
|
|
|
|
)
|
|
|
|
} else if (isInsert(op) || isDelete(op)) {
|
|
|
|
historyOps.push(op)
|
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
rangesTracker.applyOp(op, { user_id: update.meta?.user_id })
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-04-16 04:56:07 -04:00
|
|
|
if (historyOps.length > 0) {
|
|
|
|
historyUpdates.push({ ...update, op: historyOps })
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2017-01-09 08:41:18 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
2024-02-06 08:08:59 -05:00
|
|
|
rangesTracker.changes?.length > RangesManager.MAX_CHANGES ||
|
|
|
|
rangesTracker.comments?.length > RangesManager.MAX_COMMENTS
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
2024-02-06 08:08:59 -05:00
|
|
|
throw new Error('too many comments or tracked changes')
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2017-03-15 10:12:06 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
try {
|
|
|
|
// This is a consistency check that all of our ranges and
|
|
|
|
// comments still match the corresponding text
|
|
|
|
rangesTracker.validate(newDocLines.join('\n'))
|
2024-02-06 08:08:59 -05:00
|
|
|
} catch (err) {
|
2020-05-06 06:09:33 -04:00
|
|
|
logger.error(
|
2024-02-06 08:08:59 -05:00
|
|
|
{ err, projectId, docId, newDocLines, updates },
|
2020-05-06 06:09:33 -04:00
|
|
|
'error validating ranges'
|
|
|
|
)
|
2024-02-06 08:08:59 -05:00
|
|
|
throw err
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2017-01-09 08:41:18 -05:00
|
|
|
|
2022-04-07 05:02:19 -04:00
|
|
|
const [emptyRangeCountAfter, totalRangeCountAfter] =
|
|
|
|
RangesManager._emptyRangesCount(rangesTracker)
|
2022-05-09 09:07:25 -04:00
|
|
|
const rangesWereCollapsed =
|
|
|
|
emptyRangeCountAfter > emptyRangeCountBefore ||
|
|
|
|
totalRangeCountAfter + 1 < totalRangeCountBefore // also include the case where multiple ranges were removed
|
2022-04-07 05:02:19 -04:00
|
|
|
// monitor the change in range count, we may want to snapshot before large decreases
|
|
|
|
if (totalRangeCountAfter < totalRangeCountBefore) {
|
|
|
|
Metrics.histogram(
|
|
|
|
'range-delta',
|
|
|
|
totalRangeCountBefore - totalRangeCountAfter,
|
|
|
|
RANGE_DELTA_BUCKETS,
|
|
|
|
{ status_code: rangesWereCollapsed ? 'saved' : 'unsaved' }
|
|
|
|
)
|
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
const newRanges = RangesManager._getRanges(rangesTracker)
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2020-05-06 06:09:33 -04:00
|
|
|
{
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2024-02-06 08:08:59 -05:00
|
|
|
changesCount: newRanges.changes?.length,
|
|
|
|
commentsCount: newRanges.comments?.length,
|
2021-07-13 07:04:42 -04:00
|
|
|
rangesWereCollapsed,
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
'applied updates to ranges'
|
|
|
|
)
|
2024-02-13 08:15:43 -05:00
|
|
|
return { newRanges, rangesWereCollapsed, historyUpdates }
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2017-05-04 10:32:54 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
acceptChanges(changeIds, ranges) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { changes, comments } = ranges
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug(`accepting ${changeIds.length} changes in ranges`)
|
2020-05-06 06:09:33 -04:00
|
|
|
const rangesTracker = new RangesTracker(changes, comments)
|
2023-03-21 08:06:13 -04:00
|
|
|
rangesTracker.removeChangeIds(changeIds)
|
2024-02-06 08:08:59 -05:00
|
|
|
const newRanges = RangesManager._getRanges(rangesTracker)
|
|
|
|
return newRanges
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
deleteComment(commentId, ranges) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { changes, comments } = ranges
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ commentId }, 'deleting comment in ranges')
|
2020-05-06 06:09:33 -04:00
|
|
|
const rangesTracker = new RangesTracker(changes, comments)
|
2023-03-21 08:06:13 -04:00
|
|
|
rangesTracker.removeCommentId(commentId)
|
2024-02-06 08:08:59 -05:00
|
|
|
const newRanges = RangesManager._getRanges(rangesTracker)
|
|
|
|
return newRanges
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2024-04-16 04:55:56 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} args
|
|
|
|
* @param {string} args.docId
|
|
|
|
* @param {string[]} args.acceptedChangeIds
|
|
|
|
* @param {TrackedChange[]} args.changes
|
|
|
|
* @param {string} args.pathname
|
|
|
|
* @param {string} args.projectHistoryId
|
|
|
|
* @param {string[]} args.lines
|
|
|
|
*/
|
|
|
|
getHistoryUpdatesForAcceptedChanges({
|
|
|
|
docId,
|
|
|
|
acceptedChangeIds,
|
|
|
|
changes,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
lines,
|
|
|
|
}) {
|
|
|
|
/** @type {(change: TrackedChange) => boolean} */
|
|
|
|
const isAccepted = change => acceptedChangeIds.includes(change.id)
|
|
|
|
|
|
|
|
const historyOps = []
|
|
|
|
|
|
|
|
// Keep ops in order of offset, with deletes before inserts
|
|
|
|
const sortedChanges = changes.slice().sort(function (c1, c2) {
|
|
|
|
const result = c1.op.p - c2.op.p
|
|
|
|
if (result !== 0) {
|
|
|
|
return result
|
|
|
|
} else if (isInsert(c1.op) && isDelete(c2.op)) {
|
|
|
|
return 1
|
|
|
|
} else if (isDelete(c1.op) && isInsert(c2.op)) {
|
|
|
|
return -1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const docLength = getDocLength(lines)
|
|
|
|
let historyDocLength = docLength
|
|
|
|
for (const change of sortedChanges) {
|
|
|
|
if (isDelete(change.op)) {
|
|
|
|
historyDocLength += change.op.d.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let unacceptedDeletes = 0
|
|
|
|
for (const change of sortedChanges) {
|
|
|
|
/** @type {HistoryOp | undefined} */
|
|
|
|
let op
|
|
|
|
|
|
|
|
if (isDelete(change.op)) {
|
|
|
|
if (isAccepted(change)) {
|
|
|
|
op = {
|
|
|
|
p: change.op.p,
|
|
|
|
d: change.op.d,
|
|
|
|
}
|
|
|
|
if (unacceptedDeletes > 0) {
|
|
|
|
op.hpos = op.p + unacceptedDeletes
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unacceptedDeletes += change.op.d.length
|
|
|
|
}
|
|
|
|
} else if (isInsert(change.op)) {
|
|
|
|
if (isAccepted(change)) {
|
|
|
|
op = {
|
|
|
|
p: change.op.p,
|
|
|
|
r: change.op.i,
|
2024-05-16 08:23:57 -04:00
|
|
|
tracking: { type: 'none' },
|
2024-04-16 04:55:56 -04:00
|
|
|
}
|
|
|
|
if (unacceptedDeletes > 0) {
|
|
|
|
op.hpos = op.p + unacceptedDeletes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!op) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {HistoryUpdate} */
|
|
|
|
const historyOp = {
|
|
|
|
doc: docId,
|
|
|
|
op: [op],
|
|
|
|
meta: {
|
|
|
|
...change.metadata,
|
|
|
|
doc_length: docLength,
|
|
|
|
pathname,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if (projectHistoryId) {
|
|
|
|
historyOp.projectHistoryId = projectHistoryId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (historyOp.meta && historyDocLength !== docLength) {
|
|
|
|
historyOp.meta.history_doc_length = historyDocLength
|
|
|
|
}
|
|
|
|
|
|
|
|
historyOps.push(historyOp)
|
|
|
|
|
|
|
|
if (isDelete(change.op) && isAccepted(change)) {
|
|
|
|
historyDocLength -= change.op.d.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return historyOps
|
|
|
|
},
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
_getRanges(rangesTracker) {
|
|
|
|
// Return the minimal data structure needed, since most documents won't have any
|
|
|
|
// changes or comments
|
2024-02-13 08:15:43 -05:00
|
|
|
|
|
|
|
const response = {}
|
2024-02-06 08:08:59 -05:00
|
|
|
if (rangesTracker.changes != null && rangesTracker.changes.length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
response.changes = rangesTracker.changes
|
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
if (rangesTracker.comments != null && rangesTracker.comments.length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
response.comments = rangesTracker.comments
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
},
|
|
|
|
|
|
|
|
_emptyRangesCount(ranges) {
|
2022-04-07 05:02:19 -04:00
|
|
|
let emptyCount = 0
|
|
|
|
let totalCount = 0
|
2024-02-06 08:08:59 -05:00
|
|
|
for (const comment of ranges.comments || []) {
|
2022-04-07 05:02:19 -04:00
|
|
|
totalCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
if (comment.op.c === '') {
|
2022-04-07 05:02:19 -04:00
|
|
|
emptyCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
for (const change of ranges.changes || []) {
|
2022-04-07 05:02:19 -04:00
|
|
|
totalCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
if (change.op.i != null) {
|
|
|
|
if (change.op.i === '') {
|
2022-04-07 05:02:19 -04:00
|
|
|
emptyCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 05:02:19 -04:00
|
|
|
return [emptyCount, totalCount]
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-01-30 10:35:54 -05:00
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
/**
|
|
|
|
* Calculate ops to be sent to the history system.
|
|
|
|
*
|
|
|
|
* @param {Op} op - the editor op
|
|
|
|
* @param {TrackedChange[]} changes - the list of tracked changes in the
|
|
|
|
* document before the op is applied. That list, coming from
|
|
|
|
* RangesTracker is ordered by position.
|
|
|
|
* @returns {HistoryOp}
|
|
|
|
*/
|
2024-02-14 07:40:51 -05:00
|
|
|
function getHistoryOp(op, comments, changes, opts = {}) {
|
2024-02-13 08:15:43 -05:00
|
|
|
if (isInsert(op)) {
|
2024-02-14 07:40:51 -05:00
|
|
|
return getHistoryOpForInsert(op, comments, changes)
|
2024-02-13 08:15:43 -05:00
|
|
|
} else if (isDelete(op)) {
|
|
|
|
return getHistoryOpForDelete(op, changes)
|
|
|
|
} else if (isComment(op)) {
|
|
|
|
return getHistoryOpForComment(op, changes)
|
|
|
|
} else {
|
|
|
|
throw new OError('Unrecognized op', { op })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate history ops for an insert
|
|
|
|
*
|
|
|
|
* Inserts are moved forward by tracked deletes placed strictly before the
|
|
|
|
* op. When an insert is made at the same position as a tracked delete, the
|
|
|
|
* insert is placed before the tracked delete.
|
|
|
|
*
|
2024-02-14 07:40:51 -05:00
|
|
|
* We also add a commentIds property when inserts are made inside a comment.
|
|
|
|
* The current behaviour is to include the insert in the comment only if the
|
|
|
|
* insert is made strictly inside the comment. Inserts made at the edges are
|
|
|
|
* not included in the comment.
|
|
|
|
*
|
2024-02-13 08:15:43 -05:00
|
|
|
* @param {InsertOp} op
|
2024-02-14 07:40:51 -05:00
|
|
|
* @param {Comment[]} comments
|
2024-02-13 08:15:43 -05:00
|
|
|
* @param {TrackedChange[]} changes
|
|
|
|
* @returns {HistoryInsertOp}
|
|
|
|
*/
|
2024-02-14 07:40:51 -05:00
|
|
|
function getHistoryOpForInsert(op, comments, changes) {
|
2024-02-13 08:15:43 -05:00
|
|
|
let hpos = op.p
|
|
|
|
let trackedDeleteRejection = false
|
2024-02-14 07:40:51 -05:00
|
|
|
const commentIds = new Set()
|
|
|
|
|
|
|
|
for (const comment of comments) {
|
|
|
|
if (comment.op.p < op.p && op.p < comment.op.p + comment.op.c.length) {
|
|
|
|
// Insert is inside the comment; add the comment id
|
|
|
|
commentIds.add(comment.op.t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const change of changes) {
|
|
|
|
if (!isDelete(change.op)) {
|
|
|
|
// We're only interested in tracked deletes
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.op.p < op.p) {
|
|
|
|
// Tracked delete is before the op. Move the op forward.
|
|
|
|
hpos += change.op.d.length
|
|
|
|
} else if (change.op.p === op.p) {
|
|
|
|
// Tracked delete is at the same position as the op. The insert comes before
|
|
|
|
// the tracked delete so it doesn't move.
|
|
|
|
if (op.u && change.op.d.startsWith(op.i)) {
|
|
|
|
// We're undoing and the insert matches the start of the tracked
|
|
|
|
// delete. RangesManager treats this as a tracked delete rejection. We
|
|
|
|
// will note this in the op so that project-history can take the
|
|
|
|
// appropriate action.
|
|
|
|
trackedDeleteRejection = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Tracked delete is after the insert. Tracked deletes are ordered, so
|
|
|
|
// we know that all subsequent tracked deletes will be after the insert
|
|
|
|
// and we can bail out.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {HistoryInsertOp} */
|
|
|
|
const historyOp = { ...op }
|
2024-02-14 07:40:51 -05:00
|
|
|
if (commentIds.size > 0) {
|
|
|
|
historyOp.commentIds = Array.from(commentIds)
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
if (hpos !== op.p) {
|
|
|
|
historyOp.hpos = hpos
|
|
|
|
}
|
|
|
|
if (trackedDeleteRejection) {
|
|
|
|
historyOp.trackedDeleteRejection = true
|
|
|
|
}
|
|
|
|
return historyOp
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate history op for a delete
|
|
|
|
*
|
|
|
|
* Deletes are moved forward by tracked deletes placed before or at the position of the
|
|
|
|
* op. If a tracked delete is inside the delete, the delete is split in parts
|
|
|
|
* so that characters are deleted around the tracked delete, but the tracked
|
|
|
|
* delete itself is not deleted.
|
|
|
|
*
|
|
|
|
* @param {DeleteOp} op
|
|
|
|
* @param {TrackedChange[]} changes
|
|
|
|
* @returns {HistoryDeleteOp}
|
|
|
|
*/
|
|
|
|
function getHistoryOpForDelete(op, changes, opts = {}) {
|
|
|
|
let hpos = op.p
|
2024-03-26 07:28:14 -04:00
|
|
|
const opEnd = op.p + op.d.length
|
|
|
|
/** @type HistoryDeleteTrackedChange[] */
|
|
|
|
const changesInsideDelete = []
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const change of changes) {
|
|
|
|
if (change.op.p <= op.p) {
|
2024-03-26 07:28:14 -04:00
|
|
|
if (isDelete(change.op)) {
|
|
|
|
// Tracked delete is before or at the position of the incoming delete.
|
|
|
|
// Move the op forward.
|
|
|
|
hpos += change.op.d.length
|
|
|
|
} else if (isInsert(change.op)) {
|
|
|
|
const changeEnd = change.op.p + change.op.i.length
|
|
|
|
const endPos = Math.min(changeEnd, opEnd)
|
|
|
|
if (endPos > op.p) {
|
|
|
|
// Part of the tracked insert is inside the delete
|
|
|
|
changesInsideDelete.push({
|
|
|
|
type: 'insert',
|
|
|
|
offset: 0,
|
|
|
|
length: endPos - op.p,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
} else if (change.op.p < op.p + op.d.length) {
|
2024-03-26 07:28:14 -04:00
|
|
|
// Tracked change inside the deleted text. Record it for the history system.
|
|
|
|
if (isDelete(change.op)) {
|
|
|
|
changesInsideDelete.push({
|
|
|
|
type: 'delete',
|
|
|
|
offset: change.op.p - op.p,
|
|
|
|
length: change.op.d.length,
|
|
|
|
})
|
|
|
|
} else if (isInsert(change.op)) {
|
|
|
|
changesInsideDelete.push({
|
|
|
|
type: 'insert',
|
|
|
|
offset: change.op.p - op.p,
|
|
|
|
length: Math.min(change.op.i.length, opEnd - change.op.p),
|
|
|
|
})
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
} else {
|
2024-03-26 07:28:14 -04:00
|
|
|
// We've seen all tracked changes before or inside the delete
|
2024-02-13 08:15:43 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {HistoryDeleteOp} */
|
|
|
|
const historyOp = { ...op }
|
|
|
|
if (hpos !== op.p) {
|
|
|
|
historyOp.hpos = hpos
|
|
|
|
}
|
2024-03-26 07:28:14 -04:00
|
|
|
if (changesInsideDelete.length > 0) {
|
|
|
|
historyOp.trackedChanges = changesInsideDelete
|
2024-02-13 08:15:43 -05:00
|
|
|
}
|
|
|
|
return historyOp
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate history ops for a comment
|
|
|
|
*
|
|
|
|
* Comments are moved forward by tracked deletes placed before or at the
|
|
|
|
* position of the op. If a tracked delete is inside the comment, the length of
|
|
|
|
* the comment is extended to include the tracked delete.
|
|
|
|
*
|
|
|
|
* @param {CommentOp} op
|
|
|
|
* @param {TrackedChange[]} changes
|
|
|
|
* @returns {HistoryCommentOp}
|
|
|
|
*/
|
|
|
|
function getHistoryOpForComment(op, changes) {
|
|
|
|
let hpos = op.p
|
|
|
|
let hlen = op.c.length
|
|
|
|
for (const change of changes) {
|
|
|
|
if (!isDelete(change.op)) {
|
|
|
|
// We're only interested in tracked deletes
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.op.p <= op.p) {
|
|
|
|
// Tracked delete is before or at the position of the incoming comment.
|
|
|
|
// Move the op forward.
|
|
|
|
hpos += change.op.d.length
|
|
|
|
} else if (change.op.p < op.p + op.c.length) {
|
|
|
|
// Tracked comment inside the comment. Extend the length
|
|
|
|
hlen += change.op.d.length
|
|
|
|
} else {
|
|
|
|
// We've seen all tracked deletes before or inside the comment
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {HistoryCommentOp} */
|
|
|
|
const historyOp = { ...op }
|
|
|
|
if (hpos !== op.p) {
|
|
|
|
historyOp.hpos = hpos
|
|
|
|
}
|
|
|
|
if (hlen !== op.c.length) {
|
|
|
|
historyOp.hlen = hlen
|
|
|
|
}
|
|
|
|
return historyOp
|
|
|
|
}
|
|
|
|
|
2024-01-30 10:35:54 -05:00
|
|
|
module.exports = RangesManager
|