feat(edge-options): add labelBgPadding #421
This commit is contained in:
@@ -283,6 +283,7 @@ If you wanted to display this edge, you would need a node with id = 1 (source no
|
|||||||
- `labelStyle`: css properties for the text
|
- `labelStyle`: css properties for the text
|
||||||
- `labelShowBg`: boolean - default: `true`
|
- `labelShowBg`: boolean - default: `true`
|
||||||
- `labelBgStyle`: css properties for the text background
|
- `labelBgStyle`: css properties for the text background
|
||||||
|
- `labelBgPadding`: [number, number] background rectangle padding - default: `[2, 4]`
|
||||||
- `arrowHeadType`: 'arrow' or 'arrowclosed' - defines the arrowhead of the edge
|
- `arrowHeadType`: 'arrow' or 'arrowclosed' - defines the arrowhead of the edge
|
||||||
- `markerEndId`: custom marker end url - if this is used `arrowHeadType` gets ignored
|
- `markerEndId`: custom marker end url - if this is used `arrowHeadType` gets ignored
|
||||||
- `isHidden`: if `true`, the edge will not be rendered
|
- `isHidden`: if `true`, the edge will not be rendered
|
||||||
|
|||||||
@@ -38,8 +38,9 @@ const initialElements = [
|
|||||||
source: '5',
|
source: '5',
|
||||||
target: '7',
|
target: '7',
|
||||||
label: 'label with styled bg',
|
label: 'label with styled bg',
|
||||||
labelBgStyle: { fill: '#eee', fillOpacity: 0.7 },
|
labelBgStyle: { fill: '#555', color: '#fff', fillOpacity: 0.7 },
|
||||||
arrowHeadType: 'arrowclosed',
|
arrowHeadType: 'arrowclosed',
|
||||||
|
labelBgPadding: [8, 4],
|
||||||
},
|
},
|
||||||
{ id: 'e5-8', source: '5', target: '8', type: 'custom', data: { text: 'custom edge' }, arrowHeadType: 'arrowclosed' },
|
{ id: 'e5-8', source: '5', target: '8', type: 'custom', data: { text: 'custom edge' }, arrowHeadType: 'arrowclosed' },
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ export default memo(
|
|||||||
style,
|
style,
|
||||||
arrowHeadType,
|
arrowHeadType,
|
||||||
markerEndId,
|
markerEndId,
|
||||||
|
labelBgPadding,
|
||||||
}: EdgeBezierProps) => {
|
}: EdgeBezierProps) => {
|
||||||
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY });
|
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY });
|
||||||
const path = getBezierPath({
|
const path = getBezierPath({
|
||||||
@@ -72,6 +73,7 @@ export default memo(
|
|||||||
labelStyle={labelStyle}
|
labelStyle={labelStyle}
|
||||||
labelShowBg={labelShowBg}
|
labelShowBg={labelShowBg}
|
||||||
labelBgStyle={labelBgStyle}
|
labelBgStyle={labelBgStyle}
|
||||||
|
labelBgPadding={labelBgPadding}
|
||||||
/>
|
/>
|
||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
|
|||||||
@@ -2,44 +2,44 @@ import React, { memo, useRef, useState, useEffect } from 'react';
|
|||||||
|
|
||||||
import { EdgeTextProps, Rect } from '../../types';
|
import { EdgeTextProps, Rect } from '../../types';
|
||||||
|
|
||||||
const textBgPadding = 2;
|
export default memo(
|
||||||
|
({ x, y, label, labelStyle = {}, labelShowBg = true, labelBgStyle = {}, labelBgPadding = [2, 4] }: EdgeTextProps) => {
|
||||||
|
const edgeRef = useRef<SVGTextElement>(null);
|
||||||
|
const [edgeTextBbox, setEdgeTextBbox] = useState<Rect>({ x: 0, y: 0, width: 0, height: 0 });
|
||||||
|
|
||||||
export default memo(({ x, y, label, labelStyle = {}, labelShowBg = true, labelBgStyle = {} }: EdgeTextProps) => {
|
useEffect(() => {
|
||||||
const edgeRef = useRef<SVGTextElement>(null);
|
if (edgeRef.current) {
|
||||||
const [edgeTextBbox, setEdgeTextBbox] = useState<Rect>({ x: 0, y: 0, width: 0, height: 0 });
|
const textBbox = edgeRef.current.getBBox();
|
||||||
|
|
||||||
useEffect(() => {
|
setEdgeTextBbox({
|
||||||
if (edgeRef.current) {
|
x: textBbox.x,
|
||||||
const textBbox = edgeRef.current.getBBox();
|
y: textBbox.y,
|
||||||
|
width: textBbox.width,
|
||||||
|
height: textBbox.height,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
setEdgeTextBbox({
|
if (typeof label === 'undefined' || !label) {
|
||||||
x: textBbox.x,
|
return null;
|
||||||
y: textBbox.y,
|
|
||||||
width: textBbox.width,
|
|
||||||
height: textBbox.height,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (typeof label === 'undefined' || !label) {
|
return (
|
||||||
return null;
|
<g transform={`translate(${x - edgeTextBbox.width / 2} ${y - edgeTextBbox.height / 2})`}>
|
||||||
|
{labelShowBg && (
|
||||||
|
<rect
|
||||||
|
width={edgeTextBbox.width + 2 * labelBgPadding[0]}
|
||||||
|
x={-labelBgPadding[0]}
|
||||||
|
y={-labelBgPadding[1]}
|
||||||
|
height={edgeTextBbox.height + 2 * labelBgPadding[1]}
|
||||||
|
className="react-flow__edge-textbg"
|
||||||
|
style={labelBgStyle}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<text className="react-flow__edge-text" y={edgeTextBbox.height / 2} dy="0.3em" ref={edgeRef} style={labelStyle}>
|
||||||
|
{label}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
return (
|
|
||||||
<g transform={`translate(${x - edgeTextBbox.width / 2} ${y - edgeTextBbox.height / 2})`}>
|
|
||||||
{labelShowBg && (
|
|
||||||
<rect
|
|
||||||
width={edgeTextBbox.width + 2 * textBgPadding}
|
|
||||||
x={-textBgPadding}
|
|
||||||
y={-textBgPadding}
|
|
||||||
height={edgeTextBbox.height + 2 * textBgPadding}
|
|
||||||
className="react-flow__edge-textbg"
|
|
||||||
style={labelBgStyle}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
<text className="react-flow__edge-text" y={edgeTextBbox.height / 2} dy="0.3em" ref={edgeRef} style={labelStyle}>
|
|
||||||
{label}
|
|
||||||
</text>
|
|
||||||
</g>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ export default memo(
|
|||||||
labelStyle,
|
labelStyle,
|
||||||
labelShowBg,
|
labelShowBg,
|
||||||
labelBgStyle,
|
labelBgStyle,
|
||||||
|
labelBgPadding,
|
||||||
style,
|
style,
|
||||||
sourcePosition = Position.Bottom,
|
sourcePosition = Position.Bottom,
|
||||||
targetPosition = Position.Top,
|
targetPosition = Position.Top,
|
||||||
@@ -160,6 +161,7 @@ export default memo(
|
|||||||
labelStyle={labelStyle}
|
labelStyle={labelStyle}
|
||||||
labelShowBg={labelShowBg}
|
labelShowBg={labelShowBg}
|
||||||
labelBgStyle={labelBgStyle}
|
labelBgStyle={labelBgStyle}
|
||||||
|
labelBgPadding={labelBgPadding}
|
||||||
/>
|
/>
|
||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export default memo(
|
|||||||
labelStyle,
|
labelStyle,
|
||||||
labelShowBg,
|
labelShowBg,
|
||||||
labelBgStyle,
|
labelBgStyle,
|
||||||
|
labelBgPadding,
|
||||||
style,
|
style,
|
||||||
arrowHeadType,
|
arrowHeadType,
|
||||||
markerEndId,
|
markerEndId,
|
||||||
@@ -33,6 +34,7 @@ export default memo(
|
|||||||
labelStyle={labelStyle}
|
labelStyle={labelStyle}
|
||||||
labelShowBg={labelShowBg}
|
labelShowBg={labelShowBg}
|
||||||
labelBgStyle={labelBgStyle}
|
labelBgStyle={labelBgStyle}
|
||||||
|
labelBgPadding={labelBgPadding}
|
||||||
/>
|
/>
|
||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ interface EdgeWrapperProps {
|
|||||||
label?: string;
|
label?: string;
|
||||||
labelStyle?: CSSProperties;
|
labelStyle?: CSSProperties;
|
||||||
labelShowBg?: boolean;
|
labelShowBg?: boolean;
|
||||||
labelBgStyle: CSSProperties;
|
labelBgStyle?: CSSProperties;
|
||||||
|
labelBgPadding?: [number, number];
|
||||||
className?: string;
|
className?: string;
|
||||||
onClick?: (event: React.MouseEvent, edge: Edge) => void;
|
onClick?: (event: React.MouseEvent, edge: Edge) => void;
|
||||||
animated?: boolean;
|
animated?: boolean;
|
||||||
@@ -36,6 +37,7 @@ export default (EdgeComponent: ComponentType<EdgeCompProps>) => {
|
|||||||
labelStyle,
|
labelStyle,
|
||||||
labelShowBg,
|
labelShowBg,
|
||||||
labelBgStyle,
|
labelBgStyle,
|
||||||
|
labelBgPadding,
|
||||||
className,
|
className,
|
||||||
isHidden,
|
isHidden,
|
||||||
data,
|
data,
|
||||||
@@ -77,6 +79,7 @@ export default (EdgeComponent: ComponentType<EdgeCompProps>) => {
|
|||||||
labelStyle={labelStyle}
|
labelStyle={labelStyle}
|
||||||
labelShowBg={labelShowBg}
|
labelShowBg={labelShowBg}
|
||||||
labelBgStyle={labelBgStyle}
|
labelBgStyle={labelBgStyle}
|
||||||
|
labelBgPadding={labelBgPadding}
|
||||||
data={data}
|
data={data}
|
||||||
{...rest}
|
{...rest}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -171,6 +171,7 @@ function renderEdge(
|
|||||||
labelStyle={edge.labelStyle}
|
labelStyle={edge.labelStyle}
|
||||||
labelShowBg={edge.labelShowBg}
|
labelShowBg={edge.labelShowBg}
|
||||||
labelBgStyle={edge.labelBgStyle}
|
labelBgStyle={edge.labelBgStyle}
|
||||||
|
labelBgPadding={edge.labelBgPadding}
|
||||||
style={edge.style}
|
style={edge.style}
|
||||||
arrowHeadType={edge.arrowHeadType}
|
arrowHeadType={edge.arrowHeadType}
|
||||||
source={sourceId}
|
source={sourceId}
|
||||||
|
|||||||
@@ -94,6 +94,10 @@ const useElementUpdater = (propElements: Elements): void => {
|
|||||||
edgeProps.labelShowBg = propEdge.labelShowBg;
|
edgeProps.labelShowBg = propEdge.labelShowBg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof propEdge.labelBgPadding !== 'undefined') {
|
||||||
|
edgeProps.labelBgPadding = propEdge.labelBgPadding;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof labelBgStyle !== 'undefined') {
|
if (typeof labelBgStyle !== 'undefined') {
|
||||||
edgeProps.labelBgStyle = labelBgStyle;
|
edgeProps.labelBgStyle = labelBgStyle;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ export interface Edge {
|
|||||||
labelStyle?: CSSProperties;
|
labelStyle?: CSSProperties;
|
||||||
labelShowBg?: boolean;
|
labelShowBg?: boolean;
|
||||||
labelBgStyle?: CSSProperties;
|
labelBgStyle?: CSSProperties;
|
||||||
|
labelBgPadding?: [number, number];
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
animated?: boolean;
|
animated?: boolean;
|
||||||
arrowHeadType?: ArrowHeadType;
|
arrowHeadType?: ArrowHeadType;
|
||||||
@@ -96,6 +97,7 @@ export interface EdgeProps {
|
|||||||
labelStyle?: CSSProperties;
|
labelStyle?: CSSProperties;
|
||||||
labelShowBg?: boolean;
|
labelShowBg?: boolean;
|
||||||
labelBgStyle?: CSSProperties;
|
labelBgStyle?: CSSProperties;
|
||||||
|
labelBgPadding?: [number, number];
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
arrowHeadType?: ArrowHeadType;
|
arrowHeadType?: ArrowHeadType;
|
||||||
markerEndId?: string;
|
markerEndId?: string;
|
||||||
@@ -235,6 +237,7 @@ export interface EdgeCompProps {
|
|||||||
labelStyle?: CSSProperties;
|
labelStyle?: CSSProperties;
|
||||||
labelShowBg?: boolean;
|
labelShowBg?: boolean;
|
||||||
labelBgStyle?: CSSProperties;
|
labelBgStyle?: CSSProperties;
|
||||||
|
labelBgPadding?: [number, number];
|
||||||
onClick?: (event: ReactMouseEvent, edge: Edge) => void;
|
onClick?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||||
animated?: boolean;
|
animated?: boolean;
|
||||||
selected?: boolean;
|
selected?: boolean;
|
||||||
@@ -248,6 +251,7 @@ export interface EdgeTextProps {
|
|||||||
labelStyle?: CSSProperties;
|
labelStyle?: CSSProperties;
|
||||||
labelShowBg?: boolean;
|
labelShowBg?: boolean;
|
||||||
labelBgStyle?: CSSProperties;
|
labelBgStyle?: CSSProperties;
|
||||||
|
labelBgPadding?: [number, number];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NodePosUpdate = {
|
export type NodePosUpdate = {
|
||||||
|
|||||||
Reference in New Issue
Block a user