]> BookStack Code Mirror - bookstack/blob - resources/js/services/util.ts
Deps: Updated composer/npm packages, fixed test namespace
[bookstack] / resources / js / services / util.ts
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  */
8 export function debounce(func: Function, waitMs: number, immediate: boolean): Function {
9     let timeout: number|null = null;
10     return function debouncedWrapper(this: any, ...args: any[]) {
11         const context: any = this;
12         const later = function debouncedTimeout() {
13             timeout = null;
14             if (!immediate) func.apply(context, args);
15         };
16         const callNow = immediate && !timeout;
17         if (timeout) {
18             clearTimeout(timeout);
19         }
20         timeout = window.setTimeout(later, waitMs);
21         if (callNow) func.apply(context, args);
22     };
23 }
24
25 function isDetailsElement(element: HTMLElement): element is HTMLDetailsElement {
26     return element.nodeName === 'DETAILS';
27 }
28
29 /**
30  * Scroll-to and highlight an element.
31  */
32 export function scrollAndHighlightElement(element: HTMLElement): void {
33     if (!element) return;
34
35     // Open up parent <details> elements if within
36     let parent = element;
37     while (parent.parentElement) {
38         parent = parent.parentElement;
39         if (isDetailsElement(parent) && !parent.open) {
40             parent.open = true;
41         }
42     }
43
44     element.scrollIntoView({behavior: 'smooth'});
45
46     const highlight = getComputedStyle(document.body).getPropertyValue('--color-link');
47     element.style.outline = `2px dashed ${highlight}`;
48     element.style.outlineOffset = '5px';
49     element.style.removeProperty('transition');
50     setTimeout(() => {
51         element.style.transition = 'outline linear 3s';
52         element.style.outline = '2px dashed rgba(0, 0, 0, 0)';
53         const listener = () => {
54             element.removeEventListener('transitionend', listener);
55             element.style.removeProperty('transition');
56             element.style.removeProperty('outline');
57             element.style.removeProperty('outlineOffset');
58         };
59         element.addEventListener('transitionend', listener);
60     }, 1000);
61 }
62
63 /**
64  * Escape any HTML in the given 'unsafe' string.
65  * Take from https://stackoverflow.com/a/6234804.
66  */
67 export function escapeHtml(unsafe: string): string {
68     return unsafe
69         .replace(/&/g, '&amp;')
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 export function uniqueId(): string {
80     // eslint-disable-next-line no-bitwise
81     const S4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
82     return (`${S4() + S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`);
83 }
84
85 /**
86  * Generate a random smaller unique ID.
87  */
88 export function uniqueIdSmall(): string {
89     // eslint-disable-next-line no-bitwise
90     const S4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
91     return S4();
92 }
93
94 /**
95  * Create a promise that resolves after the given time.
96  */
97 export function wait(timeMs: number): Promise<any> {
98     return new Promise(res => {
99         setTimeout(res, timeMs);
100     });
101 }
102
103 /**
104  * Generate a full URL from the given relative URL, using a base
105  * URL defined in the head of the page.
106  */
107 export function baseUrl(path: string): string {
108     let targetPath = path;
109     const baseUrlMeta = document.querySelector('meta[name="base-url"]');
110     if (!baseUrlMeta) {
111         throw new Error('Could not find expected base-url meta tag in document');
112     }
113
114     let basePath = baseUrlMeta.getAttribute('content') || '';
115     if (basePath[basePath.length - 1] === '/') {
116         basePath = basePath.slice(0, basePath.length - 1);
117     }
118
119     if (targetPath[0] === '/') {
120         targetPath = targetPath.slice(1);
121     }
122
123     return `${basePath}/${targetPath}`;
124 }
125
126 /**
127  * Get the current version of BookStack in use.
128  * Grabs this from the version query used on app assets.
129  */
130 function getVersion(): string {
131     const styleLink = document.querySelector('link[href*="/dist/styles.css?version="]');
132     if (!styleLink) {
133         throw new Error('Could not find expected style link in document for version use');
134     }
135
136     const href = (styleLink.getAttribute('href') || '');
137     return href.split('?version=').pop() || '';
138 }
139
140 /**
141  * Perform a module import, Ensuring the import is fetched with the current
142  * app version as a cache-breaker.
143  */
144 export function importVersioned(moduleName: string): Promise<object> {
145     const importPath = window.baseUrl(`dist/${moduleName}.js?version=${getVersion()}`);
146     return import(importPath);
147 }
148
149 /*
150     cyrb53 (c) 2018 bryc (github.com/bryc)
151     License: Public domain (or MIT if needed). Attribution appreciated.
152     A fast and simple 53-bit string hash function with decent collision resistance.
153     Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
154     Taken from: https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
155 */
156 export function cyrb53(str: string, seed: number = 0): string {
157     let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
158     for(let i = 0, ch; i < str.length; i++) {
159         ch = str.charCodeAt(i);
160         h1 = Math.imul(h1 ^ ch, 2654435761);
161         h2 = Math.imul(h2 ^ ch, 1597334677);
162     }
163     h1  = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
164     h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
165     h2  = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
166     h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
167     return String((4294967296 * (2097151 & h2) + (h1 >>> 0)));
168 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.