text-api.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. // Text document API for text
  10. let text
  11. if (typeof WEB === 'undefined') {
  12. text = require('./text')
  13. }
  14. text.api = {
  15. provides: { text: true },
  16. // The number of characters in the string
  17. getLength() {
  18. return this.snapshot.length
  19. },
  20. // Get the text contents of a document
  21. getText() {
  22. return this.snapshot
  23. },
  24. insert(pos, text, callback) {
  25. const op = [{ p: pos, i: text }]
  26. this.submitOp(op, callback)
  27. return op
  28. },
  29. del(pos, length, callback) {
  30. const op = [{ p: pos, d: this.snapshot.slice(pos, pos + length) }]
  31. this.submitOp(op, callback)
  32. return op
  33. },
  34. _register() {
  35. return this.on('remoteop', function (op) {
  36. return Array.from(op).map(component =>
  37. component.i !== undefined
  38. ? this.emit('insert', component.p, component.i)
  39. : this.emit('delete', component.p, component.d)
  40. )
  41. })
  42. },
  43. }