|
1 | 1 | <script> |
| 2 | + import { browser } from '$app/environment'; |
2 | 3 | import { page } from '$app/stores'; |
3 | 4 | import { readable, get } from 'svelte/store'; |
4 | 5 |
|
|
99 | 100 | */ |
100 | 101 | export let theme = 'default'; |
101 | 102 |
|
| 103 | + ///// Collapse behavior /////////////////////////////////////////// |
| 104 | +
|
| 105 | + /** |
| 106 | + * Will show a collapse bar at the bottom of the component, that can be used to hide and show the output. |
| 107 | + * |
| 108 | + * Default is `false`. |
| 109 | + */ |
| 110 | + export let collapsible = false; |
| 111 | +
|
| 112 | + let collapsed = false; |
| 113 | + if (browser && collapsible) setCollapse(); |
| 114 | +
|
| 115 | + /** |
| 116 | + * @param {boolean|undefined} status |
| 117 | + */ |
| 118 | + function setCollapse(status = undefined) { |
| 119 | + let data; |
| 120 | + const route = $page.route.id ?? ''; |
| 121 | + try { |
| 122 | + data = JSON.parse(sessionStorage.SuperDebug); |
| 123 | + if (!('collapsed' in data)) data.collapsed = {}; |
| 124 | + data.collapsed[route] = |
| 125 | + status === undefined ? data.collapsed[route] ?? false : status; |
| 126 | + } catch { |
| 127 | + data = { |
| 128 | + collapsed: { |
| 129 | + [route]: false |
| 130 | + } |
| 131 | + }; |
| 132 | + } |
| 133 | +
|
| 134 | + if (status !== undefined) { |
| 135 | + sessionStorage.SuperDebug = JSON.stringify(data); |
| 136 | + } |
| 137 | +
|
| 138 | + collapsed = data.collapsed[route]; |
| 139 | + } |
| 140 | +
|
| 141 | + /////////////////////////////////////////////////////////////////// |
| 142 | +
|
102 | 143 | /** |
103 | 144 | * @param {unknown} json |
104 | 145 | * @returns {string} |
|
278 | 319 | {/if} |
279 | 320 | <pre |
280 | 321 | class="super-debug--pre {label === '' ? 'pt-4' : 'pt-0'}" |
| 322 | + class:hidden={collapsed} |
281 | 323 | bind:this={ref}><code class="super-debug--code" |
282 | 324 | ><slot |
283 | 325 | >{#if assertPromise($debugData, raw, promise)}{#await $debugData}<div |
|
290 | 332 | )}{/await}{:else}{@html syntaxHighlight($debugData)}{/if}</slot |
291 | 333 | ></code |
292 | 334 | ></pre> |
| 335 | + {#if collapsible} |
| 336 | + <button |
| 337 | + on:click={() => setCollapse(!collapsed)} |
| 338 | + class="super-debug--collapse" |
| 339 | + > |
| 340 | + <svg |
| 341 | + xmlns="http://www.w3.org/2000/svg" |
| 342 | + width="20" |
| 343 | + height="20" |
| 344 | + viewBox="0 0 24 24" |
| 345 | + class:rotated={collapsed} |
| 346 | + ><path |
| 347 | + fill="currentColor" |
| 348 | + d="M4.08 11.92L12 4l7.92 7.92l-1.42 1.41l-5.5-5.5V22h-2V7.83l-5.5 5.5l-1.42-1.41M12 4h10V2H2v2h10Z" |
| 349 | + /></svg |
| 350 | + > |
| 351 | + </button> |
| 352 | + {/if} |
293 | 353 | </div> |
294 | 354 | {/if} |
295 | 355 |
|
|
340 | 400 | padding-top: 1em; |
341 | 401 | } |
342 | 402 |
|
| 403 | + .hidden { |
| 404 | + height: 0; |
| 405 | + overflow: hidden; |
| 406 | + } |
| 407 | +
|
| 408 | + .rotated { |
| 409 | + transform: rotate(180deg); |
| 410 | + } |
| 411 | +
|
343 | 412 | .super-debug { |
344 | 413 | --_sd-bg-color: var( |
345 | 414 | --sd-bg-color, |
|
351 | 420 | overflow: hidden; |
352 | 421 | } |
353 | 422 |
|
| 423 | + .super-debug--collapse { |
| 424 | + display: block; |
| 425 | + width: 100%; |
| 426 | + color: rgba(255, 255, 255, 0.25); |
| 427 | + background-color: rgba(255, 255, 255, 0.15); |
| 428 | + padding: 5px 0; |
| 429 | + display: flex; |
| 430 | + justify-content: center; |
| 431 | + border-color: transparent; |
| 432 | + margin: 0; |
| 433 | + padding: 3px 0; |
| 434 | + } |
| 435 | +
|
| 436 | + .super-debug--collapse:is(:hover) { |
| 437 | + color: rgba(255, 255, 255, 0.35); |
| 438 | + background-color: rgba(255, 255, 255, 0.25); |
| 439 | + } |
| 440 | +
|
354 | 441 | .super-debug--status { |
355 | 442 | display: flex; |
356 | 443 | padding: 1em; |
|
382 | 469 | color: var(--sd-code-default, var(--sd-vscode-code-default, #999)); |
383 | 470 | background-color: var(--_sd-bg-color); |
384 | 471 | font-size: 1em; |
385 | | - padding: 1em 0 0 1em; |
| 472 | + margin-bottom: 0; |
| 473 | + padding: 1em 0 1em 1em; |
386 | 474 | } |
387 | 475 |
|
388 | 476 | .info { |
|
0 commit comments