1
- import React , { useContext , useEffect , useRef } from "react" ;
1
+ import React , { SVGProps , useContext , useEffect , useRef } from "react" ;
2
2
import { InternalContext } from "../context/InternalContext" ;
3
3
import { cx } from "@emotion/css" ;
4
- import { EDGE_CLASS , EDGE_HANDLE_CLASS , EDGE_LABEL_BACKGROUND_CLASS , EDGE_LABEL_CLASS , EDGE_PATH_CLASS } from "../util/constants" ;
4
+ import { EDGE_CLASS , EDGE_HANDLE_CLASS , EDGE_IN_PROGRESS_CLASS , EDGE_LABEL_BACKGROUND_CLASS , EDGE_LABEL_CLASS , EDGE_PATH_CLASS } from "../util/constants" ;
5
5
import { errorUnknownEdge , warnInvalidPropValue } from "../util/log" ;
6
6
// Used by documentation
7
7
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -49,6 +49,10 @@ export interface BaseEdgeProps {
49
49
* Label rotation angle.
50
50
*/
51
51
labelAngle : number
52
+ /**
53
+ * Label background border radius
54
+ */
55
+ labelBackgroundRadius : NonNullable < SVGProps < SVGRectElement > [ "rx" ] >
52
56
/**
53
57
* Whether this edge is selected
54
58
*/
@@ -69,10 +73,15 @@ export interface BaseEdgeProps {
69
73
* Whether to enable pointer events for this edge.
70
74
*/
71
75
pointerEvents : boolean
76
+ /**
77
+ * Whether this is an in-progress edge (i.e. currently being created by the user by dragging from a node handle).
78
+ * This will swap the base class "react-grapher-edge", on the root element, for "react-grapher-edge-in-progress".
79
+ */
80
+ inProgress ?: boolean
72
81
}
73
82
74
- export function BaseEdge ( { id, path, classes, boxWidth, label, labelPosition, labelOffset, labelAnchor, labelBaseline, labelAngle,
75
- selected, grabbed, markerStart, markerEnd, pointerEvents} : BaseEdgeProps ) {
83
+ export function BaseEdge ( { id, path, classes, boxWidth, label, labelPosition, labelOffset, labelAnchor, labelBaseline, labelAngle, labelBackgroundRadius ,
84
+ selected, grabbed, markerStart, markerEnd, pointerEvents, inProgress } : BaseEdgeProps ) {
76
85
const internals = useContext ( InternalContext )
77
86
78
87
// Ref to the Edge <g> and <path> elements
@@ -81,7 +90,7 @@ export function BaseEdge({id, path, classes, boxWidth, label, labelPosition, lab
81
90
const labelRef = useRef < SVGTextElement > ( null ) , labelBgRef = useRef < SVGRectElement > ( null )
82
91
// Get internal edge object
83
92
const edge = internals . getEdge ( id )
84
- if ( edge == null ) errorUnknownEdge ( id )
93
+ if ( edge == null && ! inProgress ) errorUnknownEdge ( id )
85
94
86
95
// Set listeners
87
96
useEffect ( ( ) => {
@@ -146,19 +155,19 @@ export function BaseEdge({id, path, classes, boxWidth, label, labelPosition, lab
146
155
labelBg . setAttribute ( "y" , String ( labelBounds . y - p ) )
147
156
labelBg . setAttribute ( "width" , String ( labelBounds . width + p * 2 ) )
148
157
labelBg . setAttribute ( "height" , String ( labelBounds . height + p * 2 ) )
149
- } else console . log ( { labelElem , labelBg , path , label } )
158
+ }
150
159
151
160
} , [ internals , edge , id , path , grabbed , selected , label , labelPosition , labelOffset , labelAngle ] )
152
161
153
162
const baseID = internals . id
154
- return < g ref = { ref } id = { `${ baseID } -edge-${ id } ` } className = { cx ( classes , EDGE_CLASS ) } pointerEvents = { ! pointerEvents || internals . isStatic ? "none" : "stroke "}
155
- data-grabbed = { grabbed } data-selected = { selected } data-id = { id } data-type = { "edge" } >
163
+ return < g ref = { ref } id = { `${ baseID } -edge-${ id } ` } className = { cx ( classes , inProgress ? EDGE_IN_PROGRESS_CLASS : EDGE_CLASS ) } data-id = { id } data-type = { "edge "}
164
+ pointerEvents = { ! pointerEvents || internals . isStatic || inProgress ? "none" : "stroke" } data-grabbed = { grabbed } data-selected = { selected } >
156
165
< path d = { path } className = { EDGE_HANDLE_CLASS } stroke = { "transparent" } fill = { "none" } strokeWidth = { boxWidth } />
157
166
< path ref = { pathRef } d = { path } className = { EDGE_PATH_CLASS }
158
167
markerStart = { markerStart != null ? `url(#${ baseID } -${ markerStart } )` : undefined }
159
168
markerEnd = { markerEnd != null ? `url(#${ baseID } -${ markerEnd } )` : undefined } />
160
169
{ label != null && < >
161
- < rect ref = { labelBgRef } className = { EDGE_LABEL_BACKGROUND_CLASS } rx = { edge ?. labelRadius } pointerEvents = { ! pointerEvents || internals . isStatic ? "none" : "fill" } />
170
+ < rect ref = { labelBgRef } className = { EDGE_LABEL_BACKGROUND_CLASS } rx = { labelBackgroundRadius } pointerEvents = { ! pointerEvents || internals . isStatic ? "none" : "fill" } />
162
171
< text ref = { labelRef } className = { EDGE_LABEL_CLASS } data-label-pos = { String ( labelPosition ) } textAnchor = { labelAnchor } dominantBaseline = { labelBaseline }
163
172
x = { typeof labelPosition === "object" ? labelPosition . x : undefined }
164
173
y = { typeof labelPosition === "object" ? labelPosition . y : undefined } >
0 commit comments