-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathRepl.svelte
297 lines (260 loc) · 6.54 KB
/
Repl.svelte
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
<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.js';
import ComponentSelector from './Input/ComponentSelector.svelte';
import Output from './Output/Output.svelte';
import { set_repl_context } from './context.js';
import { Workspace, Editor, type File } from 'editor';
import type { Bundle, ReplContext } from './types.js';
interface Props {
packagesUrl?: string;
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';
onchange?: () => void;
download?: () => void;
}
let {
packagesUrl = 'https://unpkg.com',
svelteVersion = 'latest',
embedded = false,
orientation = 'columns',
relaxed = false,
can_escape = false,
fixed = false,
fixedPos = 50,
injectedJS = '',
injectedCSS = '',
previewTheme = 'light',
onchange = () => {},
download
}: Props = $props();
// TODO pass in real data
const dummy: File = {
type: 'file',
name: 'App.svelte',
basename: 'App.svelte',
contents: '',
text: true
};
const workspace = new Workspace([dummy], {
initial: 'App.svelte',
svelte_version: svelteVersion,
onupdate() {
rebundle();
onchange?.();
},
onreset() {
rebundle();
}
});
// TODO get rid
export function toJSON() {
return {
imports: $bundle?.imports ?? [],
files: workspace.files,
tailwind: workspace.tailwind
};
}
// TODO get rid
export async function set(data: { files: File[]; tailwind?: boolean }) {
workspace.reset(data.files, { tailwind: data.tailwind ?? false }, 'App.svelte');
}
// TODO get rid
export function markSaved() {
workspace.mark_saved();
}
const bundle: ReplContext['bundle'] = writable(null);
const toggleable: ReplContext['toggleable'] = writable(false);
set_repl_context({
bundle,
toggleable,
workspace,
svelteVersion
});
let current_token: Symbol;
async function rebundle() {
const token = (current_token = Symbol());
const result = await bundler!.bundle(workspace.files as File[], {
tailwind: workspace.tailwind,
// @ts-ignore
templatingMode: workspace.compiler_options.templatingMode
});
if (token === current_token) $bundle = result as Bundle;
}
async function migrate() {
if (!can_migrate) return; // belt and braces — button is already disabled
workspace.update_file({
...workspace.current!,
contents: migration!.code
});
rebundle();
}
let width = $state(0);
let show_output = $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({
packages_url: packagesUrl,
svelte_version: svelteVersion,
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;
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';
});
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={show_output}>
<SplitPane
id="main"
type={orientation === 'rows' ? 'vertical' : 'horizontal'}
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}
runtimeError={status_visible ? runtime_error : null}
/>
</section>
{/snippet}
</SplitPane>
</div>
{#if $toggleable}
<ScreenToggle bind:checked={show_output} />
{/if}
</div>
<style>
.container {
position: relative;
flex: 1;
height: 100%;
min-height: 0;
background: var(--sk-bg-1);
padding: 0;
&.embedded {
height: 100%;
}
:global {
section {
position: relative;
padding: var(--sk-pane-controls-height) 0 0 0;
height: 100%;
box-sizing: border-box;
& > :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%;
}
}
[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));
transition: transform 0.3s;
}
.toggleable .viewport.output {
transform: translate(-50%);
}
/* 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>