增加运行选中内容功能

This commit is contained in:
purocean
2019-02-18 20:03:33 +08:00
parent 5fddecab7d
commit a7b27a4d1f
2 changed files with 24 additions and 9 deletions
+4
View File
@@ -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())
})
+20 -9
View File
@@ -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
}
}
</script>