text-tp2-api.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-tp2
  15. let type
  16. if (typeof WEB !== 'undefined' && WEB !== null) {
  17. type = exports.types['text-tp2']
  18. } else {
  19. type = require('./text-tp2')
  20. }
  21. const { _takeDoc: takeDoc, _append: append } = type
  22. const appendSkipChars = (op, doc, pos, maxlength) =>
  23. (() => {
  24. const result = []
  25. while (
  26. (maxlength === undefined || maxlength > 0) &&
  27. pos.index < doc.data.length
  28. ) {
  29. const part = takeDoc(doc, pos, maxlength, true)
  30. if (maxlength !== undefined && typeof part === 'string') {
  31. maxlength -= part.length
  32. }
  33. result.push(append(op, part.length || part))
  34. }
  35. return result
  36. })()
  37. type.api = {
  38. provides: { text: true },
  39. // The number of characters in the string
  40. getLength() {
  41. return this.snapshot.charLength
  42. },
  43. // Flatten a document into a string
  44. getText() {
  45. const strings = Array.from(this.snapshot.data).filter(
  46. elem => typeof elem === 'string'
  47. )
  48. return strings.join('')
  49. },
  50. insert(pos, text, callback) {
  51. if (pos === undefined) {
  52. pos = 0
  53. }
  54. const op = []
  55. const docPos = { index: 0, offset: 0 }
  56. appendSkipChars(op, this.snapshot, docPos, pos)
  57. append(op, { i: text })
  58. appendSkipChars(op, this.snapshot, docPos)
  59. this.submitOp(op, callback)
  60. return op
  61. },
  62. del(pos, length, callback) {
  63. const op = []
  64. const docPos = { index: 0, offset: 0 }
  65. appendSkipChars(op, this.snapshot, docPos, pos)
  66. while (length > 0) {
  67. const part = takeDoc(this.snapshot, docPos, length, true)
  68. if (typeof part === 'string') {
  69. append(op, { d: part.length })
  70. length -= part.length
  71. } else {
  72. append(op, part)
  73. }
  74. }
  75. appendSkipChars(op, this.snapshot, docPos)
  76. this.submitOp(op, callback)
  77. return op
  78. },
  79. _register() {
  80. // Interpret recieved ops + generate more detailed events for them
  81. return this.on('remoteop', function (op, snapshot) {
  82. let textPos = 0
  83. const docPos = { index: 0, offset: 0 }
  84. for (const component of Array.from(op)) {
  85. var part, remainder
  86. if (typeof component === 'number') {
  87. // Skip
  88. remainder = component
  89. while (remainder > 0) {
  90. part = takeDoc(snapshot, docPos, remainder)
  91. if (typeof part === 'string') {
  92. textPos += part.length
  93. }
  94. remainder -= part.length || part
  95. }
  96. } else if (component.i !== undefined) {
  97. // Insert
  98. if (typeof component.i === 'string') {
  99. this.emit('insert', textPos, component.i)
  100. textPos += component.i.length
  101. }
  102. } else {
  103. // Delete
  104. remainder = component.d
  105. while (remainder > 0) {
  106. part = takeDoc(snapshot, docPos, remainder)
  107. if (typeof part === 'string') {
  108. this.emit('delete', textPos, part)
  109. }
  110. remainder -= part.length || part
  111. }
  112. }
  113. }
  114. })
  115. },
  116. }