|
| 1 | +/** @jsx svg */ |
| 2 | +import { injectable } from "inversify"; |
| 3 | +import { |
| 4 | + PolylineEdgeViewWithGapsOnIntersections, |
| 5 | + SEdgeImpl, |
| 6 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 7 | + svg, |
| 8 | + RenderingContext, |
| 9 | + IViewArgs, |
| 10 | + WithEditableLabel, |
| 11 | + isEditableLabel, |
| 12 | + SRoutingHandleView, |
| 13 | +} from "sprotty"; |
| 14 | +import { VNode } from "snabbdom"; |
| 15 | +import { Point, angleOfPoint, toDegrees, SEdge } from "sprotty-protocol"; |
| 16 | + |
| 17 | +export interface ArrowEdge extends SEdge { |
| 18 | + text?: string; |
| 19 | +} |
| 20 | + |
| 21 | +export class ArrowEdgeImpl extends SEdgeImpl implements WithEditableLabel { |
| 22 | + text?: string; |
| 23 | + |
| 24 | + get editableLabel() { |
| 25 | + const label = this.children.find((element) => element.type.startsWith("label")); |
| 26 | + if (label && isEditableLabel(label)) { |
| 27 | + return label; |
| 28 | + } |
| 29 | + |
| 30 | + return undefined; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +@injectable() |
| 35 | +export class ArrowEdgeView extends PolylineEdgeViewWithGapsOnIntersections { |
| 36 | + |
| 37 | + override render(edge: Readonly<SEdgeImpl>, context: RenderingContext, args?: IViewArgs): VNode | undefined { |
| 38 | + // In the default implementation children of the edge are always rendered, because they |
| 39 | + // may be visible when the rest of the edge is not. |
| 40 | + // We only have the edge label as an children which only must be rendered when the rest of the edge is visible. |
| 41 | + // So as an optimization for big diagrams we don't render the label when the rest of the edge is not visible either. |
| 42 | + // Otherwise all these labels would be added to the DOM, making it slow.. |
| 43 | + const route = this.edgeRouterRegistry.route(edge, args); |
| 44 | + if (!this.isVisible(edge, route, context)) { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + |
| 48 | + return super.render(edge, context, args); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Renders an arrow at the end of the edge. |
| 54 | + */ |
| 55 | + protected override renderAdditionals(edge: SEdgeImpl, segments: Point[], context: RenderingContext): VNode[] { |
| 56 | + const additionals = super.renderAdditionals(edge, segments, context); |
| 57 | + const p1 = segments[segments.length - 2]; |
| 58 | + const p2 = segments[segments.length - 1]; |
| 59 | + const arrow = ( |
| 60 | + <path |
| 61 | + class-arrow={true} |
| 62 | + d="M 0.5,0 L 10,-4 L 10,4 Z" |
| 63 | + transform={`rotate(${toDegrees(angleOfPoint({ x: p1.x - p2.x, y: p1.y - p2.y }))} ${p2.x} ${ |
| 64 | + p2.y |
| 65 | + }) translate(${p2.x} ${p2.y})`} |
| 66 | + style={{ opacity: edge.opacity.toString() }} |
| 67 | + /> |
| 68 | + ); |
| 69 | + additionals.push(arrow); |
| 70 | + return additionals; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Renders the edge line. |
| 75 | + * In contrast to the default implementation that we override here, |
| 76 | + * this implementation makes the edge line 10px shorter at the end to make space for the arrow without any overlap. |
| 77 | + */ |
| 78 | + protected renderLine(edge: SEdgeImpl, segments: Point[]): VNode { |
| 79 | + const firstPoint = segments[0]; |
| 80 | + let path = `M ${firstPoint.x},${firstPoint.y}`; |
| 81 | + for (let i = 1; i < segments.length; i++) { |
| 82 | + const p = segments[i]; |
| 83 | + if (i === segments.length - 1) { |
| 84 | + // Make edge line 9.5px shorter to make space for the arrow |
| 85 | + // The arrow is 10px long, but we only shorten by 9.5 px to have overlap at the edge between line and arrow. |
| 86 | + // Otherwise edges would be exactly next to each other which would result in a small gap and flickering if you zoom in enough. |
| 87 | + const prevP = segments[i - 1]; |
| 88 | + const dx = p.x - prevP.x; |
| 89 | + const dy = p.y - prevP.y; |
| 90 | + const length = Math.sqrt(dx * dx + dy * dy); |
| 91 | + const ratio = (length - 9.5) / length; |
| 92 | + path += ` L ${prevP.x + dx * ratio},${prevP.y + dy * ratio}`; |
| 93 | + } else { |
| 94 | + // Lines between points in between are not shortened |
| 95 | + path += ` L ${p.x},${p.y}`; |
| 96 | + } |
| 97 | + } |
| 98 | + return ( |
| 99 | + <g> |
| 100 | + {/* This is the actual path being rendered */} |
| 101 | + <path d={path} style={{ opacity: edge.opacity.toString() }} /> |
| 102 | + {/* This is a transparent path that is rendered on top of the actual path to make it easier to select the edge */} |
| 103 | + <path d={path} class-select-path={true} /> |
| 104 | + </g> |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Smaller version of the default edge routing handle. |
| 111 | + */ |
| 112 | +@injectable() |
| 113 | +export class CustomRoutingHandleView extends SRoutingHandleView { |
| 114 | + getRadius(): number { |
| 115 | + return 5; |
| 116 | + } |
| 117 | +} |
0 commit comments