2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-cond-assign,
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// 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
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
// A TP2 implementation of text, following this spec:
|
|
|
|
// http://code.google.com/p/lightwave/source/browse/trunk/experimental/ot/README
|
|
|
|
//
|
|
|
|
// A document is made up of a string and a set of tombstones inserted throughout
|
|
|
|
// the string. For example, 'some ', (2 tombstones), 'string'.
|
|
|
|
//
|
|
|
|
// This is encoded in a document as: {s:'some string', t:[5, -2, 6]}
|
|
|
|
//
|
|
|
|
// Ops are lists of components which iterate over the whole document.
|
|
|
|
// Components are either:
|
|
|
|
// N: Skip N characters in the original document
|
|
|
|
// {i:'str'}: Insert 'str' at the current position in the document
|
|
|
|
// {i:N}: Insert N tombstones at the current position in the document
|
|
|
|
// {d:N}: Delete (tombstone) N characters at the current position in the document
|
|
|
|
//
|
|
|
|
// Eg: [3, {i:'hi'}, 5, {d:8}]
|
|
|
|
//
|
|
|
|
// Snapshots are lists with characters and tombstones. Characters are stored in strings
|
|
|
|
// and adjacent tombstones are flattened into numbers.
|
|
|
|
//
|
|
|
|
// Eg, the document: 'Hello .....world' ('.' denotes tombstoned (deleted) characters)
|
|
|
|
// would be represented by a document snapshot of ['Hello ', 5, 'world']
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
let append, appendDoc, takeDoc
|
2021-10-26 04:08:56 -04:00
|
|
|
const type = {
|
2020-05-06 06:08:21 -04:00
|
|
|
name: 'text-tp2',
|
|
|
|
tp2: true,
|
2020-05-06 06:09:33 -04:00
|
|
|
create() {
|
|
|
|
return { charLength: 0, totalLength: 0, positionCache: [], data: [] }
|
|
|
|
},
|
2020-05-06 06:08:21 -04:00
|
|
|
serialize(doc) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (!doc.data) {
|
|
|
|
throw new Error('invalid doc snapshot')
|
|
|
|
}
|
|
|
|
return doc.data
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
deserialize(data) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const doc = type.create()
|
|
|
|
doc.data = data
|
|
|
|
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const component of Array.from(data)) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof component === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
doc.charLength += component.length
|
|
|
|
doc.totalLength += component.length
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
doc.totalLength += component
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return doc
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const checkOp = function (op) {
|
|
|
|
if (!Array.isArray(op)) {
|
|
|
|
throw new Error('Op must be an array of components')
|
|
|
|
}
|
|
|
|
let last = null
|
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 c of Array.from(op)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof c === 'object') {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (c.i !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
(typeof c.i !== 'string' || !(c.i.length > 0)) &&
|
|
|
|
(typeof c.i !== 'number' || !(c.i > 0))
|
|
|
|
) {
|
|
|
|
throw new Error('Inserts must insert a string or a +ive number')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.d !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof c.d !== 'number' || !(c.d > 0)) {
|
|
|
|
throw new Error('Deletes must be a +ive number')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
throw new Error('Operation component must define .i or .d')
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof c !== 'number') {
|
|
|
|
throw new Error('Op components must be objects or numbers')
|
|
|
|
}
|
|
|
|
if (!(c > 0)) {
|
|
|
|
throw new Error('Skip components must be a positive number')
|
|
|
|
}
|
|
|
|
if (typeof last === 'number') {
|
|
|
|
throw new Error('Adjacent skip components should be combined')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
result.push((last = c))
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return result
|
|
|
|
})()
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Take the next part from the specified position in a document snapshot.
|
|
|
|
// position = {index, offset}. It will be updated.
|
2020-05-06 06:09:33 -04:00
|
|
|
type._takeDoc = takeDoc = function (
|
|
|
|
doc,
|
|
|
|
position,
|
|
|
|
maxlength,
|
|
|
|
tombsIndivisible
|
|
|
|
) {
|
|
|
|
if (position.index >= doc.data.length) {
|
|
|
|
throw new Error('Operation goes past the end of the document')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const part = doc.data[position.index]
|
2020-05-06 06:08:21 -04:00
|
|
|
// peel off data[0]
|
2020-05-06 06:09:33 -04:00
|
|
|
const result =
|
|
|
|
typeof part === 'string'
|
|
|
|
? maxlength !== undefined
|
|
|
|
? part.slice(position.offset, position.offset + maxlength)
|
|
|
|
: part.slice(position.offset)
|
|
|
|
: maxlength === undefined || tombsIndivisible
|
|
|
|
? part - position.offset
|
|
|
|
: Math.min(maxlength, part - position.offset)
|
|
|
|
|
|
|
|
const resultLen = result.length || result
|
|
|
|
|
|
|
|
if ((part.length || part) - position.offset > resultLen) {
|
|
|
|
position.offset += resultLen
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
position.index++
|
|
|
|
position.offset = 0
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Append a part to the end of a document
|
2020-05-06 06:09:33 -04:00
|
|
|
type._appendDoc = appendDoc = function (doc, p) {
|
|
|
|
if (p === 0 || p === '') {
|
|
|
|
return
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
if (typeof p === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
doc.charLength += p.length
|
|
|
|
doc.totalLength += p.length
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
doc.totalLength += p
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const { data } = doc
|
2020-05-06 06:08:21 -04:00
|
|
|
if (data.length === 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
data.push(p)
|
|
|
|
} else if (typeof data[data.length - 1] === typeof p) {
|
|
|
|
data[data.length - 1] += p
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
data.push(p)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Apply the op to the document. The document is not modified in the process.
|
2020-05-06 06:09:33 -04:00
|
|
|
type.apply = function (doc, op) {
|
|
|
|
if (
|
|
|
|
doc.totalLength === undefined ||
|
|
|
|
doc.charLength === undefined ||
|
|
|
|
doc.data.length === undefined
|
|
|
|
) {
|
|
|
|
throw new Error('Snapshot is invalid')
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
checkOp(op)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const newDoc = type.create()
|
|
|
|
const position = { index: 0, offset: 0 }
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const component of Array.from(op)) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let part, remainder
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof component === 'number') {
|
|
|
|
remainder = component
|
2020-05-06 06:08:21 -04:00
|
|
|
while (remainder > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
part = takeDoc(doc, position, remainder)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
appendDoc(newDoc, part)
|
|
|
|
remainder -= part.length || part
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (component.i !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
appendDoc(newDoc, component.i)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (component.d !== undefined) {
|
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(doc, position, remainder)
|
|
|
|
remainder -= part.length || part
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
appendDoc(newDoc, component.d)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
|
|
|
return newDoc
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Append an op component to the end of the specified op.
|
|
|
|
// Exported for the randomOpGenerator.
|
2020-05-06 06:09:33 -04:00
|
|
|
type._append = append = function (op, component) {
|
|
|
|
if (
|
|
|
|
component === 0 ||
|
|
|
|
component.i === '' ||
|
|
|
|
component.i === 0 ||
|
|
|
|
component.d === 0
|
|
|
|
) {
|
2021-10-26 04:09:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (op.length === 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return op.push(component)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
const last = op[op.length - 1]
|
|
|
|
if (typeof component === 'number' && typeof last === 'number') {
|
|
|
|
return (op[op.length - 1] += component)
|
|
|
|
} else if (
|
|
|
|
component.i !== undefined &&
|
|
|
|
last.i != null &&
|
|
|
|
typeof last.i === typeof component.i
|
|
|
|
) {
|
|
|
|
return (last.i += component.i)
|
|
|
|
} else if (component.d !== undefined && last.d != null) {
|
|
|
|
return (last.d += component.d)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return op.push(component)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Makes 2 functions for taking components from the start of an op, and for peeking
|
|
|
|
// at the next op that could be taken.
|
2020-05-06 06:09:33 -04:00
|
|
|
const makeTake = function (op) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// The index of the next component to take
|
2020-05-06 06:09:33 -04:00
|
|
|
let index = 0
|
2020-05-06 06:08:21 -04:00
|
|
|
// The offset into the component
|
2020-05-06 06:09:33 -04:00
|
|
|
let offset = 0
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Take up to length maxlength from the op. If maxlength is not defined, there is no max.
|
|
|
|
// If insertsIndivisible is true, inserts (& insert tombstones) won't be separated.
|
|
|
|
//
|
|
|
|
// Returns null when op is fully consumed.
|
2020-05-06 06:09:33 -04:00
|
|
|
const take = function (maxlength, insertsIndivisible) {
|
|
|
|
let current
|
|
|
|
if (index === op.length) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const e = op[index]
|
|
|
|
if (
|
|
|
|
typeof (current = e) === 'number' ||
|
|
|
|
typeof (current = e.i) === 'number' ||
|
|
|
|
(current = e.d) !== undefined
|
|
|
|
) {
|
|
|
|
let c
|
|
|
|
if (
|
|
|
|
maxlength == null ||
|
|
|
|
current - offset <= maxlength ||
|
|
|
|
(insertsIndivisible && e.i !== undefined)
|
|
|
|
) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// Return the rest of the current element.
|
2020-05-06 06:09:33 -04:00
|
|
|
c = current - offset
|
|
|
|
++index
|
|
|
|
offset = 0
|
|
|
|
} else {
|
|
|
|
offset += maxlength
|
|
|
|
c = maxlength
|
|
|
|
}
|
|
|
|
if (e.i !== undefined) {
|
|
|
|
return { i: c }
|
|
|
|
} else if (e.d !== undefined) {
|
|
|
|
return { d: c }
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return c
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Take from the inserted string
|
2020-05-06 06:09:33 -04:00
|
|
|
let result
|
|
|
|
if (
|
|
|
|
maxlength == null ||
|
|
|
|
e.i.length - offset <= maxlength ||
|
|
|
|
insertsIndivisible
|
|
|
|
) {
|
|
|
|
result = { i: e.i.slice(offset) }
|
|
|
|
++index
|
|
|
|
offset = 0
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
result = { i: e.i.slice(offset, offset + maxlength) }
|
|
|
|
offset += maxlength
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return result
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const peekType = () => op[index]
|
|
|
|
|
|
|
|
return [take, peekType]
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Find and return the length of an op component
|
2020-05-06 06:09:33 -04:00
|
|
|
const componentLength = function (component) {
|
|
|
|
if (typeof component === 'number') {
|
|
|
|
return component
|
|
|
|
} else if (typeof component.i === 'string') {
|
|
|
|
return component.i.length
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
|
|
|
// This should work because c.d and c.i must be +ive.
|
2020-05-06 06:09:33 -04:00
|
|
|
return component.d || component.i
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Normalize an op, removing all empty skips and empty inserts / deletes. Concatenate
|
|
|
|
// adjacent inserts and deletes.
|
2020-05-06 06:09:33 -04:00
|
|
|
type.normalize = function (op) {
|
|
|
|
const newOp = []
|
|
|
|
for (const component of Array.from(op)) {
|
|
|
|
append(newOp, component)
|
|
|
|
}
|
|
|
|
return newOp
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// This is a helper method to transform and prune. goForwards is true for transform, false for prune.
|
2020-05-06 06:09:33 -04:00
|
|
|
const transformer = function (op, otherOp, goForwards, side) {
|
|
|
|
let component
|
|
|
|
checkOp(op)
|
|
|
|
checkOp(otherOp)
|
|
|
|
const newOp = []
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const [take, peek] = Array.from(makeTake(op))
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
for (component of Array.from(otherOp)) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let chunk
|
2020-05-06 06:09:33 -04:00
|
|
|
let length = componentLength(component)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (component.i !== undefined) {
|
|
|
|
// Insert text or tombs
|
|
|
|
if (goForwards) {
|
|
|
|
// transform - insert skips over inserted parts
|
2020-05-06 06:08:21 -04:00
|
|
|
if (side === 'left') {
|
|
|
|
// The left insert should go first.
|
2021-07-13 07:04:42 -04:00
|
|
|
while (__guard__(peek(), x => x.i) !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
append(newOp, take())
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// In any case, skip the inserted text.
|
2020-05-06 06:09:33 -04:00
|
|
|
append(newOp, length)
|
|
|
|
} else {
|
|
|
|
// Prune. Remove skips for inserts.
|
2020-05-06 06:08:21 -04:00
|
|
|
while (length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
chunk = take(length, true)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (chunk === null) {
|
|
|
|
throw new Error('The transformed op is invalid')
|
|
|
|
}
|
|
|
|
if (chunk.d !== undefined) {
|
|
|
|
throw new Error(
|
|
|
|
'The transformed op deletes locally inserted characters - it cannot be purged of the insert.'
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
if (typeof chunk === 'number') {
|
2020-05-06 06:09:33 -04:00
|
|
|
length -= chunk
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
append(newOp, chunk)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else {
|
|
|
|
// Skip or delete
|
2020-05-06 06:08:21 -04:00
|
|
|
while (length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
chunk = take(length, true)
|
|
|
|
if (chunk === null) {
|
|
|
|
throw new Error(
|
|
|
|
'The op traverses more elements than the document has'
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
append(newOp, chunk)
|
|
|
|
if (!chunk.i) {
|
|
|
|
length -= componentLength(chunk)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Append extras from op1
|
2020-05-06 06:09:33 -04:00
|
|
|
while ((component = take())) {
|
|
|
|
if (component.i === undefined) {
|
|
|
|
throw new Error(`Remaining fragments in the op: ${component}`)
|
|
|
|
}
|
|
|
|
append(newOp, component)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return newOp
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// transform op1 by op2. Return transformed version of op1.
|
|
|
|
// op1 and op2 are unchanged by transform.
|
|
|
|
// side should be 'left' or 'right', depending on if op1.id <> op2.id. 'left' == client op.
|
2020-05-06 06:09:33 -04:00
|
|
|
type.transform = function (op, otherOp, side) {
|
|
|
|
if (side !== 'left' && side !== 'right') {
|
|
|
|
throw new Error(`side (${side}) should be 'left' or 'right'`)
|
|
|
|
}
|
|
|
|
return transformer(op, otherOp, true, side)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Prune is the inverse of transform.
|
2020-05-06 06:09:33 -04:00
|
|
|
type.prune = (op, otherOp) => transformer(op, otherOp, false)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Compose 2 ops into 1 op.
|
2020-05-06 06:09:33 -04:00
|
|
|
type.compose = function (op1, op2) {
|
|
|
|
let component
|
|
|
|
if (op1 === null || op1 === undefined) {
|
|
|
|
return op2
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
checkOp(op1)
|
|
|
|
checkOp(op2)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const result = []
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const [take, _] = Array.from(makeTake(op1))
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
for (component of Array.from(op2)) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let chunk, length
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof component === 'number') {
|
|
|
|
// Skip
|
2020-05-06 06:08:21 -04:00
|
|
|
// Just copy from op1.
|
2020-05-06 06:09:33 -04:00
|
|
|
length = component
|
2020-05-06 06:08:21 -04:00
|
|
|
while (length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
chunk = take(length)
|
|
|
|
if (chunk === null) {
|
|
|
|
throw new Error(
|
|
|
|
'The op traverses more elements than the document has'
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
append(result, chunk)
|
|
|
|
length -= componentLength(chunk)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (component.i !== undefined) {
|
|
|
|
// Insert
|
|
|
|
append(result, { i: component.i })
|
|
|
|
} else {
|
|
|
|
// Delete
|
|
|
|
length = component.d
|
2020-05-06 06:08:21 -04:00
|
|
|
while (length > 0) {
|
2020-05-06 06:09:33 -04:00
|
|
|
chunk = take(length)
|
|
|
|
if (chunk === null) {
|
|
|
|
throw new Error(
|
|
|
|
'The op traverses more elements than the document has'
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const chunkLength = componentLength(chunk)
|
2020-05-06 06:08:21 -04:00
|
|
|
if (chunk.i !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
append(result, { i: chunkLength })
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
append(result, { d: chunkLength })
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
length -= chunkLength
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Append extras from op1
|
2020-05-06 06:09:33 -04:00
|
|
|
while ((component = take())) {
|
|
|
|
if (component.i === undefined) {
|
|
|
|
throw new Error(`Remaining fragments in op1: ${component}`)
|
|
|
|
}
|
|
|
|
append(result, component)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return result
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof WEB !== 'undefined' && WEB !== null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
exports.types['text-tp2'] = type
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = type
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
function __guard__(value, transform) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|