diff --git a/src/components/CommandRunResult.vue b/src/components/CommandRunResult.vue index 1ceaf0d..0bc48d1 100644 --- a/src/components/CommandRunResult.vue +++ b/src/components/CommandRunResult.vue @@ -55,7 +55,8 @@ export default { runResult: "", runResultStatus: true, subInputValue: "", - listener: null, + subInputListener: null, + quickcommandListener: null, history: [], historyIdx: null, enableHtml: false, @@ -171,7 +172,7 @@ export default { setTimeout(() => { if (this.subInputValue) querySubInput(); }, 100); - this.listener = (event) => { + this.subInputListener = (event) => { event.preventDefault(); switch (event.keyCode) { case 13: @@ -193,7 +194,7 @@ export default { break; } }; - document.addEventListener("keydown", this.listener, true); + document.addEventListener("keydown", this.subInputListener, true); }, // payload 临时赋值 async getTempPayload(currentCommand) { @@ -243,15 +244,24 @@ export default { } quickcommand.closeWaitBtn?.(); quickcommand.closeWaitBtn = () => {}; - if (!!this.listener) { + if (!!this.subInputListener) { this.subInputValue = ""; utools.removeSubInput(); - document.removeEventListener("keydown", this.listener, true); + document.removeEventListener("keydown", this.subInputListener, true); + } + if (!!this.quickcommandListener) { + document.removeEventListener("keydown", this.quickcommandListener); } }, }, unmounted() { this.stopRun(); }, + mounted() { + quickcommand.listenKeydown = (callback) => { + this.quickcommandListener = callback; + document.addEventListener("keydown", callback); + }; + }, }; diff --git a/src/plugins/monaco/types/quickcommand.api.d.ts b/src/plugins/monaco/types/quickcommand.api.d.ts index e19b01e..c669db7 100644 --- a/src/plugins/monaco/types/quickcommand.api.d.ts +++ b/src/plugins/monaco/types/quickcommand.api.d.ts @@ -170,13 +170,28 @@ interface quickcommandApi { * @param callback 点击后的回调 * @param label 按钮的标题 */ - showWaitButton(callback, label?: string): void; + showWaitButton(callback: () => void, label?: string): void; /** * 关掉现有等待操作按钮 */ closeWaitBtn(): void; + /** + * 监听用户按键,并执行回调函数 + * + * ```js + * quickcommand.listenKeydown(e=>{ + * if(e.key === 'c' && e.ctrlKey){ + * console.log("取消") + * } + * }) + * ``` + * + * @param callback 按键回调函数 + */ + listenKeydown(callback: (event) => void): void; + /** * 同步等待,会阻塞进程 * @param ms 等待的毫秒数