From a7b27a4d1fe38ef077b2d5b507ff80fa2f1da714 Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 18 Feb 2019 20:03:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=90=E8=A1=8C=E9=80=89?= =?UTF-8?q?=E4=B8=AD=E5=86=85=E5=AE=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/Editor.vue | 4 ++++ frontend/src/components/Xterm.vue | 29 ++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/frontend/src/components/Editor.vue b/frontend/src/components/Editor.vue index bf18277c..7ace726a 100644 --- a/frontend/src/components/Editor.vue +++ b/frontend/src/components/Editor.vue @@ -103,6 +103,10 @@ export default { this.insert(dayjs().format('HH:mm:ss')) }) + this.editor.addCommand(KM.CtrlCmd | KM.Alt | KC.KEY_R, () => { + this.$bus.emit('run-in-terminal', this.editor.getModel().getValueInRange(this.editor.getSelection())) + }) + this.editor.addCommand(KM.CtrlCmd | KC.Enter, () => { this.insert(this.editor.getModel().getEOL()) }) diff --git a/frontend/src/components/Xterm.vue b/frontend/src/components/Xterm.vue index 771f9eca..a593bf03 100644 --- a/frontend/src/components/Xterm.vue +++ b/frontend/src/components/Xterm.vue @@ -19,6 +19,12 @@ export default { name: 'xterm', created () { window.runInXterm = this.runInXterm + this.$bus.on('run-in-terminal', this.handleRunInXterm) + }, + beforeDestroy () { + window.removeEventListener('resize', this.fitXterm) + window.runInXterm = null + this.$bus.off('run-in-terminal', this.handleRunInXterm) }, methods: { init () { @@ -67,6 +73,9 @@ export default { focus () { xterm.focus() }, + handleRunInXterm (code) { + this.runInXterm(null, code, false) + }, runInXterm (language, code, exit = true) { this.$bus.emit('toggle-xterm', true) this.$nextTick(() => { @@ -81,16 +90,22 @@ export default { js: {start: 'node', exit: '.exit', eol: '\n'} } - if (map[language]) { + const run = (code, eol) => { + code.split('\n').forEach((x, i) => { + this.input(x.trim()) + this.input(eol) + }) + } + + if (!language) { + run(code, '\n') + } else if (map[language]) { this.input(map[language].start) this.input(map[language].eol) // 延迟一下等待子进程启动 setTimeout(() => { - code.split('\n').forEach((x, i) => { - this.input(x.trim()) - this.input(map[language].eol) - }) + run(code, map[language].eol) if (exit) { this.input(map[language].exit) @@ -102,10 +117,6 @@ export default { } }) } - }, - beforeDestroy () { - window.removeEventListener('resize', this.fitXterm) - window.runInXterm = null } }