2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-undef,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
// Text document API for text
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
let type
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof WEB !== 'undefined' && WEB !== null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
type = exports.types['text-composable']
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
type = require('./text-composable')
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
type.api = {
|
2020-05-06 06:09:33 -04:00
|
|
|
provides: { text: true },
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// The number of characters in the string
|
2020-05-06 06:09:33 -04:00
|
|
|
getLength() {
|
|
|
|
return this.snapshot.length
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Get the text contents of a document
|
2020-05-06 06:09:33 -04:00
|
|
|
getText() {
|
|
|
|
return this.snapshot
|
|
|
|
},
|
|
|
|
|
|
|
|
insert(pos, text, callback) {
|
|
|
|
const op = type.normalize([pos, { i: text }, this.snapshot.length - pos])
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
this.submitOp(op, callback)
|
|
|
|
return op
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
del(pos, length, callback) {
|
|
|
|
const op = type.normalize([
|
|
|
|
pos,
|
|
|
|
{ d: this.snapshot.slice(pos, pos + length) },
|
2021-07-13 07:04:42 -04:00
|
|
|
this.snapshot.length - pos - length,
|
2020-05-06 06:09:33 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
this.submitOp(op, callback)
|
|
|
|
return op
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_register() {
|
2020-05-06 06:09:33 -04:00
|
|
|
return this.on('remoteop', function (op) {
|
|
|
|
let pos = 0
|
2020-05-06 06:08:21 -04:00
|
|
|
return (() => {
|
2020-05-06 06:09:33 -04:00
|
|
|
const result = []
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const component of Array.from(op)) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof component === 'number') {
|
2020-05-06 06:09:33 -04:00
|
|
|
result.push((pos += component))
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (component.i !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
this.emit('insert', pos, component.i)
|
|
|
|
result.push((pos += component.i.length))
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
|
|
|
// delete
|
2020-05-06 06:09:33 -04:00
|
|
|
result.push(this.emit('delete', pos, component.d))
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return result
|
|
|
|
})()
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
// We don't increment pos, because the position
|
|
|
|
// specified is after the delete has happened.
|