Skip to content

Commit

Permalink
Merge pull request #17 from binhtran432k/featur/editor
Browse files Browse the repository at this point in the history
feat: enhance monaco editor
  • Loading branch information
binhtran432k authored Jun 19, 2024
2 parents f30151a + b705f30 commit be57da2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"type": "module",
"dependencies": {
"clsx": "^2.1.1"
"clsx": "^2.1.1",
"monaco-editor": "^0.49.0"
}
}
33 changes: 33 additions & 0 deletions packages/www/src/lib/components/Editor.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
import { onDestroy, onMount } from 'svelte';
/** @type {HTMLDivElement|undefined} */
let divElement;
/** @type {import('monaco-editor')|undefined} */
let monaco;
/** @type {import('monaco-editor').editor.IStandaloneCodeEditor|undefined} */
let editor;
/** @type {import('monaco-editor').editor.IStandaloneEditorConstructionOptions} */
const editorOptions = {
minimap: { enabled: false },
theme: 'vs-dark',
overviewRulerLanes: 0
};
onMount(async () => {
if (!divElement) {
throw new Error('div element is undefined');
}
monaco = (await import('$lib/utils/monaco')).default;
editor = monaco.editor.create(divElement, editorOptions);
editor.setValue('Hello World');
});
onDestroy(() => {
editor?.dispose();
});
</script>
<div class="flex h-full flex-col">
<div bind:this={divElement} id="editor" class="h-full flex-grow overflow-hidden" />
</div>
34 changes: 34 additions & 0 deletions packages/www/src/lib/utils/monaco.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as monaco from 'monaco-editor';

// Import the workers in a production-safe way.
// This is different than in Monaco's documentation for Vite,
// but avoids a weird error ("Unexpected usage") at runtime
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';

self.MonacoEnvironment = {
getWorker: function (_, label) {
switch (label) {
case 'json':
return new jsonWorker();
case 'css':
case 'scss':
case 'less':
return new cssWorker();
case 'html':
case 'handlebars':
case 'razor':
return new htmlWorker();
case 'typescript':
case 'javascript':
return new tsWorker();
default:
return new editorWorker();
}
}
};

export default monaco;
3 changes: 2 additions & 1 deletion packages/www/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script>
import Editor from '$lib/components/Editor.svelte';
import Navbar from '$lib/components/Navbar.svelte';
</script>

<div class="flex h-full flex-col overflow-hidden">
<Navbar />
TODO: Add body
<Editor />
</div>

<style>
Expand Down

0 comments on commit be57da2

Please sign in to comment.