Merge branch 'nested-nodes-flat' of github.com:wbkd/react-flow into nested-nodes-flat

This commit is contained in:
Christopher Möller
2021-10-27 12:53:36 +02:00
2 changed files with 13 additions and 11 deletions
+3 -3
View File
@@ -81,14 +81,14 @@ function Node({
const childRect = useMemo(() => getRectOfNodes(childNodes), [childNodes]); const childRect = useMemo(() => getRectOfNodes(childNodes), [childNodes]);
const isParentNode = !!childNodes.length; const isParentNode = !!childNodes.length;
node.style = useMemo(() => { const style = useMemo(() => {
if (isParentNode) { if (isParentNode) {
return { return {
...node.style, ...node.style,
width: Math.floor(childRect.width) + 20, width: Math.floor(childRect.width) + 20,
height: Math.floor(childRect.height) + 20, height: Math.floor(childRect.height) + 20,
boxSizing: 'border-box', boxSizing: 'border-box',
}; } as React.CSSProperties;
} }
return node.style; return node.style;
}, [childRect.width, childRect.height, isParentNode, node.style]); }, [childRect.width, childRect.height, isParentNode, node.style]);
@@ -109,7 +109,7 @@ function Node({
<NodeComponent <NodeComponent
id={node.id} id={node.id}
className={node.className} className={node.className}
style={node.style} style={style}
type={nodeType} type={nodeType}
data={node.data} data={node.data}
sourcePosition={node.sourcePosition} sourcePosition={node.sourcePosition}
+10 -8
View File
@@ -6,14 +6,16 @@ type Keys = Array<string>;
type PressedKeys = Set<string>; type PressedKeys = Set<string>;
type KeyOrCode = 'key' | 'code'; type KeyOrCode = 'key' | 'code';
export interface UseKeyPressOptions { export interface UseKeyPressOptions {
target: Document | HTMLElement | ShadowRoot; target: Document | HTMLElement | ShadowRoot | null;
} }
const doc = typeof document !== 'undefined' ? document : null;
// the keycode can be a string 'a' or an array of strings ['a', 'a+d'] // the keycode can be a string 'a' or an array of strings ['a', 'a+d']
// a string means a single key 'a' or a combination when '+' is used 'a+d' // a string means a single key 'a' or a combination when '+' is used 'a+d'
// an array means different possibilites. Explainer: ['a', 'd+s'] here the // an array means different possibilites. Explainer: ['a', 'd+s'] here the
// user can use the single key 'a' or the combination 'd' + 's' // user can use the single key 'a' or the combination 'd' + 's'
export default (keyCode: KeyCode | null = null, options: UseKeyPressOptions = { target: document }): boolean => { export default (keyCode: KeyCode | null = null, options: UseKeyPressOptions = { target: doc }): boolean => {
const [keyPressed, setKeyPressed] = useState(false); const [keyPressed, setKeyPressed] = useState(false);
// we need to remember the pressed keys in order to support combinations // we need to remember the pressed keys in order to support combinations
@@ -64,16 +66,16 @@ export default (keyCode: KeyCode | null = null, options: UseKeyPressOptions = {
setKeyPressed(false); setKeyPressed(false);
}; };
options.target.addEventListener('keydown', downHandler as EventListenerOrEventListenerObject); options?.target?.addEventListener('keydown', downHandler as EventListenerOrEventListenerObject);
options.target.addEventListener('keyup', upHandler as EventListenerOrEventListenerObject); options?.target?.addEventListener('keyup', upHandler as EventListenerOrEventListenerObject);
options.target.addEventListener('blur', resetHandler); options?.target?.addEventListener('blur', resetHandler);
return () => { return () => {
pressedKeys.current.clear(); pressedKeys.current.clear();
options.target.removeEventListener('keydown', downHandler as EventListenerOrEventListenerObject); options?.target?.removeEventListener('keydown', downHandler as EventListenerOrEventListenerObject);
options.target.removeEventListener('keyup', upHandler as EventListenerOrEventListenerObject); options?.target?.removeEventListener('keyup', upHandler as EventListenerOrEventListenerObject);
options.target.removeEventListener('blur', resetHandler); options?.target?.removeEventListener('blur', resetHandler);
}; };
} }
}, [keyCode, setKeyPressed]); }, [keyCode, setKeyPressed]);