2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
no-useless-catch,
|
|
|
|
*/
|
|
|
|
// 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
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
// This is the implementation of the JSON OT type.
|
|
|
|
//
|
|
|
|
// Spec is here: https://github.com/josephg/ShareJS/wiki/JSON-Operations
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
let text
|
2020-05-06 06:08:21 -04:00
|
|
|
if (typeof WEB !== 'undefined' && WEB !== null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
;({ text } = exports.types)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
text = require('./text')
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const json = {}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.name = 'json'
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.create = () => null
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.invertComponent = function (c) {
|
|
|
|
const c_ = { p: c.p }
|
|
|
|
if (c.si !== undefined) {
|
|
|
|
c_.sd = c.si
|
|
|
|
}
|
|
|
|
if (c.sd !== undefined) {
|
|
|
|
c_.si = c.sd
|
|
|
|
}
|
|
|
|
if (c.oi !== undefined) {
|
|
|
|
c_.od = c.oi
|
|
|
|
}
|
|
|
|
if (c.od !== undefined) {
|
|
|
|
c_.oi = c.od
|
|
|
|
}
|
|
|
|
if (c.li !== undefined) {
|
|
|
|
c_.ld = c.li
|
|
|
|
}
|
|
|
|
if (c.ld !== undefined) {
|
|
|
|
c_.li = c.ld
|
|
|
|
}
|
|
|
|
if (c.na !== undefined) {
|
|
|
|
c_.na = -c.na
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
if (c.lm !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c_.lm = c.p[c.p.length - 1]
|
|
|
|
c_.p = c.p.slice(0, c.p.length - 1).concat([c.lm])
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return c_
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.invert = (op) =>
|
|
|
|
Array.from(op.slice().reverse()).map((c) => json.invertComponent(c))
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkValidOp = function (op) {}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const isArray = (o) => Object.prototype.toString.call(o) === '[object Array]'
|
|
|
|
json.checkList = function (elem) {
|
|
|
|
if (!isArray(elem)) {
|
|
|
|
throw new Error('Referenced element not a list')
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkObj = function (elem) {
|
|
|
|
if (elem.constructor !== Object) {
|
|
|
|
throw new Error(
|
|
|
|
`Referenced element not an object (it was ${JSON.stringify(elem)})`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.apply = function (snapshot, op) {
|
|
|
|
json.checkValidOp(op)
|
|
|
|
op = clone(op)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const container = { data: clone(snapshot) }
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < op.length; i++) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const c = op[i]
|
|
|
|
let parent = null
|
|
|
|
let parentkey = null
|
|
|
|
let elem = container
|
|
|
|
let key = 'data'
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const p of Array.from(c.p)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
parent = elem
|
|
|
|
parentkey = key
|
|
|
|
elem = elem[key]
|
|
|
|
key = p
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (parent == null) {
|
|
|
|
throw new Error('Path invalid')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c.na !== undefined) {
|
|
|
|
// Number add
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof elem[key] !== 'number') {
|
|
|
|
throw new Error('Referenced element not a number')
|
|
|
|
}
|
|
|
|
elem[key] += c.na
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.si !== undefined) {
|
|
|
|
// String insert
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof elem !== 'string') {
|
|
|
|
throw new Error(
|
|
|
|
`Referenced element not a string (it was ${JSON.stringify(elem)})`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
parent[parentkey] = elem.slice(0, key) + c.si + elem.slice(key)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.sd !== undefined) {
|
|
|
|
// String delete
|
2020-05-06 06:09:33 -04:00
|
|
|
if (typeof elem !== 'string') {
|
|
|
|
throw new Error('Referenced element not a string')
|
|
|
|
}
|
|
|
|
if (elem.slice(key, key + c.sd.length) !== c.sd) {
|
|
|
|
throw new Error('Deleted string does not match')
|
|
|
|
}
|
|
|
|
parent[parentkey] = elem.slice(0, key) + elem.slice(key + c.sd.length)
|
|
|
|
} else if (c.li !== undefined && c.ld !== undefined) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// List replace
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkList(elem)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Should check the list element matches c.ld
|
2020-05-06 06:09:33 -04:00
|
|
|
elem[key] = c.li
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.li !== undefined) {
|
|
|
|
// List insert
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkList(elem)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
elem.splice(key, 0, c.li)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.ld !== undefined) {
|
|
|
|
// List delete
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkList(elem)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Should check the list element matches c.ld here too.
|
2020-05-06 06:09:33 -04:00
|
|
|
elem.splice(key, 1)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.lm !== undefined) {
|
|
|
|
// List move
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkList(elem)
|
2020-05-06 06:08:21 -04:00
|
|
|
if (c.lm !== key) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const e = elem[key]
|
2020-05-06 06:08:21 -04:00
|
|
|
// Remove it...
|
2020-05-06 06:09:33 -04:00
|
|
|
elem.splice(key, 1)
|
2020-05-06 06:08:21 -04:00
|
|
|
// And insert it back.
|
2020-05-06 06:09:33 -04:00
|
|
|
elem.splice(c.lm, 0, e)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else if (c.oi !== undefined) {
|
|
|
|
// Object insert / replace
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkObj(elem)
|
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Should check that elem[key] == c.od
|
2020-05-06 06:09:33 -04:00
|
|
|
elem[key] = c.oi
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.od !== undefined) {
|
|
|
|
// Object delete
|
2020-05-06 06:09:33 -04:00
|
|
|
json.checkObj(elem)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Should check that elem[key] == c.od
|
2020-05-06 06:09:33 -04:00
|
|
|
delete elem[key]
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
throw new Error('invalid / missing instruction in op')
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// TODO: Roll back all already applied changes. Write tests before implementing this code.
|
2020-05-06 06:09:33 -04:00
|
|
|
throw error
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return container.data
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Checks if two paths, p1 and p2 match.
|
2020-05-06 06:09:33 -04:00
|
|
|
json.pathMatches = function (p1, p2, ignoreLast) {
|
|
|
|
if (p1.length !== p2.length) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
for (let i = 0; i < p1.length; i++) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const p = p1[i]
|
|
|
|
if (p !== p2[i] && (!ignoreLast || i !== p1.length - 1)) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
json.append = function (dest, c) {
|
|
|
|
let last
|
|
|
|
c = clone(c)
|
|
|
|
if (
|
|
|
|
dest.length !== 0 &&
|
|
|
|
json.pathMatches(c.p, (last = dest[dest.length - 1]).p)
|
|
|
|
) {
|
|
|
|
if (last.na !== undefined && c.na !== undefined) {
|
|
|
|
return (dest[dest.length - 1] = { p: last.p, na: last.na + c.na })
|
|
|
|
} else if (
|
|
|
|
last.li !== undefined &&
|
|
|
|
c.li === undefined &&
|
|
|
|
c.ld === last.li
|
|
|
|
) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// insert immediately followed by delete becomes a noop.
|
|
|
|
if (last.ld !== undefined) {
|
|
|
|
// leave the delete part of the replace
|
2020-05-06 06:09:33 -04:00
|
|
|
return delete last.li
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest.pop()
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (
|
|
|
|
last.od !== undefined &&
|
|
|
|
last.oi === undefined &&
|
|
|
|
c.oi !== undefined &&
|
|
|
|
c.od === undefined
|
|
|
|
) {
|
|
|
|
return (last.oi = c.oi)
|
|
|
|
} else if (c.lm !== undefined && c.p[c.p.length - 1] === c.lm) {
|
|
|
|
return null // don't do anything
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest.push(c)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest.push(c)
|
2020-05-06 06:08:21 -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
|
|
|
json.compose = function (op1, op2) {
|
|
|
|
json.checkValidOp(op1)
|
|
|
|
json.checkValidOp(op2)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const newOp = clone(op1)
|
|
|
|
for (const c of Array.from(op2)) {
|
|
|
|
json.append(newOp, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newOp
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
json.normalize = function (op) {
|
|
|
|
const newOp = []
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (!isArray(op)) {
|
|
|
|
op = [op]
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const c of Array.from(op)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.p == null) {
|
|
|
|
c.p = []
|
|
|
|
}
|
|
|
|
json.append(newOp, c)
|
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
|
|
|
|
|
|
|
// hax, copied from test/types/json. Apparently this is still the fastest way to deep clone an object, assuming
|
|
|
|
// we have browser support for JSON.
|
|
|
|
// http://jsperf.com/cloning-an-object/12
|
2020-05-06 06:09:33 -04:00
|
|
|
var clone = (o) => JSON.parse(JSON.stringify(o))
|
|
|
|
|
|
|
|
json.commonPath = function (p1, p2) {
|
|
|
|
p1 = p1.slice()
|
|
|
|
p2 = p2.slice()
|
|
|
|
p1.unshift('data')
|
|
|
|
p2.unshift('data')
|
|
|
|
p1 = p1.slice(0, p1.length - 1)
|
|
|
|
p2 = p2.slice(0, p2.length - 1)
|
|
|
|
if (p2.length === 0) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
let i = 0
|
|
|
|
while (p1[i] === p2[i] && i < p1.length) {
|
|
|
|
i++
|
2020-05-06 06:08:21 -04:00
|
|
|
if (i === p2.length) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return i - 1
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// transform c so it applies to a document with otherC applied.
|
2020-05-06 06:09:33 -04:00
|
|
|
json.transformComponent = function (dest, c, otherC, type) {
|
|
|
|
let oc
|
|
|
|
c = clone(c)
|
|
|
|
if (c.na !== undefined) {
|
|
|
|
c.p.push(0)
|
|
|
|
}
|
|
|
|
if (otherC.na !== undefined) {
|
|
|
|
otherC.p.push(0)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const common = json.commonPath(c.p, otherC.p)
|
|
|
|
const common2 = json.commonPath(otherC.p, c.p)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const cplength = c.p.length
|
|
|
|
const otherCplength = otherC.p.length
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.na !== undefined) {
|
|
|
|
c.p.pop()
|
|
|
|
} // hax
|
|
|
|
if (otherC.na !== undefined) {
|
|
|
|
otherC.p.pop()
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
if (otherC.na) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
common2 != null &&
|
|
|
|
otherCplength >= cplength &&
|
|
|
|
otherC.p[common2] === c.p[common2]
|
|
|
|
) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (c.ld !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
oc = clone(otherC)
|
|
|
|
oc.p = oc.p.slice(cplength)
|
|
|
|
c.ld = json.apply(clone(c.ld), [oc])
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.od !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
oc = clone(otherC)
|
|
|
|
oc.p = oc.p.slice(cplength)
|
|
|
|
c.od = json.apply(clone(c.od), [oc])
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
json.append(dest, c)
|
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
common2 != null &&
|
|
|
|
otherCplength > cplength &&
|
|
|
|
c.p[common2] === otherC.p[common2]
|
|
|
|
) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// transform based on c
|
|
|
|
if (c.ld !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
oc = clone(otherC)
|
|
|
|
oc.p = oc.p.slice(cplength)
|
|
|
|
c.ld = json.apply(clone(c.ld), [oc])
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.od !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
oc = clone(otherC)
|
|
|
|
oc.p = oc.p.slice(cplength)
|
|
|
|
c.od = json.apply(clone(c.od), [oc])
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (common != null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
let from, p, to
|
|
|
|
const commonOperand = cplength === otherCplength
|
2020-05-06 06:08:21 -04:00
|
|
|
// transform based on otherC
|
|
|
|
if (otherC.na !== undefined) {
|
|
|
|
// this case is handled above due to icky path hax
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (otherC.si !== undefined || otherC.sd !== undefined) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// String op vs string op - pass through to text type
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.si !== undefined || c.sd !== undefined) {
|
|
|
|
if (!commonOperand) {
|
|
|
|
throw new Error('must be a string?')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// Convert an op component to a text op component
|
2020-05-06 06:09:33 -04:00
|
|
|
const convert = function (component) {
|
|
|
|
const newC = { p: component.p[component.p.length - 1] }
|
2020-05-06 06:08:21 -04:00
|
|
|
if (component.si) {
|
2020-05-06 06:09:33 -04:00
|
|
|
newC.i = component.si
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
newC.d = component.sd
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return newC
|
|
|
|
}
|
|
|
|
|
|
|
|
const tc1 = convert(c)
|
|
|
|
const tc2 = convert(otherC)
|
|
|
|
|
|
|
|
const res = []
|
|
|
|
text._tc(res, tc1, tc2, type)
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const tc of Array.from(res)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const jc = { p: c.p.slice(0, common) }
|
|
|
|
jc.p.push(tc.p)
|
|
|
|
if (tc.i != null) {
|
|
|
|
jc.si = tc.i
|
|
|
|
}
|
|
|
|
if (tc.d != null) {
|
|
|
|
jc.sd = tc.d
|
|
|
|
}
|
|
|
|
json.append(dest, jc)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (otherC.li !== undefined && otherC.ld !== undefined) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (otherC.p[common] === c.p[common]) {
|
|
|
|
// noop
|
|
|
|
if (!commonOperand) {
|
|
|
|
// we're below the deleted element, so -> noop
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.ld !== undefined) {
|
|
|
|
// we're trying to delete the same element, -> noop
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.li !== undefined && type === 'left') {
|
2020-05-06 06:08:21 -04:00
|
|
|
// we're both replacing one element with another. only one can
|
|
|
|
// survive!
|
2020-05-06 06:09:33 -04:00
|
|
|
c.ld = clone(otherC.li)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (otherC.li !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
c.li !== undefined &&
|
|
|
|
c.ld === undefined &&
|
|
|
|
commonOperand &&
|
|
|
|
c.p[common] === otherC.p[common]
|
|
|
|
) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// in li vs. li, left wins.
|
|
|
|
if (type === 'right') {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else if (otherC.p[common] <= c.p[common]) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c.lm !== undefined) {
|
|
|
|
if (commonOperand) {
|
|
|
|
// otherC edits the same list we edit
|
|
|
|
if (otherC.p[common] <= c.lm) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
// changing c.from is handled above.
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (otherC.ld !== undefined) {
|
|
|
|
if (c.lm !== undefined) {
|
|
|
|
if (commonOperand) {
|
|
|
|
if (otherC.p[common] === c.p[common]) {
|
|
|
|
// they deleted the thing we're trying to move
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
// otherC edits the same list we edit
|
2020-05-06 06:09:33 -04:00
|
|
|
p = otherC.p[common]
|
|
|
|
from = c.p[common]
|
|
|
|
to = c.lm
|
|
|
|
if (p < to || (p === to && from < to)) {
|
|
|
|
c.lm--
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (otherC.p[common] < c.p[common]) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]--
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (otherC.p[common] === c.p[common]) {
|
|
|
|
if (otherCplength < cplength) {
|
|
|
|
// we're below the deleted element, so -> noop
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (c.ld !== undefined) {
|
|
|
|
if (c.li !== undefined) {
|
|
|
|
// we're replacing, they're deleting. we become an insert.
|
2020-05-06 06:09:33 -04:00
|
|
|
delete c.ld
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
|
|
|
// we're trying to delete the same element, -> noop
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (otherC.lm !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.lm !== undefined && cplength === otherCplength) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// lm vs lm, here we go!
|
2020-05-06 06:09:33 -04:00
|
|
|
from = c.p[common]
|
|
|
|
to = c.lm
|
|
|
|
const otherFrom = otherC.p[common]
|
|
|
|
const otherTo = otherC.lm
|
2020-05-06 06:08:21 -04:00
|
|
|
if (otherFrom !== otherTo) {
|
|
|
|
// if otherFrom == otherTo, we don't need to change our op.
|
|
|
|
|
|
|
|
// where did my thing go?
|
|
|
|
if (from === otherFrom) {
|
|
|
|
// they moved it! tie break.
|
|
|
|
if (type === 'left') {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common] = otherTo
|
|
|
|
if (from === to) {
|
|
|
|
// ugh
|
|
|
|
c.lm = otherTo
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// they moved around it
|
|
|
|
if (from > otherFrom) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]--
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
if (from > otherTo) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (from === otherTo) {
|
|
|
|
if (otherFrom > otherTo) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
|
|
|
if (from === to) {
|
|
|
|
// ugh, again
|
|
|
|
c.lm++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// step 2: where am i going to put it?
|
|
|
|
if (to > otherFrom) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm--
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (to === otherFrom) {
|
|
|
|
if (to > from) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm--
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (to > otherTo) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm++
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (to === otherTo) {
|
|
|
|
// if we're both moving in the same direction, tie break
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
(otherTo > otherFrom && to > from) ||
|
|
|
|
(otherTo < otherFrom && to < from)
|
|
|
|
) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (type === 'right') {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (to > from) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm++
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (to === otherFrom) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.lm--
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (c.li !== undefined && c.ld === undefined && commonOperand) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// li
|
2020-05-06 06:09:33 -04:00
|
|
|
from = otherC.p[common]
|
|
|
|
to = otherC.lm
|
|
|
|
p = c.p[common]
|
2020-05-06 06:08:21 -04:00
|
|
|
if (p > from) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]--
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
if (p > to) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ld, ld+li, si, sd, na, oi, od, oi+od, any li on an element beneath
|
|
|
|
// the lm
|
|
|
|
//
|
|
|
|
// i.e. things care about where their item is after the move.
|
2020-05-06 06:09:33 -04:00
|
|
|
from = otherC.p[common]
|
|
|
|
to = otherC.lm
|
|
|
|
p = c.p[common]
|
2020-05-06 06:08:21 -04:00
|
|
|
if (p === from) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common] = to
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
|
|
|
if (p > from) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]--
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
if (p > to) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (p === to) {
|
|
|
|
if (from > to) {
|
2020-05-06 06:09:33 -04:00
|
|
|
c.p[common]++
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (otherC.oi !== undefined && otherC.od !== undefined) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if (c.p[common] === otherC.p[common]) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.oi !== undefined && commonOperand) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// we inserted where someone else replaced
|
|
|
|
if (type === 'right') {
|
|
|
|
// left wins
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
|
|
|
// we win, make our op replace what they inserted
|
2020-05-06 06:09:33 -04:00
|
|
|
c.od = otherC.oi
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// -> noop if the other component is deleting the same object (or any
|
|
|
|
// parent)
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (otherC.oi !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.oi !== undefined && c.p[common] === otherC.p[common]) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// left wins if we try to insert at the same place
|
|
|
|
if (type === 'left') {
|
2020-05-06 06:09:33 -04:00
|
|
|
json.append(dest, { p: c.p, od: otherC.oi })
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (otherC.od !== undefined) {
|
|
|
|
if (c.p[common] === otherC.p[common]) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (!commonOperand) {
|
|
|
|
return dest
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
if (c.oi !== undefined) {
|
2020-05-06 06:09:33 -04:00
|
|
|
delete c.od
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
return dest
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
|
|
|
json.append(dest, c)
|
|
|
|
return dest
|
|
|
|
}
|
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
|
|
|
if (!exports.types) {
|
|
|
|
exports.types = {}
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// This is kind of awful - come up with a better way to hook this helper code up.
|
2020-05-06 06:09:33 -04:00
|
|
|
exports._bt(json, json.transformComponent, json.checkValidOp, json.append)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// [] is used to prevent closure from renaming types.text
|
2020-05-06 06:09:33 -04:00
|
|
|
exports.types.json = json
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = json
|
|
|
|
|
|
|
|
require('./helpers').bootstrapTransform(
|
|
|
|
json,
|
|
|
|
json.transformComponent,
|
|
|
|
json.checkValidOp,
|
|
|
|
json.append
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|