|
| 1 | +<template> |
| 2 | + <div ref="refCode" :class="{ 'child-error': hasError }"></div> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script lang="ts" setup> |
| 6 | +import { computed, onMounted, ref, watch } from "vue"; |
| 7 | +import { EditorView, basicSetup } from "codemirror"; |
| 8 | +import { linter, lintGutter } from "@codemirror/lint"; |
| 9 | +import { json, jsonParseLinter } from "@codemirror/lang-json"; |
| 10 | +import { html } from "@codemirror/lang-html"; |
| 11 | +import { yaml } from "@codemirror/lang-yaml"; |
| 12 | +import { EditorState, Compartment, type Extension } from "@codemirror/state"; |
| 13 | +import { indentWithTab } from "@codemirror/commands"; |
| 14 | +import { keymap } from "@codemirror/view"; |
| 15 | +import { oneDark } from "@codemirror/theme-one-dark"; |
| 16 | +
|
| 17 | +const model = defineModel<string>(); |
| 18 | +const props = defineProps<{ |
| 19 | + lang?: string; |
| 20 | + readonly?: boolean; |
| 21 | + contentType?: string; |
| 22 | +}>(); |
| 23 | +const emit = defineEmits<{ |
| 24 | + cursorMove: [line: number]; |
| 25 | + focus: []; |
| 26 | + blur: []; |
| 27 | +}>(); |
| 28 | +defineExpose({ replaceLine }); |
| 29 | +
|
| 30 | +const langExtMap: Record<string, () => Extension[]> = { |
| 31 | + json: () => [json(), linter(jsonParseLinter()), lintGutter()], |
| 32 | + html: () => [html()], |
| 33 | + yaml: () => [yaml()], |
| 34 | +}; |
| 35 | +
|
| 36 | +const refCode = ref<HTMLElement>(); |
| 37 | +
|
| 38 | +let view: EditorView | undefined; |
| 39 | +let lastModel = ""; |
| 40 | +
|
| 41 | +const langExtComp = new Compartment(); |
| 42 | +const themeComp = new Compartment(); |
| 43 | +
|
| 44 | +const hasError = ref(false); |
| 45 | +const langExt = computed(() => langExtMap[props.lang || ""]?.() || []); |
| 46 | +
|
| 47 | +const result = window.matchMedia("(prefers-color-scheme: dark"); |
| 48 | +const isDark = ref(result.matches); |
| 49 | +result.addEventListener("change", (e) => { |
| 50 | + isDark.value = e.matches; |
| 51 | +}); |
| 52 | +
|
| 53 | +watch(langExt, (ext) => { |
| 54 | + view?.dispatch({ |
| 55 | + effects: langExtComp.reconfigure(ext), |
| 56 | + }); |
| 57 | +}); |
| 58 | +
|
| 59 | +watch(isDark, (dark) => { |
| 60 | + view?.dispatch({ |
| 61 | + effects: themeComp.reconfigure(dark ? oneDark : []), |
| 62 | + }); |
| 63 | +}); |
| 64 | +
|
| 65 | +watch(model, (value) => { |
| 66 | + if (!view || value === lastModel) return; |
| 67 | + view.dispatch({ |
| 68 | + changes: { |
| 69 | + from: 0, |
| 70 | + to: view.state.doc.length, |
| 71 | + insert: value, |
| 72 | + }, |
| 73 | + }); |
| 74 | +}); |
| 75 | +
|
| 76 | +function replaceLine(lineNo: number, content: string) { |
| 77 | + if (!view) return; |
| 78 | + const line = view.state.doc.line(lineNo); |
| 79 | + view.dispatch({ |
| 80 | + changes: { |
| 81 | + from: line.from, |
| 82 | + to: line.to, |
| 83 | + insert: content, |
| 84 | + }, |
| 85 | + scrollIntoView: true, |
| 86 | + }); |
| 87 | +} |
| 88 | +
|
| 89 | +onMounted(() => { |
| 90 | + view = new EditorView({ |
| 91 | + doc: model.value || "", |
| 92 | + extensions: [ |
| 93 | + basicSetup, |
| 94 | + keymap.of([indentWithTab]), |
| 95 | + EditorState.readOnly.of(props.readonly), |
| 96 | + langExtComp.of(langExt.value), |
| 97 | + themeComp.of(isDark.value ? oneDark : []), |
| 98 | + EditorView.updateListener.of((update) => { |
| 99 | + if (update.focusChanged) { |
| 100 | + if (update.view.hasFocus) emit("focus"); |
| 101 | + else emit("blur"); |
| 102 | + } |
| 103 | + if (update.selectionSet) { |
| 104 | + const lineNo = update.view.state.doc.lineAt( |
| 105 | + update.view.state.selection.main.head |
| 106 | + ).number; |
| 107 | + emit("cursorMove", lineNo); |
| 108 | + } |
| 109 | + if (update.docChanged) { |
| 110 | + lastModel = update.view.state.doc.toString(); |
| 111 | + model.value = lastModel; |
| 112 | + } |
| 113 | + }), |
| 114 | + ], |
| 115 | + parent: refCode.value, |
| 116 | + }); |
| 117 | +}); |
| 118 | +</script> |
0 commit comments