Merge pull request #393 from sharelatex/as-fix-cm-sharejs

Fix CM/ShareJS adapter from throwing error when deleting
This commit is contained in:
Alasdair Smith 2018-03-13 11:21:52 +00:00 committed by GitHub
commit 1f7ef3ad6e

View file

@ -12,21 +12,10 @@ applyCMToShareJS = (editorDoc, delta, doc) ->
while i < delta.from.line
startPos += editorDoc.lineInfo(i).text.length + 1 # Add 1 for '\n'
i++
startPos += delta.from.ch
if delta.to.line == delta.from.line &&
delta.to.ch == delta.from.ch # Then nothing was removed.
doc.insert startPos, delta.text.join '\n'
else
delLen = delta.to.ch - delta.from.ch
while i < delta.to.line
delLen += editorDoc.lineInfo(i).text.length + 1 # Add 1 for '\n'
i++
doc.del startPos, delLen
doc.insert startPos, delta.text.join '\n' if delta.text
applyCMToShareJS editorDoc, delta.next, doc if delta.next
doc.del startPos, delta.removed.join('\n').length if delta.removed
doc.insert startPos, delta.text.join('\n') if delta.text
# Attach a CodeMirror editor to the document. The editor's contents are replaced
# with the document's contents unless keepEditorContents is true. (In which case
@ -47,8 +36,9 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) ->
console.error "Text does not match!"
console.error "editor: #{editorText}"
console.error "ot: #{otText}"
# Replace the editor text with the doc snapshot.
editor.setValue sharedoc.getText()
# Removed editor.setValue here as it would cause recursive loops if
# consistency check failed - because setting the value would trigger
# the change event
, 0
if keepEditorContents