previewDialog.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div class="sys-codeGenPreview-container">
  3. <el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="90vw">
  4. <template #header>
  5. <div style="color: #fff">
  6. <el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
  7. <span> {{ props.title }} </span>
  8. </div>
  9. </template>
  10. <div :class="[state.current?.endsWith('.cs') ? 'cs-style' : state.current?.endsWith('.vue') ? 'vue-style' : 'js-style']">
  11. <el-segmented v-model="state.current" :options="state.options" block @change="handleChange">
  12. <template #default="{ item }">
  13. <div class="pd4">
  14. <SvgIcon :name="item.icon" class="mb4" />
  15. <div>{{ item.value }}</div>
  16. </div>
  17. </template>
  18. </el-segmented>
  19. </div>
  20. <div ref="monacoEditorRef" style="width: 100%; height: 660px; margin-top: 6px"></div>
  21. <template #footer>
  22. <span class="dialog-footer">
  23. <el-button icon="ele-Close" @click="cancel">关 闭</el-button>
  24. <el-button icon="ele-CopyDocument" type="primary" @click="handleCopy">复 制</el-button>
  25. </span>
  26. </template>
  27. </el-dialog>
  28. </div>
  29. </template>
  30. <script lang="ts" setup name="sysPreviewCode">
  31. import { reactive, ref, nextTick, toRaw } from 'vue';
  32. import * as monaco from 'monaco-editor';
  33. import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
  34. import commonFunction from '/@/utils/commonFunction';
  35. import { getAPI } from '/@/utils/axios-utils';
  36. import { SysCodeGenApi } from '/@/api-services/api';
  37. const { copyText } = commonFunction();
  38. const props = defineProps({
  39. title: String,
  40. });
  41. const monacoEditorRef = ref();
  42. const state = reactive({
  43. isShowDialog: false,
  44. options: [], //分段器的选项
  45. current: '', // 选中的分段
  46. codes: [], //预览的代码
  47. });
  48. // 防止 monaco 报黄
  49. self.MonacoEnvironment = {
  50. getWorker: (_: string, label: string) => new EditorWorker(),
  51. };
  52. // 初始化monacoEditor对象
  53. var monacoEditor: any = null;
  54. const initMonacoEditor = () => {
  55. monacoEditor = monaco.editor.create(monacoEditorRef.value, {
  56. theme: 'vs-dark', // 主题 vs vs-dark hc-black
  57. value: '', // 默认显示的值
  58. language: 'csharp',
  59. formatOnPaste: true,
  60. wordWrap: 'on', //自动换行,注意大小写
  61. wrappingIndent: 'indent',
  62. folding: true, // 是否折叠
  63. foldingHighlight: true, // 折叠等高线
  64. foldingStrategy: 'indentation', // 折叠方式 auto | indentation
  65. showFoldingControls: 'always', // 是否一直显示折叠 always | mouSEOver
  66. disableLayerHinting: true, // 等宽优化
  67. emptySelectionClipboard: false, // 空选择剪切板
  68. selectionClipboard: false, // 选择剪切板
  69. automaticLayout: true, // 自动布局
  70. codeLens: false, // 代码镜头
  71. scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
  72. colorDecorators: true, // 颜色装饰器
  73. accessibilitySupport: 'auto', // 辅助功能支持 "auto" | "off" | "on"
  74. lineNumbers: 'on', // 行号 取值: "on" | "off" | "relative" | "interval" | function
  75. lineNumbersMinChars: 5, // 行号最小字符 number
  76. //enableSplitViewResizing: false,
  77. readOnly: false, //是否只读 取值 true | false
  78. });
  79. };
  80. // 打开弹窗
  81. const openDialog = async (row: any) => {
  82. state.isShowDialog = true;
  83. const { data } = await getAPI(SysCodeGenApi).apiSysCodeGenPreviewPost(row);
  84. state.codes = data.result ?? [];
  85. state.options = Object.keys(data.result).map((fileName: string) => ({
  86. value: fileName,
  87. icon: fileName?.endsWith('.cs') ? 'fa fa-hashtag' : fileName?.endsWith('.vue') ? 'fa fa-vimeo' : 'fa fa-file-code-o',
  88. }));
  89. state.current = state.options?.[0]?.value ?? '';
  90. if (monacoEditor == null) initMonacoEditor();
  91. // 防止取不到
  92. nextTick(() => {
  93. monacoEditor.setValue(state.codes[state.current]);
  94. });
  95. };
  96. // 分段器改变时切换代码
  97. const handleChange = (current: any) => {
  98. monacoEditor.setValue(state.codes[current]);
  99. };
  100. // 取消
  101. const cancel = () => {
  102. state.isShowDialog = false;
  103. };
  104. //复制代码
  105. const handleCopy = () => {
  106. copyText(state.codes[state.current]);
  107. };
  108. // 导出对象
  109. defineExpose({ openDialog });
  110. </script>
  111. <style scoped>
  112. .cs-style .el-segmented {
  113. --el-segmented-item-selected-bg-color: #5c2d91;
  114. }
  115. .vue-style .el-segmented {
  116. --el-segmented-item-selected-bg-color: #42b883;
  117. }
  118. .js-style .el-segmented {
  119. --el-segmented-item-selected-bg-color: #e44d26;
  120. }
  121. </style>