2023-05-29 17:07:58 +02:00
|
|
|
<script>
|
|
|
|
import { indentWithTab } from '@codemirror/commands';
|
|
|
|
import { javascript } from '@codemirror/lang-javascript';
|
|
|
|
import { indentOnInput } from '@codemirror/language';
|
|
|
|
import { EditorState } from '@codemirror/state';
|
|
|
|
import { EditorView, keymap } from '@codemirror/view';
|
|
|
|
import { basicSetup } from 'codemirror';
|
2023-05-29 20:51:54 +02:00
|
|
|
import { createEventDispatcher, onMount } from 'svelte';
|
2023-05-29 17:07:58 +02:00
|
|
|
|
|
|
|
export let text = '';
|
2023-05-29 20:51:54 +02:00
|
|
|
export let editor = undefined;
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
let editorParent;
|
2023-05-29 17:07:58 +02:00
|
|
|
|
|
|
|
const editorState = EditorState.create({
|
|
|
|
doc: '',
|
|
|
|
extensions: [
|
|
|
|
basicSetup,
|
|
|
|
keymap.of([ indentWithTab, indentOnInput ]),
|
|
|
|
javascript(),
|
|
|
|
EditorState.tabSize.of(4),
|
|
|
|
EditorView.updateListener.of(e => {
|
2023-05-29 20:51:54 +02:00
|
|
|
if (!e.docChanged) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-29 17:07:58 +02:00
|
|
|
text = e.state.doc.toString();
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
editor = new EditorView({
|
|
|
|
parent: editorParent,
|
|
|
|
state: editorState,
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.dispatch({
|
|
|
|
changes: {
|
|
|
|
from: 0,
|
2023-05-29 20:51:54 +02:00
|
|
|
to: editor.state.doc.length,
|
2023-05-29 17:07:58 +02:00
|
|
|
insert: text,
|
|
|
|
},
|
|
|
|
});
|
2023-05-29 20:51:54 +02:00
|
|
|
|
|
|
|
dispatch('inited', { editor });
|
2023-05-29 17:07:58 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div bind:this={editorParent} class="editor"></div>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.editor {
|
|
|
|
width: 100%;
|
2023-05-29 20:51:54 +02:00
|
|
|
background-color: #fff;
|
2023-05-29 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.editor :global(.cm-editor) {
|
|
|
|
overflow: auto;
|
2023-05-29 20:51:54 +02:00
|
|
|
height: 100%;
|
2023-05-29 17:07:58 +02:00
|
|
|
}
|
|
|
|
</style>
|