valtiojs / valtio-yjs

valtio-yjs makes yjs state easy
MIT License
270 stars 15 forks source link

Swapping items in array #7

Closed rubenen closed 2 years ago

rubenen commented 3 years ago

Amazing project this one!

I have some issues moving items in an array though - in plain valtio I can move an element like this:

import { proxy } from "valtio";

const list = proxy([]);
...
const moveUp = (fromIndex, item) => {
    list.splice(fromIndex, 1);
    list.splice(fromIndex - 1, 0, item);
};

This breaks if I activate the yjs binding (uncomment https://codesandbox.io/s/valtio-yjs-array-demo-forked-j1djs?file=/src/App.js:353-388).

I think I can make it work using the Y.array methods directly (first yarray.delete(index) and then yarray.insert(newIndex, [item])).

I'm not sure if this is a bug or just a limitation since js arrays don't have delete and insert methods.

dai-shi commented 3 years ago

Nice catch. That's something not covered in tests. I wish it to work (if we just need to yarray.delete and yarray.insert), so this can be said a bug.

I will look into it, when I get time. Feel free to play around the internals if you are interested.

dai-shi commented 3 years ago

Added some failing tests in #8.

dai-shi commented 2 years ago

Published: https://www.npmjs.com/package/valtio-yjs/v/0.1.7

Sorry for taking so much time.