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

Fix BUG: Dragging multiple blocks reverses the order #3547 #4220

Merged
merged 1 commit into from
Apr 4, 2022

Conversation

lexoyo
Copy link
Contributor

@lexoyo lexoyo commented Mar 28, 2022

Hello
I'm trying to fix #3547
This is more of an exercise for me to dive in grapesjs code, let me know if I'm loosing my time or if it sounds useful

@lexoyo
Copy link
Contributor Author

lexoyo commented Mar 29, 2022

Here is what I did:

  • when the user drags multiple elements, reorder the dragged elements to reflect the elements positions in the dom - needs to handle the case where the elements are not in the same container
  • when the user drops multiple elements, move them to the right place, i.e. on top of the previously dropped one

I'd appreciate any feedback on the code or tests to make sure I didn't break anything @japo32 ?

Also I did not create any tests because I couldn't find a suitable place, should I @artf ?

Here is how it behaves now:

Uploading 2022-03-29 12-50-56.mp4…

@lexoyo lexoyo changed the title Fix #3547 Fix BUG: Dragging multiple blocks reverses the order #3547 Mar 29, 2022
Copy link
Member

@artf artf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lexoyo thanks for the PR.
I'd say I'd not expect any change in MoveComponent and I'd not rely on DOM for sorting as it might differ from the component tree (the component view can be completely custom).

I think this issue might be solved on the sorter level by relying on the component methods like component.index() and component.parent().

Here what I'd expect from a simple sort by component index.

const toMoveArr = ...;

if (toMoveArr.length > 1) {
  toMoveArr.sort((a, b) => {
    // inverted sort as we append always to the same lastPos
    return b.index() - a.index();
  })
}

toMoveArr.forEach(model => { ...

This obviously doesn't count the sort across different parents which is probably the main reason I always postponed this fix 😅

@lexoyo lexoyo force-pushed the dev branch 2 times, most recently from d0b37a0 to 49f3214 Compare March 31, 2022 18:30
@lexoyo
Copy link
Contributor Author

lexoyo commented Mar 31, 2022

Hi @artf , thx for the quick reply

Hi @lexoyo thanks for the PR. I'd say I'd not expect any change in MoveComponent and I'd not rely on DOM for sorting as it might differ from the component tree (the component view can be completely custom).

It makes sense of course !

I think this issue might be solved on the sorter level by relying on the component methods like component.index() and component.parent().

Ok, very feasable, I did this

Here what I'd expect from a simple sort by component index.

const toMoveArr = ...;

if (toMoveArr.length > 1) {
  toMoveArr.sort((a, b) => {
    // inverted sort as we append always to the same lastPos
    return b.index() - a.index();
  })
}

toMoveArr.forEach(model => { ...

I did it like this, but then the problem is when we drop in the same container, after the elements, they get inverted

This is why this commit is needed
d0b37a0

You can test it here, I am still testing it too
https://lexoyo.me/grapesjs/demo.html

PS: Sorry for the useless mess in some files, due to linter, I'll clean it at one point, the only changes I made are in src/utils/Sorter.js
PPS: I added some unit tests, what do you think?

Copy link
Member

@artf artf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks to work pretty well 👏

src/commands/view/MoveComponent.js Outdated Show resolved Hide resolved
test/specs/utils/Sorter.js Show resolved Hide resolved
src/utils/Sorter.js Show resolved Hide resolved
src/utils/Sorter.js Outdated Show resolved Hide resolved
@lexoyo
Copy link
Contributor Author

lexoyo commented Apr 1, 2022

@artf I took into account your review

I reverted MoveComponent.js file to its version before I changed it

For your last 2 comments, I think I got it, please confirm. I have tested dropping images, now it works :))

@lexoyo
Copy link
Contributor Author

lexoyo commented Apr 1, 2022

also i updated https://lexoyo.me/grapesjs/demo.html

@artf artf merged commit d8bd222 into GrapesJS:dev Apr 4, 2022
@artf
Copy link
Member

artf commented Apr 4, 2022

Awesome, thank you @lexoyo ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Dragging multiple blocks reverses the order
2 participants