|
1 | 1 | <script>
|
2 | 2 | import { get_repl_context } from '$lib/context.js';
|
3 |
| - import { BROWSER } from 'esm-env'; |
4 | 3 | import { marked } from 'marked';
|
5 | 4 | import CodeMirror from '../CodeMirror.svelte';
|
6 | 5 | import AstView from './AstView.svelte';
|
7 |
| - import Compiler from './Compiler.js'; |
8 | 6 | import CompilerOptions from './CompilerOptions.svelte';
|
9 | 7 | import PaneWithPanel from './PaneWithPanel.svelte';
|
10 | 8 | import Viewer from './Viewer.svelte';
|
11 | 9 |
|
12 |
| - export let svelteUrl; |
13 |
| -
|
14 | 10 | /** @type {string | null} */
|
15 | 11 | export let status;
|
16 | 12 |
|
17 |
| - /** @type {import('$lib/types').StartOrEnd | null} */ |
18 |
| - export let sourceErrorLoc = null; |
19 |
| -
|
20 | 13 | /** @type {import('$lib/types').MessageDetails | null} */
|
21 | 14 | export let runtimeError = null;
|
22 | 15 |
|
|
35 | 28 | /** @type {'light' | 'dark'} */
|
36 | 29 | export let previewTheme;
|
37 | 30 |
|
38 |
| - /** |
39 |
| - * @param {import('$lib/types').File} file |
40 |
| - * @param {import('svelte/compiler').CompileOptions} options |
41 |
| - */ |
42 |
| - export async function set(file, options) { |
43 |
| - selected_type = file.type; |
| 31 | + /** @type {import('../types').File | null} */ |
| 32 | + export let selected; |
44 | 33 |
|
45 |
| - if (file.type === 'json') { |
| 34 | + /** @type {import('../workers/workers').CompileMessageData | null} */ |
| 35 | + export let compiled; |
| 36 | +
|
| 37 | + $: if (selected) { |
| 38 | + if (selected.type === 'json') { |
46 | 39 | js_editor.set({ code: `/* Select a component to see its compiled code */`, lang: 'js' });
|
47 | 40 | css_editor.set({ code: `/* Select a component to see its compiled code */`, lang: 'css' });
|
48 |
| - return; |
49 |
| - } |
50 |
| -
|
51 |
| - if (file.type === 'md') { |
52 |
| - markdown = marked(file.source); |
53 |
| - return; |
54 |
| - } |
55 |
| -
|
56 |
| - if (!compiler) return console.error('Compiler not initialized.'); |
57 |
| -
|
58 |
| - const compiled = await compiler.compile(file, options, showAst); |
59 |
| - if (!js_editor) return; // unmounted |
60 |
| -
|
61 |
| - js_editor.set({ |
62 |
| - code: compiled.js, |
63 |
| - lang: 'js' |
64 |
| - }); |
65 |
| - css_editor.set({ code: compiled.css, lang: 'css' }); |
66 |
| - ast = compiled.ast; |
67 |
| - } |
68 |
| -
|
69 |
| - /** |
70 |
| - * @param {import('$lib/types').File} selected |
71 |
| - * @param {import('svelte/compiler').CompileOptions} options |
72 |
| - */ |
73 |
| - export async function update(selected, options) { |
74 |
| - if (selected.type === 'json') return; |
75 |
| -
|
76 |
| - if (selected.type === 'md') { |
| 41 | + } else if (selected.type === 'md') { |
77 | 42 | markdown = marked(selected.source);
|
78 |
| - return; |
| 43 | + } else if (compiled) { |
| 44 | + js_editor.set({ code: compiled.result.js, lang: 'js' }); |
| 45 | + css_editor.set({ code: compiled.result.css, lang: 'css' }); |
79 | 46 | }
|
80 |
| -
|
81 |
| - if (!compiler) return console.error('Compiler not initialized.'); |
82 |
| -
|
83 |
| - const { result, metadata } = await compiler.compile(selected, options, showAst); |
84 |
| -
|
85 |
| - js_editor.update({ code: result.js, lang: 'js' }); |
86 |
| - css_editor.update({ code: result.css, lang: 'css' }); |
87 |
| - $runes_mode = metadata?.runes; |
88 |
| - ast = result.ast; |
89 | 47 | }
|
90 | 48 |
|
91 |
| - const { module_editor, runes_mode } = get_repl_context(); |
92 |
| -
|
93 |
| - const compiler = BROWSER ? new Compiler(svelteUrl) : null; |
| 49 | + const { module_editor } = get_repl_context(); |
94 | 50 |
|
95 | 51 | /** @type {CodeMirror} */
|
96 | 52 | let js_editor;
|
|
100 | 56 |
|
101 | 57 | /** @type {'result' | 'js' | 'css' | 'ast'} */
|
102 | 58 | let view = 'result';
|
103 |
| - let selected_type = ''; |
104 | 59 | let markdown = '';
|
105 | 60 |
|
106 | 61 | /** @type {import('svelte/types/compiler/interfaces').Ast} */
|
107 | 62 | let ast;
|
108 | 63 | </script>
|
109 | 64 |
|
110 | 65 | <div class="view-toggle">
|
111 |
| - {#if selected_type === 'md'} |
| 66 | + {#if selected?.type === 'md'} |
112 | 67 | <button class="active">Markdown</button>
|
113 | 68 | {:else}
|
114 | 69 | <button class:active={view === 'result'} on:click={() => (view = 'result')}>Result</button>
|
|
121 | 76 | </div>
|
122 | 77 |
|
123 | 78 | <!-- component viewer -->
|
124 |
| -<div class="tab-content" class:visible={selected_type !== 'md' && view === 'result'}> |
| 79 | +<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'result'}> |
125 | 80 | <Viewer
|
126 | 81 | bind:error={runtimeError}
|
127 | 82 | {status}
|
|
133 | 88 | </div>
|
134 | 89 |
|
135 | 90 | <!-- js output -->
|
136 |
| -<div class="tab-content" class:visible={selected_type !== 'md' && view === 'js'}> |
| 91 | +<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'js'}> |
137 | 92 | {#if embedded}
|
138 |
| - <CodeMirror bind:this={js_editor} errorLoc={sourceErrorLoc} readonly /> |
| 93 | + <CodeMirror bind:this={js_editor} readonly /> |
139 | 94 | {:else}
|
140 | 95 | <PaneWithPanel pos="50%" panel="Compiler options">
|
141 | 96 | <div slot="main">
|
142 |
| - <CodeMirror bind:this={js_editor} errorLoc={sourceErrorLoc} readonly /> |
| 97 | + <CodeMirror bind:this={js_editor} readonly /> |
143 | 98 | </div>
|
144 | 99 |
|
145 | 100 | <div slot="panel-body">
|
|
150 | 105 | </div>
|
151 | 106 |
|
152 | 107 | <!-- css output -->
|
153 |
| -<div class="tab-content" class:visible={selected_type !== 'md' && view === 'css'}> |
154 |
| - <CodeMirror bind:this={css_editor} errorLoc={sourceErrorLoc} readonly /> |
| 108 | +<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'css'}> |
| 109 | + <CodeMirror bind:this={css_editor} readonly /> |
155 | 110 | </div>
|
156 | 111 |
|
157 | 112 | <!-- ast output -->
|
158 | 113 | {#if showAst}
|
159 |
| - <div class="tab-content" class:visible={selected_type !== 'md' && view === 'ast'}> |
| 114 | + <div class="tab-content" class:visible={selected?.type !== 'md' && view === 'ast'}> |
160 | 115 | <!-- ast view interacts with the module editor, wait for it first -->
|
161 | 116 | {#if $module_editor}
|
162 |
| - <AstView {ast} autoscroll={selected_type !== 'md' && view === 'ast'} /> |
| 117 | + <AstView {ast} autoscroll={selected?.type !== 'md' && view === 'ast'} /> |
163 | 118 | {/if}
|
164 | 119 | </div>
|
165 | 120 | {/if}
|
166 | 121 |
|
167 | 122 | <!-- markdown output -->
|
168 |
| -<div class="tab-content" class:visible={selected_type === 'md'}> |
| 123 | +<div class="tab-content" class:visible={selected?.type === 'md'}> |
169 | 124 | <iframe title="Markdown" srcdoc={markdown} />
|
170 | 125 | </div>
|
171 | 126 |
|
|
0 commit comments