2024-02-20 08:30:36 -05:00
|
|
|
// @ts-check
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
import _ from 'lodash'
|
|
|
|
import Core from 'overleaf-editor-core'
|
|
|
|
import * as Errors from './Errors.js'
|
|
|
|
import * as OperationsCompressor from './OperationsCompressor.js'
|
2024-05-22 08:15:07 -04:00
|
|
|
import { isInsert, isRetain, isDelete, isComment } from './Utils.js'
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
2024-09-20 09:52:23 -04:00
|
|
|
* @import { AddDocUpdate, AddFileUpdate, DeleteCommentUpdate, Op, RawScanOp } from './types'
|
|
|
|
* @import { RenameUpdate, TextUpdate, TrackingDirective, TrackingProps } from './types'
|
|
|
|
* @import { SetCommentStateUpdate, SetFileMetadataOperation, Update, UpdateWithBlob } from './types'
|
2024-02-20 08:30:36 -05:00
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* Convert updates into history changes
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {UpdateWithBlob[]} updatesWithBlobs
|
|
|
|
* @returns {Array<Core.Change | null>}
|
|
|
|
*/
|
|
|
|
export function convertToChanges(projectId, updatesWithBlobs) {
|
|
|
|
return updatesWithBlobs.map(update => _convertToChange(projectId, update))
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* Convert an update into a history change
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {UpdateWithBlob} updateWithBlob
|
|
|
|
* @returns {Core.Change | null}
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
function _convertToChange(projectId, updateWithBlob) {
|
|
|
|
let operations
|
|
|
|
const { update } = updateWithBlob
|
|
|
|
|
|
|
|
let projectVersion = null
|
|
|
|
const v2DocVersions = {}
|
|
|
|
|
|
|
|
if (_isRenameUpdate(update)) {
|
|
|
|
operations = [
|
|
|
|
{
|
|
|
|
pathname: _convertPathname(update.pathname),
|
|
|
|
newPathname: _convertPathname(update.new_pathname),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
projectVersion = update.version
|
|
|
|
} else if (isAddUpdate(update)) {
|
2024-05-28 09:17:55 -04:00
|
|
|
const op = {
|
|
|
|
pathname: _convertPathname(update.pathname),
|
|
|
|
file: {
|
|
|
|
hash: updateWithBlob.blobHashes.file,
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
2024-05-28 09:17:55 -04:00
|
|
|
}
|
|
|
|
if (_isAddDocUpdate(update)) {
|
|
|
|
op.file.rangesHash = updateWithBlob.blobHashes.ranges
|
|
|
|
}
|
2024-08-05 04:52:23 -04:00
|
|
|
if (_isAddFileUpdate(update)) {
|
|
|
|
op.file.metadata = update.metadata
|
|
|
|
}
|
2024-05-28 09:17:55 -04:00
|
|
|
operations = [op]
|
2023-01-13 07:42:29 -05:00
|
|
|
projectVersion = update.version
|
|
|
|
} else if (isTextUpdate(update)) {
|
2024-03-27 12:19:15 -04:00
|
|
|
const docLength = update.meta.history_doc_length ?? update.meta.doc_length
|
2023-01-13 07:42:29 -05:00
|
|
|
let pathname = update.meta.pathname
|
|
|
|
|
|
|
|
pathname = _convertPathname(pathname)
|
2024-02-20 08:30:36 -05:00
|
|
|
const builder = new OperationsBuilder(docLength, pathname)
|
2023-01-13 07:42:29 -05:00
|
|
|
// convert ops
|
|
|
|
for (const op of update.op) {
|
2024-03-27 12:19:15 -04:00
|
|
|
builder.addOp(op, update)
|
2024-02-20 08:30:36 -05:00
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
operations = builder.finish()
|
|
|
|
// add doc version information if present
|
|
|
|
if (update.v != null) {
|
|
|
|
v2DocVersions[update.doc] = { pathname, v: update.v }
|
|
|
|
}
|
2024-04-09 05:15:06 -04:00
|
|
|
} else if (isSetCommentStateUpdate(update)) {
|
|
|
|
operations = [
|
|
|
|
{
|
|
|
|
pathname: _convertPathname(update.pathname),
|
|
|
|
commentId: update.commentId,
|
|
|
|
resolved: update.resolved,
|
|
|
|
},
|
|
|
|
]
|
2024-08-05 04:52:23 -04:00
|
|
|
} else if (isSetFileMetadataOperation(update)) {
|
|
|
|
operations = [
|
|
|
|
{
|
|
|
|
pathname: _convertPathname(update.pathname),
|
|
|
|
metadata: update.metadata,
|
|
|
|
},
|
|
|
|
]
|
2024-03-11 12:24:43 -04:00
|
|
|
} else if (isDeleteCommentUpdate(update)) {
|
|
|
|
operations = [
|
|
|
|
{
|
|
|
|
pathname: _convertPathname(update.pathname),
|
|
|
|
deleteComment: update.deleteComment,
|
|
|
|
},
|
|
|
|
]
|
2023-01-13 07:42:29 -05:00
|
|
|
} else {
|
|
|
|
const error = new Errors.UpdateWithUnknownFormatError(
|
|
|
|
'update with unknown format',
|
|
|
|
{ projectId, update }
|
|
|
|
)
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2023-07-03 07:11:51 -04:00
|
|
|
let v2Authors
|
|
|
|
if (update.meta.user_id === 'anonymous-user') {
|
|
|
|
// history-v1 uses null to represent an anonymous author
|
|
|
|
v2Authors = [null]
|
|
|
|
} else {
|
|
|
|
// user_id is missing on resync operations that update the contents of a doc
|
|
|
|
v2Authors = _.compact([update.meta.user_id])
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
const rawChange = {
|
|
|
|
operations,
|
2023-07-03 07:11:51 -04:00
|
|
|
v2Authors,
|
2023-01-13 07:42:29 -05:00
|
|
|
timestamp: new Date(update.meta.ts).toISOString(),
|
|
|
|
projectVersion,
|
|
|
|
v2DocVersions: Object.keys(v2DocVersions).length ? v2DocVersions : null,
|
|
|
|
}
|
|
|
|
if (update.meta.origin) {
|
|
|
|
rawChange.origin = update.meta.origin
|
|
|
|
} else if (update.meta.type === 'external' && update.meta.source) {
|
|
|
|
rawChange.origin = { kind: update.meta.source }
|
|
|
|
}
|
|
|
|
const change = Core.Change.fromRaw(rawChange)
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
if (change != null) {
|
|
|
|
change.operations = OperationsCompressor.compressOperations(
|
|
|
|
change.operations
|
|
|
|
)
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
|
|
|
|
return change
|
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is RenameUpdate}
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
function _isRenameUpdate(update) {
|
2024-02-20 08:30:36 -05:00
|
|
|
return 'new_pathname' in update && update.new_pathname != null
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is AddDocUpdate}
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
function _isAddDocUpdate(update) {
|
2024-02-20 08:30:36 -05:00
|
|
|
return (
|
|
|
|
'doc' in update &&
|
|
|
|
update.doc != null &&
|
|
|
|
'docLines' in update &&
|
|
|
|
update.docLines != null
|
|
|
|
)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is AddFileUpdate}
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
function _isAddFileUpdate(update) {
|
2024-02-20 08:30:36 -05:00
|
|
|
return (
|
|
|
|
'file' in update &&
|
|
|
|
update.file != null &&
|
|
|
|
'url' in update &&
|
|
|
|
update.url != null
|
|
|
|
)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is TextUpdate}
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
export function isTextUpdate(update) {
|
|
|
|
return (
|
2024-02-20 08:30:36 -05:00
|
|
|
'doc' in update &&
|
2023-01-13 07:42:29 -05:00
|
|
|
update.doc != null &&
|
2024-02-20 08:30:36 -05:00
|
|
|
'op' in update &&
|
2023-01-13 07:42:29 -05:00
|
|
|
update.op != null &&
|
2024-02-20 08:30:36 -05:00
|
|
|
'pathname' in update.meta &&
|
2023-01-13 07:42:29 -05:00
|
|
|
update.meta.pathname != null &&
|
2024-02-20 08:30:36 -05:00
|
|
|
'doc_length' in update.meta &&
|
2023-01-13 07:42:29 -05:00
|
|
|
update.meta.doc_length != null
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isProjectStructureUpdate(update) {
|
|
|
|
return isAddUpdate(update) || _isRenameUpdate(update)
|
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is AddDocUpdate | AddFileUpdate}
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
export function isAddUpdate(update) {
|
|
|
|
return _isAddDocUpdate(update) || _isAddFileUpdate(update)
|
|
|
|
}
|
|
|
|
|
2024-04-09 05:15:06 -04:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is SetCommentStateUpdate}
|
|
|
|
*/
|
|
|
|
export function isSetCommentStateUpdate(update) {
|
|
|
|
return 'commentId' in update && 'resolved' in update
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:24:43 -04:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is DeleteCommentUpdate}
|
|
|
|
*/
|
|
|
|
export function isDeleteCommentUpdate(update) {
|
|
|
|
return 'deleteComment' in update
|
|
|
|
}
|
|
|
|
|
2024-08-05 04:52:23 -04:00
|
|
|
/**
|
|
|
|
* @param {Update} update
|
|
|
|
* @returns {update is SetFileMetadataOperation}
|
|
|
|
*/
|
|
|
|
export function isSetFileMetadataOperation(update) {
|
|
|
|
return 'metadata' in update
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
export function _convertPathname(pathname) {
|
|
|
|
// Strip leading /
|
|
|
|
pathname = pathname.replace(/^\//, '')
|
|
|
|
// Replace \\ with _. Backslashes are no longer allowed
|
|
|
|
// in projects in web, but we have some which have gone through
|
|
|
|
// into history before this restriction was added. This makes
|
|
|
|
// them valid for the history store.
|
|
|
|
// See https://github.com/overleaf/write_latex/issues/4471
|
|
|
|
pathname = pathname.replace(/\\/g, '_')
|
|
|
|
// workaround for filenames containing asterisks, this will
|
|
|
|
// fail if a corresponding replacement file already exists but it
|
|
|
|
// would fail anyway without this attempt to fix the pathname.
|
2024-02-06 04:34:15 -05:00
|
|
|
// See https://github.com/overleaf/internal/issues/900
|
2023-01-13 07:42:29 -05:00
|
|
|
pathname = pathname.replace(/\*/g, '__ASTERISK__')
|
|
|
|
// workaround for filenames beginning with spaces
|
2024-02-06 04:34:15 -05:00
|
|
|
// See https://github.com/overleaf/internal/issues/1404
|
2023-01-13 07:42:29 -05:00
|
|
|
// note: we have already stripped any leading slash above
|
|
|
|
pathname = pathname.replace(/^ /, '__SPACE__') // handle top-level
|
|
|
|
pathname = pathname.replace(/\/ /g, '/__SPACE__') // handle folders
|
|
|
|
return pathname
|
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
class OperationsBuilder {
|
|
|
|
/**
|
|
|
|
* @param {number} docLength
|
|
|
|
* @param {string} pathname
|
|
|
|
*/
|
2023-09-25 08:09:52 -04:00
|
|
|
constructor(docLength, pathname) {
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* List of operations being built
|
|
|
|
*/
|
2023-01-13 07:42:29 -05:00
|
|
|
this.operations = []
|
2024-02-20 08:30:36 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently built text operation
|
|
|
|
*
|
2024-03-27 12:19:15 -04:00
|
|
|
* @type {RawScanOp[]}
|
2024-02-20 08:30:36 -05:00
|
|
|
*/
|
|
|
|
this.textOperation = []
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cursor inside the current text operation
|
|
|
|
*/
|
2023-09-25 08:09:52 -04:00
|
|
|
this.cursor = 0
|
2024-02-20 08:30:36 -05:00
|
|
|
|
2023-09-25 08:09:52 -04:00
|
|
|
this.docLength = docLength
|
2023-01-13 07:42:29 -05:00
|
|
|
this.pathname = pathname
|
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
/**
|
|
|
|
* @param {Op} op
|
2024-05-03 09:27:39 -04:00
|
|
|
* @param {TextUpdate} update
|
2024-02-20 08:30:36 -05:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2024-03-27 12:19:15 -04:00
|
|
|
addOp(op, update) {
|
|
|
|
// We sometimes receive operations that operate at positions outside the
|
|
|
|
// docLength. Document updater coerces the position to the end of the
|
|
|
|
// document. We do the same here.
|
|
|
|
const pos = Math.min(op.hpos ?? op.p, this.docLength)
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
if (isComment(op)) {
|
|
|
|
// Close the current text operation
|
|
|
|
this.pushTextOperation()
|
|
|
|
|
|
|
|
// Add a comment operation
|
2024-06-04 07:36:44 -04:00
|
|
|
const commentLength = op.hlen ?? op.c.length
|
2024-05-28 07:24:06 -04:00
|
|
|
const commentOp = {
|
2024-02-20 08:30:36 -05:00
|
|
|
pathname: this.pathname,
|
|
|
|
commentId: op.t,
|
2024-06-04 07:36:44 -04:00
|
|
|
ranges: commentLength > 0 ? [{ pos, length: commentLength }] : [],
|
2024-05-28 07:24:06 -04:00
|
|
|
}
|
|
|
|
if ('resolved' in op) {
|
|
|
|
commentOp.resolved = op.resolved
|
|
|
|
}
|
|
|
|
this.operations.push(commentOp)
|
2024-02-20 08:30:36 -05:00
|
|
|
return
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-02-20 08:30:36 -05:00
|
|
|
|
2024-04-16 04:55:56 -04:00
|
|
|
if (!isInsert(op) && !isDelete(op) && !isRetain(op)) {
|
2023-01-13 07:42:29 -05:00
|
|
|
throw new Errors.UnexpectedOpTypeError('unexpected op type', { op })
|
|
|
|
}
|
|
|
|
|
2023-09-25 08:09:52 -04:00
|
|
|
if (pos < this.cursor) {
|
2024-02-20 08:30:36 -05:00
|
|
|
this.pushTextOperation()
|
2023-09-25 08:09:52 -04:00
|
|
|
// At this point, this.cursor === 0 and we can continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos > this.cursor) {
|
|
|
|
this.retain(pos - this.cursor)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
if (isInsert(op)) {
|
2024-03-27 12:19:15 -04:00
|
|
|
if (op.trackedDeleteRejection) {
|
|
|
|
this.retain(op.i.length, {
|
2024-05-16 08:23:57 -04:00
|
|
|
tracking: { type: 'none' },
|
2024-03-27 12:19:15 -04:00
|
|
|
})
|
2024-04-02 08:24:43 -04:00
|
|
|
} else {
|
|
|
|
const opts = {}
|
|
|
|
if (update.meta.tc != null) {
|
|
|
|
opts.tracking = {
|
2024-03-27 12:19:15 -04:00
|
|
|
type: 'insert',
|
|
|
|
userId: update.meta.user_id,
|
|
|
|
ts: new Date(update.meta.ts).toISOString(),
|
2024-04-02 08:24:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (op.commentIds != null) {
|
|
|
|
opts.commentIds = op.commentIds
|
|
|
|
}
|
|
|
|
this.insert(op.i, opts)
|
2024-03-27 12:19:15 -04:00
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-04-16 04:55:56 -04:00
|
|
|
if (isRetain(op)) {
|
|
|
|
if (op.tracking) {
|
|
|
|
this.retain(op.r.length, { tracking: op.tracking })
|
|
|
|
} else {
|
|
|
|
this.retain(op.r.length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
if (isDelete(op)) {
|
2024-03-27 12:19:15 -04:00
|
|
|
const changes = op.trackedChanges ?? []
|
|
|
|
|
|
|
|
// Tracked changes should already be ordered by offset, but let's make
|
|
|
|
// sure they are.
|
|
|
|
changes.sort((a, b) => {
|
|
|
|
const posOrder = a.offset - b.offset
|
|
|
|
if (posOrder !== 0) {
|
|
|
|
return posOrder
|
|
|
|
} else if (a.type === 'insert' && b.type === 'delete') {
|
|
|
|
return 1
|
|
|
|
} else if (a.type === 'delete' && b.type === 'insert') {
|
|
|
|
return -1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
let offset = 0
|
|
|
|
for (const change of changes) {
|
|
|
|
if (change.offset > offset) {
|
|
|
|
// Handle the portion before the tracked change
|
2024-06-05 10:25:17 -04:00
|
|
|
if (update.meta.tc != null) {
|
2024-03-27 12:19:15 -04:00
|
|
|
// This is a tracked delete
|
|
|
|
this.retain(change.offset - offset, {
|
|
|
|
tracking: {
|
|
|
|
type: 'delete',
|
|
|
|
userId: update.meta.user_id,
|
|
|
|
ts: new Date(update.meta.ts).toISOString(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// This is a regular delete
|
|
|
|
this.delete(change.offset - offset)
|
|
|
|
}
|
|
|
|
offset = change.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, handle the portion inside the tracked change
|
|
|
|
if (change.type === 'delete') {
|
|
|
|
// Tracked deletes are skipped over when deleting
|
|
|
|
this.retain(change.length)
|
|
|
|
} else if (change.type === 'insert') {
|
|
|
|
// Deletes inside tracked inserts are always regular deletes
|
|
|
|
this.delete(change.length)
|
|
|
|
offset += change.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset < op.d.length) {
|
|
|
|
// Handle the portion after the last tracked change
|
2024-06-05 10:25:17 -04:00
|
|
|
if (update.meta.tc != null) {
|
2024-03-27 12:19:15 -04:00
|
|
|
// This is a tracked delete
|
|
|
|
this.retain(op.d.length - offset, {
|
|
|
|
tracking: {
|
|
|
|
type: 'delete',
|
|
|
|
userId: update.meta.user_id,
|
|
|
|
ts: new Date(update.meta.ts).toISOString(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// This is a regular delete
|
|
|
|
this.delete(op.d.length - offset)
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2023-09-25 08:09:52 -04:00
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-03-27 12:19:15 -04:00
|
|
|
/**
|
|
|
|
* @param {number} length
|
|
|
|
* @param {object} opts
|
2024-05-22 08:15:07 -04:00
|
|
|
* @param {TrackingDirective} [opts.tracking]
|
2024-03-27 12:19:15 -04:00
|
|
|
*/
|
|
|
|
retain(length, opts = {}) {
|
|
|
|
if (opts.tracking) {
|
|
|
|
this.textOperation.push({ r: length, ...opts })
|
|
|
|
} else {
|
|
|
|
this.textOperation.push(length)
|
|
|
|
}
|
2023-09-25 08:09:52 -04:00
|
|
|
this.cursor += length
|
|
|
|
}
|
|
|
|
|
2024-03-27 12:19:15 -04:00
|
|
|
/**
|
|
|
|
* @param {string} str
|
|
|
|
* @param {object} opts
|
|
|
|
* @param {TrackingProps} [opts.tracking]
|
2024-04-02 08:24:43 -04:00
|
|
|
* @param {string[]} [opts.commentIds]
|
2024-03-27 12:19:15 -04:00
|
|
|
*/
|
|
|
|
insert(str, opts = {}) {
|
2024-04-02 08:24:43 -04:00
|
|
|
if (opts.tracking || opts.commentIds) {
|
2024-03-27 12:19:15 -04:00
|
|
|
this.textOperation.push({ i: str, ...opts })
|
|
|
|
} else {
|
|
|
|
this.textOperation.push(str)
|
|
|
|
}
|
2023-09-25 08:09:52 -04:00
|
|
|
this.cursor += str.length
|
|
|
|
this.docLength += str.length
|
|
|
|
}
|
|
|
|
|
2024-03-27 12:19:15 -04:00
|
|
|
/**
|
|
|
|
* @param {number} length
|
|
|
|
* @param {object} opts
|
|
|
|
*/
|
|
|
|
delete(length, opts = {}) {
|
2024-02-20 08:30:36 -05:00
|
|
|
this.textOperation.push(-length)
|
2023-09-25 08:09:52 -04:00
|
|
|
this.docLength -= length
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:30:36 -05:00
|
|
|
pushTextOperation() {
|
|
|
|
if (this.textOperation.length > 0)
|
|
|
|
if (this.cursor < this.docLength) {
|
|
|
|
this.retain(this.docLength - this.cursor)
|
|
|
|
}
|
|
|
|
if (this.textOperation.length > 0) {
|
2023-09-25 08:09:52 -04:00
|
|
|
this.operations.push({
|
|
|
|
pathname: this.pathname,
|
2024-02-20 08:30:36 -05:00
|
|
|
textOperation: this.textOperation,
|
2023-09-25 08:09:52 -04:00
|
|
|
})
|
2024-02-20 08:30:36 -05:00
|
|
|
this.textOperation = []
|
2023-09-25 08:09:52 -04:00
|
|
|
}
|
|
|
|
this.cursor = 0
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
finish() {
|
2024-02-20 08:30:36 -05:00
|
|
|
this.pushTextOperation()
|
2023-01-13 07:42:29 -05:00
|
|
|
return this.operations
|
|
|
|
}
|
|
|
|
}
|