|
14 | 14 | import type { Writable } from 'svelte/store'; |
15 | 15 | import type { BundleResult } from '$lib/public'; |
16 | 16 |
|
17 | | - export let error: Error | null; |
18 | | - /** status by Bundler class instance */ |
19 | | - export let status: string | null; |
20 | | - /** sandbox allow-same-origin */ |
21 | | - export let relaxed = false; |
22 | | - /** sandbox allow-popups-to-escape-sandbox (i.e. links within the REPL to other pages work) */ |
23 | | - export let can_escape = false; |
24 | | - /** Any additional JS you may want to inject */ |
25 | | - export let injectedJS = ''; |
26 | | - /** Any additional CSS you may want to inject */ |
27 | | - export let injectedCSS = ''; |
28 | | - export let theme: 'light' | 'dark'; |
29 | | - /** A store containing the current bundle result. Takes precedence over REPL context, if set */ |
30 | | - export let bundle: Writable<BundleResult | null> | undefined = undefined; |
31 | | - /** Called everytime a log is pushed. If this is set, the built-in console coming with the Viewer isn't shown */ |
32 | | - export let onLog: ((logs: Log[]) => void) | undefined = undefined; |
| 17 | + interface Props { |
| 18 | + error: Error | null; |
| 19 | + /** status by Bundler class instance */ |
| 20 | + status: string | null; |
| 21 | + /** sandbox allow-same-origin */ |
| 22 | + relaxed?: boolean; |
| 23 | + /** sandbox allow-popups-to-escape-sandbox (i.e. links within the REPL to other pages work) */ |
| 24 | + can_escape?: boolean; |
| 25 | + /** Any additional JS you may want to inject */ |
| 26 | + injectedJS?: string; |
| 27 | + /** Any additional CSS you may want to inject */ |
| 28 | + injectedCSS?: string; |
| 29 | + theme: 'light' | 'dark'; |
| 30 | + /** A store containing the current bundle result. Takes precedence over REPL context, if set */ |
| 31 | + bundle?: Writable<BundleResult | null> | undefined; |
| 32 | + /** Called everytime a log is pushed. If this is set, the built-in console coming with the Viewer isn't shown */ |
| 33 | + onLog?: ((logs: Log[]) => void) | undefined; |
| 34 | + } |
| 35 | +
|
| 36 | + let { |
| 37 | + error = $bindable(), |
| 38 | + status, |
| 39 | + relaxed = false, |
| 40 | + can_escape = false, |
| 41 | + injectedJS = '', |
| 42 | + injectedCSS = '', |
| 43 | + theme, |
| 44 | + bundle = $bindable(undefined), |
| 45 | + onLog = undefined |
| 46 | + }: Props = $props(); |
33 | 47 |
|
34 | 48 | const context = get_repl_context(); |
35 | 49 | bundle ??= context.bundle; |
36 | 50 |
|
37 | | - let logs: Log[] = []; |
| 51 | + let logs: Log[] = $state([]); |
38 | 52 | let log_group_stack: Log[][] = []; |
39 | 53 | let current_log_group = logs; |
40 | 54 |
|
41 | | - let iframe: HTMLIFrameElement; |
42 | | - let pending_imports = 0; |
| 55 | + let iframe: HTMLIFrameElement = $state(); |
| 56 | + let pending_imports = $state(0); |
43 | 57 | let pending = false; |
44 | 58 |
|
45 | | - let proxy: ReplProxy | null = null; |
46 | | - let ready = false; |
47 | | - let inited = false; |
| 59 | + let proxy: ReplProxy | null = $state(null); |
| 60 | + let ready = $state(false); |
| 61 | + let inited = $state(false); |
48 | 62 |
|
49 | 63 | let log_height = 90; |
50 | 64 | let prev_height: number; |
|
99 | 113 | }; |
100 | 114 | }); |
101 | 115 |
|
102 | | - $: if (ready) proxy?.iframe_command('set_theme', { theme }); |
| 116 | + $effect(() => { |
| 117 | + if (ready) { |
| 118 | + proxy?.iframe_command('set_theme', { theme }); |
| 119 | + } |
| 120 | + }); |
103 | 121 |
|
104 | 122 | async function apply_bundle($bundle: BundleResult | null | undefined) { |
105 | 123 | if (!$bundle) return; |
|
198 | 216 | inited = true; |
199 | 217 | } |
200 | 218 |
|
201 | | - $: if (ready) apply_bundle($bundle); |
| 219 | + $effect(() => { |
| 220 | + if (ready) { |
| 221 | + apply_bundle($bundle); |
| 222 | + } |
| 223 | + }); |
202 | 224 |
|
203 | | - $: if (injectedCSS && proxy && ready) { |
204 | | - proxy.eval( |
205 | | - `{ |
206 | | - const style = document.createElement('style'); |
207 | | - style.textContent = ${JSON.stringify(injectedCSS)}; |
208 | | - document.head.appendChild(style); |
209 | | - }` |
210 | | - ); |
211 | | - } |
| 225 | + $effect(() => { |
| 226 | + if (injectedCSS && proxy && ready) { |
| 227 | + proxy.eval( |
| 228 | + `{ |
| 229 | + const style = document.createElement('style'); |
| 230 | + style.textContent = ${JSON.stringify(injectedCSS)}; |
| 231 | + document.head.appendChild(style); |
| 232 | + }` |
| 233 | + ); |
| 234 | + } |
| 235 | + }); |
212 | 236 |
|
213 | 237 | function show_error(e: CompileError & { loc: { line: number; column: number } }) { |
214 | 238 | const map = $bundle?.client?.map; |
|
301 | 325 | {#if !onLog} |
302 | 326 | <PaneWithPanel pos="100%" panel="Console" {main}> |
303 | 327 | {#snippet header()} |
304 | | - <button class="raised" disabled={logs.length === 0} on:click|stopPropagation={clear_logs}> |
| 328 | + <button |
| 329 | + class="raised" |
| 330 | + disabled={logs.length === 0} |
| 331 | + onclick={(e) => { |
| 332 | + e.stopPropagation(); |
| 333 | + clear_logs(); |
| 334 | + }} |
| 335 | + > |
305 | 336 | {#if logs.length > 0} |
306 | 337 | ({logs.length}) |
307 | 338 | {/if} |
|
0 commit comments