text-composable-api.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* eslint-disable
  2. no-undef,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS205: Consider reworking code to avoid use of IIFEs
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. // Text document API for text
  15. let type
  16. if (typeof WEB !== 'undefined' && WEB !== null) {
  17. type = exports.types['text-composable']
  18. } else {
  19. type = require('./text-composable')
  20. }
  21. type.api = {
  22. provides: { text: true },
  23. // The number of characters in the string
  24. getLength() {
  25. return this.snapshot.length
  26. },
  27. // Get the text contents of a document
  28. getText() {
  29. return this.snapshot
  30. },
  31. insert(pos, text, callback) {
  32. const op = type.normalize([pos, { i: text }, this.snapshot.length - pos])
  33. this.submitOp(op, callback)
  34. return op
  35. },
  36. del(pos, length, callback) {
  37. const op = type.normalize([
  38. pos,
  39. { d: this.snapshot.slice(pos, pos + length) },
  40. this.snapshot.length - pos - length,
  41. ])
  42. this.submitOp(op, callback)
  43. return op
  44. },
  45. _register() {
  46. return this.on('remoteop', function (op) {
  47. let pos = 0
  48. return (() => {
  49. const result = []
  50. for (const component of Array.from(op)) {
  51. if (typeof component === 'number') {
  52. result.push((pos += component))
  53. } else if (component.i !== undefined) {
  54. this.emit('insert', pos, component.i)
  55. result.push((pos += component.i.length))
  56. } else {
  57. // delete
  58. result.push(this.emit('delete', pos, component.d))
  59. }
  60. }
  61. return result
  62. })()
  63. })
  64. },
  65. }
  66. // We don't increment pos, because the position
  67. // specified is after the delete has happened.