Skip to content

Commit

Permalink
feat: Change to press and drag to create nodes [PT-185572242]
Browse files Browse the repository at this point in the history
This adds the ability to "drag" nodes from the tool palette along with keeping the click to create behavior
  • Loading branch information
dougmartin committed Jul 11, 2023
1 parent e626570 commit 0dbe2a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/components/drawing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ export const Drawing = (props: Props) => {
}
}, [drawingMode, addNode, handleSetSelectMode]);

// allow nodes to be "dragged" from the toolbar to the canvas
const handleMouseUp = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
if (drawingMode === "addNode") {
handleClicked(e);
e.preventDefault();
e.stopPropagation();
}
}, [drawingMode, handleClicked]);

const handleNodeClicked = useCallback((id: string, onLoop?: boolean) => {
const node = getNode(id);
if (!node) {
Expand Down Expand Up @@ -296,7 +305,7 @@ export const Drawing = (props: Props) => {
</button>
<button
title="Add State"
onClick={handleSetAddNodeMode}
onMouseDown={handleSetAddNodeMode}
className={clsx({selected: drawingMode === "addNode"})}
>
<AddNodeIcon />
Expand Down Expand Up @@ -328,6 +337,7 @@ export const Drawing = (props: Props) => {
autoArrange={false}
rubberBand={rubberBand}
onClick={handleClicked}
onMouseUp={handleMouseUp}
onNodeClick={handleNodeClicked}
onNodeDoubleClick={handleNodeDoubleClicked}
onEdgeClick={handleEdgeClicked}
Expand Down
11 changes: 9 additions & 2 deletions src/components/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Props = {
autoArrange: boolean;
rubberBand?: RubberBand;
onClick?: (e: React.MouseEvent<HTMLDivElement>) => void;
onMouseUp?: (e: React.MouseEvent<HTMLDivElement>) => void;
onNodeClick?: (id: string, onLoop?: boolean) => void;
onNodeDoubleClick?: (id: string) => void;
onEdgeClick?: (options: {from: string, to: string}) => void;
Expand Down Expand Up @@ -211,7 +212,7 @@ const calculateNodeFontSize = (d: D3Node) => {
export const Graph = (props: Props) => {
const {graph, highlightNode, highlightLoopOnNode, highlightEdge, highlightAllNextNodes,
highlightColor, allowDragging, autoArrange, mode, rubberBand,
onClick, onNodeClick, onNodeDoubleClick, onEdgeClick, onDragStop} = props;
onClick, onMouseUp, onNodeClick, onNodeDoubleClick, onEdgeClick, onDragStop} = props;
const svgRef = useRef<SVGSVGElement | null>(null);
const wrapperRef = useRef<HTMLDivElement | null>(null);
const dimensions = useResizeObserver(wrapperRef);
Expand Down Expand Up @@ -553,10 +554,16 @@ export const Graph = (props: Props) => {
}
}, [autoArrange, onClick]);

const handleMouseUp = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
if (!autoArrange && onMouseUp) {
onMouseUp(e);
}
}, [autoArrange, onMouseUp]);

const viewBox = mode === "drawing" ? `0 0 ${width} ${height}` : `${-width / 2} ${-height / 2} ${width} ${height}`;

return (
<div className="graph" ref={wrapperRef} onClick={handleClick}>
<div className="graph" ref={wrapperRef} onClick={handleClick} onMouseUp={handleMouseUp}>
<svg
width={width}
height={height}
Expand Down

0 comments on commit 0dbe2a2

Please sign in to comment.