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

Use screenToFlowPosition() instead of project() #2459

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ export const RepresentativeNodeWrapper = memo(
const createNodeFromSelector = useCallback(() => {
if (!reactFlowWrapper.current) return;

const { height: wHeight, width } = reactFlowWrapper.current.getBoundingClientRect();

const position = reactFlowInstance.project({
x: width / 2,
y: wHeight / 2,
const {
height: wHeight,
width,
x,
y,
} = reactFlowWrapper.current.getBoundingClientRect();

const position = reactFlowInstance.screenToFlowPosition({
x: (width + x) / 2,
y: (wHeight + y) / 2,
});

createNode({
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/ReactFlowBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export const ReactFlowBox = memo(({ wrapperRef, nodeTypes, edgeTypes }: ReactFlo
// [setEdges]
// );

const { onConnectStart, onConnectStop, onPaneContextMenu } = usePaneNodeSearchMenu(wrapperRef);
const { onConnectStart, onConnectStop, onPaneContextMenu } = usePaneNodeSearchMenu();

const [selectedNodes, setSelectedNodes] = useState<Node<NodeData>[]>([]);
const selectionMenu = useNodesMenu(selectedNodes);
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/contexts/GlobalNodeState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const GlobalProvider = memo(
setNodes: rfSetNodes,
setEdges: rfSetEdges,
viewportInitialized,
project,
screenToFlowPosition,
} = useReactFlow<NodeData, EdgeData>();

const currentViewport = useViewport();
Expand Down Expand Up @@ -1231,8 +1231,14 @@ export const GlobalProvider = memo(
copyToClipboard(getNodes(), getEdges());
}, [getNodes, getEdges]);
const pasteFn = useCallback(() => {
pasteFromClipboard(changeNodes, changeEdges, createNode, project, reactFlowWrapper);
}, [changeNodes, changeEdges, createNode, project, reactFlowWrapper]);
pasteFromClipboard(
changeNodes,
changeEdges,
createNode,
screenToFlowPosition,
reactFlowWrapper
);
}, [changeNodes, changeEdges, createNode, screenToFlowPosition, reactFlowWrapper]);
const selectAllFn = useCallback(() => {
changeNodes((nodes) => nodes.map((n) => ({ ...n, selected: true })));
changeEdges((edges) => edges.map((e) => ({ ...e, selected: true })));
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/helpers/copyAndPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const pasteFromClipboard = (
setNodes: SetState<Node<NodeData>[]>,
setEdges: SetState<Edge<EdgeData>[]>,
createNode: (proto: NodeProto, parentId?: string) => void,
project: Project,
screenToFlowPosition: Project,
reactFlowWrapper: React.RefObject<Element>
) => {
const availableFormats = clipboard.availableFormats();
Expand Down Expand Up @@ -114,14 +114,14 @@ export const pasteFromClipboard = (
let positionX = 0;
let positionY = 0;
if (reactFlowWrapper.current) {
const { height, width } =
const { height, width, x, y } =
reactFlowWrapper.current.getBoundingClientRect();
positionX = width / 2;
positionY = height / 2;
positionX = (width + x) / 2;
positionY = (height + y) / 2;
}
createNode({
nodeType: 'regularNode',
position: project({ x: positionX, y: positionY }),
position: screenToFlowPosition({ x: positionX, y: positionY }),
data: {
schemaId: 'chainner:image:load' as SchemaId,
inputData: {
Expand Down
15 changes: 4 additions & 11 deletions src/renderer/hooks/usePaneNodeSearchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,7 @@ interface Position {
readonly y: number;
}

export const usePaneNodeSearchMenu = (
wrapperRef: React.RefObject<HTMLDivElement>
): UsePaneNodeSearchMenuValue => {
export const usePaneNodeSearchMenu = (): UsePaneNodeSearchMenuValue => {
const typeState = useContextSelector(GlobalVolatileContext, (c) => c.typeState);
const useConnectingFrom = useContextSelector(GlobalVolatileContext, (c) => c.useConnectingFrom);
const { createNode, createConnection } = useContext(GlobalContext);
Expand All @@ -393,7 +391,7 @@ export const usePaneNodeSearchMenu = (
const [connectingFrom, setConnectingFrom] = useState<OnConnectStartParams | null>(null);
const [, setGlobalConnectingFrom] = useConnectingFrom;

const { getNode, project, getNodes, getEdges } = useReactFlow();
const { getNode, screenToFlowPosition, getNodes, getEdges } = useReactFlow();

const [mousePosition, setMousePosition] = useState<Position>({ x: 0, y: 0 });

Expand Down Expand Up @@ -428,12 +426,8 @@ export const usePaneNodeSearchMenu = (

const onSchemaSelect = useCallback(
(schema: NodeSchema, target: ConnectionTarget) => {
const reactFlowBounds = wrapperRef.current!.getBoundingClientRect();
const { x, y } = mousePosition;
const projPosition = project({
x: x - reactFlowBounds.left,
y: y - reactFlowBounds.top,
});
const projPosition = screenToFlowPosition({ x, y });
const nodeId = createUniqueId();
createNode({
id: nodeId,
Expand Down Expand Up @@ -483,9 +477,8 @@ export const usePaneNodeSearchMenu = (
createNode,
functionDefinitions,
mousePosition,
project,
screenToFlowPosition,
setGlobalConnectingFrom,
wrapperRef,
]
);

Expand Down