-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathRepl.svelte
More file actions
357 lines (318 loc) · 7.85 KB
/
Repl.svelte
File metadata and controls
357 lines (318 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
<script lang="ts">
import { SplitPane } from '@rich_harris/svelte-split-pane';
import { ScreenToggle } from '@sveltejs/site-kit/components';
import { BROWSER } from 'esm-env';
import { writable } from 'svelte/store';
import Bundler from './Bundler.svelte.js';
import ComponentSelector from './Input/ComponentSelector.svelte';
import Output from './Output/Output.svelte';
import { set_repl_context } from './context.js';
import { Workspace, type File } from './Workspace.svelte.js';
import Editor from './Editor/Editor.svelte';
import type { ReplContext } from './types.js';
interface Props {
svelteVersion?: string;
embedded?: boolean | 'output-only';
orientation?: 'columns' | 'rows';
relaxed?: boolean;
can_escape?: boolean;
fixed?: boolean;
fixedPos?: number;
injectedJS?: string;
injectedCSS?: string;
previewTheme?: 'light' | 'dark';
showOutput?: boolean;
onversion?: (version: string) => void;
onchange?: () => void;
download?: () => void;
/**
* Invoked whenever there's a bundler or runtime error
*/
onerror?: (error: Error) => void;
}
let {
svelteVersion = 'latest',
embedded = false,
orientation = 'columns',
relaxed = false,
can_escape = false,
fixed = false,
fixedPos = 50,
injectedJS = '',
injectedCSS = '',
previewTheme = 'light',
showOutput = true,
onversion,
onchange = () => {},
download,
onerror
}: Props = $props();
// TODO pass in real data
const dummy: File = {
type: 'file',
name: 'App.svelte',
basename: 'App.svelte',
contents: '',
text: true
};
const workspace: Workspace = new Workspace([dummy], {
initial: 'App.svelte',
// svelte-ignore state_referenced_locally
svelte_version: svelteVersion,
onupdate() {
rebundle();
onchange?.();
},
onreset() {
// Return promise so we can await it in the workspace
return rebundle();
}
});
// TODO get rid
export function toJSON() {
return {
imports: bundler!.result?.imports ?? [],
files: workspace.file_nodes,
tailwind: workspace.tailwind,
aliases: workspace.aliases
};
}
// Our own playground / v0 need this
export async function set(data: {
files: File[];
tailwind?: boolean;
aliases?: Record<string, string>;
}) {
// Await promise so that users (v0 in this case) can know when the bundling is done
await workspace.reset(
data.files,
{ tailwind: data.tailwind ?? false, aliases: data.aliases },
'App.svelte'
);
}
// v0 needs this
export function get_asts() {
return Object.fromEntries(
Object.entries(workspace.compiled).map(([name, compiled]) => [
name,
compiled.result?.ast ?? null
])
);
}
// TODO get rid
export function markSaved() {
workspace.mark_saved();
}
const toggleable: ReplContext['toggleable'] = writable(false);
function rebundle() {
return bundler!.bundle(workspace.files as File[], {
svelte_version: workspace.svelte_version,
tailwind: workspace.tailwind,
fragments: workspace.compiler_options.fragments,
async: workspace.compiler_options.async,
aliases: workspace.aliases
});
}
async function migrate() {
if (!can_migrate) return; // belt and braces — button is already disabled
workspace.update_file({
...workspace.current!,
contents: migration!.code
});
}
let width = $state(0);
let toggled = $state(false);
let status: string | null = $state(null);
let runtime_error: Error | null = $state(null);
let status_visible = $state(false);
let status_timeout: NodeJS.Timeout | undefined = undefined;
const bundler = BROWSER
? new Bundler({
// svelte-ignore state_referenced_locally
svelte_version: svelteVersion,
onversion: (version, supports_async) => {
workspace.set_svelte_version(version);
workspace.supports_async = supports_async;
if (!supports_async && workspace.compiler_options.async) {
workspace.update_compiler_options({ async: false });
}
onversion?.(version);
},
onstatus: (message) => {
if (message) {
// show bundler status, but only after time has elapsed, to
// prevent the banner flickering
if (!status_visible && !status_timeout) {
status_timeout = setTimeout(() => {
status_visible = true;
}, 400);
}
} else {
clearTimeout(status_timeout);
status_visible = false;
status_timeout = undefined;
}
status = message;
},
onerror: (message) => {
runtime_error = new Error(message);
}
})
: null;
set_repl_context({
bundler,
toggleable,
workspace,
get svelteVersion() {
// we want this to be reactive since we are checking in
// the compiler options
return svelteVersion;
}
});
function before_unload(event: BeforeUnloadEvent) {
if (Object.keys(workspace.modified).length > 0) {
event.preventDefault();
event.returnValue = '';
}
}
let mobile = $derived(width < 540);
$effect(() => {
$toggleable = mobile && orientation === 'columns' && embedded !== 'output-only';
});
$effect(() => {
if (runtime_error) {
onerror?.(runtime_error);
}
});
$effect(() => {
if (bundler?.result?.error) {
onerror?.(bundler.result.error as Error);
}
});
let runes = $derived(
workspace.current.name.endsWith('.svelte.js') ||
(workspace.current_compiled?.result?.metadata.runes ?? false)
);
let migration = $derived(workspace.current_compiled?.migration);
let can_migrate = $derived(migration ? migration.code !== workspace.current?.contents : false);
</script>
<svelte:window onbeforeunload={before_unload} />
<div
class="container {embedded === 'output-only' ? '' : 'container-normal'}"
class:embedded
class:toggleable={$toggleable}
bind:clientWidth={width}
>
<div class="viewport" class:output={showOutput} class:transition={toggled}>
<SplitPane
id="main"
type={orientation}
pos="{embedded === 'output-only'
? 0
: mobile || fixed
? fixedPos
: orientation === 'rows'
? 60
: 50}%"
min={embedded === 'output-only' ? '0px' : '100px'}
max="-4.1rem"
>
{#snippet a()}
<section>
<ComponentSelector {runes} {onchange} {workspace} {can_migrate} {migrate} {download} />
<Editor {workspace} />
</section>
{/snippet}
{#snippet b()}
<section>
<Output
status={status_visible ? status : null}
{embedded}
{relaxed}
{can_escape}
{injectedJS}
{injectedCSS}
{previewTheme}
{workspace}
bind:runtimeError={runtime_error}
/>
</section>
{/snippet}
</SplitPane>
</div>
{#if $toggleable}
<ScreenToggle
bind:checked={
() => showOutput,
(v) => {
toggled ||= true;
showOutput = v;
}
}
/>
{/if}
</div>
<style>
.container {
position: relative;
flex: 1;
height: 100%;
min-height: 0;
background: var(--sk-bg-1);
padding: 0;
&.embedded {
height: 100%;
}
section {
position: relative;
padding: var(--sk-pane-controls-height) 0 0 0;
height: 100%;
box-sizing: border-box;
:global {
& > :first-child {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: var(--sk-pane-controls-height);
box-sizing: border-box;
}
& > :last-child {
width: 100%;
height: 100%;
}
}
}
:global [data-pane='main'] > svelte-split-pane-divider::after {
height: calc(100% - var(--sk-pane-controls-height));
top: var(--sk-pane-controls-height);
}
}
.viewport {
height: 100%;
}
.toggleable .viewport {
width: 200%;
height: calc(100% - var(--sk-pane-controls-height));
}
.toggleable .viewport.output {
transform: translate(-50%);
}
.toggleable .viewport.transition {
transition: transform 0.3s;
}
/* on mobile, override the <SplitPane> controls */
@media (max-width: 799px) {
.container-normal :global {
[data-pane='main'] {
--pos: 50% !important;
}
[data-pane='editor'] {
--pos: 5.4rem !important;
}
[data-pane] svelte-split-pane-divider {
cursor: default;
}
}
}
</style>