From 0678ed1eb520f471f46c2f8a4f929565c2d622f9 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Mon, 10 Jun 2024 12:18:16 +0200 Subject: [PATCH] fix event.path in observeDeep - closes #457 --- src/utils/YEvent.js | 4 ++-- tests/y-array.tests.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/utils/YEvent.js b/src/utils/YEvent.js index 13174148..d6a60242 100644 --- a/src/utils/YEvent.js +++ b/src/utils/YEvent.js @@ -264,8 +264,8 @@ const getPathTo = (parent, child) => { let i = 0 let c = /** @type {AbstractType} */ (child._item.parent)._start while (c !== child._item && c !== null) { - if (!c.deleted) { - i++ + if (!c.deleted && c.countable) { + i += c.length } c = c.right } diff --git a/tests/y-array.tests.js b/tests/y-array.tests.js index 941598ac..9875e85e 100644 --- a/tests/y-array.tests.js +++ b/tests/y-array.tests.js @@ -330,6 +330,29 @@ export const testObserveDeepEventOrder = tc => { } } +/** + * Correct index when computing event.path in observeDeep - https://github.com/yjs/yjs/issues/457 + * + * @param {t.TestCase} _tc + */ +export const testObservedeepIndexes = _tc => { + const doc = new Y.Doc() + const map = doc.getMap() + // Create a field with the array as value + map.set('my-array', new Y.Array()) + // Fill the array with some strings and our Map + map.get('my-array').push(['a', 'b', 'c', new Y.Map()]) + /** + * @type {Array} + */ + let eventPath = [] + map.observeDeep((events) => { eventPath = events[0].path }) + // set a value on the map inside of our array + map.get('my-array').get(3).set('hello', 'world') + console.log(eventPath) + t.compare(eventPath, ['my-array', 3]) +} + /** * @param {t.TestCase} tc */