api-client/components/smart/CodeMirror.vue

54 lines
1.1 KiB
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"
import { Completer } from "~/helpers/editor/completion"
2021-09-01 11:11:14 +00:00
const props = withDefaults(
defineProps<{
value: string
mode: string
2021-09-07 06:44:13 +00:00
placeholder?: string
wrap?: boolean
2021-09-01 11:11:14 +00:00
linter: LinterDefinition | null
completer: Completer | null
2021-09-01 11:11:14 +00:00
}>(),
{
2021-09-07 06:44:13 +00:00
placeholder: "",
wrap: true,
linter: null as any,
completer: null as any,
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,
completer: props.completer,
2021-08-31 05:17:00 +00:00
})
2021-08-30 18:33:07 +00:00
</script>