Update RangeTracker to support upserting and moving comments for cut and paste

This commit is contained in:
James Allen 2017-03-16 15:49:41 +00:00
parent 16ebd155ec
commit 9c5299ec7c

View file

@ -1,5 +1,8 @@
load = (EventEmitter) -> # This file is shared between document-updater and web, so that the server and client share
class RangesTracker extends EventEmitter # an identical track changes implementation. Do not edit it directly in web or document-updater,
# instead edit it at https://github.com/sharelatex/ranges-tracker, where it has a suite of tests
load = () ->
class RangesTracker
# The purpose of this class is to track a set of inserts and deletes to a document, like # The purpose of this class is to track a set of inserts and deletes to a document, like
# track changes in Word. We store these as a set of ShareJs style ranges: # track changes in Word. We store these as a set of ShareJs style ranges:
# {i: "foo", p: 42} # Insert 'foo' at offset 42 # {i: "foo", p: 42} # Insert 'foo' at offset 42
@ -36,6 +39,7 @@ load = (EventEmitter) ->
# middle of a previous insert by the first user, the original insert will be split into two. # middle of a previous insert by the first user, the original insert will be split into two.
constructor: (@changes = [], @comments = []) -> constructor: (@changes = [], @comments = []) ->
@setIdSeed(RangesTracker.generateIdSeed()) @setIdSeed(RangesTracker.generateIdSeed())
@resetDirtyState()
getIdSeed: () -> getIdSeed: () ->
return @id_seed return @id_seed
@ -75,8 +79,15 @@ load = (EventEmitter) ->
comment = @getComment(comment_id) comment = @getComment(comment_id)
return if !comment? return if !comment?
@comments = @comments.filter (c) -> c.id != comment_id @comments = @comments.filter (c) -> c.id != comment_id
@emit "comment:removed", comment @_markAsDirty comment, "comment", "removed"
moveCommentId: (comment_id, position, text) ->
for comment in @comments
if comment.id == comment_id
comment.op.p = position
comment.op.c = text
@_markAsDirty comment, "comment", "moved"
getChange: (change_id) -> getChange: (change_id) ->
change = null change = null
for c in @changes for c in @changes
@ -89,6 +100,18 @@ load = (EventEmitter) ->
change = @getChange(change_id) change = @getChange(change_id)
return if !change? return if !change?
@_removeChange(change) @_removeChange(change)
validate: (text) ->
for change in @changes
if change.op.i?
content = text.slice(change.op.p, change.op.p + change.op.i.length)
if content != change.op.i
throw new Error("Change (#{JSON.stringify(change)}) doesn't match text (#{JSON.stringify(content)})")
for comment in @comments
content = text.slice(comment.op.p, comment.op.p + comment.op.c.length)
if content != comment.op.c
throw new Error("Comment (#{JSON.stringify(comment)}) doesn't match text (#{JSON.stringify(content)})")
return true
applyOp: (op, metadata = {}) -> applyOp: (op, metadata = {}) ->
metadata.ts ?= new Date() metadata.ts ?= new Date()
@ -103,29 +126,37 @@ load = (EventEmitter) ->
@addComment(op, metadata) @addComment(op, metadata)
else else
throw new Error("unknown op type") throw new Error("unknown op type")
applyOps: (ops, metadata = {}) ->
for op in ops
@applyOp(op, metadata)
addComment: (op, metadata) -> addComment: (op, metadata) ->
# TODO: Don't allow overlapping comments? existing = @getComment(op.t)
@comments.push comment = { if existing?
id: op.t or @newId() @moveCommentId(op.t, op.p, op.c)
op: # Copy because we'll modify in place return existing
c: op.c else
p: op.p @comments.push comment = {
t: op.t id: op.t or @newId()
metadata op: # Copy because we'll modify in place
} c: op.c
@emit "comment:added", comment p: op.p
return comment t: op.t
metadata
}
@_markAsDirty comment, "comment", "added"
return comment
applyInsertToComments: (op) -> applyInsertToComments: (op) ->
for comment in @comments for comment in @comments
if op.p <= comment.op.p if op.p <= comment.op.p
comment.op.p += op.i.length comment.op.p += op.i.length
@emit "comment:moved", comment @_markAsDirty comment, "comment", "moved"
else if op.p < comment.op.p + comment.op.c.length else if op.p < comment.op.p + comment.op.c.length
offset = op.p - comment.op.p offset = op.p - comment.op.p
comment.op.c = comment.op.c[0..(offset-1)] + op.i + comment.op.c[offset...] comment.op.c = comment.op.c[0..(offset-1)] + op.i + comment.op.c[offset...]
@emit "comment:moved", comment @_markAsDirty comment, "comment", "moved"
applyDeleteToComments: (op) -> applyDeleteToComments: (op) ->
op_start = op.p op_start = op.p
@ -138,7 +169,7 @@ load = (EventEmitter) ->
if op_end <= comment_start if op_end <= comment_start
# delete is fully before comment # delete is fully before comment
comment.op.p -= op_length comment.op.p -= op_length
@emit "comment:moved", comment @_markAsDirty comment, "comment", "moved"
else if op_start >= comment_end else if op_start >= comment_end
# delete is fully after comment, nothing to do # delete is fully after comment, nothing to do
else else
@ -161,12 +192,13 @@ load = (EventEmitter) ->
comment.op.p = Math.min(comment_start, op_start) comment.op.p = Math.min(comment_start, op_start)
comment.op.c = remaining_before + remaining_after comment.op.c = remaining_before + remaining_after
@emit "comment:moved", comment @_markAsDirty comment, "comment", "moved"
applyInsertToChanges: (op, metadata) -> applyInsertToChanges: (op, metadata) ->
op_start = op.p op_start = op.p
op_length = op.i.length op_length = op.i.length
op_end = op.p + op_length op_end = op.p + op_length
undoing = !!op.u
already_merged = false already_merged = false
@ -184,8 +216,9 @@ load = (EventEmitter) ->
change.op.p += op_length change.op.p += op_length
moved_changes.push change moved_changes.push change
else if op_start == change_start else if op_start == change_start
# If the insert matches the start of the delete, just remove it from the delete instead # If we are undoing, then we want to cancel any existing delete ranges if we can.
if change.op.d.length >= op.i.length and change.op.d.slice(0, op.i.length) == op.i # Check if the insert matches the start of the delete, and just remove it from the delete instead if so.
if undoing and change.op.d.length >= op.i.length and change.op.d.slice(0, op.i.length) == op.i
change.op.d = change.op.d.slice(op.i.length) change.op.d = change.op.d.slice(op.i.length)
change.op.p += op.i.length change.op.p += op.i.length
if change.op.d == "" if change.op.d == ""
@ -203,15 +236,15 @@ load = (EventEmitter) ->
# Only merge inserts if they are from the same user # Only merge inserts if they are from the same user
is_same_user = metadata.user_id == change.metadata.user_id is_same_user = metadata.user_id == change.metadata.user_id
# If this is an insert op at the end of an existing insert with a delete following, and it cancels out the following # If we are undoing, then our changes will be removed from any delete ops just after. In that case, if there is also
# delete then we shouldn't append it to this insert, but instead only cancel the following delete. # an insert op just before, then we shouldn't append it to this insert, but instead only cancel the following delete.
# E.g. # E.g.
# foo|<--- about to insert 'b' here # foo|<--- about to insert 'b' here
# inserted 'foo' --^ ^-- deleted 'bar' # inserted 'foo' --^ ^-- deleted 'bar'
# should become just 'foo' not 'foob' (with the delete marker becoming just 'ar'), . # should become just 'foo' not 'foob' (with the delete marker becoming just 'ar'), .
next_change = @changes[i+1] next_change = @changes[i+1]
is_op_adjacent_to_next_delete = next_change? and next_change.op.d? and op.p == change_end and next_change.op.p == op.p is_op_adjacent_to_next_delete = next_change? and next_change.op.d? and op.p == change_end and next_change.op.p == op.p
will_op_cancel_next_delete = is_op_adjacent_to_next_delete and next_change.op.d.slice(0, op.i.length) == op.i will_op_cancel_next_delete = undoing and is_op_adjacent_to_next_delete and next_change.op.d.slice(0, op.i.length) == op.i
# If there is a delete at the start of the insert, and we're inserting # If there is a delete at the start of the insert, and we're inserting
# at the start, we SHOULDN'T merge since the delete acts as a partition. # at the start, we SHOULDN'T merge since the delete acts as a partition.
@ -281,8 +314,8 @@ load = (EventEmitter) ->
for change in remove_changes for change in remove_changes
@_removeChange change @_removeChange change
if moved_changes.length > 0 for change in moved_changes
@emit "changes:moved", moved_changes @_markAsDirty change, "change", "moved"
applyDeleteToChanges: (op, metadata) -> applyDeleteToChanges: (op, metadata) ->
op_start = op.p op_start = op.p
@ -406,8 +439,8 @@ load = (EventEmitter) ->
@_removeChange change @_removeChange change
moved_changes = moved_changes.filter (c) -> c != change moved_changes = moved_changes.filter (c) -> c != change
if moved_changes.length > 0 for change in moved_changes
@emit "changes:moved", moved_changes @_markAsDirty change, "change", "moved"
_addOp: (op, metadata) -> _addOp: (op, metadata) ->
change = { change = {
@ -427,17 +460,11 @@ load = (EventEmitter) ->
else else
return -1 return -1
if op.d? @_markAsDirty(change, "change", "added")
@emit "delete:added", change
else if op.i?
@emit "insert:added", change
_removeChange: (change) -> _removeChange: (change) ->
@changes = @changes.filter (c) -> c.id != change.id @changes = @changes.filter (c) -> c.id != change.id
if change.op.d? @_markAsDirty change, "change", "removed"
@emit "delete:removed", change
else if change.op.i?
@emit "insert:removed", change
_applyOpModifications: (content, op_modifications) -> _applyOpModifications: (content, op_modifications) ->
# Put in descending position order, with deleting first if at the same offset # Put in descending position order, with deleting first if at the same offset
@ -486,13 +513,32 @@ load = (EventEmitter) ->
previous_change = change previous_change = change
return { moved_changes, remove_changes } return { moved_changes, remove_changes }
resetDirtyState: () ->
@_dirtyState = {
comment: {
moved: {}
removed: {}
added: {}
}
change: {
moved: {}
removed: {}
added: {}
}
}
getDirtyState: () ->
return @_dirtyState
_markAsDirty: (object, type, action) ->
@_dirtyState[type][action][object.id] = object
_clone: (object) -> _clone: (object) ->
clone = {} clone = {}
(clone[k] = v for k,v of object) (clone[k] = v for k,v of object)
return clone return clone
if define? if define?
define ["utils/EventEmitter"], load define [], load
else else
EventEmitter = require("events").EventEmitter module.exports = load()
module.exports = load(EventEmitter)