simple.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. // This is a really simple OT type. Its not compiled with the web client, but it could be.
  9. //
  10. // Its mostly included for demonstration purposes and its used in a lot of unit tests.
  11. //
  12. // This defines a really simple text OT type which only allows inserts. (No deletes).
  13. //
  14. // Ops look like:
  15. // {position:#, text:"asdf"}
  16. //
  17. // Document snapshots look like:
  18. // {str:string}
  19. module.exports = {
  20. // The name of the OT type. The type is stored in types[type.name]. The name can be
  21. // used in place of the actual type in all the API methods.
  22. name: 'simple',
  23. // Create a new document snapshot
  24. create() {
  25. return { str: '' }
  26. },
  27. // Apply the given op to the document snapshot. Returns the new snapshot.
  28. //
  29. // The original snapshot should not be modified.
  30. apply(snapshot, op) {
  31. if (!(op.position >= 0 && op.position <= snapshot.str.length)) {
  32. throw new Error('Invalid position')
  33. }
  34. let { str } = snapshot
  35. str = str.slice(0, op.position) + op.text + str.slice(op.position)
  36. return { str }
  37. },
  38. // transform op1 by op2. Return transformed version of op1.
  39. // sym describes the symmetry of the op. Its 'left' or 'right' depending on whether the
  40. // op being transformed comes from the client or the server.
  41. transform(op1, op2, sym) {
  42. let pos = op1.position
  43. if (op2.position < pos || (op2.position === pos && sym === 'left')) {
  44. pos += op2.text.length
  45. }
  46. return { position: pos, text: op1.text }
  47. },
  48. }