Files
yjs/src/structs/ItemJSON.js

111 lines
3.1 KiB
JavaScript
Raw Normal View History

/**
2018-11-25 03:17:00 +01:00
* @module structs
*/
2017-10-11 03:41:54 +02:00
2019-03-26 01:14:15 +01:00
import { AbstractItem, logItemHelper, AbstractItemRef, splitItem } from './AbstractItem.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-27 14:59:12 +01:00
import { Y } from '../utils/Y.js' // eslint-disable-line
2019-03-26 01:14:15 +01:00
import { ID } from '../utils/ID.js' // eslint-disable-line
import { ItemType } from './ItemType.js' // eslint-disable-line
import { getItemCleanEnd, getItemCleanStart, getItemType } from '../utils/StructStore.js'
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
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
* @param {ItemType | null} parent
* @param {string | null} parentSub
* @param {Array<any>} content
*/
constructor (id, left, right, parent, parentSub, content) {
super(id, left, right, parent, parentSub)
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
* @param {ItemType | null} parent
* @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
/**
* Transform this Type to a readable format.
* Useful for logging as all Items and Delete implement this method.
*
* @private
*/
logString () {
return logItemHelper('ItemJSON', this, `content:${JSON.stringify(this.content)}`)
}
get length () {
return this.content.length
2017-10-11 03:41:54 +02:00
}
/**
2019-03-26 01:14:15 +01:00
* @param {number} diff
*/
2019-03-26 01:14:15 +01:00
splitAt (diff) {
/**
* @type {ItemJSON}
*/
const right = splitItem(this, diff)
right.content = this.content.splice(diff)
return right
2017-10-11 03:41:54 +02:00
}
/**
* @param {encoding.Encoder} encoder
*/
2019-03-26 01:14:15 +01:00
write (encoder) {
super.write(encoder, structJSONRefNumber)
const len = this.content.length
encoding.writeVarUint(encoder, len)
2017-10-11 03:41:54 +02:00
for (let i = 0; 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 {number} info
*/
constructor (decoder, info) {
super(decoder, info)
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
}
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) {
const store = transaction.y.store
return new ItemJSON(
this.id,
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
this.parent === null ? null : getItemType(store, this.parent),
this.parentSub,
this.content
)
2017-10-16 04:53:12 +02:00
}
2017-10-11 03:41:54 +02:00
}