overleaf/services/document-updater/app/js/sharejs/text-api.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
// Text document API for text
2014-02-12 10:40:42 +00:00
let text
if (typeof WEB === 'undefined') {
text = require('./text')
}
2014-02-12 10:40:42 +00:00
text.api = {
provides: { text: true },
2014-02-12 10:40:42 +00:00
// The number of characters in the string
getLength() {
return this.snapshot.length
},
2014-02-12 10:40:42 +00:00
// Get the text contents of a document
getText() {
return this.snapshot
},
2014-02-12 10:40:42 +00:00
insert(pos, text, callback) {
const op = [{ p: pos, i: text }]
this.submitOp(op, callback)
return op
},
del(pos, length, callback) {
const op = [{ p: pos, d: this.snapshot.slice(pos, pos + length) }]
2014-02-12 10:40:42 +00:00
this.submitOp(op, callback)
return op
},
_register() {
return this.on('remoteop', function (op) {
return Array.from(op).map((component) =>
component.i !== undefined
? this.emit('insert', component.p, component.i)
: this.emit('delete', component.p, component.d)
)
})
}
}