Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
8ad52f0f
Commit
8ad52f0f
authored
Jun 10, 2024
by
Timothy J. Baek
Browse files
feat: code editor
parent
5a3736f1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
11 deletions
+73
-11
src/app.css
src/app.css
+9
-0
src/lib/components/common/CodeEditor.svelte
src/lib/components/common/CodeEditor.svelte
+64
-11
No files found.
src/app.css
View file @
8ad52f0f
...
@@ -24,6 +24,15 @@ math {
...
@@ -24,6 +24,15 @@ math {
margin-top
:
1rem
;
margin-top
:
1rem
;
}
}
.cm-editor
{
height
:
100%
;
width
:
100%
;
}
.cm-editor.cm-focused
{
outline
:
none
;
}
.hljs
{
.hljs
{
@apply
rounded-lg;
@apply
rounded-lg;
}
}
...
...
src/lib/components/common/CodeEditor.svelte
View file @
8ad52f0f
<script lang="ts">
<script lang="ts">
import { basicSetup, EditorView } from 'codemirror';
import { basicSetup, EditorView } from 'codemirror';
import { keymap, placeholder } from '@codemirror/view';
import { keymap, placeholder } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import {
Compartment,
EditorState } from '@codemirror/state';
import { acceptCompletion } from '@codemirror/autocomplete';
import { acceptCompletion } from '@codemirror/autocomplete';
import { indentWithTab } from '@codemirror/commands';
import { indentWithTab } from '@codemirror/commands';
...
@@ -13,22 +13,75 @@
...
@@ -13,22 +13,75 @@
export let value = '';
export let value = '';
onMount(() => {
let codeEditor;
// python code editor, highlight python code
const codeEditor = new EditorView({
let isDarkMode = false;
state: EditorState.create({
let editorTheme = new Compartment();
doc: value,
extensions
:
[
let
extensions
=
[
basicSetup,
basicSetup,
keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab]),
keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab]),
python(),
python(),
oneDark,
placeholder('Enter your code here...'),
placeholder('Enter your code here...')
EditorView.updateListener.of((e) => {
]
if (e.docChanged) {
console.log(e);
value = e.state.doc.toString();
}
}),
editorTheme.of([])
];
onMount(() => {
// Check if html class has dark mode
isDarkMode = document.documentElement.classList.contains('dark');
// python code editor, highlight python code
codeEditor = new EditorView({
state: EditorState.create({
doc: '',
extensions: extensions
}),
}),
parent: document.getElementById('code-textarea')
parent: document.getElementById('code-textarea')
});
});
if (isDarkMode) {
codeEditor.dispatch({
effects: editorTheme.reconfigure(oneDark)
});
}
// listen to html class changes this should fire only when dark mode is toggled
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
const _isDarkMode = document.documentElement.classList.contains('dark');
if (_isDarkMode !== isDarkMode) {
isDarkMode = _isDarkMode;
if (_isDarkMode) {
codeEditor.dispatch({
effects: editorTheme.reconfigure(oneDark)
});
} else {
codeEditor.dispatch({
effects: editorTheme.reconfigure()
});
}
}
}
});
});
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class']
});
return () => {
observer.disconnect();
};
});
});
</script>
</script>
<div id="code-textarea" />
<div id="code-textarea"
class="h-full w-full"
/>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment