Skip to content

Commit 99b65c5

Browse files
committed
[refactor] move monaco logic into editor component
1 parent c0c41f8 commit 99b65c5

File tree

3 files changed

+159
-171
lines changed

3 files changed

+159
-171
lines changed

src/lib/client/monaco/monaco.js

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -43,101 +43,4 @@ if (browser) {
4343
monaco = await import('monaco-editor');
4444
}
4545

46-
/**
47-
* file extension -> monaco language
48-
* @type {Record<string, string>}
49-
* */
50-
const types = {
51-
js: 'javascript',
52-
ts: 'typescript',
53-
svelte: 'html' // TODO
54-
};
55-
56-
/**
57-
* URL -> model
58-
* @type {Map<string, import('monaco-editor').editor.ITextModel>}
59-
* */
60-
const models = new Map();
61-
62-
let notify_adapter = true;
63-
64-
/**
65-
*
66-
* @param {import('$lib/types').Stub[]} stubs
67-
* @param {() => import('$lib/types').Adapter} adapter
68-
* @param {boolean} [notify]
69-
*/
70-
function update_files(stubs, adapter, notify = true) {
71-
notify_adapter = notify;
72-
for (const stub of stubs) {
73-
if (stub.type === 'directory') {
74-
continue;
75-
}
76-
77-
const model = models.get(stub.name);
78-
79-
if (model) {
80-
const value = model.getValue();
81-
82-
if (stub.contents !== value) {
83-
model.pushEditOperations(
84-
[],
85-
[
86-
{
87-
range: model.getFullModelRange(),
88-
text: stub.contents
89-
}
90-
],
91-
() => null
92-
);
93-
}
94-
} else {
95-
create_file(stub, adapter);
96-
}
97-
}
98-
99-
for (const [name, model] of models) {
100-
if (!stubs.some((stub) => stub.name === name)) {
101-
model.dispose();
102-
models.delete(name);
103-
}
104-
}
105-
notify = true;
106-
}
107-
108-
/**
109-
* @param {import('$lib/types').FileStub} stub
110-
* @param {() => import('$lib/types').Adapter} adapter
111-
*/
112-
function create_file(stub, adapter) {
113-
// deep-copy stub so we can mutate it and not create a memory leak
114-
stub = JSON.parse(JSON.stringify(stub));
115-
116-
const type = /** @type {string} */ (stub.basename.split('.').pop());
117-
118-
const model = monaco.editor.createModel(
119-
stub.contents,
120-
types[type] || type,
121-
new monaco.Uri().with({ path: stub.name })
122-
);
123-
124-
model.updateOptions({ tabSize: 2 });
125-
126-
model.onDidChangeContent(() => {
127-
const contents = model.getValue();
128-
129-
if (notify_adapter) {
130-
stub.contents = contents;
131-
adapter()?.update([stub]);
132-
}
133-
});
134-
135-
models.set(stub.name, model);
136-
}
137-
138-
/** @param {string} name */
139-
function get_model(name) {
140-
return models.get(name);
141-
}
142-
143-
export { monaco, get_model, create_file, update_files };
46+
export { monaco };

src/routes/tutorial/[slug]/+page.svelte

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import SplitPane from '$lib/components/SplitPane.svelte';
66
import Editor from './Editor.svelte';
77
import Folder from './Folder.svelte';
8-
import { browser, dev } from '$app/environment';
8+
import { dev } from '$app/environment';
99
import ImageViewer from './ImageViewer.svelte';
1010
import Sidebar from './Sidebar.svelte';
1111
import Chrome from './Chrome.svelte';
@@ -25,14 +25,6 @@
2525
/** @type {Map<string, string>} */
2626
let expected;
2727
28-
/**
29-
* Only available in the browser
30-
* @type {Promise<typeof import('$lib/client/monaco/monaco.js')>}
31-
* */
32-
const monaco_import = browser ? import('$lib/client/monaco/monaco.js') : new Promise(() => {});
33-
34-
/** @type {import('monaco-editor').editor.ITextModel} */
35-
let current_model;
3628
/** @type {import('$lib/types').Stub[]}*/
3729
let current_stubs = [];
3830
@@ -71,18 +63,11 @@
7163
const { select } = setContext('filetree', {
7264
select: async (file) => {
7365
$selected = file;
74-
const { get_model } = await monaco_import;
75-
current_model = /** @type {import("monaco-editor").editor.ITextModel} */ (
76-
get_model(file.name)
77-
);
7866
},
7967
8068
add: async (stubs) => {
8169
current_stubs = [...current_stubs, ...stubs];
8270
83-
const { update_files } = await monaco_import;
84-
update_files(current_stubs, () => adapter);
85-
8671
await load_files(current_stubs);
8772
8873
if (stubs[0].type === 'file') {
@@ -109,9 +94,6 @@
10994
return new_stub;
11095
});
11196
112-
const { update_files } = await monaco_import;
113-
update_files(current_stubs, () => adapter);
114-
11597
await load_files(current_stubs);
11698
11799
if (to_rename.type === 'file') {
@@ -123,9 +105,6 @@
123105
const out = current_stubs.filter((s) => s.name.startsWith(stub.name));
124106
current_stubs = current_stubs.filter((s) => !out.includes(s));
125107
126-
const { update_files } = await monaco_import;
127-
update_files(current_stubs, () => adapter);
128-
129108
if ($selected && out.includes($selected)) {
130109
$selected = null;
131110
}
@@ -154,9 +133,6 @@
154133
complete_states = {};
155134
current_stubs = Object.values(data.section.a);
156135
157-
const { update_files } = await monaco_import;
158-
update_files(current_stubs, () => adapter);
159-
160136
select(
161137
/** @type {import('$lib/types').FileStub} */ (
162138
current_stubs.find((stub) => stub.name === data.section.focus)
@@ -410,8 +386,6 @@
410386
411387
completed = !completed;
412388
current_stubs = Object.values(completed ? b : data.section.a);
413-
const { update_files } = await monaco_import;
414-
update_files(current_stubs, () => adapter, false);
415389
adapter?.reset(current_stubs);
416390
417391
completing = false;
@@ -426,7 +400,7 @@
426400
</section>
427401
428402
<section class="editor-container" slot="b">
429-
<Editor model={current_model} />
403+
<Editor stubs={current_stubs} selected={$selected} {adapter} />
430404
<ImageViewer selected={$selected} />
431405
</section>
432406
</SplitPane>

0 commit comments

Comments
 (0)