project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits
| 1 | //! The command palette: the Ctrl/Cmd+K dialog that the layout renders, and `GET /palette`, |
| 2 | //! the results fragment the dialog fetches as the user types. |
| 3 | //! |
| 4 | //! Commands, notes, and notebooks share one `role="listbox"`. Links are `<a>` elements. |
| 5 | //! Actions that stay on the page (theme, new notebook) and form posts are `<button>`s that |
| 6 | //! the inline script runs. The shortcut itself is handled by `SHELL_SCRIPT` in `shell.rs`, |
| 7 | //! which calls `window.openPalette`. |
| 8 | |
| 9 | use HashSet; |
| 10 | use Arc; |
| 11 | |
| 12 | use |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | ; |
| 18 | |
| 19 | use crateshell; |
| 20 | use crate; |
| 21 | use crate; |
| 22 | use crate; |
| 23 | use crateALL as SCHEMES; |
| 24 | use crate; |
| 25 | |
| 26 | /// Most notes, and most notebooks, listed in one result set. |
| 27 | const LIMIT: usize = 8; |
| 28 | /// How long the palette reads long notes for full-text hits; title hits come anyway. |
| 29 | const PALETTE_BUDGET: Duration = from_millis; |
| 30 | |
| 31 | /// Classes of the dialog. It is hidden until `showModal()`, then fades in. |
| 32 | const DIALOG: &str = "m-0 mx-auto top-[15vh] bottom-auto h-auto w-full max-w-none overflow-visible border-0 bg-transparent p-0 text-popover-foreground sm:w-[560px] backdrop:bg-black/40 opacity-0 open:opacity-100 starting:open:opacity-0 transition-[opacity,display] transition-discrete duration-150 ease-out motion-reduce:transition-none"; |
| 33 | /// Classes of the panel inside the dialog. |
| 34 | const PANEL: &str = "flex flex-col overflow-hidden border border-border bg-popover shadow-float sm:rounded-float"; |
| 35 | /// Classes of one result row. |
| 36 | const OPTION: &str = "flex min-h-9 w-full items-center gap-3 rounded-control px-3 py-1.5 text-left text-sm transition-colors hover:bg-foreground/5"; |
| 37 | |
| 38 | /// Picks the text for `lang`. Every string in the palette goes through here with both |
| 39 | /// languages, so a missing Turkish text does not compile. |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | /// The `q` parameter of the palette fragment. |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | /// One row of the palette. |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | /// The commands, in both languages. Labels are matched against the query. |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | /// One command per color scheme, named "Color scheme: <Name>" (the name is not translated). |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | /// A note row. `snippet` is the search excerpt, if this is a search result. |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | /// GET /palette?q=...: the palette's results as an HTML fragment, without the page layout. |
| 149 | /// An empty query lists the commands, the 8 most recently updated notes, and all notebooks |
| 150 | /// (up to 8). A query lists the matching commands, the search results, and matching notebooks. |
| 151 | |
| 152 | async |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | /// A labelled group of options inside the listbox. |
| 243 | |
| 244 | async |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | /// One option: a link when it has an `href`, otherwise a button the script runs. |
| 256 | |
| 257 | async |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | /// The text and badges inside an option. |
| 290 | |
| 291 | async |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | /// The palette dialog, rendered once in the layout. It stays closed until the shortcut |
| 314 | /// (or `window.openPalette`) opens it. |
| 315 | |
| 316 | pub async |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | /// Palette behaviour. Defines `window.openPalette` (called by the shortcut in `shell.rs`). |
| 368 | /// Results load on open and 120ms after each keystroke; a new request aborts the last one. |
| 369 | /// Up and Down move the highlight (`aria-activedescendant`), Enter runs it, and Esc closes |
| 370 | /// the dialog natively. A click on the backdrop closes it. Closing returns focus to the |
| 371 | /// element that was focused before. |
| 372 | const PALETTE_SCRIPT: &str = r#" |
| 373 | (function () { |
| 374 | var dialog = document.getElementById('palette'); |
| 375 | if (!dialog) return; |
| 376 | var input = document.getElementById('palette-input'); |
| 377 | var results = document.getElementById('palette-results'); |
| 378 | var desktop = window.matchMedia('(min-width: 1024px)'); |
| 379 | var opener = null; |
| 380 | var active = -1; |
| 381 | var timer = null; |
| 382 | var controller = null; |
| 383 | |
| 384 | function options() { |
| 385 | return Array.prototype.slice.call(results.querySelectorAll('[role="option"]')); |
| 386 | } |
| 387 | function setActive(index) { |
| 388 | var list = options(); |
| 389 | if (list.length === 0) { |
| 390 | active = -1; |
| 391 | input.removeAttribute('aria-activedescendant'); |
| 392 | return; |
| 393 | } |
| 394 | if (index < 0) index = list.length - 1; |
| 395 | if (index >= list.length) index = 0; |
| 396 | list.forEach(function (opt, i) { |
| 397 | var on = i === index; |
| 398 | opt.setAttribute('aria-selected', on ? 'true' : 'false'); |
| 399 | opt.classList.toggle('marker', on); |
| 400 | }); |
| 401 | active = index; |
| 402 | input.setAttribute('aria-activedescendant', list[index].id); |
| 403 | list[index].scrollIntoView({ block: 'nearest' }); |
| 404 | } |
| 405 | function assignIds() { |
| 406 | options().forEach(function (opt, i) { opt.id = 'palette-opt-' + i; }); |
| 407 | } |
| 408 | function load(q) { |
| 409 | if (controller) controller.abort(); |
| 410 | controller = new AbortController(); |
| 411 | var signal = controller.signal; |
| 412 | fetch('/palette?q=' + encodeURIComponent(q), { |
| 413 | headers: { Accept: 'text/html' }, |
| 414 | cache: 'no-store', |
| 415 | signal: signal |
| 416 | }) |
| 417 | .then(function (res) { |
| 418 | if (!res.ok) throw new Error('palette ' + res.status); |
| 419 | return res.text(); |
| 420 | }) |
| 421 | .then(function (html) { |
| 422 | if (signal.aborted) return; |
| 423 | results.innerHTML = html; |
| 424 | assignIds(); |
| 425 | setActive(0); |
| 426 | }) |
| 427 | .catch(function () { /* aborted or failed: keep the last results */ }); |
| 428 | } |
| 429 | function newNotebook() { |
| 430 | if (desktop.matches) { |
| 431 | document.documentElement.dataset.sidebar = 'expanded'; |
| 432 | try { localStorage.setItem('sidebar', 'expanded'); } catch (err) {} |
| 433 | } else { |
| 434 | var toggle = document.getElementById('drawer-open'); |
| 435 | if (toggle) toggle.click(); |
| 436 | } |
| 437 | var field = document.querySelector('#sidebar input[name="name"]'); |
| 438 | if (field) setTimeout(function () { field.focus(); }, 0); |
| 439 | } |
| 440 | function post(url, json) { |
| 441 | var fields = JSON.parse(json || '{}'); |
| 442 | if (url === '/notes') { |
| 443 | var nb = document.querySelector('#new-note-form input[name="notebook_id"]'); |
| 444 | fields.notebook_id = nb ? nb.value : ''; |
| 445 | } |
| 446 | if (url === '/lang') fields.back = location.pathname + location.search; |
| 447 | var form = document.createElement('form'); |
| 448 | form.method = 'post'; |
| 449 | form.action = url; |
| 450 | Object.keys(fields).forEach(function (name) { |
| 451 | var field = document.createElement('input'); |
| 452 | field.type = 'hidden'; |
| 453 | field.name = name; |
| 454 | field.value = fields[name]; |
| 455 | form.appendChild(field); |
| 456 | }); |
| 457 | document.body.appendChild(form); |
| 458 | form.submit(); |
| 459 | } |
| 460 | function run(opt) { |
| 461 | var action = opt.getAttribute('data-action'); |
| 462 | if (action === 'theme') { |
| 463 | if (typeof window.cycleTheme === 'function') window.cycleTheme(); |
| 464 | dialog.close(); |
| 465 | } else if (action === 'scheme') { |
| 466 | if (typeof window.setScheme === 'function') window.setScheme(opt.getAttribute('data-fields')); |
| 467 | dialog.close(); |
| 468 | } else if (action === 'new-notebook') { |
| 469 | opener = null; |
| 470 | dialog.close(); |
| 471 | newNotebook(); |
| 472 | } else if (action === 'post') { |
| 473 | opener = null; |
| 474 | post(opt.getAttribute('data-url'), opt.getAttribute('data-fields')); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | window.openPalette = function () { |
| 479 | if (dialog.open) { |
| 480 | input.focus(); |
| 481 | return; |
| 482 | } |
| 483 | opener = document.activeElement; |
| 484 | input.value = ''; |
| 485 | dialog.showModal(); |
| 486 | input.focus(); |
| 487 | load(''); |
| 488 | }; |
| 489 | |
| 490 | input.addEventListener('input', function () { |
| 491 | clearTimeout(timer); |
| 492 | timer = setTimeout(function () { load(input.value.trim()); }, 120); |
| 493 | }); |
| 494 | input.addEventListener('keydown', function (e) { |
| 495 | if (e.isComposing) return; |
| 496 | if (e.key === 'ArrowDown') { |
| 497 | e.preventDefault(); |
| 498 | setActive(active + 1); |
| 499 | } else if (e.key === 'ArrowUp') { |
| 500 | e.preventDefault(); |
| 501 | setActive(active - 1); |
| 502 | } else if (e.key === 'Enter') { |
| 503 | var opt = options()[active]; |
| 504 | if (opt) { |
| 505 | e.preventDefault(); |
| 506 | opt.click(); |
| 507 | } |
| 508 | } |
| 509 | }); |
| 510 | results.addEventListener('mousemove', function (e) { |
| 511 | var opt = e.target.closest('[role="option"]'); |
| 512 | if (!opt) return; |
| 513 | var i = options().indexOf(opt); |
| 514 | if (i !== active) setActive(i); |
| 515 | }); |
| 516 | results.addEventListener('click', function (e) { |
| 517 | var opt = e.target.closest('[role="option"]'); |
| 518 | if (!opt || opt.tagName === 'A') return; |
| 519 | e.preventDefault(); |
| 520 | run(opt); |
| 521 | }); |
| 522 | dialog.addEventListener('click', function (e) { |
| 523 | if (e.target === dialog) dialog.close(); |
| 524 | }); |
| 525 | dialog.addEventListener('close', function () { |
| 526 | clearTimeout(timer); |
| 527 | if (controller) controller.abort(); |
| 528 | var back = opener; |
| 529 | opener = null; |
| 530 | if (back && back.isConnected && typeof back.focus === 'function') back.focus(); |
| 531 | }); |
| 532 | })(); |
| 533 | "#; |