2020-06-22 07:00:45 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2022-02-04 09:36:34 +01:00
|
|
|
import { DebouncedFunc, throttle } from "lodash-es";
|
|
|
|
|
2021-03-21 10:35:25 +01:00
|
|
|
import Tooltip from "./Tooltip.svelte";
|
2020-06-22 07:00:45 +02:00
|
|
|
|
2021-03-21 10:35:25 +01:00
|
|
|
let tooltip: Tooltip | null = null;
|
2021-02-22 14:10:35 +01:00
|
|
|
|
2021-03-21 10:35:25 +01:00
|
|
|
function getOrCreateTooltip(): Tooltip {
|
|
|
|
if (tooltip) {
|
|
|
|
return tooltip;
|
2021-02-22 14:10:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 10:35:25 +01:00
|
|
|
const target = document.createElement("div");
|
|
|
|
tooltip = new Tooltip({ target });
|
|
|
|
document.body.appendChild(target);
|
2021-02-22 14:10:35 +01:00
|
|
|
|
2021-03-21 10:35:25 +01:00
|
|
|
return tooltip;
|
2021-02-22 14:10:35 +01:00
|
|
|
}
|
2020-06-22 07:00:45 +02:00
|
|
|
|
|
|
|
function showTooltipInner(msg: string, x: number, y: number): void {
|
2021-03-21 10:35:25 +01:00
|
|
|
const tooltip = getOrCreateTooltip();
|
2021-02-22 14:10:35 +01:00
|
|
|
|
2021-03-21 10:35:25 +01:00
|
|
|
tooltip.$set({ html: msg, x, y, show: true });
|
2020-06-22 07:00:45 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:21:33 +02:00
|
|
|
export const showTooltip: DebouncedFunc<(msg: string, x: number, y: number) => void> =
|
|
|
|
throttle(showTooltipInner, 16);
|
2020-06-22 07:00:45 +02:00
|
|
|
|
|
|
|
export function hideTooltip(): void {
|
2021-03-21 10:35:25 +01:00
|
|
|
const tooltip = getOrCreateTooltip();
|
2020-06-22 07:00:45 +02:00
|
|
|
showTooltip.cancel();
|
2021-03-21 10:35:25 +01:00
|
|
|
tooltip.$set({ show: false });
|
2020-06-22 07:00:45 +02:00
|
|
|
}
|