Zoom and Pan: move helpers into its namespace to avoid littering global scope

This commit is contained in:
Aarni Koskela 2023-06-05 10:19:06 +03:00
parent 9781f31f74
commit 8fd20bd4c3

View File

@ -1,6 +1,7 @@
// Helper functions onUiLoaded(async() => {
// Get active tab // Helper functions
function getActiveTab(elements, all = false) { // Get active tab
function getActiveTab(elements, all = false) {
const tabs = elements.img2imgTabs.querySelectorAll("button"); const tabs = elements.img2imgTabs.querySelectorAll("button");
if (all) return tabs; if (all) return tabs;
@ -10,10 +11,10 @@ function getActiveTab(elements, all = false) {
return tab; return tab;
} }
} }
} }
// Get tab ID // Get tab ID
function getTabId(elements, elementIDs) { function getTabId(elements, elementIDs) {
const activeTab = getActiveTab(elements); const activeTab = getActiveTab(elements);
const tabIdLookup = { const tabIdLookup = {
"Sketch": elementIDs.sketch, "Sketch": elementIDs.sketch,
@ -21,10 +22,10 @@ function getTabId(elements, elementIDs) {
"Inpaint": elementIDs.inpaint "Inpaint": elementIDs.inpaint
}; };
return tabIdLookup[activeTab.innerText]; return tabIdLookup[activeTab.innerText];
} }
// Wait until opts loaded // Wait until opts loaded
async function waitForOpts() { async function waitForOpts() {
return new Promise(resolve => { return new Promise(resolve => {
const checkInterval = setInterval(() => { const checkInterval = setInterval(() => {
if (window.opts && Object.keys(window.opts).length !== 0) { if (window.opts && Object.keys(window.opts).length !== 0) {
@ -33,17 +34,17 @@ async function waitForOpts() {
} }
}, 100); }, 100);
}); });
} }
// Check is hotkey valid // Check is hotkey valid
function isSingleLetter(value) { function isSingleLetter(value) {
return ( return (
typeof value === "string" && value.length === 1 && /[a-z]/i.test(value) typeof value === "string" && value.length === 1 && /[a-z]/i.test(value)
); );
} }
// Create hotkeyConfig from opts // Create hotkeyConfig from opts
function createHotkeyConfig(defaultHotkeysConfig, hotkeysConfigOpts) { function createHotkeyConfig(defaultHotkeysConfig, hotkeysConfigOpts) {
const result = {}; const result = {};
const usedKeys = new Set(); const usedKeys = new Set();
@ -70,17 +71,16 @@ function createHotkeyConfig(defaultHotkeysConfig, hotkeysConfigOpts) {
} }
return result; return result;
} }
/** /**
* The restoreImgRedMask function displays a red mask around an image to indicate the aspect ratio. * The restoreImgRedMask function displays a red mask around an image to indicate the aspect ratio.
* If the image display property is set to 'none', the mask breaks. To fix this, the function * If the image display property is set to 'none', the mask breaks. To fix this, the function
* temporarily sets the display property to 'block' and then hides the mask again after 300 milliseconds * temporarily sets the display property to 'block' and then hides the mask again after 300 milliseconds
* to avoid breaking the canvas. Additionally, the function adjusts the mask to work correctly on * to avoid breaking the canvas. Additionally, the function adjusts the mask to work correctly on
* very long images. * very long images.
*/ */
function restoreImgRedMask(elements, elementIDs) {
function restoreImgRedMask(elements, elementIDs) {
const mainTabId = getTabId(elements, elementIDs); const mainTabId = getTabId(elements, elementIDs);
if (!mainTabId) return; if (!mainTabId) return;
@ -112,10 +112,8 @@ function restoreImgRedMask(elements, elementIDs) {
setTimeout(() => { setTimeout(() => {
img.style.display = "none"; img.style.display = "none";
}, 400); }, 400);
} }
// Main
onUiLoaded(async() => {
const hotkeysConfigOpts = await waitForOpts(); const hotkeysConfigOpts = await waitForOpts();
// Default config // Default config