Skip to content

Instantly share code, notes, and snippets.

@hanspagel
Created February 14, 2025 10:21
Show Gist options
  • Save hanspagel/8acf72ba5e7ac84508fa8ae2b3179b56 to your computer and use it in GitHub Desktop.
Save hanspagel/8acf72ba5e7ac84508fa8ae2b3179b56 to your computer and use it in GitHub Desktop.
Mount CodeMirror in Vue
<template>
<div ref="editorRef" class="editor" />
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { EditorView, basicSetup } from 'codemirror'
import { json } from '@codemirror/lang-json'
const editorRef = ref<HTMLDivElement | null>(null)
let editor: EditorView | null = null
onMounted(() => {
if (!editorRef.value) return
editor = new EditorView({
doc: JSON.stringify({"foo": "bar"}, null, 2),
extensions: [
basicSetup,
json(),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
console.log('Content changed:', update.state.doc.toString())
}
})
],
parent: editorRef.value
})
})
onBeforeUnmount(() => {
editor?.destroy()
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment