diff --git a/package.json b/package.json index 889249af..17014c30 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "@bedframe/cli": "^0.0.85", "@codemirror/lang-css": "^6.3.0", "@codemirror/lang-less": "^6.0.2", + "@codemirror/theme-one-dark": "^6.1.2", "@million/lint": "latest", "@sveltejs/vite-plugin-svelte": "4.0.0-next.7", "@tailwindcss/forms": "^0.5.9", diff --git a/src/svelte-interface/components/CodeEditor.svelte b/src/svelte-interface/components/CodeEditor.svelte index 7b817d00..1c784899 100644 --- a/src/svelte-interface/components/CodeEditor.svelte +++ b/src/svelte-interface/components/CodeEditor.svelte @@ -2,26 +2,27 @@ import { EditorState } from '@codemirror/state'; import { highlightSelectionMatches } from '@codemirror/search'; import { indentWithTab, history, defaultKeymap, historyKeymap } from '@codemirror/commands'; - import { foldGutter, indentOnInput, indentUnit, bracketMatching, foldKeymap, syntaxHighlighting, defaultHighlightStyle } from '@codemirror/language'; + import { indentOnInput, indentUnit, bracketMatching, foldKeymap, syntaxHighlighting, defaultHighlightStyle } from '@codemirror/language'; import { closeBrackets, autocompletion, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete'; - import { lineNumbers, highlightActiveLineGutter, highlightSpecialChars, drawSelection, rectangularSelection, crosshairCursor, highlightActiveLine, keymap, EditorView } from '@codemirror/view'; // dropCursor + import { highlightSpecialChars, drawSelection, rectangularSelection, crosshairCursor, highlightActiveLine, keymap, EditorView } from '@codemirror/view'; // dropCursor // Theme import { oneDark } from "@codemirror/theme-one-dark"; // Language import { css } from "@codemirror/lang-css"; - import { onMount } from 'svelte' + import { onDestroy, onMount } from 'svelte' - function createEditorState(initialContents: string, options = {}) { + let editor = $state(null) + let view: EditorView | null = null; + let { value, onChange } = $props<{value: string, onChange: (value: string) => void}>() + + function createEditorState(initialContents: string, options = {oneDark: false}) { let extensions = [ - /* lineNumbers(), - highlightActiveLineGutter(), highlightSpecialChars(), history(), - foldGutter(), drawSelection(), - indentUnit.of(" "), + indentUnit.of(" "), EditorState.allowMultipleSelections.of(true), indentOnInput(), bracketMatching(), @@ -38,13 +39,18 @@ ...historyKeymap, ...foldKeymap, ...completionKeymap, - ]), */ + ]), + EditorView.updateListener.of((update) => { + if (update.docChanged) { + onChange(update.state.doc.toString()) + } + }), css(), syntaxHighlighting(defaultHighlightStyle, { fallback: true }), ]; - /* if (options.oneDark) - extensions.push(oneDark); */ + if (options.oneDark) + extensions.push(oneDark); return EditorState.create({ doc: initialContents, @@ -52,19 +58,22 @@ }); } - function createEditorView(state: any, parent: HTMLElement) { + function createEditorView(state: EditorState, parent: HTMLElement) { return new EditorView({ state, parent }); } onMount(() => { - const editor = document.querySelector('#cm-target'); if (editor) { - const state = createEditorState('body {\n color: red;\n}'); - const view = createEditorView(state, editor as HTMLElement); - - console.log(view) + const state = createEditorState(value); + view = createEditorView(state, editor as HTMLElement); } }); + + onDestroy(() => { + if (view) { + view.destroy(); + } + }) -
+
\ No newline at end of file diff --git a/src/svelte-interface/index.css b/src/svelte-interface/index.css index 0f5866fc..0a6d322c 100644 --- a/src/svelte-interface/index.css +++ b/src/svelte-interface/index.css @@ -39,4 +39,9 @@ input { to { opacity: 1; } +} + +.cm-editor { + width: 100%; + min-height: 100px; } \ No newline at end of file diff --git a/src/svelte-interface/pages/themeCreator.svelte b/src/svelte-interface/pages/themeCreator.svelte index 1659eb7e..7bcd063b 100644 --- a/src/svelte-interface/pages/themeCreator.svelte +++ b/src/svelte-interface/pages/themeCreator.svelte @@ -32,7 +32,6 @@ }) onMount(async () => { - console.log(themeID) if (themeID) { const tempTheme = await getTheme(themeID) @@ -66,19 +65,17 @@

{item.title}

{item.description}

-
- {#if item.type === 'switch'} - - {:else if item.type === 'button'} -
+ {#if item.type === 'switch'} + + {:else if item.type === 'button'} +