refactor(utils): remove react dependencies

This commit is contained in:
moklick
2023-03-02 14:32:39 +01:00
parent 3b83e029e6
commit df6c3381b1
7 changed files with 19 additions and 49 deletions
@@ -59,7 +59,7 @@ export function handlePointerDown({
let autoPanId = 0; let autoPanId = 0;
let prevClosestHandle: ConnectionHandle | null; let prevClosestHandle: ConnectionHandle | null;
const { x, y } = getEventPosition(event); const { x, y } = getEventPosition(event.nativeEvent);
const clickedHandle = doc?.elementFromPoint(x, y); const clickedHandle = doc?.elementFromPoint(x, y);
const handleType = getHandleType(edgeUpdaterType, clickedHandle); const handleType = getHandleType(edgeUpdaterType, clickedHandle);
const containerBounds = domNode?.getBoundingClientRect(); const containerBounds = domNode?.getBoundingClientRect();
@@ -69,7 +69,7 @@ export function handlePointerDown({
} }
let prevActiveHandle: Element; let prevActiveHandle: Element;
let connectionPosition = getEventPosition(event, containerBounds); let connectionPosition = getEventPosition(event.nativeEvent, containerBounds);
let autoPanStarted = false; let autoPanStarted = false;
let connection: Connection | null = null; let connection: Connection | null = null;
let isValid = false; let isValid = false;
@@ -69,9 +69,9 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
}; };
const onPointerDown = (event: ReactMouseEvent<HTMLDivElement> | ReactTouchEvent<HTMLDivElement>) => { const onPointerDown = (event: ReactMouseEvent<HTMLDivElement> | ReactTouchEvent<HTMLDivElement>) => {
const isMouseTriggered = isMouseEvent(event); const isMouseTriggered = isMouseEvent(event.nativeEvent);
if ((isMouseTriggered && event.button === 0) || !isMouseTriggered) { if ((isMouseTriggered && (event as ReactMouseEvent<HTMLDivElement>).button === 0) || !isMouseTriggered) {
handlePointerDown({ handlePointerDown({
event, event,
handleId, handleId,
@@ -85,9 +85,9 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
} }
if (isMouseTriggered) { if (isMouseTriggered) {
onMouseDown?.(event); onMouseDown?.(event as ReactMouseEvent<HTMLDivElement>);
} else { } else {
onTouchStart?.(event); onTouchStart?.(event as ReactTouchEvent<HTMLDivElement>);
} }
}; };
@@ -101,7 +101,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
const doc = getHostForElement(event.target as HTMLElement); const doc = getHostForElement(event.target as HTMLElement);
const { connection, isValid } = isValidHandle( const { connection, isValid } = isValidHandle(
event, event.nativeEvent,
{ {
nodeId, nodeId,
id: handleId, id: handleId,
+1 -1
View File
@@ -73,7 +73,7 @@ const nullConnection: Connection = { source: null, target: null, sourceHandle: n
// checks if and returns connection in fom of an object { source: 123, target: 312 } // checks if and returns connection in fom of an object { source: 123, target: 312 }
export function isValidHandle( export function isValidHandle(
event: MouseEvent | TouchEvent | ReactMouseEvent | ReactTouchEvent, event: MouseEvent | TouchEvent,
handle: Pick<ConnectionHandle, 'nodeId' | 'id' | 'type'> | null, handle: Pick<ConnectionHandle, 'nodeId' | 'id' | 'type'> | null,
connectionMode: ConnectionMode, connectionMode: ConnectionMode,
fromNodeId: string, fromNodeId: string,
@@ -21,7 +21,6 @@
const handleId = id || null; const handleId = id || null;
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
const { const {
connectionMode, connectionMode,
domNode, domNode,
@@ -34,13 +33,13 @@
updateConnection updateConnection
} = useStore(); } = useStore();
function dispatchEvent(eventName: string) { function dispatchEvent(eventName: string, params?: Connection) {
dispatch(eventName, { nodeId, handleId, type }); dispatch(eventName, params || { nodeId, handleId, type });
} }
function onConnectExtended(params: Connection) { function onConnectExtended(params: Connection) {
addEdge(params); addEdge(params);
dispatchEvent('connect') dispatchEvent('connect', params)
} }
function onPointerDown(event: MouseEvent | TouchEvent) { function onPointerDown(event: MouseEvent | TouchEvent) {
@@ -66,12 +65,6 @@
onConnectEnd: () => dispatchEvent('connect:end') onConnectEnd: () => dispatchEvent('connect:end')
}); });
} }
// if (isMouseTriggered) {
// onMouseDown?.(event);
// } else {
// onTouchStart?.(event);
// }
} }
</script> </script>
+2 -4
View File
@@ -3,14 +3,12 @@
"version": "11.5.5", "version": "11.5.5",
"description": "Core system of React Flow.", "description": "Core system of React Flow.",
"keywords": [ "keywords": [
"react",
"svelte",
"node-based UI", "node-based UI",
"graph", "graph",
"diagram", "diagram",
"workflow", "workflow",
"react-flow", "reactflow",
"svelte-flow" "svelteflow"
], ],
"files": [ "files": [
"dist" "dist"
+2 -9
View File
@@ -3,12 +3,12 @@
"version": "11.5.5", "version": "11.5.5",
"description": "Core utils of React Flow.", "description": "Core utils of React Flow.",
"keywords": [ "keywords": [
"react",
"node-based UI", "node-based UI",
"graph", "graph",
"diagram", "diagram",
"workflow", "workflow",
"react-flow" "reactflow",
"svelteflow"
], ],
"files": [ "files": [
"dist" "dist"
@@ -43,18 +43,11 @@
"@types/d3-zoom": "^3.0.1", "@types/d3-zoom": "^3.0.1",
"d3-zoom": "^3.0.0" "d3-zoom": "^3.0.0"
}, },
"peerDependencies": {
"react": ">=17",
"react-dom": ">=17"
},
"devDependencies": { "devDependencies": {
"@reactflow/eslint-config": "workspace:*", "@reactflow/eslint-config": "workspace:*",
"@reactflow/rollup-config": "workspace:*", "@reactflow/rollup-config": "workspace:*",
"@reactflow/tsconfig": "workspace:*", "@reactflow/tsconfig": "workspace:*",
"@types/node": "^18.7.16", "@types/node": "^18.7.16",
"@types/react": ">=17",
"@types/react-dom": ">=17",
"react": "^18.2.0",
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
"rollup": { "rollup": {
+4 -18
View File
@@ -1,8 +1,3 @@
import type {
KeyboardEvent as ReactKeyboardEvent,
MouseEvent as ReactMouseEvent,
TouchEvent as ReactTouchEvent,
} from 'react';
import type { Dimensions, XYPosition, CoordinateExtent, Box, Rect, BaseNode, BaseEdge } from '@reactflow/system'; import type { Dimensions, XYPosition, CoordinateExtent, Box, Rect, BaseNode, BaseEdge } from '@reactflow/system';
import { getConnectedEdgesBase } from './graph'; import { getConnectedEdgesBase } from './graph';
@@ -93,13 +88,9 @@ export const devWarn = (id: string, message: string) => {
} }
}; };
const isReactKeyboardEvent = (event: KeyboardEvent | ReactKeyboardEvent): event is ReactKeyboardEvent => export function isInputDOMNode(event: KeyboardEvent): boolean {
'nativeEvent' in event;
export function isInputDOMNode(event: KeyboardEvent | ReactKeyboardEvent): boolean {
const kbEvent = isReactKeyboardEvent(event) ? event.nativeEvent : event;
// using composed path for handling shadow dom // using composed path for handling shadow dom
const target = (kbEvent.composedPath?.()?.[0] || event.target) as HTMLElement; const target = (event.composedPath?.()?.[0] || event.target) as HTMLElement;
const isInput = ['INPUT', 'SELECT', 'TEXTAREA'].includes(target?.nodeName) || target?.hasAttribute('contenteditable'); const isInput = ['INPUT', 'SELECT', 'TEXTAREA'].includes(target?.nodeName) || target?.hasAttribute('contenteditable');
// we want to be able to do a multi selection event if we are in an input field // we want to be able to do a multi selection event if we are in an input field
@@ -109,14 +100,9 @@ export function isInputDOMNode(event: KeyboardEvent | ReactKeyboardEvent): boole
return (isInput && !isModifierKey) || !!target?.closest('.nokey'); return (isInput && !isModifierKey) || !!target?.closest('.nokey');
} }
export const isMouseEvent = ( export const isMouseEvent = (event: MouseEvent | TouchEvent): event is MouseEvent => 'clientX' in event;
event: MouseEvent | ReactMouseEvent | TouchEvent | ReactTouchEvent
): event is MouseEvent | ReactMouseEvent => 'clientX' in event;
export const getEventPosition = ( export const getEventPosition = (event: MouseEvent | TouchEvent, bounds?: DOMRect) => {
event: MouseEvent | ReactMouseEvent | TouchEvent | ReactTouchEvent,
bounds?: DOMRect
) => {
const isMouseTriggered = isMouseEvent(event); const isMouseTriggered = isMouseEvent(event);
const evtX = isMouseTriggered ? event.clientX : event.touches?.[0].clientX; const evtX = isMouseTriggered ? event.clientX : event.touches?.[0].clientX;
const evtY = isMouseTriggered ? event.clientY : event.touches?.[0].clientY; const evtY = isMouseTriggered ? event.clientY : event.touches?.[0].clientY;