]> BookStack Code Mirror - bookstack/blobdiff - resources/js/services/dom.ts
ZIP Imports: Added API examples, finished testing
[bookstack] / resources / js / services / dom.ts
index c3817536c85422c8d0e480cbd05f267be3f6633f..8696fe81639c84aea40294dc9b3c1db376ca129f 100644 (file)
@@ -256,4 +256,22 @@ export function findTargetNodeAndOffset(parentNode: HTMLElement, offset: number)
 export function hashElement(element: HTMLElement): string {
     const normalisedElemText = (element.textContent || '').replace(/\s{2,}/g, '');
     return cyrb53(normalisedElemText);
+}
+
+/**
+ * Find the closest scroll container parent for the given element
+ * otherwise will default to the body element.
+ */
+export function findClosestScrollContainer(start: HTMLElement): HTMLElement {
+    let el: HTMLElement|null = start;
+    do {
+        const computed = window.getComputedStyle(el);
+        if (computed.overflowY === 'scroll') {
+            return el;
+        }
+
+        el = el.parentElement;
+    } while (el);
+
+    return document.body;
 }
\ No newline at end of file