DiffCodec.coffee 701 B

12345678910111213141516171819202122232425262728293031
  1. diff_match_patch = require("../lib/diff_match_patch").diff_match_patch
  2. dmp = new diff_match_patch()
  3. module.exports = DiffCodec =
  4. ADDED: 1
  5. REMOVED: -1
  6. UNCHANGED: 0
  7. diffAsShareJsOp: (before, after, callback = (error, ops) ->) ->
  8. diffs = dmp.diff_main(before.join("\n"), after.join("\n"))
  9. dmp.diff_cleanupSemantic(diffs)
  10. ops = []
  11. position = 0
  12. for diff in diffs
  13. type = diff[0]
  14. content = diff[1]
  15. if type == @ADDED
  16. ops.push
  17. i: content
  18. p: position
  19. position += content.length
  20. else if type == @REMOVED
  21. ops.push
  22. d: content
  23. p: position
  24. else if type == @UNCHANGED
  25. position += content.length
  26. else
  27. throw "Unknown type"
  28. callback null, ops