index.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <script setup>
  2. import { watch, ref, toRefs, computed } from 'vue'
  3. import { Codemirror } from 'vue-codemirror'
  4. import { javascript } from '@codemirror/lang-javascript'
  5. import { python } from '@codemirror/lang-python'
  6. import { json } from '@codemirror/lang-json'
  7. // import { oneDark } from '@codemirror/theme-one-dark'
  8. const code = ref('')
  9. const props = defineProps({
  10. modelValue: {
  11. type: String,
  12. required: false,
  13. default: ''
  14. },
  15. language: {
  16. type: String,
  17. default: 'javascript'
  18. },
  19. disabled: {
  20. type: [String, Boolean],
  21. default: false
  22. },
  23. style: {
  24. type: [Object],
  25. default: () => ({
  26. height: '400px'
  27. })
  28. }
  29. })
  30. const emit = defineEmits(['update:modelValue'])
  31. const { modelValue, language, disabled, style } = toRefs(props)
  32. const fullScreen = ref(false)
  33. const lang = json()
  34. // const extensions = [lang(), oneDark]
  35. const extensions = [lang]
  36. watch(
  37. () => modelValue.value,
  38. val => {
  39. code.value = val
  40. },
  41. {
  42. immediate: true,
  43. deep: true
  44. }
  45. )
  46. watch(
  47. () => code.value,
  48. val => {
  49. emit('update:modelValue', val)
  50. }
  51. )
  52. const comStyle = computed(() => ({ ...style.value, ...{ height: fullScreen.value ? '100%' : '400px' } }))
  53. </script>
  54. <template>
  55. <Codemirror
  56. v-model="code"
  57. :mode="'application/json'"
  58. :disabled="disabled"
  59. placeholder="暂无数据..."
  60. :style="comStyle"
  61. :autofocus="true"
  62. :indent-with-tab="true"
  63. :tab-size="2"
  64. :full-screen="true"
  65. :extensions="extensions"
  66. @ready="() => {}"
  67. @change="() => {}"
  68. @focus="() => {}"
  69. @blur="() => {}"
  70. ></Codemirror>
  71. </template>