Skip to content

Instantly share code, notes, and snippets.

@vincentwoo
Last active April 2, 2016 11:38
Show Gist options
  • Save vincentwoo/7a22ec696a2bbaad6f631c281b27c00c to your computer and use it in GitHub Desktop.
Save vincentwoo/7a22ec696a2bbaad6f631c281b27c00c to your computer and use it in GitHub Desktop.
before = null
$ ->
return unless editor?.constructor == CodeMirror
editor.on 'beforeChange', (cm) -> before ||= cm.getValue()
Firepad.RichTextCodeMirrorAdapter::onChange = (_, changes) ->
if changes[0].origin != 'RTCMADAPTER'
pair = Firepad.RichTextCodeMirrorAdapter.operationFromCodeMirrorChanges changes, this.cm
this.trigger 'change', pair[0], pair[1]
before = null
operationFromCodeMirrorChanges = Firepad.RichTextCodeMirrorAdapter.operationFromCodeMirrorChanges
Firepad.RichTextCodeMirrorAdapter.operationFromCodeMirrorChanges = (changes, cm) ->
after = cm.getValue()
diffObj = new DiffMatchPatch()
diffs = diffObj.diff_main before, after
diffObj.diff_cleanupSemantic(diffs)
operation = new Firepad.TextOperation()
inverse = new Firepad.TextOperation()
for [type, value] in diffs
if type == 1
operation.insert value
inverse['delete'] value
else if type == -1
operation['delete'] value
inverse.insert value
else
operation.retain value.length
inverse.retain value.length
try
oldResults = operationFromCodeMirrorChanges changes, cm
catch
Raven.captureMessage 'firepad textop exploded, trying to save it', extra: {
before, after
changes: changes
dmp: operation.toJSON()
}
return [operation, inverse]
# console.log "new: ", operation.toJSON()
# console.log "old: ", oldResults[0].toJSON()
unless operation.apply(before) == oldResults[0].apply(before)
Raven.captureMessage 'firepad and dmp disagree', extra: {
before, after
changes: changes
firepad: oldResults[0],
dmp: operation
}
oldResults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment