From ac3a1e235a2b4a385f2a8ba09cf0f960785365a6 Mon Sep 17 00:00:00 2001 From: fofolee Date: Sun, 15 May 2022 00:59:48 +0800 Subject: [PATCH] =?UTF-8?q?ctrl+space=20=E5=BF=AB=E9=80=9F=20console.log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/CommandEditor.vue | 9 +++++++-- src/components/CommandRunResult.vue | 20 ++++++-------------- src/components/MonacoEditor.vue | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/components/CommandEditor.vue b/src/components/CommandEditor.vue index 380608f..ce5132c 100644 --- a/src/components/CommandEditor.vue +++ b/src/components/CommandEditor.vue @@ -368,8 +368,9 @@ export default { }); }, // 运行 - runCurrentCommand() { + runCurrentCommand(cmd) { let command = _.cloneDeep(this.quickcommandInfo); + if (cmd) command.cmd = cmd; command.output = this.$refs.sidebar?.currentCommand.output || (command.program === "html" ? "html" : "text"); @@ -382,7 +383,7 @@ export default { command.cursorPosition = this.$refs.editor.getCursorPosition(); this.$root.utools.putDB(command, "cfg_codeHistory"); }, - monacoKeyStroke(event) { + monacoKeyStroke(event, data) { switch (event) { case "run": this.runCurrentCommand(); @@ -390,6 +391,10 @@ export default { case "save": this.saveCurrentCommand(); break; + case "log": + if (this.quickcommandInfo.program !== "quickcommand") return; + this.runCurrentCommand(`console.log(${data})`); + break; default: break; } diff --git a/src/components/CommandRunResult.vue b/src/components/CommandRunResult.vue index 7cc06f4..09654d8 100644 --- a/src/components/CommandRunResult.vue +++ b/src/components/CommandRunResult.vue @@ -38,6 +38,9 @@ :textbtn="!enableHtml" :imagebtn="!enableHtml && isDataUrl" @showImg="showBase64Img" + :style="{ + height: headerHeight + 'px', + }" />
{ utools.outPlugin(); }, 500); + let resultOpts = { outPlugin, action, earlyExit }; switch (currentCommand.program) { case "quickcommand": window.runCodeInSandbox( currentCommand.cmd, - (stdout, stderr) => { - this.handleResult(stdout, stderr, { - outPlugin, - action, - earlyExit, - }); - }, + (stdout, stderr) => this.handleResult(stdout, stderr, resultOpts), { enterData: this.$root.enterData } ); break; @@ -165,13 +163,7 @@ export default { currentCommand.cmd, this.getCommandOpt(currentCommand), currentCommand.output === "terminal", - (stdout, stderr) => { - this.handleResult(stdout, stderr, { - outPlugin, - action, - earlyExit, - }); - } + (stdout, stderr) => this.handleResult(stdout, stderr, resultOpts) ); this.listenStopSign(); break; diff --git a/src/components/MonacoEditor.vue b/src/components/MonacoEditor.vue index 2fe47e6..7d1128a 100644 --- a/src/components/MonacoEditor.vue +++ b/src/components/MonacoEditor.vue @@ -232,6 +232,27 @@ export default { that.$emit("keyStroke", "save"); } ); + // ctrl + space 快速 console.log + this.rawEditor.addCommand( + monaco.KeyMod.CtrlCmd | monaco.KeyCode.Space, + () => { + that.$emit("keyStroke", "log", that.getSelectionOrLineContent()); + } + ); + }, + getSelectionOrLineContent() { + let selection = this.rawEditor.getSelection(); + let range = new monaco.Range( + selection.startLineNumber, + selection.startColumn, + selection.endLineNumber, + selection.endColumn + ); + let model = this.rawEditor.getModel(); + return ( + model.getValueInRange(range) || + model.getLineContent(selection.startLineNumber) + ); }, }, beforeUnmount() {