chore(node-resizer): cleanup

This commit is contained in:
moklick
2024-01-09 12:30:29 +01:00
parent 0cbfcb5c7b
commit 616fb14c34
15 changed files with 62 additions and 71 deletions
@@ -15,6 +15,7 @@
'interaction', 'interaction',
'intersections', 'intersections',
'node-toolbar', 'node-toolbar',
'node-resizer',
'overview', 'overview',
'stress', 'stress',
'subflows', 'subflows',
@@ -22,8 +23,7 @@
'usenodesdata', 'usenodesdata',
'usesvelteflow', 'usesvelteflow',
'useupdatenodeinternals', 'useupdatenodeinternals',
'validation', 'validation'
'node-resizer'
]; ];
const onChange = (event: Event) => { const onChange = (event: Event) => {
-3
View File
@@ -48,7 +48,6 @@
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"@types/d3": "^7.4.0",
"@xyflow/system": "workspace:*", "@xyflow/system": "workspace:*",
"classcat": "^5.0.3", "classcat": "^5.0.3",
"zustand": "^4.4.0" "zustand": "^4.4.0"
@@ -78,8 +77,6 @@
"rollup": { "rollup": {
"globals": { "globals": {
"classcat": "cc", "classcat": "cc",
"d3-selection": "d3Selection",
"d3-drag": "d3Drag",
"zustand": "zustand", "zustand": "zustand",
"zustand/shallow": "zustandShallow" "zustand/shallow": "zustandShallow"
}, },
@@ -1,15 +1,16 @@
import { useRef, useEffect, memo } from 'react'; import { useRef, useEffect, memo } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import { XYResizer, ResizerControlVariant, type XYResizerInstance, type XYResizerChange } from '@xyflow/system'; import { XYResizer, ResizeControlVariant, type XYResizerInstance, type XYResizerChange } from '@xyflow/system';
import { useStoreApi } from '../../hooks/useStore'; import { useStoreApi } from '../../hooks/useStore';
import { useNodeId } from '../../contexts/NodeIdContext'; import { useNodeId } from '../../contexts/NodeIdContext';
import type { NodeChange, NodeDimensionChange, NodePositionChange } from '../../types'; import type { NodeChange, NodeDimensionChange, NodePositionChange } from '../../types';
import { type ResizeControlProps, type ResizeControlLineProps } from './types'; import type { ResizeControlProps, ResizeControlLineProps } from './types';
function ResizeControl({ function ResizeControl({
nodeId, nodeId,
position, position,
variant = ResizerControlVariant.Handle, variant = ResizeControlVariant.Handle,
className, className,
style = {}, style = {},
children, children,
@@ -28,7 +29,7 @@ function ResizeControl({
const id = typeof nodeId === 'string' ? nodeId : contextNodeId; const id = typeof nodeId === 'string' ? nodeId : contextNodeId;
const store = useStoreApi(); const store = useStoreApi();
const resizeControlRef = useRef<HTMLDivElement>(null); const resizeControlRef = useRef<HTMLDivElement>(null);
const defaultPosition = variant === ResizerControlVariant.Line ? 'right' : 'bottom-right'; const defaultPosition = variant === ResizeControlVariant.Line ? 'right' : 'bottom-right';
const controlPosition = position ?? defaultPosition; const controlPosition = position ?? defaultPosition;
const resizer = useRef<XYResizerInstance | null>(null); const resizer = useRef<XYResizerInstance | null>(null);
@@ -128,7 +129,7 @@ function ResizeControl({
]); ]);
const positionClassNames = controlPosition.split('-'); const positionClassNames = controlPosition.split('-');
const colorStyleProp = variant === ResizerControlVariant.Line ? 'borderColor' : 'backgroundColor'; const colorStyleProp = variant === ResizeControlVariant.Line ? 'borderColor' : 'backgroundColor';
const controlStyle = color ? { ...style, [colorStyleProp]: color } : style; const controlStyle = color ? { ...style, [colorStyleProp]: color } : style;
return ( return (
@@ -143,7 +144,7 @@ function ResizeControl({
} }
export function ResizeControlLine(props: ResizeControlLineProps) { export function ResizeControlLine(props: ResizeControlLineProps) {
return <ResizeControl {...props} variant={ResizerControlVariant.Line} />; return <ResizeControl {...props} variant={ResizeControlVariant.Line} />;
} }
export default memo(ResizeControl); export const NodeResizeControl = memo(ResizeControl);
@@ -1,8 +1,9 @@
import ResizeControl from './ResizeControl'; import { ResizeControlVariant, XY_RESIZER_HANDLE_POSITIONS, XY_RESIZER_LINE_POSITIONS } from '@xyflow/system';
import { NodeResizerProps } from './types';
import { ResizerControlVariant, XY_RESIZER_HANDLE_POSITIONS, XY_RESIZER_LINE_POSITIONS } from '@xyflow/system';
export default function NodeResizer({ import { NodeResizeControl } from './NodeResizeControl';
import type { NodeResizerProps } from './types';
export function NodeResizer({
nodeId, nodeId,
isVisible = true, isVisible = true,
handleClassName, handleClassName,
@@ -27,13 +28,13 @@ export default function NodeResizer({
return ( return (
<> <>
{XY_RESIZER_LINE_POSITIONS.map((position) => ( {XY_RESIZER_LINE_POSITIONS.map((position) => (
<ResizeControl <NodeResizeControl
key={position} key={position}
className={lineClassName} className={lineClassName}
style={lineStyle} style={lineStyle}
nodeId={nodeId} nodeId={nodeId}
position={position} position={position}
variant={ResizerControlVariant.Line} variant={ResizeControlVariant.Line}
color={color} color={color}
minWidth={minWidth} minWidth={minWidth}
minHeight={minHeight} minHeight={minHeight}
@@ -47,7 +48,7 @@ export default function NodeResizer({
/> />
))} ))}
{XY_RESIZER_HANDLE_POSITIONS.map((position) => ( {XY_RESIZER_HANDLE_POSITIONS.map((position) => (
<ResizeControl <NodeResizeControl
key={position} key={position}
className={handleClassName} className={handleClassName}
style={handleStyle} style={handleStyle}
@@ -1,4 +1,4 @@
export { default as NodeResizer } from './NodeResizer'; export { NodeResizer } from './NodeResizer';
export { default as NodeResizeControl } from './ResizeControl'; export { NodeResizeControl } from './NodeResizeControl';
export * from './types'; export * from './types';
@@ -1,7 +1,7 @@
import type { CSSProperties, ReactNode } from 'react'; import type { CSSProperties, ReactNode } from 'react';
import type { import type {
XYResizeControlPosition, ControlPosition,
XYResizeControlLinePosition, ControlLinePosition,
ResizeControlVariant, ResizeControlVariant,
ShouldResize, ShouldResize,
OnResizeStart, OnResizeStart,
@@ -42,7 +42,7 @@ export type ResizeControlProps = Pick<
| 'onResize' | 'onResize'
| 'onResizeEnd' | 'onResizeEnd'
> & { > & {
position?: XYResizeControlPosition; position?: ControlPosition;
variant?: ResizeControlVariant; variant?: ResizeControlVariant;
className?: string; className?: string;
style?: CSSProperties; style?: CSSProperties;
@@ -50,5 +50,5 @@ export type ResizeControlProps = Pick<
}; };
export type ResizeControlLineProps = ResizeControlProps & { export type ResizeControlLineProps = ResizeControlProps & {
position?: XYResizeControlLinePosition; position?: ControlLinePosition;
}; };
+7
View File
@@ -81,6 +81,13 @@ export {
type ColorModeClass, type ColorModeClass,
type HandleType, type HandleType,
type OnBeforeDelete, type OnBeforeDelete,
type ShouldResize,
type OnResizeStart,
type OnResize,
type OnResizeEnd,
type ControlPosition,
type ControlLinePosition,
type ResizeControlVariant,
} from '@xyflow/system'; } from '@xyflow/system';
// system utils // system utils
+8 -1
View File
@@ -90,7 +90,14 @@ export {
type Transform, type Transform,
type CoordinateExtent, type CoordinateExtent,
type ColorMode, type ColorMode,
type ColorModeClass type ColorModeClass,
type ShouldResize,
type OnResizeStart,
type OnResize,
type OnResizeEnd,
type ControlPosition,
type ControlLinePosition,
type ResizeControlVariant
} from '@xyflow/system'; } from '@xyflow/system';
// system utils // system utils
@@ -2,7 +2,7 @@
import ResizeControl from './ResizeControl.svelte'; import ResizeControl from './ResizeControl.svelte';
import type { NodeResizerProps } from './types'; import type { NodeResizerProps } from './types';
import { import {
ResizerControlVariant, ResizeControlVariant,
XY_RESIZER_HANDLE_POSITIONS, XY_RESIZER_HANDLE_POSITIONS,
XY_RESIZER_LINE_POSITIONS XY_RESIZER_LINE_POSITIONS
} from '@xyflow/system'; } from '@xyflow/system';
@@ -39,7 +39,7 @@
style={lineStyle} style={lineStyle}
{nodeId} {nodeId}
{position} {position}
variant={ResizerControlVariant.Line} variant={ResizeControlVariant.Line}
{color} {color}
minWidth={_minWidth} minWidth={_minWidth}
minHeight={_minHeight} minHeight={_minHeight}
@@ -4,8 +4,8 @@
import { useStore } from '$lib/store'; import { useStore } from '$lib/store';
import { import {
XYResizer, XYResizer,
ResizerControlVariant, ResizeControlVariant,
type XYResizerControlPosition, type ControlPosition,
type XYResizerInstance, type XYResizerInstance,
type XYResizerChange type XYResizerChange
} from '@xyflow/system'; } from '@xyflow/system';
@@ -15,7 +15,7 @@
export let nodeId: $$Props['nodeId'] = undefined; export let nodeId: $$Props['nodeId'] = undefined;
export let position: $$Props['position'] = undefined; export let position: $$Props['position'] = undefined;
export let variant: $$Props['variant'] = ResizerControlVariant.Handle; export let variant: $$Props['variant'] = ResizeControlVariant.Handle;
export let color: $$Props['color'] = undefined; export let color: $$Props['color'] = undefined;
export let minWidth: $$Props['minWidth'] = 10; export let minWidth: $$Props['minWidth'] = 10;
$: _minWidth = minWidth ?? 10; $: _minWidth = minWidth ?? 10;
@@ -43,13 +43,13 @@
let resizer: XYResizerInstance | null = null; let resizer: XYResizerInstance | null = null;
$: defaultPosition = ( $: defaultPosition = (
variant === ResizerControlVariant.Line ? 'right' : 'bottom-right' variant === ResizeControlVariant.Line ? 'right' : 'bottom-right'
) as XYResizerControlPosition; ) as ControlPosition;
$: controlPosition = position ?? defaultPosition; $: controlPosition = position ?? defaultPosition;
$: positionClassNames = controlPosition.split('-'); $: positionClassNames = controlPosition.split('-');
$: colorStyleProp = variant === ResizerControlVariant.Line ? 'border-color' : 'background-color'; $: colorStyleProp = variant === ResizeControlVariant.Line ? 'border-color' : 'background-color';
$: _style = style ?? ''; $: _style = style ?? '';
$: controlStyle = !!color ? `${_style} ${colorStyleProp}: ${color};` : _style; $: controlStyle = !!color ? `${_style} ${colorStyleProp}: ${color};` : _style;
@@ -1,10 +1,10 @@
import type { import type {
XYResizeParams, ControlPosition,
XYResizeParamsWithDirection,
XYResizeControlPosition,
ResizeControlVariant, ResizeControlVariant,
D3DragEvent, ShouldResize,
SubjectPosition OnResizeStart,
OnResize,
OnResizeEnd
} from '@xyflow/system'; } from '@xyflow/system';
export type NodeResizerProps = { export type NodeResizerProps = {
@@ -40,18 +40,8 @@ export type ResizeControlProps = Pick<
| 'onResize' | 'onResize'
| 'onResizeEnd' | 'onResizeEnd'
> & { > & {
position?: XYResizeControlPosition; position?: ControlPosition;
variant?: ResizeControlVariant; variant?: ResizeControlVariant;
class?: string; class?: string;
style?: string; style?: string;
}; };
type OnResizeHandler<Params = XYResizeParams, Result = void> = (
event: ResizeDragEvent,
params: Params
) => Result;
export type ResizeDragEvent = D3DragEvent<HTMLDivElement, null, SubjectPosition>;
export type ShouldResize = OnResizeHandler<XYResizeParamsWithDirection, boolean>;
export type OnResizeStart = OnResizeHandler;
export type OnResize = OnResizeHandler<XYResizeParamsWithDirection>;
export type OnResizeEnd = OnResizeHandler;
-2
View File
@@ -1,5 +1,3 @@
import type { Optional } from '../utils/types';
export enum Position { export enum Position {
Left = 'left', Left = 'left',
Top = 'top', Top = 'top',
+2 -2
View File
@@ -2,7 +2,7 @@ import { drag } from 'd3-drag';
import { select } from 'd3-selection'; import { select } from 'd3-selection';
import { NodeLookup, Transform } from '../types'; import { NodeLookup, Transform } from '../types';
import { OnResize, OnResizeEnd, OnResizeStart, ResizeDragEvent, ShouldResize, XYResizerControlPosition } from './types'; import { OnResize, OnResizeEnd, OnResizeStart, ResizeDragEvent, ShouldResize, ControlPosition } from './types';
import { getControlDirection, getDimensionsAfterResize, getPositionAfterResize, getResizeDirection } from './utils'; import { getControlDirection, getDimensionsAfterResize, getPositionAfterResize, getResizeDirection } from './utils';
import { getPointerPosition } from '../utils'; import { getPointerPosition } from '../utils';
@@ -51,7 +51,7 @@ type XYResizerParams = {
}; };
type XYResizerUpdateParams = { type XYResizerUpdateParams = {
controlPosition: XYResizerControlPosition; controlPosition: ControlPosition;
boundries: { boundries: {
minWidth: number; minWidth: number;
minHeight: number; minHeight: number;
+5 -15
View File
@@ -11,27 +11,17 @@ export type XYResizerParamsWithDirection = XYResizerParams & {
direction: number[]; direction: number[];
}; };
export type XYResizerControlLinePosition = 'top' | 'bottom' | 'left' | 'right'; export type ControlLinePosition = 'top' | 'bottom' | 'left' | 'right';
export type XYResizerControlPosition = export type ControlPosition = ControlLinePosition | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
| XYResizerControlLinePosition
| 'top-left'
| 'top-right'
| 'bottom-left'
| 'bottom-right';
export enum ResizerControlVariant { export enum ResizeControlVariant {
Line = 'line', Line = 'line',
Handle = 'handle', Handle = 'handle',
} }
export const XY_RESIZER_HANDLE_POSITIONS: XYResizerControlPosition[] = [ export const XY_RESIZER_HANDLE_POSITIONS: ControlPosition[] = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
'top-left', export const XY_RESIZER_LINE_POSITIONS: ControlLinePosition[] = ['top', 'right', 'bottom', 'left'];
'top-right',
'bottom-left',
'bottom-right',
];
export const XY_RESIZER_LINE_POSITIONS: XYResizerControlLinePosition[] = ['top', 'right', 'bottom', 'left'];
type OnResizeHandler<Params = XYResizerParams, Result = void> = (event: ResizeDragEvent, params: Params) => Result; type OnResizeHandler<Params = XYResizerParams, Result = void> = (event: ResizeDragEvent, params: Params) => Result;
export type ResizeDragEvent = D3DragEvent<HTMLDivElement, null, SubjectPosition>; export type ResizeDragEvent = D3DragEvent<HTMLDivElement, null, SubjectPosition>;
+2 -2
View File
@@ -1,5 +1,5 @@
import { clamp, getPointerPosition } from '../utils'; import { clamp, getPointerPosition } from '../utils';
import { XYResizerControlPosition } from './types'; import { ControlPosition } from './types';
type GetResizeDirectionParams = { type GetResizeDirectionParams = {
width: number; width: number;
@@ -48,7 +48,7 @@ export function getResizeDirection({
* @param controlPosition - position of the control that is being dragged * @param controlPosition - position of the control that is being dragged
* @returns isHorizontal, isVertical, affectsX, affectsY, * @returns isHorizontal, isVertical, affectsX, affectsY,
*/ */
export function getControlDirection(controlPosition: XYResizerControlPosition) { export function getControlDirection(controlPosition: ControlPosition) {
const isHorizontal = controlPosition.includes('right') || controlPosition.includes('left'); const isHorizontal = controlPosition.includes('right') || controlPosition.includes('left');
const isVertical = controlPosition.includes('bottom') || controlPosition.includes('top'); const isVertical = controlPosition.includes('bottom') || controlPosition.includes('top');
const affectsX = controlPosition.includes('left'); const affectsX = controlPosition.includes('left');