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-tp2
|
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-tp2']
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
type = require('./text-tp2')
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const { _takeDoc: takeDoc, _append: append } = type
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const appendSkipChars = (op, doc, pos, maxlength) =>
|
|
|
|
(() => {
|
|
|
|
const result = []
|
|
|
|
while (
|
|
|
|
(maxlength === undefined || maxlength > 0) &&
|
|
|
|
pos.index < doc.data.length
|
|
|
|
) {
|
|
|
|
const part = takeDoc(doc, pos, maxlength, true)
|
|
|
|
if (maxlength !== undefined && typeof part === 'string') {
|
|
|
|
maxlength -= part.length
|
|
|
|
}
|
|
|
|
result.push(append(op, part.length || part))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
})()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:15 -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.charLength
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Flatten a document into a string
|
2020-05-06 06:09:33 -04:00
|
|
|
getText() {
|
|
|
|
const strings = Array.from(this.snapshot.data).filter(
|
2021-07-13 07:04:42 -04:00
|
|
|
elem => typeof elem === 'string'
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
|
|
|
return strings.join('')
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
insert(pos, text, callback) {
|
|
|
|
if (pos === undefined) {
|
|
|
|
pos = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const op = []
|
|
|
|
const docPos = { index: 0, offset: 0 }
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
appendSkipChars(op, this.snapshot, docPos, pos)
|
|
|
|
append(op, { i: text })
|
|
|
|
appendSkipChars(op, this.snapshot, docPos)
|
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
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
del(pos, length, callback) {
|
|
|
|
const op = []
|
|
|
|
const docPos = { index: 0, offset: 0 }
|
|
|
|
|
|
|
|
appendSkipChars(op, this.snapshot, docPos, pos)
|
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
while (length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const part = takeDoc(this.snapshot, docPos, length, true)
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof part === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
append(op, { d: part.length })
|
|
|
|
length -= part.length
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
append(op, part)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
appendSkipChars(op, this.snapshot, docPos)
|
|
|
|
|
|
|
|
this.submitOp(op, callback)
|
|
|
|
return op
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
_register() {
|
2020-05-06 06:08:21 -04:00
|
|
|
// Interpret recieved ops + generate more detailed events for them
|
2020-05-06 06:09:33 -04:00
|
|
|
return this.on('remoteop', function (op, snapshot) {
|
|
|
|
let textPos = 0
|
|
|
|
const docPos = { index: 0, offset: 0 }
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const component of Array.from(op)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
var part, remainder
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof component === 'number') {
|
|
|
|
// Skip
|
2020-05-06 06:09:33 -04:00
|
|
|
remainder = component
|
2020-05-06 06:08:21 -04:00
|
|
|
while (remainder > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
part = takeDoc(snapshot, docPos, remainder)
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof part === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
textPos += part.length
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
remainder -= part.length || part
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else if (component.i !== undefined) {
|
|
|
|
// Insert
|
|
|
|
if (typeof component.i === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
this.emit('insert', textPos, component.i)
|
|
|
|
textPos += component.i.length
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Delete
|
2020-05-06 06:09:33 -04:00
|
|
|
remainder = component.d
|
2020-05-06 06:08:21 -04:00
|
|
|
while (remainder > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
part = takeDoc(snapshot, docPos, remainder)
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof part === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
this.emit('delete', textPos, part)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
remainder -= part.length || part
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|