diff --git a/src/types/AbstractType.js b/src/types/AbstractType.js index 8d3beb8b..bd4ccc2e 100644 --- a/src/types/AbstractType.js +++ b/src/types/AbstractType.js @@ -54,6 +54,8 @@ export const useSearchMarker = (tr, yarray, index, f) => { const prevItem = /** @type {Item} */ (sm.nextItem) if (createFreshMarker) { searchMarker.push(fsm) + } else { + fsm.reinit(tr) } const diff = fsm.index - index if (diff > 0) { diff --git a/src/types/YArray.js b/src/types/YArray.js index 2f425a58..3a0d806e 100644 --- a/src/types/YArray.js +++ b/src/types/YArray.js @@ -124,14 +124,16 @@ export class YArray extends AbstractType { * @param {Array} content The array of content */ insert (index, content) { - if (this.doc !== null) { - transact(this.doc, transaction => { - useSearchMarker(transaction, this, index, walker => - walker.insertArrayValue(transaction, content) - ) - }) - } else { - /** @type {Array} */ (this._prelimContent).splice(index, 0, ...content) + if (content.length > 0) { + if (this.doc !== null) { + transact(this.doc, transaction => { + useSearchMarker(transaction, this, index, walker => + walker.insertArrayValue(transaction, content) + ) + }) + } else { + /** @type {Array} */ (this._prelimContent).splice(index, 0, ...content) + } } } diff --git a/src/utils/ListIterator.js b/src/utils/ListIterator.js index 1e7385dc..1258e13d 100644 --- a/src/utils/ListIterator.js +++ b/src/utils/ListIterator.js @@ -1,4 +1,5 @@ import * as error from 'lib0/error' +import { getItem } from 'yjs' import { getItemCleanStart, @@ -73,6 +74,18 @@ export class ListIterator { return iter } + /** + * @param {Transaction} tr + */ + reinit (tr) { + if (this.nextItem) { + const nextId = this.nextItem.id + const reinitId = createID(nextId.client, nextId.clock + this.rel) + this.nextItem = getItem(tr.doc.store, reinitId) + this.rel = reinitId.clock - this.nextItem.id.clock + } + } + /** * @type {Item | null} */ @@ -333,7 +346,7 @@ export class ListIterator { left = new Item(createID(ownClientId, getState(store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, c) left.integrate(tr, 0) }) - if (right === null && left !== null) { + if (right === null) { this.nextItem = left this.reachedEnd = true } else {