feat(packages): add @reactflow/system and @reactflow/utils

This commit is contained in:
moklick
2023-02-16 21:54:09 +01:00
parent a12c893226
commit 3920fca670
90 changed files with 375 additions and 1152 deletions
+2 -2
View File
@@ -2,13 +2,13 @@ import { useEffect, useRef, useState } from 'react';
import type { RefObject, MouseEvent } from 'react';
import { drag } from 'd3-drag';
import { select } from 'd3-selection';
import { calcAutoPan, getEventPosition } from '@reactflow/utils';
import type { NodeDragItem, Node, SelectionDragHandler, UseDragEvent, XYPosition } from '@reactflow/system';
import { useStoreApi } from '../../hooks/useStore';
import { getDragItems, getEventHandlerParams, hasSelector, calcNextPosition } from './utils';
import { handleNodeClick } from '../../components/Nodes/utils';
import useGetPointerPosition from '../useGetPointerPosition';
import { calcAutoPan, getEventPosition } from '../../utils';
import type { NodeDragItem, Node, SelectionDragHandler, UseDragEvent, XYPosition } from '../../types';
export type UseDragData = { dx: number; dy: number };
+11 -5
View File
@@ -1,9 +1,15 @@
import type { RefObject } from 'react';
import { clampPosition, isNumeric } from '../../utils';
import type { CoordinateExtent, Node, NodeDragItem, NodeInternals, NodeOrigin, OnError, XYPosition } from '../../types';
import { getNodePositionWithOrigin } from '../../utils/graph';
import { errorMessages } from '../../contants';
import {
errorMessages,
type CoordinateExtent,
type Node,
type NodeDragItem,
type NodeInternals,
type NodeOrigin,
type OnError,
type XYPosition,
} from '@reactflow/system';
import { clampPosition, isNumeric, getNodePositionWithOrigin } from '@reactflow/utils';
export function isParentSelected(node: Node, nodeInternals: NodeInternals): boolean {
if (!node.parentNode) {
+2 -1
View File
@@ -1,5 +1,6 @@
import type { Edge, ReactFlowState } from '@reactflow/system';
import { useStore } from '../hooks/useStore';
import type { Edge, ReactFlowState } from '../types';
const edgesSelector = (state: ReactFlowState) => state.edges;
@@ -1,7 +1,7 @@
import { useCallback } from 'react';
import type { UseDragEvent } from '@reactflow/system';
import { useStoreApi } from './useStore';
import type { UseDragEvent } from '../types';
function useGetPointerPosition() {
const store = useStoreApi();
@@ -1,8 +1,8 @@
import { useEffect } from 'react';
import type { KeyCode } from '@reactflow/system';
import { useStoreApi } from '../hooks/useStore';
import useKeyPress from './useKeyPress';
import type { KeyCode } from '../types';
import useReactFlow from './useReactFlow';
interface HookParams {
deleteKeyCode: KeyCode | null;
+2 -3
View File
@@ -1,7 +1,6 @@
import { useState, useEffect, useRef, useMemo } from 'react';
import { isInputDOMNode } from '../utils';
import type { KeyCode } from '../types';
import { isInputDOMNode } from '@reactflow/utils';
import type { KeyCode } from '@reactflow/system';
type Keys = Array<string>;
type PressedKeys = Set<string>;
+1 -1
View File
@@ -1,5 +1,5 @@
import { useStore } from '../hooks/useStore';
import type { Node, ReactFlowState } from '../types';
import type { Node, ReactFlowState } from '@reactflow/system';
const nodesSelector = (state: ReactFlowState) => state.getNodes();
@@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useState, useCallback } from 'react';
import type { SetStateAction, Dispatch } from 'react';
import { useState, useCallback, type SetStateAction, type Dispatch } from 'react';
import type { Node, NodeChange, Edge, EdgeChange } from '@reactflow/system';
import { applyNodeChanges, applyEdgeChanges } from '../utils/changes';
import type { Node, NodeChange, Edge, EdgeChange } from '../types';
type ApplyChanges<ItemType, ChangesType> = (changes: ChangesType[], items: ItemType[]) => ItemType[];
type OnChange<ChangesType> = (changes: ChangesType[]) => void;
@@ -1,7 +1,6 @@
import { internalsSymbol } from '@reactflow/system';
import { internalsSymbol, type ReactFlowState } from '@reactflow/system';
import { useStore } from './useStore';
import type { ReactFlowState } from '../types';
const selector = (s: ReactFlowState) => {
if (s.nodeInternals.size === 0) {
+1 -1
View File
@@ -1,7 +1,7 @@
import { useEffect, useRef } from 'react';
import type { OnInit } from '@reactflow/system';
import useReactFlow from './useReactFlow';
import type { OnInit } from '../types';
function useOnInitHandler(onInit: OnInit | undefined) {
const rfInstance = useReactFlow();
@@ -1,7 +1,7 @@
import { useEffect } from 'react';
import type { OnSelectionChangeFunc } from '@reactflow/system';
import { useStoreApi } from './useStore';
import type { OnSelectionChangeFunc } from '../types';
export type UseOnSelectionChangeOptions = {
onChange?: OnSelectionChangeFunc;
@@ -1,7 +1,7 @@
import { useEffect } from 'react';
import type { OnViewportChange } from '@reactflow/system';
import { useStoreApi } from './useStore';
import type { OnViewportChange } from '../types';
export type UseOnViewportChangeOptions = {
onStart?: OnViewportChange;
+5 -6
View File
@@ -1,7 +1,5 @@
import { useCallback, useMemo } from 'react';
import useViewportHelper from './useViewportHelper';
import { useStoreApi } from '../hooks/useStore';
import { getConnectedEdges, getOverlappingArea, isRectObject, nodeToRect } from '@reactflow/utils';
import type {
ReactFlowInstance,
Instance,
@@ -14,9 +12,10 @@ import type {
NodeChange,
Node,
Rect,
} from '../types';
import { getConnectedEdges } from '../utils/graph';
import { getOverlappingArea, isRectObject, nodeToRect } from '../utils';
} from '@reactflow/system';
import useViewportHelper from './useViewportHelper';
import { useStoreApi } from '../hooks/useStore';
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlowInstance<NodeData, EdgeData> {
+3 -4
View File
@@ -1,9 +1,8 @@
import { useEffect } from 'react';
import type { MutableRefObject } from 'react';
import { useEffect, type MutableRefObject } from 'react';
import { errorMessages } from '@reactflow/system';
import { getDimensions } from '@reactflow/utils';
import { useStoreApi } from '../hooks/useStore';
import { getDimensions } from '../utils';
import { errorMessages } from '../contants';
function useResizeHandler(rendererNode: MutableRefObject<HTMLDivElement | null>): void {
const store = useStoreApi();
+2 -4
View File
@@ -1,10 +1,8 @@
import { useContext, useMemo } from 'react';
import { useStore as useZustandStore } from 'zustand';
import type { StoreApi } from 'zustand';
import { useStore as useZustandStore, type StoreApi } from 'zustand';
import { errorMessages, type ReactFlowState } from '@reactflow/system';
import StoreContext from '../contexts/RFStoreContext';
import { errorMessages } from '../contants';
import type { ReactFlowState } from '../types';
const zustandErrorMessage = errorMessages['001']();
@@ -1,7 +1,7 @@
import { useCallback } from 'react';
import type { UpdateNodeInternals } from '@reactflow/system';
import { useStoreApi } from '../hooks/useStore';
import type { UpdateNodeInternals } from '../types';
function useUpdateNodeInternals(): UpdateNodeInternals {
const store = useStoreApi();
+1 -1
View File
@@ -1,7 +1,7 @@
import { shallow } from 'zustand/shallow';
import type { Viewport, ReactFlowState } from '@reactflow/system';
import { useStore } from '../hooks/useStore';
import type { Viewport, ReactFlowState } from '../types';
const viewportSelector = (state: ReactFlowState) => ({
x: state.transform[0],
+2 -2
View File
@@ -1,11 +1,11 @@
import { useMemo } from 'react';
import { zoomIdentity } from 'd3-zoom';
import { shallow } from 'zustand/shallow';
import { pointToRendererPoint, getTransformForBounds, getD3Transition } from '@reactflow/utils';
import type { ViewportHelperFunctions, ReactFlowState, XYPosition } from '@reactflow/system';
import { useStoreApi, useStore } from '../hooks/useStore';
import { pointToRendererPoint, getTransformForBounds, getD3Transition } from '../utils/graph';
import { fitView } from '../store/utils';
import type { ViewportHelperFunctions, ReactFlowState, XYPosition } from '../types';
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
+2 -3
View File
@@ -1,10 +1,9 @@
import { useCallback } from 'react';
import { internalsSymbol } from '@reactflow/system';
import { internalsSymbol, type ReactFlowState, type NodeInternals, type Edge } from '@reactflow/system';
import { isNumeric } from '@reactflow/utils';
import { useStore } from '../hooks/useStore';
import { isEdgeVisible } from '../container/EdgeRenderer/utils';
import { isNumeric } from '../utils';
import type { ReactFlowState, NodeInternals, Edge } from '../types';
const defaultEdgeTree = [{ level: 0, isMaxLevel: true, edges: [] }];
+2 -2
View File
@@ -1,8 +1,8 @@
import { useCallback } from 'react';
import { getNodesInside } from '@reactflow/utils';
import type { ReactFlowState } from '@reactflow/system';
import { useStore } from '../hooks/useStore';
import { getNodesInside } from '../utils/graph';
import type { ReactFlowState } from '../types';
function useVisibleNodes(onlyRenderVisible: boolean) {
const nodes = useStore(