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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<template>
    <el-container>
        <el-header style="background-color: #F7F7F7; padding: 0; height: 50px;">
            <div style="display: flex; padding-top: 10px; margin-left: 10px">
                <el-button
                    round
                    type="primary"
                    size="small"
                    icon="el-icon-check"
                    @click="handleConfirm"
                    >点击保存</el-button
                >
                <el-button
                    round
                    icon="el-icon-caret-right"
                    type="info"
                    size="small"
                    @click="handleRunCode"
                    >在线运行</el-button
                >
                <el-button
                    round
                    icon="el-icon-document-copy"
                    type="success"
                    size="small"
                    @click="handleImportCode"
                    >插入其他项目代码</el-button
                >
            </div>
        </el-header>
 
        <el-main style="padding: 0; margin-left: 10px; overflow: hidden;">
            <MonacoEditor
                ref="editor"
                :height="codeHeight"
                language="python"
                :code="code.code"
                :options="options"
                @mounted="onMounted"
                @codeChange="onCodeChange"
                :key="timeStamp"
            ></MonacoEditor>
        </el-main>
        <el-drawer
            style="margin-top: 100px;"
            :height="codeHeight"
            :destroy-on-close="true"
            :with-header="false"
            :modal="false"
            :visible.sync="isShowDebug"
            size="40%"
        >
            <RunCodeResult :msg="resp.msg"></RunCodeResult>
        </el-drawer>
 
        <el-dialog
            title="插入其他项目代码"
            :visible.sync="importDialogVisible"
            width="70%"
            :close-on-click-modal="false"
        >
            <div style="margin-bottom: 20px;">
                <el-select
                    v-model="selectedProjectId"
                    placeholder="请选择项目"
                    filterable
                    @change="handleProjectChange"
                    style="width: 100%;"
                >
                    <el-option
                        v-for="project in projectList"
                        :key="project.id"
                        :label="project.name"
                        :value="project.id"
                    >
                        <span style="float: left">{{ project.name }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ project.responsible }}</span>
                    </el-option>
                </el-select>
            </div>
 
            <div v-if="selectedProjectCode" style="margin-bottom: 20px;">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
                    <span style="font-weight: bold;">代码预览:</span>
                    <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-document-copy"
                        @click="handleCopyAll"
                    >
                        全部复制
                    </el-button>
                </div>
                <el-input
                    type="textarea"
                    :rows="15"
                    v-model="selectedProjectCode"
                    readonly
                    style="font-family: 'Courier New', monospace;"
                ></el-input>
            </div>
 
            <span slot="footer" class="dialog-footer">
                <el-button @click="importDialogVisible = false">取 消</el-button>
                <el-button
                    type="primary"
                    @click="handleConfirmImport"
                    :disabled="!selectedProjectCode"
                >
                    确定插入
                </el-button>
            </span>
        </el-dialog>
    </el-container>
</template>
 
<script>
import MonacoEditor from "vue-monaco-editor";
import RunCodeResult from "./components/RunCodeResult";
// Python代码补全功能-BaseMonacoEditor
import BaseMonacoEditor from "../monaco-editor/BaseMonacoEditor";
export default {
    name: "DebugTalk",
    components: {
        MonacoEditor,
        RunCodeResult,
        BaseMonacoEditor
    },
    data() {
        return {
            editor: null,
            timeStamp: "",
            isShowDebug: false,
            importDialogVisible: false,
            projectList: [],
            selectedProjectId: "",
            selectedProjectCode: "",
            options: {
                selectOnLineNumbers: false,
                scrollbar: {
                    vertical: "hidden",
                    verticalHasArrows: false
                }
            },
            code: {
                code: "",
                id: ""
            },
            resp: {
                msg: ""
            }
        };
    },
    methods: {
        onMounted(editor) {
            this.editor = editor;
        },
        onCodeChange(editor) {
            this.code.code = editor.getValue();
        },
        handleRunCode() {
            this.resp.msg = "";
            this.$api.runDebugtalk(this.code).then(resp => {
                this.resp = resp;
            });
        },
        handleConfirm() {
            this.$api.updateDebugtalk(this.code).then(resp => {
                this.getDebugTalk();
                this.$message.success("代码保存成功");
            });
        },
        getDebugTalk() {
            this.$api.getDebugtalk(this.$route.params.id).then(resp => {
                this.code = resp;
            });
        },
        handleImportCode() {
            this.importDialogVisible = true;
            this.getProjectList();
        },
        getProjectList() {
            this.$api.getProjectList().then(resp => {
                this.projectList = resp.results || [];
            });
        },
        handleProjectChange(projectId) {
            if (projectId) {
                this.$api.getDebugtalk(projectId).then(resp => {
                    this.selectedProjectCode = resp.code || "";
                }).catch(err => {
                    this.$message.error("获取项目驱动代码失败");
                    this.selectedProjectCode = "";
                });
            }
        },
        handleCopyAll() {
            if (this.selectedProjectCode) {
                const textarea = document.createElement("textarea");
                textarea.value = this.selectedProjectCode;
                document.body.appendChild(textarea);
                textarea.select();
                try {
                    document.execCommand("copy");
                    this.$message.success("代码已复制到剪贴板");
                } catch (err) {
                    this.$message.error("复制失败,请手动复制");
                }
                document.body.removeChild(textarea);
            }
        },
        handleConfirmImport() {
            if (this.selectedProjectCode && this.editor) {
                const currentCode = this.editor.getValue();
                const newCode = currentCode + "\n\n" + this.selectedProjectCode;
                this.editor.setValue(newCode);
                this.code.code = newCode;
                this.$message.success("代码已插入");
                this.importDialogVisible = false;
            }
        }
    },
    watch: {
        code() {
            this.timeStamp = new Date().getTime();
        },
        resp() {
            this.isShowDebug = true;
        }
    },
    computed: {
        codeHeight() {
            return window.screen.height - 230;
        }
    },
    mounted() {
        this.getDebugTalk();
    }
};
</script>
 
<style>
.el-drawer__body {
    overflow: hidden;
}
</style>