Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event stop returns incorrect store.stored value #136

Closed
lubomirblazekcz opened this issue Aug 25, 2021 · 9 comments
Closed

Event stop returns incorrect store.stored value #136

lubomirblazekcz opened this issue Aug 25, 2021 · 9 comments
Assignees
Labels
question Further information is requested

Comments

@lubomirblazekcz
Copy link

lubomirblazekcz commented Aug 25, 2021

What is the problem?

store.stored.length returns 0 when selecting one item, 1 when selecting two, etc. it's one value behind

Please provide the steps to reproduce and create a CodeSandbox.

I am using the vanilla https://codesandbox.io/s/viselectvanilla-kt332

Your environment:

Toolset: vite@2.5.0
Version: @viselect/vanilla@3.0.0-beta.9
Browser:  Firefox 91
OS:  MacOS 11.5
@lubomirblazekcz lubomirblazekcz added the unconfirmed Problem is not confirmed yet label Aug 25, 2021
@simonwep
Copy link
Owner

Could you elaborate the steps? stored contains the stored elements from the previous selection... I just noticed that these things are undocumented, I'll do that.

@lubomirblazekcz
Copy link
Author

lubomirblazekcz commented Aug 29, 2021

I'm trying to access all stored elements from the 'stop' event, but I always get incorrect values, I resolved this by accessing the elements with document.querySelectorAll('.selected') on 'move' event, But I'm not sure if there is other, better way to do this.

@simonwep
Copy link
Owner

simonwep commented Sep 4, 2021

I still don't really see the problem you're having :/ The sandbox example you've send it works as expected. Every time you add something to the selection the previously selected elements are logged... I slightly modified your sandbox to what you've described.

@lubomirblazekcz
Copy link
Author

Hmm, maybe I misunderstood the event, I though store would return current selected elemens, not previous. Anyway as I said, I tried to get the current selected elements. I resolved this by accessing the elements directly via dom.

@simonwep
Copy link
Owner

The currently selected elements can be accessed with store.selected... is this what you're looking for? 🤔

@lubomirblazekcz
Copy link
Author

That might be what I was looking for. But I see some inconsistent beheviour across event listeners. :/

When I select items with drag, it returns correct selected elements in 'move' event. But once I select one by one with cmd + click, it doesn't work.

https://codesandbox.io/s/viselect-vanilla-forked-gu7q6?file=/src/main.ts

@libasoles
Copy link

libasoles commented Nov 8, 2021

Hi. Same here. It was a bit difficult to understand that store.stored was retrieving the last time selection because a console.log will display the updated version of store.stored in the browser, while at runtime store.stored will retrieve the last results (so my script will actually fail).

Regarding store. selected , it would be nice if it could retrieve all the selected items, no matter if they were selected previously or just now. I mean, if I select two items the store.selected retrieves two, but then if I use cmd and select another two, I'd expect four now, but it only retrieves the latest two.

Anyway, I found myself performing a cumbersome math to get the selected items:

const previousSelection = this.model.userSelection(this.me());
    const added = store.changed.added.map((added) => added.dataset.slot);
    const selected = store.selected.map((selected) => selected.dataset.slot);
    const removed = store.changed.removed.map(
      (removed) => removed.dataset.slot
    );

    const selection = previousSelection
      .concat(selected)
      .concat(added)
      .filter((slot) => !removed.includes(slot));

Feels bad.

Nice to know there's this selector: document.querySelectorAll('.selected')

However, I'd still expect the library to provide this already calculated 🤷

@simonwep
Copy link
Owner

@libasoles Aren't you looking for the .getSelection() function? It returns all, currently selected elements...

@simonwep
Copy link
Owner

@lubomirblazekcz I took another look at your problem and there's a difference for what counts as a selection and the whole selection altogether.

Each drag-, click- and range-selection is a single selection and elements for this are available in store.selection.
The whole user-experience / viselect-instance consists out of multiple such "selections" which are combined in the store.stored array - if you want to get both (e.g. the elements from the current and previous selections) you'd have to use .getSelection() as asked by @libasoles!

@simonwep simonwep added question Further information is requested and removed unconfirmed Problem is not confirmed yet labels Dec 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants