Skip to content

Commit e0cca34

Browse files
committed
get stuff basically working
1 parent 799fd05 commit e0cca34

File tree

4 files changed

+52
-86
lines changed

4 files changed

+52
-86
lines changed

src/lib/client/monaco/monaco.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
99
let monaco;
1010

1111
if (browser) {
12-
monaco = await import('monaco-editor');
13-
1412
// @ts-expect-error
15-
window.monacoEnvironment = {
13+
self.MonacoEnvironment = {
1614
/**
1715
* @param {string} _moduleId
1816
* @param {string} label
@@ -35,6 +33,8 @@ if (browser) {
3533
}
3634
}
3735
};
36+
37+
monaco = await import('monaco-editor');
3838
}
3939

4040
export { monaco };

src/lib/client/viewer/Editor.svelte

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,27 @@
11
<script>
2-
import { onMount, createEventDispatcher } from 'svelte';
3-
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
4-
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
5-
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
6-
import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
7-
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
2+
import { onMount } from 'svelte';
3+
import { monaco } from '$lib/client/monaco/monaco.js';
84
9-
/** @type {import('$lib/types').FileStub | null} */
10-
export let file;
11-
12-
const dispatch = createEventDispatcher();
5+
/** @type {import('monaco-editor').editor.ITextModel} */
6+
export let model;
137
148
/** @type {HTMLDivElement} */
159
let container;
1610
17-
/** @type {import('monaco-editor')} */
18-
let monaco;
19-
2011
/** @type {import('monaco-editor').editor.IStandaloneCodeEditor}*/
2112
let editor;
2213
2314
onMount(() => {
24-
let destroyed = false;
25-
26-
import('monaco-editor').then((module) => {
27-
if (destroyed) return;
28-
29-
monaco = module;
30-
31-
// @ts-ignore
32-
self.MonacoEnvironment = {
33-
/**
34-
* @param {string} _moduleId
35-
* @param {string} label
36-
*/
37-
getWorker: function (_moduleId, label) {
38-
switch (label) {
39-
case 'json':
40-
return new jsonWorker();
41-
case 'css':
42-
case 'scss':
43-
case 'less':
44-
return new cssWorker();
45-
case 'html':
46-
return new htmlWorker();
47-
case 'javascript':
48-
case 'typescript':
49-
return new tsWorker();
50-
default:
51-
return new editorWorker();
52-
}
53-
}
54-
};
55-
56-
editor = monaco.editor.create(container);
57-
});
15+
editor = monaco.editor.create(container);
5816
5917
return () => {
60-
if (editor) editor.dispose();
61-
destroyed = true;
18+
editor.dispose();
6219
};
6320
});
21+
22+
$: if (editor) {
23+
editor.setModel(model);
24+
}
6425
</script>
6526

6627
<div bind:this={container} />
@@ -72,7 +33,6 @@
7233
height: 100%;
7334
resize: none;
7435
border: none;
75-
padding: 1rem;
7636
box-sizing: border-box;
7737
tab-size: 2;
7838
}

src/lib/client/viewer/Viewer.svelte

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import Editor from './Editor.svelte';
55
import Folder from './FileTree/Folder.svelte';
66
7-
const { files, current, selected, started, base, update } = getContext('filetree');
7+
const { files, current, active, started, base, update } = getContext('filetree');
88
</script>
99

1010
<div class="viewer">
@@ -18,16 +18,7 @@
1818
</section>
1919

2020
<section slot="b">
21-
<Editor
22-
file={$selected}
23-
on:change={(e) => {
24-
if ($selected) {
25-
// @ts-ignore for now
26-
$selected.contents = e.detail.value;
27-
update([$selected]);
28-
}
29-
}}
30-
/>
21+
<Editor model={$active} />
3122
</section>
3223
</SplitPane>
3324
</section>

src/routes/tutorial/[slug]/index.svelte

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import Viewer from '$lib/client/viewer/Viewer.svelte';
1818
import TableOfContents from './_/TableOfContents.svelte';
1919
import { monaco } from '$lib/client/monaco/monaco.js';
20+
import { browser } from '$app/env';
2021
2122
/** @type {import('$lib/types').SectionIndex} */
2223
export let index;
@@ -34,8 +35,8 @@
3435
/** @type {import('svelte/store').Writable<import('$lib/types').Stub[]>} */
3536
const files = writable([]);
3637
37-
/** @type {import('svelte/store').Writable<import('monaco-editor').editor.ITextModel[]>} */
38-
const models = writable([]);
38+
/** @type {import('svelte/store').Writable<import('monaco-editor').editor.ITextModel>} */
39+
const active = writable();
3940
4041
/** @type {import('svelte/store').Writable<import('$lib/types').Stub | null>} */
4142
const selected = writable(null);
@@ -44,17 +45,21 @@
4445
4546
const base = writable('');
4647
47-
setContext('filetree', {
48+
/** @type {Map<import('$lib/types').FileStub, import('monaco-editor').editor.ITextModel>} */
49+
const models = new Map();
50+
51+
const { select } = setContext('filetree', {
4852
/** @param {import('$lib/types').FileStub} file */
4953
select: (file) => {
50-
selected.set(file);
54+
$selected = file;
55+
$active = /** @type {import('monaco-editor').editor.ITextModel} */ (models.get(file));
5156
},
5257
5358
current,
5459
5560
files,
5661
57-
models,
62+
active,
5863
5964
selected,
6065
@@ -95,6 +100,13 @@
95100
/** @type {import('$lib/types').Adapter} */
96101
let adapter;
97102
103+
/** @type {Record<string, string>}*/
104+
const types = {
105+
js: 'javascript',
106+
ts: 'typescript',
107+
svelte: 'html' // TODO
108+
};
109+
98110
onMount(() => {
99111
let destroyed = false;
100112
@@ -110,48 +122,51 @@
110122
if (adapter) {
111123
adapter.destroy();
112124
}
125+
126+
clearInterval(interval);
113127
};
114128
});
115129
116130
afterNavigate(async () => {
117-
const data = Object.values(section.a);
131+
const stubs = Object.values(section.a);
118132
119-
console.log(section.a);
133+
files.set(stubs);
120134
121-
files.set(data);
135+
models.forEach((model) => {
136+
model.dispose();
137+
});
138+
models.clear();
122139
123-
for (const model of $models) model.dispose();
124-
$models = [];
140+
stubs.forEach((stub) => {
141+
if (stub.type === 'file') {
142+
const type = /** @type {string} */ (stub.basename.split('.').pop());
125143
126-
data.forEach((file) => {
127-
if (file.type === 'file') {
128-
const type = file.basename.split('.').pop();
129144
const model = monaco.editor.createModel(
130-
file.contents,
131-
type,
132-
new monaco.Uri().with({ path: file.name })
145+
stub.contents,
146+
types[type] || type,
147+
new monaco.Uri().with({ path: stub.name })
133148
);
134149
135150
model.onDidChangeContent(() => {
136151
const value = model.getValue();
137-
file.contents = value;
138-
adapter.update([file]);
152+
stub.contents = value;
153+
adapter.update([stub]);
139154
});
140155
141-
$models.push(model);
156+
models.set(stub, model);
142157
}
143158
});
144159
145-
selected.set(
160+
select(
146161
/** @type {import('$lib/types').FileStub} */ (
147-
data.find((file) => file.name === section.chapter.focus)
162+
stubs.find((stub) => stub.name === section.chapter.focus)
148163
)
149164
);
150165
151166
current.set(section);
152167
153168
await ready;
154-
await adapter.update(data);
169+
await adapter.update(stubs);
155170
156171
while (!$started) {
157172
try {

0 commit comments

Comments
 (0)