From 44499cb9fe9a309f0a4e1d9a0ce6aaa70aeab721 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Thu, 16 Jun 2022 20:23:34 +0200 Subject: [PATCH] fix move-range tests of moved-moved items --- src/structs/ContentMove.js | 1 + src/utils/YEvent.js | 22 +++++++++++++++++++--- tests/y-array.tests.js | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/structs/ContentMove.js b/src/structs/ContentMove.js index 49870c53..f062c998 100644 --- a/src/structs/ContentMove.js +++ b/src/structs/ContentMove.js @@ -164,6 +164,7 @@ export class ContentMove { if (adaptPriority || nextPrio < this.priority || (currMoved != null && nextPrio === this.priority && (currMoved.id.client < item.id.client || (currMoved.id.client === item.id.client && currMoved.id.clock < item.id.clock)))) { if (currMoved !== null) { this.overrides.add(currMoved) + transaction._mergeStructs.push(start) } maxPriority = math.max(maxPriority, nextPrio) // was already moved diff --git a/src/utils/YEvent.js b/src/utils/YEvent.js index cdbc18a2..4c6bbd81 100644 --- a/src/utils/YEvent.js +++ b/src/utils/YEvent.js @@ -195,6 +195,19 @@ export class YEvent { delta.push(lastOp) } } + /** + * @param {Item} item + */ + const isMovedByNew = item => { + let moved = item.moved + while (moved != null) { + if (this.adds(moved)) { + return true + } + moved = moved.moved + } + return false + } for (let item = target._start; ;) { if (item === currMoveEnd && currMove) { item = currMove @@ -210,12 +223,12 @@ export class YEvent { const { start, end } = getMovedCoords(item.content, tr) currMove = item currMoveEnd = end - currMoveIsNew = this.adds(item) + currMoveIsNew = this.adds(item) || currMoveIsNew item = start continue // do not move to item.right } } else if (item.moved !== currMove) { - if (!currMoveIsNew && item.countable && (!item.deleted || this.deletes(item)) && item.moved && !this.adds(item) && this.adds(item.moved) && (this.transaction.prevMoved.get(item) || null) === currMove) { + if (!currMoveIsNew && item.countable && (!item.deleted || this.deletes(item)) && !this.adds(item) && isMovedByNew(item) && (this.transaction.prevMoved.get(item) || null) === currMove) { if (lastOp === null || lastOp.delete === undefined) { packOp() lastOp = { delete: 0 } @@ -237,8 +250,11 @@ export class YEvent { packOp() lastOp = { insert: [] } } + // @todo push items instead (or splice..) lastOp.insert = lastOp.insert.concat(item.content.getContent()) - added.add(item) + if (!currMoveIsNew) { + added.add(item) + } } else { if (lastOp === null || lastOp.retain === undefined) { packOp() diff --git a/tests/y-array.tests.js b/tests/y-array.tests.js index 3776077c..44caca08 100644 --- a/tests/y-array.tests.js +++ b/tests/y-array.tests.js @@ -563,10 +563,10 @@ const arrayTransactions = [ return } const pos = prng.int32(gen, 0, yarray.length - 1) - const len = prng.int32(gen, 1, math.min(1, yarray.length - pos)) + const len = prng.int32(gen, 1, math.min(3, yarray.length - pos)) const _newPosAdj = prng.int32(gen, 0, yarray.length - len) // make sure that we don't insert in-between the moved range - const newPos = _newPosAdj + (_newPosAdj > yarray.length - len ? len : 0) + const newPos = _newPosAdj + (_newPosAdj > pos ? len : 0) const oldContent = yarray.toArray() yarray.moveRange(pos, pos + len - 1, newPos) const movedValues = oldContent.splice(pos, len)