1 import {PluginBase} from "../plugin-base";
3 export class SVGEdgeHoverPlugin extends PluginBase {
5 private boundEdgeEnterFunction = this.onEdgeEnter.bind(this);
7 private modelListener: { dispose: Function } = {
12 this.attachEdgeHoverBehavior();
16 this.detachEdgeHoverBehavior();
17 this.modelListener.dispose();
20 private attachEdgeHoverBehavior() {
22 this.detachEdgeHoverBehavior();
23 this.workflow.workflow.addEventListener("mouseenter", this.boundEdgeEnterFunction, true);
26 private detachEdgeHoverBehavior() {
27 this.workflow.workflow.removeEventListener("mouseenter", this.boundEdgeEnterFunction, true);
30 private onEdgeEnter(ev: MouseEvent) {
33 // Ignore if we did not enter an edge
34 if (!(ev.target! as Element).classList.contains("edge")) return;
36 const target = ev.target as SVGGElement;
37 let tipEl: SVGGElement;
39 const onMouseMove = (ev: MouseEvent) => {
40 const coords = this.workflow.transformScreenCTMtoCanvas(ev.clientX, ev.clientY);
41 tipEl.setAttribute("x", String(coords.x));
42 tipEl.setAttribute("y", String(coords.y - 16));
45 const onMouseLeave = (ev: MouseEvent) => {
47 target.removeEventListener("mousemove", onMouseMove);
48 target.removeEventListener("mouseleave", onMouseLeave)
51 this.modelListener = this.workflow.model.on("connection.remove", (source, destination) => {
53 const [tipS, tipD] = tipEl.getAttribute("data-source-destination")!.split("$!$");
54 if (tipS === source.connectionId && tipD === destination.connectionId) {
59 const sourceNode = target.getAttribute("data-source-node");
60 const destNode = target.getAttribute("data-destination-node");
61 const sourcePort = target.getAttribute("data-source-port");
62 const destPort = target.getAttribute("data-destination-port");
63 const sourceConnect = target.getAttribute("data-source-connection");
64 const destConnect = target.getAttribute("data-destination-connection");
66 const sourceLabel = sourceNode === sourcePort ? sourceNode : `${sourceNode} (${sourcePort})`;
67 const destLabel = destNode === destPort ? destNode : `${destNode} (${destPort})`;
69 const coords = this.workflow.transformScreenCTMtoCanvas(ev.clientX, ev.clientY);
71 const ns = "http://www.w3.org/2000/svg";
72 tipEl = document.createElementNS(ns, "text");
73 tipEl.classList.add("label");
74 tipEl.classList.add("label-edge");
75 tipEl.setAttribute("x", String(coords.x));
76 tipEl.setAttribute("y", String(coords.y));
77 tipEl.setAttribute("data-source-destination", sourceConnect + "$!$" + destConnect);
78 tipEl.innerHTML = sourceLabel + " → " + destLabel;
80 this.workflow.workflow.appendChild(tipEl);
82 target.addEventListener("mousemove", onMouseMove);
83 target.addEventListener("mouseleave", onMouseLeave);