Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 8.5 KiB
1/* The docs have three pieces of behaviour: a small-screen navigation drawer,
2 the search palette, and Mermaid diagrams. All are progressive: without this
3 script the links and page content still work, and the search button simply
4 does nothing. */
5(() => {
6 const body = document.body;
7
8 /* ---------- navigation drawer ---------- */
9
10 const navToggle = document.querySelector('[data-nav-toggle]');
11 const setNav = (open) => {
12 body.classList.toggle('nav-open', open);
13 if (navToggle) navToggle.setAttribute('aria-expanded', String(open));
14 };
15
16 if (navToggle) {
17 navToggle.addEventListener('click', () => {
18 setNav(!body.classList.contains('nav-open'));
19 });
20 }
21 document.querySelectorAll('[data-nav-close]').forEach((element) => {
22 element.addEventListener('click', () => setNav(false));
23 });
24 document.querySelectorAll('.sidebar a').forEach((link) => {
25 link.addEventListener('click', () => setNav(false));
26 });
27
28 /* ---------- copy buttons ---------- */
29
30 document.querySelectorAll('.content .codehilite').forEach((block) => {
31 const button = document.createElement('button');
32 button.type = 'button';
33 button.className = 'copy-code';
34 button.textContent = 'Copy';
35 button.setAttribute('aria-label', 'Copy code');
36 button.addEventListener('click', async () => {
37 const code = block.querySelector('code');
38 if (!code) return;
39 try {
40 await navigator.clipboard.writeText(code.textContent);
41 button.textContent = 'Copied';
42 } catch (err) {
43 button.textContent = 'Copy failed';
44 }
45 window.setTimeout(() => { button.textContent = 'Copy'; }, 1400);
46 });
47 block.append(button);
48 });
49
50 /* ---------- mermaid diagrams ---------- */
51
52 /* Loaded only on pages with `mermaid: true` in their front matter. Mermaid
53 renders from the .mermaid elements after the DOM is parsed. */
54 const renderMermaid = () => {
55 if (!window.mermaid || !document.querySelector('.mermaid')) return;
56 document.querySelectorAll('.mermaid[data-source]').forEach((element) => {
57 element.textContent = element.dataset.source;
58 });
59 window.mermaid.initialize({
60 startOnLoad: false,
61 securityLevel: 'strict',
62 theme: 'base',
63 fontFamily: 'Lexend, ui-sans-serif, system-ui, sans-serif',
64 themeVariables: {
65 background: '#2d353b',
66 primaryColor: '#343f44',
67 primaryTextColor: '#d3c6aa',
68 primaryBorderColor: '#4f585e',
69 secondaryColor: '#3d484d',
70 tertiaryColor: '#3d484d',
71 lineColor: '#9da9a0',
72 textColor: '#d3c6aa',
73 mainBkg: '#343f44',
74 nodeBorder: '#4f585e',
75 clusterBkg: '#2d353b',
76 clusterBorder: '#4f585e',
77 edgeLabelBackground: '#2d353b',
78 fontSize: '16px',
79 },
80 });
81 window.mermaid.run({ querySelector: '.mermaid' });
82 };
83
84 if (document.readyState === 'loading') {
85 document.addEventListener('DOMContentLoaded', renderMermaid);
86 } else {
87 renderMermaid();
88 }
89
90 /* ---------- search palette ---------- */
91
92 const dialog = document.getElementById('search-palette');
93 const input = document.getElementById('search-query');
94 const results = document.getElementById('search-results');
95 const openers = document.querySelectorAll('[data-search-open]');
96 if (!dialog || !input || !results) return;
97
98 const root = new URL(window.DOCS_BASE_URL || './', window.location.href);
99 const rootUrl = (path) => new URL(path, root).href;
100 const empty = (message) => {
101 results.replaceChildren();
102 const p = document.createElement('p');
103 p.className = 'palette-empty';
104 p.textContent = message;
105 results.append(p);
106 };
107
108 let worker = null;
109 let ready = false;
110 let minLength = 2;
111 let selected = -1;
112 let timer = 0;
113 let returnFocus = null;
114
115 const select = (index) => {
116 const rows = results.querySelectorAll('.palette-row');
117 if (!rows.length) return;
118 selected = (index + rows.length) % rows.length;
119 rows.forEach((row, i) => {
120 const on = i === selected;
121 row.classList.toggle('is-selected', on);
122 row.setAttribute('aria-selected', String(on));
123 });
124 rows[selected].scrollIntoView({ block: 'nearest' });
125 };
126
127 const render = (items) => {
128 results.replaceChildren();
129 selected = -1;
130 if (!items.length) {
131 empty('No results found.');
132 return;
133 }
134 items.forEach((item) => {
135 const row = document.createElement('a');
136 row.className = 'palette-row';
137 row.href = rootUrl(item.location);
138 row.setAttribute('role', 'option');
139
140 const title = document.createElement('span');
141 title.className = 'palette-row-title';
142 title.textContent = item.title || item.location;
143
144 const where = document.createElement('span');
145 where.className = 'palette-row-where';
146 where.textContent = item.summary || item.location;
147
148 row.append(title, where);
149 row.addEventListener('mouseenter', () => {
150 const rows = [...results.querySelectorAll('.palette-row')];
151 select(rows.indexOf(row));
152 });
153 results.append(row);
154 });
155 };
156
157 const search = () => {
158 const query = input.value.trim();
159 if (!query) {
160 empty('Type to search the documentation.');
161 return;
162 }
163 if (!ready) {
164 empty('Search is still loading…');
165 return;
166 }
167 if (query.length < minLength) {
168 empty(`Type at least ${minLength} characters.`);
169 return;
170 }
171 worker.postMessage({ query });
172 };
173
174 const openSearch = () => {
175 if (dialog.open) return;
176 returnFocus = document.activeElement;
177 if (typeof dialog.showModal === 'function') {
178 dialog.showModal();
179 } else {
180 dialog.setAttribute('open', '');
181 }
182 input.focus();
183 input.select();
184 search();
185 };
186
187 const closeSearch = () => {
188 if (typeof dialog.close === 'function') {
189 dialog.close();
190 } else {
191 dialog.removeAttribute('open');
192 }
193 if (returnFocus && typeof returnFocus.focus === 'function') returnFocus.focus();
194 };
195
196 openers.forEach((button) => button.addEventListener('click', openSearch));
197 document.querySelectorAll('[data-search-close]').forEach((button) => {
198 button.addEventListener('click', closeSearch);
199 });
200
201 // A click on the dialog itself is a click on the backdrop; clicks on the form
202 // and result rows have a deeper target.
203 dialog.addEventListener('click', (event) => {
204 if (event.target === dialog) closeSearch();
205 });
206
207 input.addEventListener('input', () => {
208 window.clearTimeout(timer);
209 timer = window.setTimeout(search, 140);
210 });
211
212 const form = dialog.querySelector('[data-search-form]');
213 if (form) {
214 form.addEventListener('submit', (event) => {
215 event.preventDefault();
216 const selectedRow = results.querySelector('.palette-row.is-selected');
217 if (selectedRow) {
218 window.location.href = selectedRow.href;
219 } else {
220 search();
221 }
222 });
223 }
224
225 input.addEventListener('keydown', (event) => {
226 if (event.key === 'ArrowDown') {
227 event.preventDefault();
228 select(selected + 1);
229 } else if (event.key === 'ArrowUp') {
230 event.preventDefault();
231 select(selected - 1);
232 } else if (event.key === 'Enter' && selected >= 0) {
233 event.preventDefault();
234 const selectedRow = results.querySelectorAll('.palette-row')[selected];
235 if (selectedRow) window.location.href = selectedRow.href;
236 }
237 });
238
239 document.addEventListener('keydown', (event) => {
240 const target = event.target;
241 const tag = (target && target.tagName ? target.tagName : '').toLowerCase();
242 const typing = tag === 'input' || tag === 'textarea' || tag === 'select' || (target && target.isContentEditable);
243 const slash = event.key === '/' && !typing && !event.metaKey && !event.ctrlKey && !event.altKey;
244 const commandK = (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'k';
245 if (slash || commandK) {
246 event.preventDefault();
247 openSearch();
248 return;
249 }
250 if (event.key === 'Escape' && body.classList.contains('nav-open')) {
251 setNav(false);
252 }
253 });
254
255 if (!window.Worker) {
256 empty('Search needs a browser with Web Workers.');
257 return;
258 }
259
260 try {
261 worker = new Worker(rootUrl('search/worker.js'));
262 } catch (err) {
263 empty('Search is unavailable in this browser.');
264 return;
265 }
266 worker.addEventListener('message', (event) => {
267 const data = event.data;
268 if (data.config) minLength = data.config.min_search_length || minLength;
269 if (data.allowSearch) {
270 ready = true;
271 if (dialog.open) search();
272 }
273 if (data.results) render(data.results);
274 });
275 worker.postMessage({ init: true });
276})();
277