| 20 | export const codemirrorRef = shallowRef<CodeMirror.EditorFromTextArea>() |
| 21 | |
| 22 | export function useCodeMirror( |
| 23 | textarea: Ref<HTMLTextAreaElement | null | undefined>, |
| 24 | input: Ref<string> | WritableComputedRef<string>, |
| 25 | options: CodeMirror.EditorConfiguration = {}, |
| 26 | ) { |
| 27 | const cm = CodeMirror.fromTextArea(textarea.value!, { |
| 28 | theme: 'vars', |
| 29 | ...options, |
| 30 | scrollbarStyle: 'simple', |
| 31 | }) |
| 32 | |
| 33 | let skip = false |
| 34 | |
| 35 | cm.on('change', () => { |
| 36 | if (skip) { |
| 37 | skip = false |
| 38 | return |
| 39 | } |
| 40 | input.value = cm.getValue() |
| 41 | }) |
| 42 | |
| 43 | watch( |
| 44 | input, |
| 45 | (v) => { |
| 46 | if (v !== cm.getValue()) { |
| 47 | skip = true |
| 48 | const selections = cm.listSelections() |
| 49 | cm.replaceRange( |
| 50 | v, |
| 51 | cm.posFromIndex(0), |
| 52 | cm.posFromIndex(Number.POSITIVE_INFINITY), |
| 53 | ) |
| 54 | cm.setSelections(selections) |
| 55 | } |
| 56 | }, |
| 57 | { immediate: true }, |
| 58 | ) |
| 59 | |
| 60 | onUnmounted(() => { |
| 61 | codemirrorRef.value = undefined |
| 62 | }) |
| 63 | |
| 64 | return markRaw(cm) |
| 65 | } |
| 66 | |
| 67 | export async function showTaskSource(task: Task) { |
| 68 | navigateTo({ |