Create Structs based on offset, if necessary

implement offset parameter in Ref.toStruct
This commit is contained in:
Kevin Jahns
2019-04-05 12:38:02 +02:00
parent e56899a02c
commit 8a7416ad50
15 changed files with 234 additions and 74 deletions

View File

@@ -9,6 +9,9 @@ import {
getItemCleanStart,
getItemType,
splitItem,
changeItemRefOffset,
GC,
ItemDeleted,
Transaction, ID, AbstractType // eslint-disable-line
} from '../internals.js'
@@ -106,6 +109,9 @@ export class ItemJSONRef extends AbstractItemRef {
cs.push(JSON.parse(c))
}
}
/**
* @type {Array<any>}
*/
this.content = cs
}
get length () {
@@ -113,17 +119,34 @@ export class ItemJSONRef extends AbstractItemRef {
}
/**
* @param {Transaction} transaction
* @return {ItemJSON}
* @param {number} offset
* @return {ItemJSON|GC}
*/
toStruct (transaction) {
toStruct (transaction, offset) {
const y = transaction.y
const store = y.store
if (offset > 0) {
changeItemRefOffset(this, offset)
this.content = this.content.slice(offset)
}
let parent
if (this.parent !== null) {
const parentItem = getItemType(store, this.parent)
switch (parentItem.constructor) {
case ItemDeleted:
case GC:
return new GC(this.id, this.content.length)
}
parent = parentItem.type
} else {
// @ts-ignore
parent = y.get(this.parentYKey)
}
return new ItemJSON(
this.id,
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
parent,
this.parentSub,
this.content
)