MCPcopy
hub / github.com/vitest-dev/vitest / useCodeMirror

Function useCodeMirror

packages/ui/client/composables/codemirror.ts:22–65  ·  view source on GitHub ↗
(
  textarea: Ref<HTMLTextAreaElement | null | undefined>,
  input: Ref<string> | WritableComputedRef<string>,
  options: CodeMirror.EditorConfiguration = {},
)

Source from the content-addressed store, hash-verified

20export const codemirrorRef = shallowRef<CodeMirror.EditorFromTextArea>()
21
22export 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
67export async function showTaskSource(task: Task) {
68 navigateTo({

Callers

nothing calls this directly

Calls 2

watchFunction · 0.85
onMethod · 0.45

Tested by

no test coverage detected