]> BookStack Code Mirror - bookstack/blob - resources/js/services/dom.ts
537af816a907ad6e7a98ed4c1643fdc91ce40f8f
[bookstack] / resources / js / services / dom.ts
1 /**
2  * Check if the given param is a HTMLElement
3  */
4 export function isHTMLElement(el: any): el is HTMLElement {
5     return el instanceof HTMLElement;
6 }
7
8 /**
9  * Create a new element with the given attrs and children.
10  * Children can be a string for text nodes or other elements.
11  */
12 export function elem(tagName: string, attrs: Record<string, string> = {}, children: Element[]|string[] = []): HTMLElement {
13     const el = document.createElement(tagName);
14
15     for (const [key, val] of Object.entries(attrs)) {
16         if (val === null) {
17             el.removeAttribute(key);
18         } else {
19             el.setAttribute(key, val);
20         }
21     }
22
23     for (const child of children) {
24         if (typeof child === 'string') {
25             el.append(document.createTextNode(child));
26         } else {
27             el.append(child);
28         }
29     }
30
31     return el;
32 }
33
34 /**
35  * Run the given callback against each element that matches the given selector.
36  */
37 export function forEach(selector: string, callback: (el: Element) => any) {
38     const elements = document.querySelectorAll(selector);
39     for (const element of elements) {
40         callback(element);
41     }
42 }
43
44 /**
45  * Helper to listen to multiple DOM events
46  */
47 export function onEvents(listenerElement: Element|null, events: string[], callback: (e: Event) => any): void {
48     if (listenerElement) {
49         for (const eventName of events) {
50             listenerElement.addEventListener(eventName, callback);
51         }
52     }
53 }
54
55 /**
56  * Helper to run an action when an element is selected.
57  * A "select" is made to be accessible, So can be a click, space-press or enter-press.
58  */
59 export function onSelect(elements: HTMLElement|HTMLElement[], callback: (e: Event) => any): void {
60     if (!Array.isArray(elements)) {
61         elements = [elements];
62     }
63
64     for (const listenerElement of elements) {
65         listenerElement.addEventListener('click', callback);
66         listenerElement.addEventListener('keydown', event => {
67             if (event.key === 'Enter' || event.key === ' ') {
68                 event.preventDefault();
69                 callback(event);
70             }
71         });
72     }
73 }
74
75 /**
76  * Listen to key press on the given element(s).
77  */
78 function onKeyPress(key: string, elements: HTMLElement|HTMLElement[], callback: (e: KeyboardEvent) => any): void {
79     if (!Array.isArray(elements)) {
80         elements = [elements];
81     }
82
83     const listener = (event: KeyboardEvent) => {
84         if (event.key === key) {
85             callback(event);
86         }
87     };
88
89     elements.forEach(e => e.addEventListener('keydown', listener));
90 }
91
92 /**
93  * Listen to enter press on the given element(s).
94  */
95 export function onEnterPress(elements: HTMLElement|HTMLElement[], callback: (e: KeyboardEvent) => any): void {
96     onKeyPress('Enter', elements, callback);
97 }
98
99 /**
100  * Listen to escape press on the given element(s).
101  */
102 export function onEscapePress(elements: HTMLElement|HTMLElement[], callback: (e: KeyboardEvent) => any): void {
103     onKeyPress('Escape', elements, callback);
104 }
105
106 /**
107  * Set a listener on an element for an event emitted by a child
108  * matching the given childSelector param.
109  * Used in a similar fashion to jQuery's $('listener').on('eventName', 'childSelector', callback)
110  */
111 export function onChildEvent(
112     listenerElement: HTMLElement,
113     childSelector: string,
114     eventName: string,
115     callback: (this: HTMLElement, e: Event, child: HTMLElement) => any
116 ): void {
117     listenerElement.addEventListener(eventName, (event: Event) => {
118         const matchingChild = (event.target as HTMLElement|null)?.closest(childSelector) as HTMLElement;
119         if (matchingChild) {
120             callback.call(matchingChild, event, matchingChild);
121         }
122     });
123 }
124
125 /**
126  * Look for elements that match the given selector and contain the given text.
127  * Is case-insensitive and returns the first result or null if nothing is found.
128  */
129 export function findText(selector: string, text: string): Element|null {
130     const elements = document.querySelectorAll(selector);
131     text = text.toLowerCase();
132     for (const element of elements) {
133         if ((element.textContent || '').toLowerCase().includes(text) && isHTMLElement(element)) {
134             return element;
135         }
136     }
137     return null;
138 }
139
140 /**
141  * Show a loading indicator in the given element.
142  * This will effectively clear the element.
143  */
144 export function showLoading(element: HTMLElement): void {
145     element.innerHTML = '<div class="loading-container"><div></div><div></div><div></div></div>';
146 }
147
148 /**
149  * Get a loading element indicator element.
150  */
151 export function getLoading(): HTMLElement {
152     const wrap = document.createElement('div');
153     wrap.classList.add('loading-container');
154     wrap.innerHTML = '<div></div><div></div><div></div>';
155     return wrap;
156 }
157
158 /**
159  * Remove any loading indicators within the given element.
160  */
161 export function removeLoading(element: HTMLElement): void {
162     const loadingEls = element.querySelectorAll('.loading-container');
163     for (const el of loadingEls) {
164         el.remove();
165     }
166 }
167
168 /**
169  * Convert the given html data into a live DOM element.
170  * Initiates any components defined in the data.
171  */
172 export function htmlToDom(html: string): HTMLElement {
173     const wrap = document.createElement('div');
174     wrap.innerHTML = html;
175     window.$components.init(wrap);
176     const firstChild = wrap.children[0];
177     if (!isHTMLElement(firstChild)) {
178         throw new Error('Could not find child HTMLElement when creating DOM element from HTML');
179     }
180
181     return firstChild;
182 }
183
184 export function normalizeNodeTextOffsetToParent(node: Node, offset: number, parentElement: HTMLElement): number {
185     if (!parentElement.contains(node)) {
186         throw new Error('ParentElement must be a prent of element');
187     }
188
189     let normalizedOffset = offset;
190     let currentNode: Node|null = node.nodeType === Node.TEXT_NODE ?
191         node : node.childNodes[offset];
192
193     while (currentNode !== parentElement && currentNode) {
194         if (currentNode.previousSibling) {
195             currentNode = currentNode.previousSibling;
196             normalizedOffset += (currentNode.textContent?.length || 0);
197         } else {
198             currentNode = currentNode.parentNode;
199         }
200     }
201
202     return normalizedOffset;
203 }