2 * Returns a function, that, as long as it continues to be invoked, will not
3 * be triggered. The function will be called after it stops being called for
4 * N milliseconds. If `immediate` is passed, trigger the function on the
5 * leading edge, instead of the trailing.
6 * @attribution https://davidwalsh.name/javascript-debounce-function
7 * @param {Function} func
8 * @param {Number} waitMs
9 * @param {Boolean} immediate
12 export function debounce(func, waitMs, immediate) {
14 return function debouncedWrapper(...args) {
16 const later = function debouncedTimeout() {
18 if (!immediate) func.apply(context, args);
20 const callNow = immediate && !timeout;
21 clearTimeout(timeout);
22 timeout = setTimeout(later, waitMs);
23 if (callNow) func.apply(context, args);
28 * Scroll and highlight an element.
29 * @param {HTMLElement} element
31 export function scrollAndHighlightElement(element) {
35 while (parent.parentElement) {
36 parent = parent.parentElement;
37 if (parent.nodeName === 'DETAILS' && !parent.open) {
42 element.scrollIntoView({behavior: 'smooth'});
44 const highlight = getComputedStyle(document.body).getPropertyValue('--color-link');
45 element.style.outline = `2px dashed ${highlight}`;
46 element.style.outlineOffset = '5px';
47 element.style.transition = null;
49 element.style.transition = 'outline linear 3s';
50 element.style.outline = '2px dashed rgba(0, 0, 0, 0)';
51 const listener = () => {
52 element.removeEventListener('transitionend', listener);
53 element.style.transition = null;
54 element.style.outline = null;
55 element.style.outlineOffset = null;
57 element.addEventListener('transitionend', listener);
62 * Escape any HTML in the given 'unsafe' string.
63 * Take from https://stackoverflow.com/a/6234804.
64 * @param {String} unsafe
67 export function escapeHtml(unsafe) {
69 .replace(/&/g, '&')
70 .replace(/</g, '<')
71 .replace(/>/g, '>')
72 .replace(/"/g, '"')
73 .replace(/'/g, ''');
77 * Generate a random unique ID.
81 export function uniqueId() {
82 // eslint-disable-next-line no-bitwise
83 const S4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
84 return (`${S4() + S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`);
88 * Create a promise that resolves after the given time.
92 export function wait(timeMs) {
93 return new Promise(res => {
94 setTimeout(res, timeMs);