| 1 |
import { pointer } from "d3-selection"; |
| 2 |
import type { Action } from "svelte/action"; |
| 3 |
|
| 4 |
/** The tooltip div, lazily created. */ |
| 5 |
const tooltip = (() => { |
| 6 |
let value: HTMLDivElement | null = null; |
| 7 |
return () => { |
| 8 |
if (value === null) { |
| 9 |
value = document.createElement("div"); |
| 10 |
value.className = "tooltip"; |
| 11 |
document.body.appendChild(value); |
| 12 |
} |
| 13 |
return value; |
| 14 |
}; |
| 15 |
})(); |
| 16 |
|
| 17 |
/** Hide the tooltip. */ |
| 18 |
const hide = (): void => { |
| 19 |
const t = tooltip(); |
| 20 |
t.style.opacity = "0"; |
| 21 |
}; |
| 22 |
|
| 23 |
/** Some small utilities to create tooltip contents. */ |
| 24 |
export const domHelpers = { |
| 25 |
/** Create a <br> element. */ |
| 26 |
br: (): HTMLBRElement => document.createElement("br"), |
| 27 |
/** Create a <em> element with the given content. */ |
| 28 |
em: (content: string): HTMLElement => { |
| 29 |
const em = document.createElement("em"); |
| 30 |
em.textContent = content; |
| 31 |
return em; |
| 32 |
}, |
| 33 |
/** Create a text node for the given text. */ |
| 34 |
t: (text: string): Text => document.createTextNode(text), |
| 35 |
/** Create a <pre> element with the given content. */ |
| 36 |
pre: (content: string): HTMLPreElement => { |
| 37 |
const pre = document.createElement("pre"); |
| 38 |
pre.textContent = content; |
| 39 |
return pre; |
| 40 |
}, |
| 41 |
}; |
| 42 |
|
| 43 |
export type TooltipContent = (HTMLElement | Text)[]; |
| 44 |
|
| 45 |
/** |
| 46 |
* Svelte action to have the given element act on mouse to show a tooltip. |
| 47 |
* |
| 48 |
* The tooltip will be positioned at the cursor and is given a tooltip getter |
| 49 |
* per <g> element. |
| 50 |
*/ |
| 51 |
export const followingTooltip: Action<SVGElement, () => TooltipContent> = ( |
| 52 |
node, |
| 53 |
text, |
| 54 |
) => { |
| 55 |
let getter = text; |
| 56 |
/** Event listener to have the tooltip follow the mouse. */ |
| 57 |
function followMouse(event: MouseEvent) { |
| 58 |
const t = tooltip(); |
| 59 |
t.style.opacity = "1"; |
| 60 |
t.style.left = `${event.pageX.toString()}px`; |
| 61 |
t.style.top = `${(event.pageY - 15).toString()}px`; |
| 62 |
} |
| 63 |
node.addEventListener("mouseenter", () => { |
| 64 |
const t = tooltip(); |
| 65 |
t.replaceChildren(...getter()); |
| 66 |
}); |
| 67 |
node.addEventListener("mousemove", followMouse); |
| 68 |
node.addEventListener("mouseleave", hide); |
| 69 |
|
| 70 |
return { |
| 71 |
destroy: hide, |
| 72 |
update(t) { |
| 73 |
getter = t; |
| 74 |
}, |
| 75 |
}; |
| 76 |
}; |
| 77 |
|
| 78 |
/** A function to find the closest node and the content to show in the tooltip. */ |
| 79 |
export type TooltipFindNode = ( |
| 80 |
x: number, |
| 81 |
y: number, |
| 82 |
) => [number, number, TooltipContent] | undefined; |
| 83 |
|
| 84 |
/** |
| 85 |
* Svelte action to have the given <g> element act on mouse to show a tooltip. |
| 86 |
* |
| 87 |
* The parameter to the tooltip is a function that takes a position (relative |
| 88 |
* to the container) as input and should return the position of the tooltip, |
| 89 |
* i.e., the found node, again relative to the container and the desired |
| 90 |
* content of the tooltip. |
| 91 |
*/ |
| 92 |
export const positionedTooltip: Action<SVGGElement, TooltipFindNode> = ( |
| 93 |
node, |
| 94 |
find, |
| 95 |
) => { |
| 96 |
function mousemove(event: MouseEvent) { |
| 97 |
const [xPointer, yPointer] = pointer(event); |
| 98 |
const res = find(xPointer, yPointer); |
| 99 |
const matrix = node.getScreenCTM(); |
| 100 |
if (res && matrix) { |
| 101 |
const [x, y, content] = res; |
| 102 |
const t = tooltip(); |
| 103 |
t.style.opacity = "1"; |
| 104 |
t.replaceChildren(...content); |
| 105 |
t.style.left = `${(window.scrollX + x + matrix.e).toString()}px`; |
| 106 |
t.style.top = `${(window.scrollY + y + matrix.f - 15).toString()}px`; |
| 107 |
} else { |
| 108 |
hide(); |
| 109 |
} |
| 110 |
} |
| 111 |
node.addEventListener("mousemove", mousemove); |
| 112 |
node.addEventListener("mouseleave", hide); |
| 113 |
|
| 114 |
return { |
| 115 |
destroy: hide, |
| 116 |
}; |
| 117 |
}; |