|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount, onDestroy } from 'svelte'; |
| 3 | + import { Compartment, EditorState } from '@codemirror/state'; |
| 4 | + import { EditorView, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers } from '@codemirror/view'; |
| 5 | + import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands'; |
| 6 | + import { bracketMatching } from '@codemirror/language'; |
| 7 | + import { autocompletion, completionKeymap } from '@codemirror/autocomplete'; |
| 8 | + import { oneDark } from '@codemirror/theme-one-dark'; |
| 9 | + import { githubLight } from '@uiw/codemirror-theme-github'; |
| 10 | + import { theme as appTheme } from '../../stores/theme'; |
| 11 | + import { getLanguageExtension } from '../../lib/editor/languages'; |
| 12 | + import type { EditorSettings } from '../../lib/api'; |
| 13 | +
|
| 14 | + interface Props { |
| 15 | + content: string; |
| 16 | + lang: string; |
| 17 | + settings: EditorSettings; |
| 18 | + onchange?: (content: string) => void; |
| 19 | + } |
| 20 | +
|
| 21 | + let { content = $bindable(''), lang, settings, onchange }: Props = $props(); |
| 22 | +
|
| 23 | + let container: HTMLElement; |
| 24 | + let view: EditorView | null = null; |
| 25 | +
|
| 26 | + const themeCompartment = new Compartment(); |
| 27 | + const fontSizeCompartment = new Compartment(); |
| 28 | + const tabSizeCompartment = new Compartment(); |
| 29 | + const lineNumbersCompartment = new Compartment(); |
| 30 | + const lineWrappingCompartment = new Compartment(); |
| 31 | + const languageCompartment = new Compartment(); |
| 32 | +
|
| 33 | + function getThemeExtension() { |
| 34 | + // Only use dark theme if explicitly set OR auto + dark mode active |
| 35 | + const useDark = settings.theme === 'one-dark' || |
| 36 | + (settings.theme !== 'github' && document.documentElement.classList.contains('dark')); |
| 37 | + return useDark ? oneDark : githubLight; |
| 38 | + } |
| 39 | +
|
| 40 | + function getStaticExtensions() { |
| 41 | + return [ |
| 42 | + lineNumbersCompartment.of(settings.show_line_numbers ? lineNumbers() : []), |
| 43 | + highlightActiveLineGutter(), |
| 44 | + highlightActiveLine(), |
| 45 | + history(), |
| 46 | + bracketMatching(), |
| 47 | + autocompletion(), |
| 48 | + EditorState.allowMultipleSelections.of(true), |
| 49 | + tabSizeCompartment.of(EditorState.tabSize.of(settings.tab_size ?? 4)), |
| 50 | + keymap.of([...defaultKeymap, ...historyKeymap, ...completionKeymap, indentWithTab]), |
| 51 | + languageCompartment.of(getLanguageExtension(lang)), |
| 52 | + lineWrappingCompartment.of(settings.word_wrap ? EditorView.lineWrapping : []), |
| 53 | + fontSizeCompartment.of(EditorView.theme({ ".cm-content": { fontSize: `${settings.font_size ?? 14}px` } })), |
| 54 | + EditorView.theme({ |
| 55 | + "&": { height: "100%", maxHeight: "100%" }, |
| 56 | + ".cm-content": { minHeight: "100%" }, |
| 57 | + ".cm-scroller": { overflow: "auto", maxHeight: "100%" } |
| 58 | + }), |
| 59 | + EditorView.updateListener.of(update => { |
| 60 | + if (update.docChanged) { |
| 61 | + const newContent = update.state.doc.toString(); |
| 62 | + content = newContent; |
| 63 | + onchange?.(newContent); |
| 64 | + } |
| 65 | + }), |
| 66 | + ]; |
| 67 | + } |
| 68 | +
|
| 69 | + function applySettings() { |
| 70 | + if (!view) return; |
| 71 | + view.dispatch({ effects: themeCompartment.reconfigure(getThemeExtension()) }); |
| 72 | + view.dispatch({ effects: fontSizeCompartment.reconfigure(EditorView.theme({ ".cm-content": { fontSize: `${settings.font_size ?? 14}px` } })) }); |
| 73 | + view.dispatch({ effects: tabSizeCompartment.reconfigure(EditorState.tabSize.of(settings.tab_size ?? 4)) }); |
| 74 | + view.dispatch({ effects: lineNumbersCompartment.reconfigure(settings.show_line_numbers ? lineNumbers() : []) }); |
| 75 | + view.dispatch({ effects: lineWrappingCompartment.reconfigure(settings.word_wrap ? EditorView.lineWrapping : []) }); |
| 76 | + } |
| 77 | +
|
| 78 | + let unsubscribeTheme: (() => void) | null = null; |
| 79 | +
|
| 80 | + onMount(() => { |
| 81 | + if (!container) return; |
| 82 | +
|
| 83 | + const startState = EditorState.create({ |
| 84 | + doc: content, |
| 85 | + extensions: [...getStaticExtensions(), themeCompartment.of(getThemeExtension())] |
| 86 | + }); |
| 87 | +
|
| 88 | + view = new EditorView({ state: startState, parent: container }); |
| 89 | +
|
| 90 | + unsubscribeTheme = appTheme.subscribe(() => { |
| 91 | + if (view && (settings.theme === 'auto' || !settings.theme)) { |
| 92 | + view.dispatch({ effects: themeCompartment.reconfigure(getThemeExtension()) }); |
| 93 | + } |
| 94 | + }); |
| 95 | + }); |
| 96 | +
|
| 97 | + onDestroy(() => { |
| 98 | + view?.destroy(); |
| 99 | + view = null; |
| 100 | + unsubscribeTheme?.(); |
| 101 | + }); |
| 102 | +
|
| 103 | + $effect(() => { |
| 104 | + if (view) { |
| 105 | + view.dispatch({ effects: languageCompartment.reconfigure(getLanguageExtension(lang)) }); |
| 106 | + } |
| 107 | + }); |
| 108 | +
|
| 109 | + $effect(() => { |
| 110 | + void settings; |
| 111 | + applySettings(); |
| 112 | + }); |
| 113 | +
|
| 114 | + export function setContent(newContent: string) { |
| 115 | + if (!view) return; |
| 116 | + view.dispatch({ |
| 117 | + changes: { from: 0, to: view.state.doc.length, insert: newContent }, |
| 118 | + selection: { anchor: 0 } |
| 119 | + }); |
| 120 | + } |
| 121 | +
|
| 122 | + export function getView(): EditorView | null { |
| 123 | + return view; |
| 124 | + } |
| 125 | +</script> |
| 126 | + |
| 127 | +<div bind:this={container} class="editor-wrapper h-full w-full"></div> |
0 commit comments