monacoEditor.vue 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div class="monacoCode" ref="main" >
  3. </div>
  4. </template>
  5. <script setup>
  6. import { ref, onMounted, watch, shallowRef } from "vue";
  7. import * as monaco from "monaco-editor";
  8. import store from '@/stores/blockly'
  9. const monacoEditor = shallowRef(null);
  10. const main = ref(null);
  11. const storePythonCode = store.useyPythonCodeStore()
  12. const props = defineProps({
  13. pythonCode: {
  14. type: String,
  15. require: true,
  16. default: ""
  17. },
  18. codeMirrorShow: {
  19. type: Boolean,
  20. require: true,
  21. default: true
  22. }
  23. });
  24. onMounted(() => {
  25. initmonaco();
  26. });
  27. const initmonaco = () => {
  28. monacoEditor.value = monaco.editor.create(main.value, {
  29. theme: "dark", // 主题
  30. value: props.pythonCode, // 默认显示的值
  31. language: "python", // 语言
  32. folding: true, // 是否折叠
  33. foldingHighlight: true, // 折叠等高线
  34. foldingStrategy: "auto", // 折叠方式 auto | indentation
  35. showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
  36. disableLayerHinting: true, // 等宽优化
  37. emptySelectionClipboard: false, // 空选择剪切板
  38. selectionClipboard: false, // 选择剪切板
  39. automaticLayout: true, // 自动布局
  40. codeLens: false, // 代码镜头
  41. scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
  42. colorDecorators: true, // 颜色装饰器
  43. accessibilitySupport: "auto", // 辅助功能支持 "auto" | "off" | "on"
  44. lineNumbers: "on", // 行号 取值: "on" | "off" | "relative" | "interval" | function
  45. lineNumbersMinChars: 5, // 行号最小字符 number
  46. enableSplitViewResizing: false,
  47. readOnly: props.codeShow, //是否只读 取值 true | false
  48. minimap: { // 关闭小地图
  49. enabled: false,
  50. },
  51. });
  52. monacoEditor.value.onDidChangeModelContent(() => {
  53. // console.log(monacoEditor.value.getValue(), 11111111111);
  54. storePythonCode.pythonCode = monacoEditor.value.getValue()
  55. });
  56. };
  57. watch(() => [props.pythonCode, props.codeMirrorShow], ([newPythonCode, newCodemirrorShow]) => {
  58. if (monacoEditor.value) {
  59. monacoEditor.value.setValue(newPythonCode);
  60. monacoEditor.value.updateOptions({
  61. readOnly: newCodemirrorShow
  62. });
  63. }
  64. });
  65. </script>
  66. <style lang="scss">
  67. .monacoCode {
  68. width: 100%;
  69. height: calc(100% - 50px);
  70. .monaco-editor {
  71. width: 100% !important;
  72. height: 100% !important;
  73. .overflow-guard {
  74. width: 100% !important;
  75. height: 100% !important;
  76. // .slider-mouseover {
  77. // display: none !important;
  78. // }
  79. }
  80. }
  81. }
  82. </style>