Skip to content

Commit ca89ae9

Browse files
committed
migrate component
1 parent aecf35e commit ca89ae9

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

packages/repl/src/lib/Output/Viewer.svelte

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,51 @@
1414
import type { Writable } from 'svelte/store';
1515
import type { BundleResult } from '$lib/public';
1616
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();
3347
3448
const context = get_repl_context();
3549
bundle ??= context.bundle;
3650
37-
let logs: Log[] = [];
51+
let logs: Log[] = $state([]);
3852
let log_group_stack: Log[][] = [];
3953
let current_log_group = logs;
4054
41-
let iframe: HTMLIFrameElement;
42-
let pending_imports = 0;
55+
let iframe: HTMLIFrameElement = $state();
56+
let pending_imports = $state(0);
4357
let pending = false;
4458
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);
4862
4963
let log_height = 90;
5064
let prev_height: number;
@@ -99,7 +113,11 @@
99113
};
100114
});
101115
102-
$: if (ready) proxy?.iframe_command('set_theme', { theme });
116+
$effect(() => {
117+
if (ready) {
118+
proxy?.iframe_command('set_theme', { theme });
119+
}
120+
});
103121
104122
async function apply_bundle($bundle: BundleResult | null | undefined) {
105123
if (!$bundle) return;
@@ -198,17 +216,23 @@
198216
inited = true;
199217
}
200218
201-
$: if (ready) apply_bundle($bundle);
219+
$effect(() => {
220+
if (ready) {
221+
apply_bundle($bundle);
222+
}
223+
});
202224
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+
});
212236
213237
function show_error(e: CompileError & { loc: { line: number; column: number } }) {
214238
const map = $bundle?.client?.map;
@@ -301,7 +325,14 @@
301325
{#if !onLog}
302326
<PaneWithPanel pos="100%" panel="Console" {main}>
303327
{#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+
>
305336
{#if logs.length > 0}
306337
({logs.length})
307338
{/if}

0 commit comments

Comments
 (0)