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