From 770fed634fb4b3ccf2f4b5e1d313933d2a4a732e Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 1 Apr 2024 08:57:57 -0500 Subject: [PATCH] Hack for dragging on self (drag data not available in dragover event!) --- src/tools/builder/src/python/builder.py | 38 +++++++++++++++---------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/tools/builder/src/python/builder.py b/src/tools/builder/src/python/builder.py index 4a7c3f2..49eaf72 100644 --- a/src/tools/builder/src/python/builder.py +++ b/src/tools/builder/src/python/builder.py @@ -34,6 +34,9 @@ def __init__(self): # The JS-side of the Invent-Builder. self._builder_model = None + # The component currently being moved or None if there is no such component. + self._moving = None + def set_view_model(self, builder_model): """ Connects the Python side of the view model to the JS side. @@ -309,7 +312,7 @@ def _inject_js_event_handlers_into_container(self, container): self._inject_js_event_handlers_into_component(container) - for item in container.content[:]: + for item in container.content: if isinstance(item, Container): self._inject_js_event_handlers_into_container(item) @@ -355,8 +358,9 @@ def on_dragstart(event): else: event.stopPropagation() - print("on_dragstart:", component.name) - event.dataTransfer.setData("move", component.id); + print("on_dragstart:", component.name, component.id) + self._moving = component + event.dataTransfer.setData("move", component.id) def on_drop(event): print("-"*80) @@ -367,18 +371,13 @@ def on_drop(event): # Moving or adding? ######################################################## move_data = event.dataTransfer.getData("move") - print("MOVE COMPONENT", move_data) + print("on_drop: move_data:", move_data) if move_data == component.id: return if move_data: component_to_move = Component.get_component_by_id(move_data) - print("MOVING:", component_to_move.name, component_to_move.id, id(component_to_move), component_to_move.parent) - if component_to_move.parent is None: - print("But something is fishy... parent is None") - self.pprint_app() - self.delete_component(component_to_move.id) new_component = component_to_move.clone() @@ -423,12 +422,22 @@ def on_dragover(event): """ Handle a JS "dragover" event on a component. """ - #print("on_dragover:", component.name) + #print("on_dragover:", component.name, component.id) event.preventDefault() event.stopPropagation() - move_data = event.dataTransfer.getData("move") - if move_data == component.id: + #move_data = event.dataTransfer.getData("move") + #print("move data is", move_data, type(move_data)) + if self._moving == component: + print("draggining over self!") + if isinstance(component, Container): + component.element.classList.remove(f"drop-zone-active") + + else: + for class_name in component.element.parentNode.classList: + if class_name.startswith("drop-zone-active"): + component.element.parentNode.classList.remove(class_name) + return pointer_offset_x = event.offsetX @@ -457,8 +466,7 @@ def on_dragover(event): self.mode = "right" else: - return - #raise ValueError("Shouldn't get here!!!", container) + raise ValueError("Shouldn't get here!!!", container) if isinstance(component, Container): component.element.classList.add(f"drop-zone-active") @@ -471,7 +479,7 @@ def on_dragover(event): component.element.parentNode.classList.add(f"drop-zone-active-{self.mode}") def on_dragleave(event): - #print("on_dragleave:", component.name) + print("on_dragleave:", component.name) event.preventDefault() if isinstance(component, Container):