api-client/components/smart/CodeMirror.vue

49 lines
979 B
Vue
Raw Normal View History

2021-08-30 18:33:07 +00:00
<template>
<div ref="editor" class="w-full block"></div>
</template>
2021-08-31 05:17:00 +00:00
<script setup lang="ts">
import "codemirror/mode/javascript/javascript"
2021-08-30 18:33:07 +00:00
import { ref, watch } from "@nuxtjs/composition-api"
import { useCodemirror } from "~/helpers/editor/codemirror"
2021-09-01 11:11:14 +00:00
import { LinterDefinition } from "~/helpers/editor/linting/linter"
const props = withDefaults(
defineProps<{
value: string
mode: string
2021-09-07 06:44:13 +00:00
placeholder?: string
2021-09-01 11:11:14 +00:00
wrap: boolean
linter: LinterDefinition | null
}>(),
{
linter: null as any,
2021-09-07 06:44:13 +00:00
placeholder: "",
2021-09-01 11:11:14 +00:00
}
)
2021-08-31 05:17:00 +00:00
const emit = defineEmits<{
(e: "input", value: string): void
}>()
const value = ref(props.value)
2021-08-31 05:17:00 +00:00
watch(
() => props.value,
(val) => (value.value = val)
2021-08-31 05:17:00 +00:00
)
watch(value, (val) => emit("input", val))
2021-08-31 05:17:00 +00:00
const editor = ref<any | null>(null)
useCodemirror(editor, value, {
2021-09-01 11:11:14 +00:00
extendedEditorConfig: {
mode: props.mode,
placeholder: props.placeholder,
lineWrapping: props.wrap,
2021-09-01 11:11:14 +00:00
},
linter: props.linter,
2021-08-31 05:17:00 +00:00
})
2021-08-30 18:33:07 +00:00
</script>