2023-07-10 09:09:03 +00:00
|
|
|
import {app} from "../../scripts/app.js";
|
2023-04-16 05:25:11 +00:00
|
|
|
|
|
|
|
app.registerExtension({
|
2023-06-19 19:32:21 +00:00
|
|
|
name: "Comfy.Keybinds",
|
2023-04-16 05:25:11 +00:00
|
|
|
init() {
|
2023-06-19 19:32:21 +00:00
|
|
|
const keybindListener = function (event) {
|
2023-04-16 05:25:11 +00:00
|
|
|
const modifierPressed = event.ctrlKey || event.metaKey;
|
|
|
|
|
|
|
|
// Queue prompt using ctrl or command + enter
|
2023-06-19 19:32:21 +00:00
|
|
|
if (modifierPressed && event.key === "Enter") {
|
|
|
|
app.queuePrompt(event.shiftKey ? -1 : 0).then();
|
2023-04-16 05:25:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-16 16:58:33 +00:00
|
|
|
const target = event.composedPath()[0];
|
2023-06-19 19:32:21 +00:00
|
|
|
if (["INPUT", "TEXTAREA"].includes(target.tagName)) {
|
2023-04-16 16:58:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-16 05:25:11 +00:00
|
|
|
const modifierKeyIdMap = {
|
2023-06-19 19:32:21 +00:00
|
|
|
s: "#comfy-save-button",
|
|
|
|
o: "#comfy-file-input",
|
|
|
|
Backspace: "#comfy-clear-button",
|
|
|
|
d: "#comfy-load-default-button",
|
2023-04-16 05:25:11 +00:00
|
|
|
};
|
|
|
|
|
2023-06-19 19:32:21 +00:00
|
|
|
const modifierKeybindId = modifierKeyIdMap[event.key];
|
2023-04-16 05:25:11 +00:00
|
|
|
if (modifierPressed && modifierKeybindId) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const elem = document.querySelector(modifierKeybindId);
|
|
|
|
elem.click();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished Handling all modifier keybinds, now handle the rest
|
|
|
|
if (event.ctrlKey || event.altKey || event.metaKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close out of modals using escape
|
2023-06-19 19:32:21 +00:00
|
|
|
if (event.key === "Escape") {
|
2023-04-16 05:25:11 +00:00
|
|
|
const modals = document.querySelectorAll(".comfy-modal");
|
|
|
|
const modal = Array.from(modals).find(modal => window.getComputedStyle(modal).getPropertyValue("display") !== "none");
|
|
|
|
if (modal) {
|
|
|
|
modal.style.display = "none";
|
|
|
|
}
|
2023-06-19 19:32:21 +00:00
|
|
|
|
|
|
|
[...document.querySelectorAll("dialog")].forEach(d => {
|
|
|
|
d.close();
|
|
|
|
});
|
2023-04-16 05:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const keyIdMap = {
|
2023-06-19 19:32:21 +00:00
|
|
|
q: "#comfy-view-queue-button",
|
|
|
|
h: "#comfy-view-history-button",
|
|
|
|
r: "#comfy-refresh-button",
|
2023-04-16 05:25:11 +00:00
|
|
|
};
|
|
|
|
|
2023-06-19 19:32:21 +00:00
|
|
|
const buttonId = keyIdMap[event.key];
|
2023-04-16 05:25:11 +00:00
|
|
|
if (buttonId) {
|
|
|
|
const button = document.querySelector(buttonId);
|
|
|
|
button.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("keydown", keybindListener, true);
|
|
|
|
}
|
|
|
|
});
|