From dece5dbd1124ab4b1de094b29317361561899f7d Mon Sep 17 00:00:00 2001 From: fofolee Date: Sun, 22 Dec 2024 11:55:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BF=AB=E6=8D=B7=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C=E7=9A=84=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/preload.js | 44 ++++++------ src/components/CommandEditor.vue | 5 +- src/components/editor/MonacoEditor.vue | 9 +-- src/plugins/monaco/types/shortcode.api.d.ts | 79 +++++++++++++++++++++ 4 files changed, 107 insertions(+), 30 deletions(-) create mode 100644 src/plugins/monaco/types/shortcode.api.d.ts diff --git a/plugin/preload.js b/plugin/preload.js index 481e84a..f09c519 100644 --- a/plugin/preload.js +++ b/plugin/preload.js @@ -73,30 +73,34 @@ if (!utools.isWindows()) if (utools.isMacOS()) process.env.PATH = `/opt/homebrew/bin:/opt/homebrew/sbin:${process.env.PATH}`; -const shortCodes = [ - (open = (path) => { +const shortCodes = { + open: (path) => { utools.shellOpenItem(path); - }), - (locate = (path) => { + }, + locate: (path) => { utools.shellShowItemInFolder(path); - }), - (visit = (url) => { + }, + visit: (url) => { utools.shellOpenExternal(url); - }), - (system = (cmd) => { - child_process.exec(cmd); - }), - (message = (msg) => { + }, + system: (cmd) => { + let result = child_process.execSync(cmd, { + windowsHide: true, + encoding: "buffer", + }); + return iconv.decode(result, utools.isWindows() ? "gbk" : "utf8"); + }, + message: (msg) => { utools.showNotification(msg); - }), - (keyTap = (key, ...modifier) => utools.simulateKeyboardTap(key, ...modifier)), - (copyTo = (text) => { + }, + keyTap: (key, ...modifier) => utools.simulateKeyboardTap(key, ...modifier), + copyTo: (text) => { electron.clipboard.writeText(text); - }), - (send = (text) => { + }, + send: (text) => { utools.hideMainWindowTypeString(text); - }), -]; + }, +}; const ctlKey = utools.isMacOs() ? "command" : "control"; @@ -580,8 +584,8 @@ let getSandboxFuns = () => { os, child_process, }; - shortCodes.forEach((f) => { - sandbox[f.name] = f; + Object.keys(shortCodes).forEach((f) => { + sandbox[f] = shortCodes[f]; }); return sandbox; }; diff --git a/src/components/CommandEditor.vue b/src/components/CommandEditor.vue index 64d463c..08c3f6f 100644 --- a/src/components/CommandEditor.vue +++ b/src/components/CommandEditor.vue @@ -127,10 +127,7 @@ export default { type: Object, required: true, }, - allQuickCommandTags: { - type: Array, - required: true, - }, + allQuickCommandTags: Array, isLeaving: { type: Boolean, default: false, diff --git a/src/components/editor/MonacoEditor.vue b/src/components/editor/MonacoEditor.vue index 21e0866..c203a3a 100644 --- a/src/components/editor/MonacoEditor.vue +++ b/src/components/editor/MonacoEditor.vue @@ -251,12 +251,9 @@ export default { } ); // F11 全屏 - this.rawEditor().addCommand( - monaco.KeyCode.F11, - () => { - this.$emit("keyStroke", "fullscreen"); - } - ); + this.rawEditor().addCommand(monaco.KeyCode.F11, () => { + this.$emit("keyStroke", "fullscreen"); + }); }, getSelectionOrLineContent() { let selection = this.rawEditor().getSelection(); diff --git a/src/plugins/monaco/types/shortcode.api.d.ts b/src/plugins/monaco/types/shortcode.api.d.ts new file mode 100644 index 0000000..80bf9c8 --- /dev/null +++ b/src/plugins/monaco/types/shortcode.api.d.ts @@ -0,0 +1,79 @@ +/** + * 打开文件或文件夹 + * + * ```js + * // 等同于 + * utools.shellOpenItem(path) + * ``` + */ +declare function open(path: string): void; + +/** + * 在文件管理器中显示文件或文件夹 + * + * ```js + * // 等同于 + * utools.shellShowItemInFolder(path) + * ``` + */ +declare function locate(path: string): void; + +/** + * 访问 URL + * + * ```js + * // 等同于 + * utools.shellOpenExternal(url) + * ``` + */ +declare function visit(url: string): void; + +/** + * 执行系统命令并返回结果,输出编码根据操作系统自出处理 + * + * ```js + * // 等同于 + * child_process.execSync(cmd, { windowsHide: true } ) + * ``` + */ +declare function system(cmd: string): string; + +/** + * 显示系统通知 + * + * ```js + * // 等同于 + * utools.showNotification(msg) + * ``` + */ +declare function message(msg: string): void; + +/** + * 模拟键盘按键 + * + * ```js + * // 等同于 + * utools.simulateKeyboardTap(key, ...modifier) + * ``` + */ +declare function keyTap(key: string, ...modifier: string[]): void; + +/** + * 复制文本到剪贴板 + * + * ```js + * // 等同于 + * electron.clipboard.writeText(text) + * ``` + */ +declare function copyTo(text: string): void; + +/** + * 隐藏主窗口并输入文本 + * + * ```js + * // 等同于 + * utools.hideMainWindowTypeString(text) + * ``` + */ +declare function send(text: string): void;