Files
yjs/src/structs/ItemJSON.js

132 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
2018-11-25 03:17:00 +01:00
* @module structs
*/
2017-10-11 03:41:54 +02:00
import {
AbstractItem,
AbstractItemRef,
getItemCleanEnd,
getItemCleanStart,
getItemType,
splitItem,
Transaction, ID, AbstractType // eslint-disable-line
} from '../internals.js'
2019-03-10 23:26:53 +01:00
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js'
2018-11-25 03:17:00 +01:00
2019-03-26 01:14:15 +01:00
export const structJSONRefNumber = 5
export class ItemJSON extends AbstractItem {
/**
* @param {ID} id
* @param {AbstractItem | null} left
* @param {AbstractItem | null} right
2019-04-03 03:08:10 +02:00
* @param {AbstractType<any>} parent
2019-03-26 01:14:15 +01:00
* @param {string | null} parentSub
* @param {Array<any>} content
*/
constructor (id, left, right, parent, parentSub, content) {
super(id, left, right, parent, parentSub)
2019-04-03 02:30:44 +02:00
/**
* @type {Array<any>}
*/
2019-03-26 01:14:15 +01:00
this.content = content
2017-10-11 03:41:54 +02:00
}
2019-03-26 01:14:15 +01:00
/**
* @param {ID} id
* @param {AbstractItem | null} left
* @param {AbstractItem | null} right
2019-04-04 13:50:00 +02:00
* @param {AbstractType<any>} parent
2019-03-26 01:14:15 +01:00
* @param {string | null} parentSub
*/
copy (id, left, right, parent, parentSub) {
return new ItemJSON(id, left, right, parent, parentSub, this.content)
2017-10-30 11:33:00 +01:00
}
2019-03-26 01:14:15 +01:00
get length () {
return this.content.length
2017-10-11 03:41:54 +02:00
}
2019-03-29 01:02:44 +01:00
getContent () {
return this.content
}
/**
2019-03-29 01:02:44 +01:00
* @param {Transaction} transaction
2019-03-26 01:14:15 +01:00
* @param {number} diff
*/
2019-03-29 01:02:44 +01:00
splitAt (transaction, diff) {
2019-03-26 01:14:15 +01:00
/**
* @type {ItemJSON}
*/
2019-03-29 01:02:44 +01:00
// @ts-ignore
const right = splitItem(transaction, this, diff)
2019-03-26 01:14:15 +01:00
right.content = this.content.splice(diff)
return right
2017-10-11 03:41:54 +02:00
}
2019-04-03 02:30:44 +02:00
/**
* @param {ItemJSON} right
* @return {boolean}
*/
mergeWith (right) {
if (right.origin === this && this.right === right) {
this.content = this.content.concat(right.content)
return true
}
return false
}
/**
* @param {encoding.Encoder} encoder
* @param {number} offset
*/
write (encoder, offset) {
super.write(encoder, offset, structJSONRefNumber)
2019-03-26 01:14:15 +01:00
const len = this.content.length
2019-04-03 02:30:44 +02:00
encoding.writeVarUint(encoder, len - offset)
for (let i = offset; i < len; i++) {
2019-03-26 01:14:15 +01:00
const c = this.content[i]
encoding.writeVarString(encoder, c === undefined ? 'undefined' : JSON.stringify(c))
}
}
}
export class ItemJSONRef extends AbstractItemRef {
/**
* @param {decoding.Decoder} decoder
* @param {ID} id
2019-03-26 01:14:15 +01:00
* @param {number} info
*/
constructor (decoder, id, info) {
super(decoder, id, info)
2019-03-26 01:14:15 +01:00
const len = decoding.readVarUint(decoder)
const cs = []
for (let i = 0; i < len; i++) {
const c = decoding.readVarString(decoder)
if (c === 'undefined') {
cs.push(undefined)
} else {
2019-03-26 01:14:15 +01:00
cs.push(JSON.parse(c))
}
2017-10-11 03:41:54 +02:00
}
2019-03-26 01:14:15 +01:00
this.content = cs
2017-10-11 03:41:54 +02:00
}
get length () {
return this.content.length
}
2018-03-29 11:58:02 +02:00
/**
2019-03-26 01:14:15 +01:00
* @param {Transaction} transaction
* @return {ItemJSON}
2018-03-29 11:58:02 +02:00
*/
2019-03-26 01:14:15 +01:00
toStruct (transaction) {
2019-03-29 01:02:44 +01:00
const y = transaction.y
const store = y.store
2019-03-26 01:14:15 +01:00
return new ItemJSON(
this.id,
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
2019-03-29 01:02:44 +01:00
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
2019-03-26 01:14:15 +01:00
this.parentSub,
this.content
)
2017-10-16 04:53:12 +02:00
}
2017-10-11 03:41:54 +02:00
}