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

Request - add Widget level drag & drop #492

Open
reubenfirmin opened this issue Oct 21, 2023 · 2 comments
Open

Request - add Widget level drag & drop #492

reubenfirmin opened this issue Oct 21, 2023 · 2 comments

Comments

@reubenfirmin
Copy link

reubenfirmin commented Oct 21, 2023

I found https://kvision.gitbook.io/kvision-guide/2.-frontend-development-guide/drag-and-drop pretty hard to understand. Looking at the source, it seems that this is thin shim on top of data transfer, but it doesn't work at the same level of abstraction as the rest of kvision (since we're expected to handle serialization/deserialization directly).

The other problem, I think, is that serialization happens on initial render, rather than at the start of the drag event, which is relatively expensive (and also doesn't allow for dynamic updates without updating setDragTargetData) -- i.e. there is no way to set "draggable = true" without also setting the serialized data that gets pushed into datatransfer.

It would be great to provide a set of generic (maybe inline/reified) functions that handle conversion to and from Widget subclasses.

It would also be really nice to be able to hook into some of the lifecyle events of drag and drop. I'm using dragula right now, which is sadly unmaintained; however it has a nice API that could be emulated: https://github.com/bevacqua/dragula/

@rjaros
Copy link
Owner

rjaros commented Oct 21, 2023

You are thinking about direct drag & drop of KVision widgets?

@reubenfirmin
Copy link
Author

reubenfirmin commented Oct 21, 2023

Right. So for example:

class MyComponent: Div() {
     
     init {
         setDropTarget(true)
     }

     // only called if canDrop returns true - dragula supports this
     override fun onDrop(widget: Widget) {

     }  

     // nice to have -  gives the widget the thing that's potentially going to be dropped, so a preview can be rendered (like trello)
     override fun onDragOver(widget: Widget) {

      }
      
     // matches above
     override fun onDragOut(widget: Widget) {

      }

     // similar to dragula. this is really useful, as it lets you ignore draggables that aren't applicable to this drop target
     override fun canDrop(widget: Widget): Boolean {
          return if (widget is MyOtherComponent && ((widget as MyOtherComponent).someProperty == "foo") {
               true
          } else {
               false
          }
     }
}
class MyOtherComponent: Div() {
      init { 
          setDraggable(true)
      } 
}

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

No branches or pull requests

2 participants