Skip to content

Commit 292b111

Browse files
authored
Merge pull request #8 from agnosticeng/chore/editor
chore(editor): add codemirror
2 parents 46c6642 + 61d1964 commit 292b111

File tree

9 files changed

+1404
-4
lines changed

9 files changed

+1404
-4
lines changed

package-lock.json

+118
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
},
1515
"license": "MIT",
1616
"dependencies": {
17+
"@codemirror/autocomplete": "^6.18.3",
18+
"@codemirror/commands": "^6.7.1",
19+
"@codemirror/lang-sql": "^6.8.0",
20+
"@codemirror/language": "^6.10.3",
21+
"@codemirror/state": "^6.4.1",
22+
"@codemirror/view": "^6.35.0",
23+
"@lezer/highlight": "^1.2.1",
1724
"@tauri-apps/api": "^1",
1825
"normalize.css": "^8.0.1"
1926
},
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<script lang="ts">
2+
import { sql } from '@codemirror/lang-sql';
3+
import { EditorState } from '@codemirror/state';
4+
import { EditorView, keymap, placeholder } from '@codemirror/view';
5+
import { untrack } from 'svelte';
6+
import './codemirror.css';
7+
import { default_extensions, default_keymaps } from './extensions';
8+
import { ProxyDialect } from './SQLDialect';
9+
10+
type Props = {
11+
value: string;
12+
onExec?: () => unknown;
13+
};
14+
15+
let { value = $bindable(''), onExec }: Props = $props();
16+
17+
let container: HTMLDivElement;
18+
let editor_view: EditorView;
19+
20+
$effect(() => {
21+
editor_view = new EditorView({ parent: container });
22+
23+
const state = EditorState.create({
24+
doc: untrack(() => value),
25+
extensions: [
26+
...default_extensions,
27+
default_keymaps,
28+
EditorView.darkTheme.of(true),
29+
sql({ dialect: ProxyDialect }),
30+
EditorView.updateListener.of((update) => {
31+
if (update.docChanged) {
32+
value = update.state.doc.toString();
33+
}
34+
}),
35+
placeholder('Enter a query...'),
36+
keymap.of([
37+
{
38+
key: 'Mod-Enter',
39+
run: () => {
40+
onExec?.();
41+
return true;
42+
}
43+
}
44+
])
45+
]
46+
});
47+
48+
editor_view.setState(state);
49+
50+
return () => editor_view.destroy();
51+
});
52+
</script>
53+
54+
<div bind:this={container}></div>
55+
56+
<style>
57+
div {
58+
width: 100%;
59+
height: 100%;
60+
padding: 7px 2px;
61+
}
62+
</style>

0 commit comments

Comments
 (0)