新增 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