Skip to content

✨ Implement "Drop Node on Link" feature - #409

Merged
MFA-X-AI merged 4 commits into
XpressAI:masterfrom
rabea-al:feature/drop-node-on-link
Jun 19, 2025
Merged

MFA-X-AI merged 4 commits into
XpressAI:masterfrom
rabea-al:feature/drop-node-on-link

Conversation

@rabea-al

@rabea-al rabea-al commented Jun 11, 2025 •

Copy link
Copy Markdown
Contributor

Description

This PR introduces a new feature that allows users to drag and drop a component directly onto an existing connection, automatically splitting the link and inserting the component in between — whether the component is dragged from the sidebar or from within the canvas itself.

Video Demo

Drop_on_link.mp4

Key Features

🔹 1. Link Hover Detection

  • Detects when a node is dragged over a valid link (out-0 → in-0) using `LinkSplitManager.detectLinkUnderPointer`.
  • Highlights the hovered link using the `.hover` class.

🔹 2. Smart Link Splitting

  • Introduces `SplitLinkCommand` that:
    • Validates required ports (`in-0`, `out-0`) on the dragged component.
    • Removes the original link and reconnects:
      • `source → draggedNode.in-0`
      • `draggedNode.out-0 → target`
  • Ensures existing links on affected ports are removed before reconnecting.

🔹 3. User Feedback

  • Adds contextual notifications to inform the user when:
    • Required ports are missing.
    • Ports are already connected and will be overwritten.
    • The link is invalid or unsupported for splitting.

🔹 4. Canvas Integration

  • `MoveItemsState.ts`: triggers `SplitLinkCommand` when dragging from canvas over a valid link.
  • `XircuitsBodyWidget.tsx`: handles `onDrop` event for sidebar-dragged components, and calls `updateHoveredLink` to support link detection and hover state.
  • Includes fallback handling when no valid link is detected.

Edge Case Handling

  • Drops are rejected if the component lacks required ports.
  • Link is validated to match `out-0 → in-0` before applying the split.
  • Automatically clears hover state after execution.

File Changes

  • `SplitLinkCommand.ts`: defines the logic for link splitting and reconnection.
  • `LinkSplitManager.ts`: manages link detection and hover state.
  • `MoveItemsState.ts`: detects drop events from canvas and executes split.
  • `XircuitsBodyWidget.tsx`: handles drop events from the sidebar and triggers `SplitLinkCommand` with link validation.

Notes

  • Only supports main flow connections (`out-0 → in-0`)

Pull Request Type

  • Xircuits Core (Jupyterlab Related changes)
  • Xircuits Canvas (Custom RD Related changes)
  • Xircuits Component Library
  • Xircuits Project Template
  • Testing Automation
  • Documentation
  • Others (Please Specify)

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Tests

Test 1: Insert node from Canvas

Steps:

  1. Drag a node (with in-0 and out-0 ports) from the canvas.
  2. Hover it over an existing out-0 → in-0 link.
  3. Observe that the link highlights in yellow dashed style.
  4. Drop the node.

Expected Result:

  • The original link is split.

  • The dragged node is inserted between the two connected nodes.

  • The canvas updates with two new links:

    • source → draggedNode.in-0
    • draggedNode.out-0 → target

Test 2: Insert node from Sidebar

Steps:

  1. Drag a new node from the sidebar.
  2. Hover it over an existing out-0 → in-0 link.
  3. Observe the link highlighting.
  4. Drop the node.

Expected Result:

  • Same as above: the link is split, and node inserted between.

Test 3: Node without required ports

Steps:

  1. Drag a node that does not contain both in-0 and out-0 ports.
  2. Drop it on a valid link.

Expected Result:

  • No link is modified.
  • An error notification appears:
    ➤ “This component can’t connect — missing ports.”

Test 4: Node with existing connections

Steps:

  1. Create a node and connect its ports manually.
  2. Drag it over a link and drop it.

Expected Result:

  • Previous connections are removed.
  • Info notification appears:
    ➤ “One or more ports are already connected. Existing connection will be overwritten.”

Tested on:

  • Linux (Fedora)
  • Windows
  • macOS
  • Others

@github-actions

Copy link
Copy Markdown

Binder 👈 Launch a binder notebook on branch rabea-al/xircuits/feature/drop-node-on-link

@MFA-X-AI MFA-X-AI left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! This is one of the most requested feature, would be very helpful so users wouldn't have to disconnect and connect nodes when trying to have something between them.
One additional thing I'd like to request is for you to save the link point coordinates, and recreate them if a node is created. Consider a case like this where a user wired the link neatly

image

adding a node between will recreate the link from scratch

image

Thanks.

@rabea-al

Copy link
Copy Markdown
Contributor Author

Unified behavior for missing-port nodes dropped from canvas or sidebar

Previously, there was inconsistent behavior when dropping a component that lacks the required ports (in-0, out-0):

  • Dragging from sidebar:

    • ✅ Link highlights on hover
    • ✅ Drop is ignored
    • ✅ Error notification appears
  • Dragging from canvas:

    • ❌ No visual highlight
    • ❌ No notification
    • ✅ Drop is ignored silently

This commit ensures both cases behave the same:

  • The link still highlights
  • The drop is ignored
  • A proper error message appears

@rabea-al

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I've implemented the requested behavior — the link's bend-points are now preserved when a node is inserted between two connected nodes. This should maintain the original wire layout as expected.

@MFA-X-AI MFA-X-AI left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The implementation for getting the points seems very convoluted. In Storm React Diagram, a line object has Points as its property. You should be able to access them, save the coordinates and recreating them. Here's an example of how I did it.
https://github.com/XpressAI/xircuits/blob/master/src/commands/NodeActionCommands.tsx#L764-L799

@rabea-al

Copy link
Copy Markdown
Contributor Author

The implementation for getting the points seems very convoluted. In Storm React Diagram, a line object has Points as its property. You should be able to access them, save the coordinates and recreating them. Here's an example of how I did it. https://github.com/XpressAI/xircuits/blob/master/src/commands/NodeActionCommands.tsx#L764-L799

Thanks for the suggestion! I refactored the logic to use clone() instead of manually slicing and rebuilding the points.
// Also added clearAllNodeLinks() to ensure the dragged node is fully disconnected (both flow & data ports) before reconnecting.

@MFA-X-AI
MFA-X-AI merged commit f20ff4d into XpressAI:master Jun 19, 2025
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.

2 participants