UNPKG

@sidequest/dashboard

Version:

@sidequest/dashboard is the web dashboard for Sidequest, a distributed background job queue system.

86 lines (69 loc) 2.65 kB
// DEV: This file was generated by Claude Sonnet 4.5 and it works :) // Honestly, I didn't want to write this myself because it's boring af let savedSelection = null; // Function to save selection window.saveSelection = function saveSelection() { const selection = window.getSelection(); if (selection.rangeCount > 0 && selection.toString().length > 0) { const range = selection.getRangeAt(0); savedSelection = { text: selection.toString(), startOffset: range.startOffset, endOffset: range.endOffset, startContainerPath: getNodePath(range.startContainer), endContainerPath: getNodePath(range.endContainer), }; } }; // Function to get path to a node for later reconstruction function getNodePath(node) { const path = []; let current = node; const root = document.body; // Use body as the root instead of job-details while (current && current !== root && current !== document.documentElement) { const parent = current.parentNode; if (!parent) break; const siblings = Array.from(parent.childNodes); const index = siblings.indexOf(current); path.unshift({ index, nodeType: current.nodeType }); current = parent; } return path; } // Function to get node from path function getNodeFromPath(path) { const root = document.body; // Use body as the root instead of job-details if (!root || !path) return null; let current = root; for (const step of path) { const children = Array.from(current.childNodes); if (step.index >= children.length) return null; current = children[step.index]; if (current.nodeType !== step.nodeType) return null; } return current; } // Function to restore selection window.restoreSelection = function restoreSelection() { if (!savedSelection) return; try { const startContainer = getNodeFromPath(savedSelection.startContainerPath); const endContainer = getNodeFromPath(savedSelection.endContainerPath); if (startContainer && endContainer) { const range = document.createRange(); range.setStart(startContainer, savedSelection.startOffset); range.setEnd(endContainer, savedSelection.endOffset); const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); } } catch (e) { // Selection restoration failed, try fallback with text search console.debug("Could not restore selection:", e); } savedSelection = null; }; // We don't do a htmx:afterSwap restore here because we do it in layout.ejs after hljs.highlightAll() document.addEventListener("htmx:beforeSwap", () => { window.saveSelection(); });