I have an bunch of draggable elements, that are absolutely positioned on an "artboard". Users can edit these elements, and also drag them.
Most important thing that user can lasso-select or shift-select many of elements, that applies to element class ".selected", so when dragging one, other selected elements should move too.
Currently this works by modifying top
and left
position of other elements, but this exposes a snapping problem.
Snapping only works for currently draggable element, but it should work for selected elements too.
Behavior that I want is best described in mockups.
Initial state of artboard
Current behavior
Desired behavior
I tried several different approaches, but with no luck.
Firstly, I tried to calculate selected position of bounding box of selected elements, and in drag
event modify ui.position to snap to correct sides with bounding box of all selected elements. That was obviously wrong, so modifying ui position -> modifying helper element, which results in some weird jumping of elements.
After that, I tried to lock position of other elements, when bounding box of selected elements about to snap, this works for other elements, but that one that is dragged keeps moving, and i don't seem a way to stop this behavior.
I tried to modify $.ui.plugin.add('draggable', 'snap', function () {})
part of widget, so it will use different bounding box when calculating snapping, but it results in other weird behavior: when bounding box of selected elements is about to snap, currently draggable element jump (and other elements) jumps to snappable elements.
I tried also to wrap selected elements into div, with helper function of draggable widget, but I can't use this approach, as elements are absolutely positioned, so if I put them inside other absolutely positioned div, they can jump like 1000px in the left for example, so I'll need to somehow recalculate positions, and then when dragging stops - keep correct previous positions.
I can't find how to make this solution work, so I dropped it.
Edit: make small playground to showcase problem and test things https://codepen.io/newfox79/pen/eYjzmGG