新增 listenKeydown

This commit is contained in:
fofolee
2022-04-23 00:34:26 +08:00
parent f99123518a
commit e260d14a36
2 changed files with 31 additions and 6 deletions

View File

@@ -55,7 +55,8 @@ export default {
runResult: "", runResult: "",
runResultStatus: true, runResultStatus: true,
subInputValue: "", subInputValue: "",
listener: null, subInputListener: null,
quickcommandListener: null,
history: [], history: [],
historyIdx: null, historyIdx: null,
enableHtml: false, enableHtml: false,
@@ -171,7 +172,7 @@ export default {
setTimeout(() => { setTimeout(() => {
if (this.subInputValue) querySubInput(); if (this.subInputValue) querySubInput();
}, 100); }, 100);
this.listener = (event) => { this.subInputListener = (event) => {
event.preventDefault(); event.preventDefault();
switch (event.keyCode) { switch (event.keyCode) {
case 13: case 13:
@@ -193,7 +194,7 @@ export default {
break; break;
} }
}; };
document.addEventListener("keydown", this.listener, true); document.addEventListener("keydown", this.subInputListener, true);
}, },
// payload 临时赋值 // payload 临时赋值
async getTempPayload(currentCommand) { async getTempPayload(currentCommand) {
@@ -243,15 +244,24 @@ export default {
} }
quickcommand.closeWaitBtn?.(); quickcommand.closeWaitBtn?.();
quickcommand.closeWaitBtn = () => {}; quickcommand.closeWaitBtn = () => {};
if (!!this.listener) { if (!!this.subInputListener) {
this.subInputValue = ""; this.subInputValue = "";
utools.removeSubInput(); utools.removeSubInput();
document.removeEventListener("keydown", this.listener, true); document.removeEventListener("keydown", this.subInputListener, true);
}
if (!!this.quickcommandListener) {
document.removeEventListener("keydown", this.quickcommandListener);
} }
}, },
}, },
unmounted() { unmounted() {
this.stopRun(); this.stopRun();
}, },
mounted() {
quickcommand.listenKeydown = (callback) => {
this.quickcommandListener = callback;
document.addEventListener("keydown", callback);
};
},
}; };
</script> </script>

View File

@@ -170,13 +170,28 @@ interface quickcommandApi {
* @param callback 点击后的回调 * @param callback 点击后的回调
* @param label 按钮的标题 * @param label 按钮的标题
*/ */
showWaitButton(callback, label?: string): void; showWaitButton(callback: () => void, label?: string): void;
/** /**
* 关掉现有等待操作按钮 * 关掉现有等待操作按钮
*/ */
closeWaitBtn(): void; closeWaitBtn(): void;
/**
* 监听用户按键,并执行回调函数
*
* ```js
* quickcommand.listenKeydown(e=>{
* if(e.key === 'c' && e.ctrlKey){
* console.log("取消")
* }
* })
* ```
*
* @param callback 按键回调函数
*/
listenKeydown(callback: (event) => void): void;
/** /**
* 同步等待,会阻塞进程 * 同步等待,会阻塞进程
* @param ms 等待的毫秒数 * @param ms 等待的毫秒数