]> BookStack Code Mirror - bookstack/blob - resources/js/services/util.js
CSS: Removed redundant calc
[bookstack] / resources / js / services / util.js
1 /**
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
10  * @returns {Function}
11  */
12 export function debounce(func, waitMs, immediate) {
13     let timeout;
14     return function debouncedWrapper(...args) {
15         const context = this;
16         const later = function debouncedTimeout() {
17             timeout = null;
18             if (!immediate) func.apply(context, args);
19         };
20         const callNow = immediate && !timeout;
21         clearTimeout(timeout);
22         timeout = setTimeout(later, waitMs);
23         if (callNow) func.apply(context, args);
24     };
25 }
26
27 /**
28  * Scroll and highlight an element.
29  * @param {HTMLElement} element
30  */
31 export function scrollAndHighlightElement(element) {
32     if (!element) return;
33
34     let parent = element;
35     while (parent.parentElement) {
36         parent = parent.parentElement;
37         if (parent.nodeName === 'DETAILS' && !parent.open) {
38             parent.open = true;
39         }
40     }
41
42     element.scrollIntoView({behavior: 'smooth'});
43
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;
48     setTimeout(() => {
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;
56         };
57         element.addEventListener('transitionend', listener);
58     }, 1000);
59 }
60
61 /**
62  * Escape any HTML in the given 'unsafe' string.
63  * Take from https://stackoverflow.com/a/6234804.
64  * @param {String} unsafe
65  * @returns {string}
66  */
67 export function escapeHtml(unsafe) {
68     return unsafe
69         .replace(/&/g, '&')
70         .replace(/</g, '&lt;')
71         .replace(/>/g, '&gt;')
72         .replace(/"/g, '&quot;')
73         .replace(/'/g, '&#039;');
74 }
75
76 /**
77  * Generate a random unique ID.
78  *
79  * @returns {string}
80  */
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()}`);
85 }
86
87 /**
88  * Create a promise that resolves after the given time.
89  * @param {int} timeMs
90  * @returns {Promise}
91  */
92 export function wait(timeMs) {
93     return new Promise(res => {
94         setTimeout(res, timeMs);
95     });
96 }