refactor(react): simplify nodes selection

This commit is contained in:
moklick
2023-06-08 12:27:06 +02:00
parent 565489758b
commit 864dc262c2
@@ -14,24 +14,27 @@ import { arrowKeyDiffs } from '../Nodes/wrapNode';
import useUpdateNodePositions from '../../hooks/useUpdateNodePositions'; import useUpdateNodePositions from '../../hooks/useUpdateNodePositions';
import type { Node, ReactFlowState } from '../../types'; import type { Node, ReactFlowState } from '../../types';
export interface NodesSelectionProps { export type NodesSelectionProps = {
onSelectionContextMenu?: (event: MouseEvent, nodes: Node[]) => void; onSelectionContextMenu?: (event: MouseEvent, nodes: Node[]) => void;
noPanClassName?: string; noPanClassName?: string;
disableKeyboardA11y: boolean; disableKeyboardA11y: boolean;
} };
const selector = (s: ReactFlowState) => { const selector = (s: ReactFlowState) => {
const selectedNodes = s.getNodes().filter((n) => n.selected); const selectedNodes = s.getNodes().filter((n) => n.selected);
const { width, height, x, y } = getRectOfNodes(selectedNodes, s.nodeOrigin);
return { return {
...getRectOfNodes(selectedNodes, s.nodeOrigin), width,
transformString: `translate(${s.transform[0]}px,${s.transform[1]}px) scale(${s.transform[2]})`, height,
userSelectionActive: s.userSelectionActive, userSelectionActive: s.userSelectionActive,
transformString: `translate(${s.transform[0]}px,${s.transform[1]}px) scale(${s.transform[2]}) translate(${x}px,${y}px)`,
}; };
}; };
function NodesSelection({ onSelectionContextMenu, noPanClassName, disableKeyboardA11y }: NodesSelectionProps) { function NodesSelection({ onSelectionContextMenu, noPanClassName, disableKeyboardA11y }: NodesSelectionProps) {
const store = useStoreApi(); const store = useStoreApi();
const { width, height, x: left, y: top, transformString, userSelectionActive } = useStore(selector, shallow); const { width, height, transformString, userSelectionActive } = useStore(selector, shallow);
const updatePositions = useUpdateNodePositions(); const updatePositions = useUpdateNodePositions();
const nodeRef = useRef<HTMLDivElement>(null); const nodeRef = useRef<HTMLDivElement>(null);
@@ -88,8 +91,6 @@ function NodesSelection({ onSelectionContextMenu, noPanClassName, disableKeyboar
style={{ style={{
width, width,
height, height,
top,
left,
}} }}
/> />
</div> </div>