From d3a80eb10cd40ec68dd1be43f7dd8420ef6f250f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20=20Sk=C3=B6ld?= Date: Wed, 7 Aug 2024 18:45:14 +0200 Subject: [PATCH] Use custom fetchApi as workaround to comfys not working --- web/js/customLoraLoaders.js | 13 +----------- web/js/sn0w.js | 40 ++++++++++++++++++++++++++++++------- web/js/textbox.js | 10 ++-------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/web/js/customLoraLoaders.js b/web/js/customLoraLoaders.js index ce6acc9..10f2476 100644 --- a/web/js/customLoraLoaders.js +++ b/web/js/customLoraLoaders.js @@ -3,18 +3,7 @@ import { app } from '../../../scripts/app.js'; import { api } from '../../../scripts/api.js'; async function fetchExtensionSettings() { - try { - const response = await api.fetchApi( - '/extensions/ComfyUI-Sn0w-Scripts/settings/sn0w_settings.json' - ); - if (!response.ok) { - throw new Error('Network response was not ok ' + response.statusText); - } - const data = await response.json(); - return data; - } catch (error) { - console.error('There was a problem with the fetch operation:', error); - } + return SettingUtils.fetchApi("/extensions/ComfyUI-Sn0w-Scripts/settings/sn0w_settings.json"); } const sn0wSettings = await fetchExtensionSettings(); diff --git a/web/js/sn0w.js b/web/js/sn0w.js index 4de6a8c..fe6e1c4 100644 --- a/web/js/sn0w.js +++ b/web/js/sn0w.js @@ -19,6 +19,23 @@ export class SettingUtils { } } + static async fetchApi(route, options) { + if (!options) { + options = {}; + } + if (!options.headers) { + options.headers = {}; + } + const url = `${location.protocol}//${location.host}${route}`; + try { + const response = await fetch(url, options); + const data = await response.json(); + return data; + } catch (error) { + console.error('There was a problem with the fetch operation:', error); + } + } + // Callback function to add or remove the filename from the settings list static async toggleFavourite(existingList, filename, setting) { try { @@ -54,28 +71,26 @@ export class SettingUtils { textarea.id = uniqueId; textarea.value = value; const maxLines = 10; - const lineHeight = 15; // Function to set the height based on the number of lines const adjustHeight = () => { + textarea.style.height = ''; // Allow to shrink const lines = textarea.value.split('\n').length; + const scrollHeight = textarea.scrollHeight + 3; if (lines > maxLines) { + const height = (scrollHeight / lines) * maxLines textarea.setAttribute( 'style', - `width: 100%; height: ${lineHeight * maxLines}px; resize: none;` + `width: 100%; height: ${height}px; resize: none;` ); return; } - const height = lines * lineHeight; - textarea.setAttribute('style', `width: 100%; height: ${height}px; resize: none;`); + textarea.setAttribute('style', `width: 100%; height: ${scrollHeight}px; resize: none;`); }; adjustHeight(); textarea.onchange = () => setter(textarea.value); textarea.addEventListener('input', adjustHeight); - // Apply event handler - textarea.onchange = () => setter(textarea.value); - // Apply additional attributes for (const [key, val] of Object.entries(attrs)) { textarea.setAttribute(key, val); @@ -94,6 +109,17 @@ export class SettingUtils { label.className = 'comfy-tooltip-indicator'; } + // Update height when settings are opened + const dialog = document.getElementById('comfy-settings-dialog'); + const observer = new MutationObserver(mutations => { + mutations.forEach(mutation => { + if (mutation.type === 'attributes' && mutation.attributeName === 'open') { + adjustHeight(); + } + }); + }); + observer.observe(dialog, { attributes: true }); + return tr; } diff --git a/web/js/textbox.js b/web/js/textbox.js index d556174..63ca543 100644 --- a/web/js/textbox.js +++ b/web/js/textbox.js @@ -464,15 +464,9 @@ app.registerExtension({ } async function getValidFiles(type) { - const apiRequest = await api.fetchApi(`${SettingUtils.API_PREFIX}/${type}`, { + return await SettingUtils.fetchApi(`${SettingUtils.API_PREFIX}/${type}`, { method: "GET", - }) - if (!apiRequest.ok) { - console.error('API request failed:', apiRequest.statusText); - } else { - const responseBody = await apiRequest.json(); - return responseBody; - } + }); } nodeType.prototype.onNodeCreated = function () {