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

Trigger a mouse event every time the ReactFlow component is clicked #724

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions wren-ui/src/components/diagram/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ const ReactFlowDiagram = forwardRef(function ReactFlowDiagram(
setForceRender(!forceRender);
};

const triggerMouseDown = (event: React.MouseEvent | MouseEvent) => {
const mouseDownEvent = new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
clientX: event.clientX,
clientY: event.clientY,
button: 0,
});
document.dispatchEvent(mouseDownEvent);
};

const isTargetInDropdown = (target: EventTarget | null) => {
const dropdowns = document.querySelectorAll('.ant-dropdown');
return Array.from(dropdowns).some((dropdown) =>
dropdown.contains(target as Node),
);
};

const dispatchMouseEvent = (event: React.MouseEvent | MouseEvent) => {
if (!event.isTrusted || isTargetInDropdown(event.target)) return;
triggerMouseDown(event);
};

return (
<>
<DiagramContext.Provider value={{ onMoreClick, onNodeClick, onAddClick }}>
Expand All @@ -123,6 +146,7 @@ const ReactFlowDiagram = forwardRef(function ReactFlowDiagram(
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
maxZoom={1}
onPointerDown={(event) => dispatchMouseEvent(event)}
proOptions={{ hideAttribution: true }}
>
<MiniMap style={minimapStyle} zoomable pannable />
Expand Down