previewDialog.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="sys-codeGenPreview-container">
  3. <el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="70%">
  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" v-loading="state.loading" class="code-container"></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: [] as any, // 分段器的选项
  45. current: '', // 选中的分段
  46. codes: [] as any, // 预览的代码
  47. loading: true
  48. });
  49. // 防止 monaco 报黄
  50. self.MonacoEnvironment = {
  51. getWorker: (_: string, label: string) => new EditorWorker(),
  52. };
  53. // 初始化monacoEditor对象
  54. var monacoEditor: any = null;
  55. const initMonacoEditor = () => {
  56. monacoEditor = monaco.editor.create(monacoEditorRef.value, {
  57. theme: 'vs-dark', // 主题 vs vs-dark hc-black
  58. value: '', // 默认显示的值
  59. language: 'csharp',
  60. formatOnPaste: true,
  61. wordWrap: 'on', // 自动换行,注意大小写
  62. wrappingIndent: 'indent',
  63. folding: true, // 是否折叠
  64. foldingHighlight: true, // 折叠等高线
  65. foldingStrategy: 'indentation', // 折叠方式 auto | indentation
  66. showFoldingControls: 'always', // 是否一直显示折叠 always | mouSEOver
  67. disableLayerHinting: true, // 等宽优化
  68. emptySelectionClipboard: false, // 空选择剪切板
  69. selectionClipboard: false, // 选择剪切板
  70. automaticLayout: true, // 自动布局
  71. codeLens: false, // 代码镜头
  72. scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
  73. colorDecorators: true, // 颜色装饰器
  74. accessibilitySupport: 'auto', // 辅助功能支持 "auto" | "off" | "on"
  75. lineNumbers: 'on', // 行号 取值: "on" | "off" | "relative" | "interval" | function
  76. lineNumbersMinChars: 5, // 行号最小字符 number
  77. //enableSplitViewResizing: false,
  78. readOnly: false, // 是否只读 取值 true | false
  79. });
  80. };
  81. // 打开弹窗
  82. const openDialog = async (row: any) => {
  83. state.loading = true;
  84. try {
  85. state.isShowDialog = true;
  86. const { data } = await getAPI(SysCodeGenApi).apiSysCodeGenPreviewPost(row);
  87. state.codes = data.result ?? [];
  88. state.options = Object.keys(data.result ?? []).map((fileName: string) => ({
  89. value: fileName,
  90. icon: fileName?.endsWith('.cs') ? 'fa fa-hashtag' : fileName?.endsWith('.vue') ? 'fa fa-vimeo' : 'fa fa-file-code-o',
  91. }));
  92. state.current = state.options?.[0]?.value ?? '';
  93. } catch (e) { /* empty */ }
  94. state.loading = false;
  95. if (monacoEditor == null) initMonacoEditor();
  96. // 防止取不到
  97. nextTick(() => {
  98. monacoEditor.setValue(state.codes[state.current]);
  99. });
  100. };
  101. // 分段器改变时切换代码
  102. const handleChange = (current: any) => {
  103. monacoEditor.setValue(state.codes[current]);
  104. };
  105. // 取消
  106. const cancel = () => {
  107. state.isShowDialog = false;
  108. };
  109. //复制代码
  110. const handleCopy = () => {
  111. copyText(state.codes[state.current]);
  112. };
  113. // 导出对象
  114. defineExpose({ openDialog });
  115. </script>
  116. <style lang="scss" scoped>
  117. .cs-style .el-segmented {
  118. --el-segmented-item-selected-bg-color: #5c2d91;
  119. }
  120. .vue-style .el-segmented {
  121. --el-segmented-item-selected-bg-color: #42b883;
  122. }
  123. .js-style .el-segmented {
  124. --el-segmented-item-selected-bg-color: #e44d26;
  125. }
  126. :deep(.el-dialog__body) {
  127. height: calc(100vh - 160px) !important;
  128. .code-container {
  129. width: 100%;
  130. height: calc(100% - 48px);
  131. }
  132. }
  133. </style>