From 1d9a67580367bac6a140eb4650c928f4120771a4 Mon Sep 17 00:00:00 2001 From: fofolee Date: Sat, 25 Jan 2025 13:57:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96RunCode=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=BB=93=E6=9E=84=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?ScriptEditor=E7=BB=84=E4=BB=B6=EF=BC=8C=E4=BC=98=E5=8C=96CodeEd?= =?UTF-8?q?itor=E4=BB=A3=E7=A0=81=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/composer/common/CodeEditor.vue | 179 +++++++++++- .../composer/param/ParamImporter.vue | 8 +- .../composer/script/ScriptEditor.vue | 266 ++++++++++++++++++ .../quickcommandUI/QuickCommand.vue | 31 +- src/js/composer/cardComponents.js | 4 + src/js/composer/commands/browserCommands.js | 2 + src/js/composer/commands/scriptCommands.js | 68 +---- .../monaco/types/quickcommand.api.d.ts | 11 +- 8 files changed, 472 insertions(+), 97 deletions(-) create mode 100644 src/components/composer/script/ScriptEditor.vue diff --git a/src/components/composer/common/CodeEditor.vue b/src/components/composer/common/CodeEditor.vue index 2d23305..a46078a 100644 --- a/src/components/composer/common/CodeEditor.vue +++ b/src/components/composer/common/CodeEditor.vue @@ -12,8 +12,36 @@ diff --git a/src/components/composer/param/ParamImporter.vue b/src/components/composer/param/ParamImporter.vue index 5ef6978..9723a10 100644 --- a/src/components/composer/param/ParamImporter.vue +++ b/src/components/composer/param/ParamImporter.vue @@ -12,7 +12,7 @@ + + diff --git a/src/components/quickcommandUI/QuickCommand.vue b/src/components/quickcommandUI/QuickCommand.vue index 0270146..f4b7846 100644 --- a/src/components/quickcommandUI/QuickCommand.vue +++ b/src/components/quickcommandUI/QuickCommand.vue @@ -273,9 +273,8 @@ export default { * @param options 选项 * @param options.language 编程语言 * @param options.args 脚本参数 - * @param options.charset 编码 - * @param options.charset.scriptCode 脚本编码 - * @param options.charset.outputCode 输出编码 + * @param options.scriptCode 脚本文件编码 + * @param options.outputCode 命令行输出编码 * @param options.runInTerminal 终端运行参数,不传则不在终端运行 * @param options.runInTerminal.dir 运行目录 * @param options.runInTerminal.windows windows使用的终端,默认wt @@ -286,11 +285,20 @@ export default { const isWin = window.utools.isWindows(); const { language = isWin ? "cmd" : "shell", - charset = {}, args = [], runInTerminal, } = options; + if (!options.scriptCode) { + options.scriptCode = ["cmd", "powershell"].includes(language) + ? "gbk" + : "utf-8"; + } + + if (!options.outputCode) { + options.outputCode = isWin ? "gbk" : "utf-8"; + } + // 兼容编排,传入true时,使用默认终端 const runInTerminalOptions = runInTerminal === true ? {} : runInTerminal; @@ -306,23 +314,20 @@ export default { } const argsStr = args.map(unescapeAndQuote).join(" "); - const defaultCharset = - isWin && ["cmd", "powershell"].includes(language) ? "gbk" : "utf-8"; - - const { scriptCode = defaultCharset, outputCode = defaultCharset } = - charset; - window.runCodeFile( code, { ...programs[language], - charset: { scriptCode, outputCode }, + charset: { + scriptCode: options.scriptCode, + outputCode: options.outputCode, + }, scptarg: argsStr, }, runInTerminalOptions, - (result, err) => (err ? reject(err) : reslove(result)) + (result, err) => (err ? reject(err) : reslove(result)), + false ); - false; }); }; diff --git a/src/js/composer/cardComponents.js b/src/js/composer/cardComponents.js index c317c28..6d6fd87 100644 --- a/src/js/composer/cardComponents.js +++ b/src/js/composer/cardComponents.js @@ -55,3 +55,7 @@ export const SelectListEditor = defineAsyncComponent(() => export const ReturnEditor = defineAsyncComponent(() => import("components/composer/script/ReturnEditor.vue") ); + +export const ScriptEditor = defineAsyncComponent(() => + import("components/composer/script/ScriptEditor.vue") +); diff --git a/src/js/composer/commands/browserCommands.js b/src/js/composer/commands/browserCommands.js index 718195e..0c390f0 100644 --- a/src/js/composer/commands/browserCommands.js +++ b/src/js/composer/commands/browserCommands.js @@ -311,6 +311,7 @@ export const browserCommands = { { label: "脚本内容", component: "CodeEditor", + language: "webjavascript", icon: "code", width: 12, placeholder: "输入JavaScript代码,使用return返回结果", @@ -382,6 +383,7 @@ export const browserCommands = { config: [ { component: "CodeEditor", + language: "css", icon: "style", width: 12, placeholder: "输入CSS代码", diff --git a/src/js/composer/commands/scriptCommands.js b/src/js/composer/commands/scriptCommands.js index ae02bac..5c2ad5a 100644 --- a/src/js/composer/commands/scriptCommands.js +++ b/src/js/composer/commands/scriptCommands.js @@ -1,26 +1,10 @@ -import programs from "js/options/programs"; - export const scriptCommands = { label: "编程相关", icon: "integration_instructions", commands: [ - { - value: "", - label: "赋值", - icon: "script", - outputVariable: "value", - saveOutput: true, - config: [ - { - label: "值或表达式", - component: "VariableInput", - width: 12, - }, - ], - }, { value: "injectJs", - label: "注入JS脚本", + label: "注入JS代码", icon: "script", neverHasOutput: true, isExpression: true, @@ -28,6 +12,7 @@ export const scriptCommands = { { label: "JS脚本", component: "CodeEditor", + language: "quickcommand", placeholder: "共享当前上下文,支持utools,quickcommand,quickcomposer等接口", width: 12, @@ -36,53 +21,10 @@ export const scriptCommands = { }, { value: "quickcommand.runCode", - label: "执行代码", - icon: "script", + label: "运行脚本", + component: "ScriptEditor", + desc: "运行各种编程语言的代码", isAsync: true, - outputVariable: "result", - saveOutput: true, - config: [ - { - label: "脚本", - component: "CodeEditor", - placeholder: "需要本机安装了对应的解释器/编译器", - width: 12, - }, - { - component: "OptionEditor", - width: 12, - options: { - language: { - label: "语言", - component: "QSelect", - icon: "language", - options: Object.keys(programs).slice(2, -1), - width: 8, - }, - runInTerminal: { - label: "终端运行", - icon: "terminal", - component: "CheckButton", - width: 4, - }, - args: { - topLabel: "参数", - icon: "data_array", - component: "ArrayEditor", - width: 12, - }, - charset: { - label: "编码", - icon: "abc", - component: "DictEditor", - options: { - optionKeys: ["scriptCode", "outputCode"], - }, - width: 12, - }, - }, - }, - ], }, { value: "return", diff --git a/src/plugins/monaco/types/quickcommand.api.d.ts b/src/plugins/monaco/types/quickcommand.api.d.ts index 44df73b..95eb35b 100644 --- a/src/plugins/monaco/types/quickcommand.api.d.ts +++ b/src/plugins/monaco/types/quickcommand.api.d.ts @@ -655,9 +655,8 @@ interface quickcommandApi { * @param options 选项 * @param options.language 编程语言,不传时则根据操作系统选择cmd或是shell * @param options.args 脚本参数 - * @param options.charset 编码,不传时则根据操作系统及语言选择utf-8或是gbk - * @param options.charset.scriptCode 脚本编码 - * @param options.charset.outputCode 输出编码 + * @param options.scriptCode 脚本文件编码 + * @param options.outputCode 命令行输出编码 * @param options.runInTerminal 终端运行参数,不传则不在终端运行 * @param options.runInTerminal.dir 运行目录 * @param options.runInTerminal.windows windows使用的终端,默认wt @@ -700,10 +699,8 @@ interface quickcommandApi { | "csharp" | "c"; args?: string[]; - charset?: { - scriptCode?: string; - outputCode?: string; - }; + scriptCode?: string; + outputCode?: string; runInTerminal?: { dir?: string; windows?: "wt" | "cmd";