notion-enhancer/extension/repo/components@36a2ffc9-27ff-480e-84a7-c7700a7d232d/hook.mjs
dragonwocky a7549cd9db api hooks - enabling core mods to extend the api
moved tooltips to one, preparing to set up sidebar
2021-10-02 00:12:30 +10:00

39 lines
1.2 KiB
JavaScript

/*
* notion-enhancer core: components
* (c) 2021 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (c) 2021 CloudHill <rl.cloudhill@gmail.com> (https://github.com/CloudHill)
* (https://notion-enhancer.github.io/) under the MIT license
*/
let _$tooltip;
export default function (api, db) {
const { web, fmt } = api;
return {
/**
* add a tooltip to show extra information on hover
* @param {HTMLElement} $ref - the element that will trigger the tooltip when hovered
* @param {string} text - the markdown content of the tooltip
*/
tooltip: ($ref, text) => {
if (!_$tooltip) {
_$tooltip = web.html`<div id="enhancer--tooltip"></div>`;
web.render(document.body, _$tooltip);
}
text = fmt.md.render(text);
$ref.addEventListener('mouseover', (event) => {
_$tooltip.innerHTML = text;
_$tooltip.style.display = 'block';
});
$ref.addEventListener('mousemove', (event) => {
_$tooltip.style.top = event.clientY - _$tooltip.clientHeight + 'px';
_$tooltip.style.left = event.clientX - _$tooltip.clientWidth + 'px';
});
$ref.addEventListener('mouseout', (event) => {
_$tooltip.style.display = '';
});
},
};
}