4 * Returns a function, that, as long as it continues to be invoked, will not
5 * be triggered. The function will be called after it stops being called for
6 * N milliseconds. If `immediate` is passed, trigger the function on the
7 * leading edge, instead of the trailing.
8 * @attribution https://davidwalsh.name/javascript-debounce-function
14 export function debounce(func, wait, immediate) {
17 const context = this, args = arguments;
18 const later = function() {
20 if (!immediate) func.apply(context, args);
22 const callNow = immediate && !timeout;
23 clearTimeout(timeout);
24 timeout = setTimeout(later, wait);
25 if (callNow) func.apply(context, args);
30 * Scroll and highlight an element.
31 * @param {HTMLElement} element
33 export function scrollAndHighlightElement(element) {
35 element.scrollIntoView({behavior: 'smooth'});
37 const color = document.getElementById('custom-styles').getAttribute('data-color-light');
38 const initColor = window.getComputedStyle(element).getPropertyValue('background-color');
39 element.style.backgroundColor = color;
41 element.classList.add('selectFade');
42 element.style.backgroundColor = initColor;
45 element.classList.remove('selectFade');
46 element.style.backgroundColor = '';
51 * Escape any HTML in the given 'unsafe' string.
52 * Take from https://stackoverflow.com/a/6234804.
53 * @param {String} unsafe
56 export function escapeHtml(unsafe) {
58 .replace(/&/g, "&")
59 .replace(/</g, "<")
60 .replace(/>/g, ">")
61 .replace(/"/g, """)
62 .replace(/'/g, "'");
66 * Generate a random unique ID.
70 export function uniqueId() {
71 const S4 = () => (((1+Math.random())*0x10000)|0).toString(16).substring(1);
72 return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());