]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/scrolling.js
Added license references to readme attribution
[bookstack] / resources / js / wysiwyg / scrolling.js
1 /**
2  * Scroll to a section dictated by the current URL query string, if present.
3  * Used when directly editing a specific section of the page.
4  * @param {Editor} editor
5  */
6 export function scrollToQueryString(editor) {
7     const queryParams = (new URL(window.location)).searchParams;
8     const scrollId = queryParams.get('content-id');
9     if (scrollId) {
10         scrollToText(editor, scrollId);
11     }
12 }
13
14 /**
15  * Override for touch events to allow scrolling on mobile devices.
16  * TODO - Check if still needed or if needs editing.
17  * @param {Editor} editor
18  */
19 export function fixScrollForMobile(editor) {
20     const container = editor.getContainer();
21     const toolbarButtons = container.querySelectorAll('.mce-btn');
22     for (let button of toolbarButtons) {
23         button.addEventListener('touchstart', event => {
24             event.stopPropagation();
25         });
26     }
27 }
28
29 /**
30  * @param {Editor} editor
31  * @param {String} scrollId
32  */
33 function scrollToText(editor, scrollId) {
34     const element = editor.dom.get(encodeURIComponent(scrollId).replace(/!/g, '%21'));
35     if (!element) {
36         return;
37     }
38
39     // scroll the element into the view and put the cursor at the end.
40     element.scrollIntoView();
41     editor.selection.select(element, true);
42     editor.selection.collapse(false);
43     editor.focus();
44 }