chore(edge-toolbar): cleanup
This commit is contained in:
@@ -1,19 +1,39 @@
|
|||||||
import { getBezierPath, BaseEdge, EdgeProps, useReactFlow } from '@xyflow/react';
|
import { getBezierPath, BaseEdge, EdgeProps, useReactFlow, getStraightPath, getSmoothStepPath } from '@xyflow/react';
|
||||||
import { EdgeToolbar } from '@xyflow/react';
|
import { EdgeToolbar } from '@xyflow/react';
|
||||||
|
|
||||||
export function CustomEdge({ id, data, ...props }: EdgeProps) {
|
const getPath = (props: EdgeProps) => {
|
||||||
const [edgePath, labelX, labelY] = getBezierPath(props);
|
switch (props.data!.type) {
|
||||||
|
case 'smoothstep':
|
||||||
|
return getSmoothStepPath(props);
|
||||||
|
case 'straight':
|
||||||
|
return getStraightPath(props);
|
||||||
|
default:
|
||||||
|
return getBezierPath(props);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export function CustomEdge(props: EdgeProps) {
|
||||||
|
const [edgePath, centerX, centerY] = getPath(props);
|
||||||
const { setEdges } = useReactFlow();
|
const { setEdges } = useReactFlow();
|
||||||
|
|
||||||
const deleteEdge = () => {
|
const deleteEdge = () => {
|
||||||
setEdges((edges) => edges.filter((edge) => edge.id !== id));
|
setEdges((edges) => edges.filter((edge) => edge.id !== props.id));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<BaseEdge id={id} path={edgePath} />
|
<BaseEdge id={props.id} path={edgePath} />
|
||||||
<EdgeToolbar edgeId={id} x={labelX} y={labelY} isVisible>
|
<EdgeToolbar
|
||||||
<button onClick={deleteEdge}>Delete</button>
|
edgeId={props.id}
|
||||||
|
x={centerX + 0}
|
||||||
|
y={centerY + 0}
|
||||||
|
alignX={props.data?.align?.[0] ?? 'center'}
|
||||||
|
alignY={props.data?.align?.[1] ?? 'center'}
|
||||||
|
isVisible
|
||||||
|
>
|
||||||
|
<button style={{}} onClick={deleteEdge}>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
</EdgeToolbar>
|
</EdgeToolbar>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import {
|
|||||||
EdgeTypes,
|
EdgeTypes,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
Node,
|
Node,
|
||||||
NodeOrigin,
|
|
||||||
Position,
|
Position,
|
||||||
ReactFlow,
|
ReactFlow,
|
||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
@@ -44,24 +43,24 @@ const initialEdges: Edge[] = [
|
|||||||
source: '1',
|
source: '1',
|
||||||
target: '2',
|
target: '2',
|
||||||
type: 'custom',
|
type: 'custom',
|
||||||
|
data: { type: 'smoothstep', align: ['left', 'bottom'] },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e3-2',
|
id: 'e3-2',
|
||||||
source: '3',
|
source: '3',
|
||||||
target: '2',
|
target: '2',
|
||||||
type: 'custom',
|
type: 'custom',
|
||||||
|
data: { type: 'bezier', align: ['right', 'bottom'] },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e1-3',
|
id: 'e1-3',
|
||||||
source: '1',
|
source: '1',
|
||||||
target: '3',
|
target: '3',
|
||||||
type: 'custom',
|
type: 'custom',
|
||||||
|
data: { type: 'straight', align: ['center', 'center'] },
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// const defaultEdgeOptions = { zIndex: 0 };
|
|
||||||
const nodeOrigin: NodeOrigin = [0.5, 0.5];
|
|
||||||
|
|
||||||
export default function EdgeToolbarExample() {
|
export default function EdgeToolbarExample() {
|
||||||
return (
|
return (
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
@@ -71,9 +70,7 @@ export default function EdgeToolbarExample() {
|
|||||||
minZoom={0.2}
|
minZoom={0.2}
|
||||||
maxZoom={4}
|
maxZoom={4}
|
||||||
fitView
|
fitView
|
||||||
// defaultEdgeOptions={defaultEdgeOptions}
|
|
||||||
edgeTypes={edgeTypes}
|
edgeTypes={edgeTypes}
|
||||||
nodeOrigin={nodeOrigin}
|
|
||||||
>
|
>
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
<MiniMap />
|
<MiniMap />
|
||||||
|
|||||||
@@ -55,8 +55,6 @@
|
|||||||
let edges = $state.raw<Edge[]>(initialEdges);
|
let edges = $state.raw<Edge[]>(initialEdges);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div style="height: 100vh;">
|
<SvelteFlow bind:nodes bind:edges {edgeTypes} fitView>
|
||||||
<SvelteFlow bind:nodes bind:edges {edgeTypes} fitView>
|
<Background />
|
||||||
<Background />
|
</SvelteFlow>
|
||||||
</SvelteFlow>
|
|
||||||
</div>
|
|
||||||
|
|||||||
@@ -1,34 +1,32 @@
|
|||||||
import { getEdgeToolbarTransform, Position } from '@xyflow/system';
|
import { useCallback } from 'react';
|
||||||
import cc from 'classcat';
|
import cc from 'classcat';
|
||||||
import { CSSProperties, useCallback } from 'react';
|
|
||||||
import { shallow } from 'zustand/shallow';
|
import { shallow } from 'zustand/shallow';
|
||||||
|
import { getEdgeToolbarTransform } from '@xyflow/system';
|
||||||
|
|
||||||
import { EdgeLabelRenderer } from '../../components/EdgeLabelRenderer';
|
import { EdgeLabelRenderer } from '../../components/EdgeLabelRenderer';
|
||||||
import { useStore } from '../../hooks/useStore';
|
import { useStore } from '../../hooks/useStore';
|
||||||
import { Edge, ReactFlowState } from '../../types';
|
import { Edge, ReactFlowState } from '../../types';
|
||||||
import type { EdgeToolbarProps } from './types';
|
import type { EdgeToolbarProps } from './types';
|
||||||
|
|
||||||
const storeSelector = (state: ReactFlowState) => ({
|
const zoomSelector = (state: ReactFlowState) => state.transform[2];
|
||||||
zoom: state.transform[2],
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This component can render a toolbar or tooltip to one side of a custom edge. This
|
* This component can render a toolbar or tooltip to one side of a custom edge. This
|
||||||
* toolbar doesn't scale with the viewport so that the content is always visible.
|
* toolbar doesn't scale with the viewport so that the content stays the same size.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @example
|
* @example
|
||||||
* ```jsx
|
* ```jsx
|
||||||
* import { EdgeToolbar } from "@xyflow/react";
|
* import { EdgeToolbar, BaseEdge, getBezierPath, type EdgeProps } from "@xyflow/react";
|
||||||
*
|
*
|
||||||
* export function CustomEdge({ id, data, ...props }: EdgeProps) {
|
* export function CustomEdge({ id, data, ...props }: EdgeProps) {
|
||||||
* const [edgePath, labelX, labelY] = getBezierPath(props);
|
* const [edgePath, centerX, centerY] = getBezierPath(props);
|
||||||
*
|
*
|
||||||
* return (
|
* return (
|
||||||
* <>
|
* <>
|
||||||
* <BaseEdge id={id} path={edgePath} />
|
* <BaseEdge id={id} path={edgePath} />
|
||||||
* <EdgeToolbar edgeId={id} x={labelX} y={labelY} isVisible>
|
* <EdgeToolbar edgeId={id} x={centerX} y={centerY} isVisible>
|
||||||
* <button onClick={() => {}}>Click me</button>
|
* <button onClick={() => console.log('edge', id, 'click')}}>Click me</button>
|
||||||
* </EdgeToolbar>
|
* </EdgeToolbar>
|
||||||
* </>
|
* </>
|
||||||
* );
|
* );
|
||||||
@@ -43,56 +41,36 @@ export function EdgeToolbar({
|
|||||||
className,
|
className,
|
||||||
style,
|
style,
|
||||||
isVisible,
|
isVisible,
|
||||||
position = Position.Top,
|
alignX = 'center',
|
||||||
offsetX = 0,
|
alignY = 'center',
|
||||||
offsetY = 0,
|
|
||||||
align = 'center',
|
|
||||||
...rest
|
...rest
|
||||||
}: EdgeToolbarProps) {
|
}: EdgeToolbarProps) {
|
||||||
const edgeSelector = useCallback(
|
const edgeSelector = useCallback((state: ReactFlowState): Edge | undefined => state.edgeLookup.get(edgeId), [edgeId]);
|
||||||
(state: ReactFlowState): Edge | undefined => {
|
const edge = useStore(edgeSelector, shallow);
|
||||||
const edge = state.edgeLookup.get(edgeId || '');
|
|
||||||
if (edge) {
|
|
||||||
return edge;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
[edgeId]
|
|
||||||
);
|
|
||||||
|
|
||||||
const edge = useStore(edgeSelector);
|
|
||||||
|
|
||||||
// if isVisible is not set, we show the toolbar only if its node is selected and no other node is selected
|
|
||||||
const isActive = typeof isVisible === 'boolean' ? isVisible : edge?.selected;
|
const isActive = typeof isVisible === 'boolean' ? isVisible : edge?.selected;
|
||||||
|
const zoom = useStore(zoomSelector);
|
||||||
|
|
||||||
if (!isActive || !edge) {
|
if (!isActive || !edge) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const zIndex = (edge.zIndex ?? 0) + 1;
|
const zIndex = (edge.zIndex ?? 0) + 1;
|
||||||
|
const transform = getEdgeToolbarTransform(x, y, zoom, alignX, alignY);
|
||||||
const { zoom } = useStore(storeSelector, shallow);
|
|
||||||
|
|
||||||
const transform = getEdgeToolbarTransform(x, y, zoom, offsetX, offsetY);
|
|
||||||
console.log('transform', transform);
|
|
||||||
const wrapperStyle: CSSProperties = {
|
|
||||||
position: 'absolute',
|
|
||||||
// TODO: offset
|
|
||||||
transform,
|
|
||||||
zIndex,
|
|
||||||
...style,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<EdgeLabelRenderer>
|
<EdgeLabelRenderer>
|
||||||
<div
|
<div
|
||||||
style={{ ...wrapperStyle, pointerEvents: 'all' }}
|
style={{
|
||||||
|
position: 'absolute',
|
||||||
|
transform,
|
||||||
|
zIndex,
|
||||||
|
pointerEvents: 'all',
|
||||||
|
transformOrigin: '0 0',
|
||||||
|
...style,
|
||||||
|
}}
|
||||||
className={cc(['react-flow__edge-toolbar', className])}
|
className={cc(['react-flow__edge-toolbar', className])}
|
||||||
{...rest}
|
|
||||||
// @todo: check if we could only do this for non-prod envs
|
|
||||||
data-id={edge.id}
|
data-id={edge.id}
|
||||||
|
{...rest}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,48 +1,10 @@
|
|||||||
import type { HTMLAttributes } from 'react';
|
import type { HTMLAttributes, ReactNode } from 'react';
|
||||||
import type { Position, Align } from '@xyflow/system';
|
import type { EdgeToolbarBaseProps } from '@xyflow/system';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expand
|
* @inline
|
||||||
*/
|
*/
|
||||||
export type EdgeToolbarProps = HTMLAttributes<HTMLDivElement> & {
|
export type EdgeToolbarProps = EdgeToolbarBaseProps &
|
||||||
/**
|
HTMLAttributes<HTMLDivElement> & {
|
||||||
* An edge toolbar must be attached to an edge.
|
children?: ReactNode;
|
||||||
*/
|
};
|
||||||
edgeId: string;
|
|
||||||
/** If `true`, edge toolbar is visible even if edge is not selected. */
|
|
||||||
isVisible?: boolean;
|
|
||||||
/**
|
|
||||||
* TODO: What and how?? Needed?
|
|
||||||
* Position of the toolbar relative to the edge.
|
|
||||||
* @default Position.Top
|
|
||||||
* @example Position.TopLeft, Position.TopRight, Position.BottomLeft, Position.BottomRight
|
|
||||||
*/
|
|
||||||
position?: Position;
|
|
||||||
/**
|
|
||||||
* The space between the edge and the toolbar, measured in pixels.
|
|
||||||
* @default 10
|
|
||||||
*/
|
|
||||||
offset?: number;
|
|
||||||
/**
|
|
||||||
* Align the toolbar relative to the edge.
|
|
||||||
* @default "center"
|
|
||||||
* @example Align.Start, Align.Center, Align.End
|
|
||||||
*/
|
|
||||||
align?: Align;
|
|
||||||
/**
|
|
||||||
* The `x` position of the edge label.
|
|
||||||
*/
|
|
||||||
x: number;
|
|
||||||
/**
|
|
||||||
* The `y` position of the edge label.
|
|
||||||
*/
|
|
||||||
y: number;
|
|
||||||
/**
|
|
||||||
* The `offsetX` position of the edge label relative to the edge in pixels.
|
|
||||||
*/
|
|
||||||
offsetX?: number;
|
|
||||||
/**
|
|
||||||
* The `offsetY` position of the edge label relative to the edge in pixels.
|
|
||||||
*/
|
|
||||||
offsetY?: number;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,38 +1,26 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Position, getEdgeToolbarTransform } from '@xyflow/system';
|
import { getEdgeToolbarTransform } from '@xyflow/system';
|
||||||
import { getContext } from 'svelte';
|
|
||||||
|
|
||||||
import { useSvelteFlow } from '$lib/hooks/useSvelteFlow.svelte';
|
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
|
|
||||||
import { EdgeLabel } from '$lib/components/EdgeLabel';
|
import { EdgeLabel } from '$lib/components/EdgeLabel';
|
||||||
import type { EdgeToolbarProps } from './types';
|
import type { EdgeToolbarProps } from './types';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
edgeId,
|
edgeId,
|
||||||
position = Position.Top,
|
|
||||||
align = 'center',
|
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
offsetX = 0,
|
alignX = 'center',
|
||||||
offsetY = 0,
|
alignY = 'center',
|
||||||
isVisible,
|
isVisible,
|
||||||
children,
|
children,
|
||||||
...rest
|
...rest
|
||||||
}: EdgeToolbarProps = $props();
|
}: EdgeToolbarProps = $props();
|
||||||
|
|
||||||
const store = useStore();
|
const store = useStore();
|
||||||
|
const edge = store.edgeLookup.get(edgeId);
|
||||||
const edge = store.edgeLookup.get(edgeId ?? '');
|
const isActive = $derived(typeof isVisible === 'boolean' ? isVisible : edge?.selected);
|
||||||
|
const transform = $derived(getEdgeToolbarTransform(x, y, store.viewport.zoom, alignX, alignY));
|
||||||
// if isVisible is not set, we show the toolbar only if its node is selected and no other node is selected
|
const zIndex = (edge?.zIndex ?? 0) + 1;
|
||||||
let isActive = $derived(typeof isVisible === 'boolean' ? isVisible : edge?.selected);
|
|
||||||
|
|
||||||
let zIndex = (edge?.zIndex ?? 0) + 1;
|
|
||||||
|
|
||||||
const { zoom } = store.viewport;
|
|
||||||
|
|
||||||
const transform = getEdgeToolbarTransform(x, y, zoom, offsetX, offsetY);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if store.domNode && isActive}
|
{#if store.domNode && isActive}
|
||||||
@@ -41,6 +29,7 @@
|
|||||||
style:position="absolute"
|
style:position="absolute"
|
||||||
style:transform
|
style:transform
|
||||||
style:z-index={zIndex}
|
style:z-index={zIndex}
|
||||||
|
style:transform-origin="0 0"
|
||||||
{...rest}
|
{...rest}
|
||||||
class="svelte-flow__edge-toolbar"
|
class="svelte-flow__edge-toolbar"
|
||||||
data-id={edgeId ?? ''}
|
data-id={edgeId ?? ''}
|
||||||
|
|||||||
@@ -1,42 +1,7 @@
|
|||||||
import type { Position, Align } from '@xyflow/system';
|
import type { EdgeToolbarBaseProps } from '@xyflow/system';
|
||||||
import type { Snippet } from 'svelte';
|
import type { Snippet } from 'svelte';
|
||||||
import type { HTMLAttributes } from 'svelte/elements';
|
import type { HTMLAttributes } from 'svelte/elements';
|
||||||
|
|
||||||
export type EdgeToolbarProps = {
|
export type EdgeToolbarProps = EdgeToolbarBaseProps & {
|
||||||
/**
|
|
||||||
* An edge toolbar must be attached to an edge.
|
|
||||||
*/
|
|
||||||
edgeId: string;
|
|
||||||
/** If `true`, edge toolbar is visible even if edge is not selected. */
|
|
||||||
isVisible?: boolean;
|
|
||||||
/**
|
|
||||||
* TODO: What and how?? Needed?
|
|
||||||
* Position of the toolbar relative to the edge.
|
|
||||||
* @default Position.Top
|
|
||||||
* @example Position.TopLeft, Position.TopRight, Position.BottomLeft, Position.BottomRight
|
|
||||||
*/
|
|
||||||
position?: Position;
|
|
||||||
/**
|
|
||||||
* Align the toolbar relative to the edge.
|
|
||||||
* @default "center"
|
|
||||||
* @example Align.Start, Align.Center, Align.End
|
|
||||||
*/
|
|
||||||
align?: Align;
|
|
||||||
/**
|
|
||||||
* The `x` position of the edge label.
|
|
||||||
*/
|
|
||||||
x: number;
|
|
||||||
/**
|
|
||||||
* The `y` position of the edge label.
|
|
||||||
*/
|
|
||||||
y: number;
|
|
||||||
/**
|
|
||||||
* The `offsetX` position of the edge label relative to the edge in pixels.
|
|
||||||
*/
|
|
||||||
offsetX?: number;
|
|
||||||
/**
|
|
||||||
* The `offsetY` position of the edge label relative to the edge in pixels.
|
|
||||||
*/
|
|
||||||
offsetY?: number;
|
|
||||||
children?: Snippet;
|
children?: Snippet;
|
||||||
} & HTMLAttributes<HTMLDivElement>;
|
} & HTMLAttributes<HTMLDivElement>;
|
||||||
|
|||||||
@@ -129,3 +129,34 @@ export type EdgePosition = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type EdgeLookup<EdgeType extends EdgeBase = EdgeBase> = Map<string, EdgeType>;
|
export type EdgeLookup<EdgeType extends EdgeBase = EdgeBase> = Map<string, EdgeType>;
|
||||||
|
|
||||||
|
export type EdgeToolbarBaseProps = {
|
||||||
|
/**
|
||||||
|
* An edge toolbar must be attached to an edge.
|
||||||
|
*/
|
||||||
|
edgeId: string;
|
||||||
|
/**
|
||||||
|
* The `x` position of the edge label.
|
||||||
|
*/
|
||||||
|
x: number;
|
||||||
|
/**
|
||||||
|
* The `y` position of the edge label.
|
||||||
|
*/
|
||||||
|
y: number;
|
||||||
|
/** If `true`, edge toolbar is visible even if edge is not selected.
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
isVisible?: boolean;
|
||||||
|
/**
|
||||||
|
* Align the toolbar x relative to the edge.
|
||||||
|
* @default "center"
|
||||||
|
* @example 'left', 'center', 'right'
|
||||||
|
*/
|
||||||
|
alignX?: 'left' | 'center' | 'right';
|
||||||
|
/**
|
||||||
|
* Align the toolbar x relative to the edge.
|
||||||
|
* @default "center"
|
||||||
|
* @example 'top', 'center', 'bottom'
|
||||||
|
*/
|
||||||
|
alignY?: 'top' | 'center' | 'bottom';
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,10 +1,24 @@
|
|||||||
|
const alignXToPercent: Record<'left' | 'center' | 'right', number> = {
|
||||||
|
left: 0,
|
||||||
|
center: 50,
|
||||||
|
right: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
const alignYToPercent: Record<'top' | 'center' | 'bottom', number> = {
|
||||||
|
top: 0,
|
||||||
|
center: 50,
|
||||||
|
bottom: 100,
|
||||||
|
};
|
||||||
|
|
||||||
export function getEdgeToolbarTransform(
|
export function getEdgeToolbarTransform(
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
zoom: number,
|
zoom: number,
|
||||||
offsetX: number = 0,
|
alignX: 'left' | 'center' | 'right' = 'center',
|
||||||
offsetY: number = 0
|
alignY: 'top' | 'center' | 'bottom' = 'center'
|
||||||
): string {
|
): string {
|
||||||
// Position the toolbar at the edge label center (scaling is handled by EdgeLabelRenderer)
|
// Position the toolbar at the edge label center (scaling is handled by EdgeLabelRenderer)
|
||||||
return `translate(${x + offsetX}px, ${y + offsetY}px) translate(-50%, -50%) scale(${1 / zoom})`;
|
return `translate(${x}px, ${y}px) scale(${1 / zoom}) translate(${-(alignXToPercent[alignX] ?? 50)}%, ${-(
|
||||||
|
alignYToPercent[alignY] ?? 50
|
||||||
|
)}%)`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user