index.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { defineComponent, nextTick, onMounted, reactive, ref, defineExpose } from 'vue'
  2. import { basicSetup } from 'codemirror'
  3. import { EditorView } from '@codemirror/view'
  4. import { EditorState, StateEffect } from '@codemirror/state'
  5. import { javascript } from '@codemirror/lang-javascript'
  6. import { useId } from '@/hooks'
  7. // '{"paramName":"page","paramType":"int","paramDesc":"分页页码","require":true}'
  8. export const CodeMirrorTsx = defineComponent({
  9. name: 'code-mirror-tsx',
  10. props: {
  11. bodyType: {
  12. type: String,
  13. default: 'javascript' // javascript json
  14. },
  15. bodyJson: {
  16. type: String,
  17. default: ''
  18. }
  19. },
  20. setup (props, ctx) {
  21. const id = useId()
  22. const state = reactive({})
  23. const editorRef = ref()
  24. const view = ref<EditorView>()
  25. const transDoc = () => {
  26. console.log(' props.bodyType:', props.bodyType)
  27. return props.bodyType === 'javascript' ? props.bodyJson : JSON.stringify(JSON.parse(props.bodyJson), null, '\t')
  28. }
  29. const createView = () => {
  30. const editorState = EditorState.create({
  31. doc: transDoc(),
  32. extensions: [basicSetup, javascript()]
  33. })
  34. // transDoc(),
  35. view.value = new EditorView({
  36. state: editorState,
  37. parent: document.getElementById(`editorRef-${id}`)!
  38. })
  39. setTimeout(() => {
  40. console.dir(document.getElementById(`editorRef-${id}`))
  41. // console.log(document.getElementById(`editorRef-${id}`)!.getValue())
  42. }, 10000)
  43. }
  44. onMounted(() => {
  45. nextTick(() => {
  46. setTimeout(() => {
  47. createView()
  48. }, 1000)
  49. })
  50. })
  51. return () => (
  52. <div ref="editorRef" id={`editorRef-${id}`}></div>
  53. )
  54. }
  55. })