Created
February 14, 2025 10:21
-
-
Save hanspagel/8acf72ba5e7ac84508fa8ae2b3179b56 to your computer and use it in GitHub Desktop.
Mount CodeMirror in Vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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