CodeEditor.svelte 910 Bytes
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
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
<script lang="ts">
	import { basicSetup, EditorView } from 'codemirror';
	import { keymap, placeholder } from '@codemirror/view';
	import { EditorState } from '@codemirror/state';

	import { acceptCompletion } from '@codemirror/autocomplete';
	import { indentWithTab } from '@codemirror/commands';

	import { python } from '@codemirror/lang-python';
	import { oneDark } from '@codemirror/theme-one-dark';

	import { onMount } from 'svelte';

	export let value = '';

	onMount(() => {
		// python code editor, highlight python code
		const codeEditor = new EditorView({
			state: EditorState.create({
				doc: value,
				extensions: [
					basicSetup,
					keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab]),
					python(),
					oneDark,
					placeholder('Enter your code here...')
				]
			}),
			parent: document.getElementById('code-textarea')
		});
	});
</script>

<div id="code-textarea" />