diff --git a/src_web/comfyui/config.ts b/src_web/comfyui/config.ts index cdb7096..2c824c9 100644 --- a/src_web/comfyui/config.ts +++ b/src_web/comfyui/config.ts @@ -1,13 +1,10 @@ // @ts-ignore import { app } from "../../scripts/app.js"; -// @ts-ignore -import { rgthreeConfig } from "rgthree/config.js"; import { RgthreeDialog, RgthreeDialogOptions } from "rgthree/common/dialog.js"; -import { rgthreeApi } from "rgthree/common/rgthree_api.js"; -import { getObjectValue, setObjectValue } from "rgthree/common/shared_utils.js"; import { createElement as $el, querySelectorAll as $$ } from "rgthree/common/utils_dom.js"; import { checkmark, logoRgthree } from "rgthree/common/media/svgs.js"; import { rgthree } from "./rgthree.js"; +import { SERVICE as CONFIG_SERVICE } from "./config_service.js"; /** Types of config used as a hint for the form handling. */ enum ConfigType { @@ -69,42 +66,12 @@ const CONFIGURABLE: { features: ConfigurationSchema[] } = { ], }; -/** - * A singleton service exported as `SERVICE` to handle configuration routines. - */ -class ConfigService extends EventTarget { - getConfigValue(key: string, def?: any) { - return getObjectValue(rgthreeConfig, key, def); - } - - /** - * Given an object of key:value changes it will send to the server and wait for a successful - * response before setting the values on the local rgthreeConfig. - */ - async setConfigValues(changed: { [key: string]: any }) { - const body = new FormData(); - body.append("json", JSON.stringify(changed)); - const response = await rgthreeApi.fetchJson("/config", { method: "POST", body }); - if (response.status === "ok") { - for (const [key, value] of Object.entries(changed)) { - setObjectValue(rgthreeConfig, key, value); - this.dispatchEvent(new CustomEvent("config-change", { detail: { key, value } })); - } - } else { - return false; - } - return true; - } -} - -/** The ConfigService singleton. */ -export const SERVICE = new ConfigService(); /** * Creates a new fieldrow for main or sub configuration items. */ function fieldrow(item: ConfigurationSchema) { - const initialValue = SERVICE.getConfigValue(item.key); + const initialValue = CONFIG_SERVICE.getConfigValue(item.key); const container = $el(`div.fieldrow.-type-${TYPE_TO_STRING[item.type]}`, { dataset: { name: item.key, @@ -193,7 +160,7 @@ export class RgthreeConfigDialog extends RgthreeDialog { this.close(); return; } - const success = await SERVICE.setConfigValues(changed); + const success = await CONFIG_SERVICE.setConfigValues(changed); if (success) { this.close(); rgthree.showMessage({ @@ -216,7 +183,7 @@ export class RgthreeConfigDialog extends RgthreeDialog { return $$("[data-name]", this.contentElement).reduce((acc: { [key: string]: any }, el) => { const name = el.dataset["name"]!; const type = el.dataset["type"]!; - const initialValue = getObjectValue(rgthreeConfig, name); + const initialValue = CONFIG_SERVICE.getConfigValue(name); let currentValueEl = $$("input, textarea", el)[0] as HTMLInputElement; let currentValue: any = null; if (type === String(ConfigType.BOOLEAN)) { diff --git a/src_web/comfyui/config_service.ts b/src_web/comfyui/config_service.ts new file mode 100644 index 0000000..b469295 --- /dev/null +++ b/src_web/comfyui/config_service.ts @@ -0,0 +1,36 @@ + +// @ts-ignore +import { rgthreeConfig } from "rgthree/config.js"; +import { getObjectValue, setObjectValue } from "rgthree/common/shared_utils.js"; +import { rgthreeApi } from "rgthree/common/rgthree_api.js"; + +/** + * A singleton service exported as `SERVICE` to handle configuration routines. + */ +class ConfigService extends EventTarget { + getConfigValue(key: string, def?: any) { + return getObjectValue(rgthreeConfig, key, def); + } + + /** + * Given an object of key:value changes it will send to the server and wait for a successful + * response before setting the values on the local rgthreeConfig. + */ + async setConfigValues(changed: { [key: string]: any }) { + const body = new FormData(); + body.append("json", JSON.stringify(changed)); + const response = await rgthreeApi.fetchJson("/config", { method: "POST", body }); + if (response.status === "ok") { + for (const [key, value] of Object.entries(changed)) { + setObjectValue(rgthreeConfig, key, value); + this.dispatchEvent(new CustomEvent("config-change", { detail: { key, value } })); + } + } else { + return false; + } + return true; + } +} + +/** The ConfigService singleton. */ +export const SERVICE = new ConfigService(); \ No newline at end of file diff --git a/src_web/comfyui/rgthree.ts b/src_web/comfyui/rgthree.ts index f758ccf..8e1a876 100644 --- a/src_web/comfyui/rgthree.ts +++ b/src_web/comfyui/rgthree.ts @@ -11,8 +11,7 @@ import type { import { app } from "../../scripts/app.js"; // @ts-ignore import { api } from "../../scripts/api.js"; -// @ts-ignore -import { SERVICE as CONFIG_SERVICE } from "./config.js"; +import { SERVICE as CONFIG_SERVICE } from "./config_service.js"; import { fixBadLinks } from "rgthree/common/link_fixer.js"; import { wait } from "rgthree/common/shared_utils.js"; import { replaceNode, waitForCanvas, waitForGraph } from "./utils.js"; diff --git a/web/comfyui/config.js b/web/comfyui/config.js index 24105a6..8450f02 100644 --- a/web/comfyui/config.js +++ b/web/comfyui/config.js @@ -1,11 +1,9 @@ import { app } from "../../scripts/app.js"; -import { rgthreeConfig } from "../../rgthree/config.js"; import { RgthreeDialog } from "../../rgthree/common/dialog.js"; -import { rgthreeApi } from "../../rgthree/common/rgthree_api.js"; -import { getObjectValue, setObjectValue } from "../../rgthree/common/shared_utils.js"; import { createElement as $el, querySelectorAll as $$ } from "../../rgthree/common/utils_dom.js"; import { checkmark, logoRgthree } from "../../rgthree/common/media/svgs.js"; import { rgthree } from "./rgthree.js"; +import { SERVICE as CONFIG_SERVICE } from "./config_service.js"; var ConfigType; (function (ConfigType) { ConfigType[ConfigType["UNKNOWN"] = 0] = "UNKNOWN"; @@ -50,29 +48,8 @@ const CONFIGURABLE = { }, ], }; -class ConfigService extends EventTarget { - getConfigValue(key, def) { - return getObjectValue(rgthreeConfig, key, def); - } - async setConfigValues(changed) { - const body = new FormData(); - body.append("json", JSON.stringify(changed)); - const response = await rgthreeApi.fetchJson("/config", { method: "POST", body }); - if (response.status === "ok") { - for (const [key, value] of Object.entries(changed)) { - setObjectValue(rgthreeConfig, key, value); - this.dispatchEvent(new CustomEvent("config-change", { detail: { key, value } })); - } - } - else { - return false; - } - return true; - } -} -export const SERVICE = new ConfigService(); function fieldrow(item) { - const initialValue = SERVICE.getConfigValue(item.key); + const initialValue = CONFIG_SERVICE.getConfigValue(item.key); const container = $el(`div.fieldrow.-type-${TYPE_TO_STRING[item.type]}`, { dataset: { name: item.key, @@ -151,7 +128,7 @@ export class RgthreeConfigDialog extends RgthreeDialog { this.close(); return; } - const success = await SERVICE.setConfigValues(changed); + const success = await CONFIG_SERVICE.setConfigValues(changed); if (success) { this.close(); rgthree.showMessage({ @@ -174,7 +151,7 @@ export class RgthreeConfigDialog extends RgthreeDialog { return $$("[data-name]", this.contentElement).reduce((acc, el) => { const name = el.dataset["name"]; const type = el.dataset["type"]; - const initialValue = getObjectValue(rgthreeConfig, name); + const initialValue = CONFIG_SERVICE.getConfigValue(name); let currentValueEl = $$("input, textarea", el)[0]; let currentValue = null; if (type === String(ConfigType.BOOLEAN)) { diff --git a/web/comfyui/config_service.js b/web/comfyui/config_service.js new file mode 100644 index 0000000..efca643 --- /dev/null +++ b/web/comfyui/config_service.js @@ -0,0 +1,24 @@ +import { rgthreeConfig } from "../../rgthree/config.js"; +import { getObjectValue, setObjectValue } from "../../rgthree/common/shared_utils.js"; +import { rgthreeApi } from "../../rgthree/common/rgthree_api.js"; +class ConfigService extends EventTarget { + getConfigValue(key, def) { + return getObjectValue(rgthreeConfig, key, def); + } + async setConfigValues(changed) { + const body = new FormData(); + body.append("json", JSON.stringify(changed)); + const response = await rgthreeApi.fetchJson("/config", { method: "POST", body }); + if (response.status === "ok") { + for (const [key, value] of Object.entries(changed)) { + setObjectValue(rgthreeConfig, key, value); + this.dispatchEvent(new CustomEvent("config-change", { detail: { key, value } })); + } + } + else { + return false; + } + return true; + } +} +export const SERVICE = new ConfigService(); diff --git a/web/comfyui/rgthree.js b/web/comfyui/rgthree.js index 38a1460..6695430 100644 --- a/web/comfyui/rgthree.js +++ b/web/comfyui/rgthree.js @@ -1,6 +1,6 @@ import { app } from "../../scripts/app.js"; import { api } from "../../scripts/api.js"; -import { SERVICE as CONFIG_SERVICE } from "./config.js"; +import { SERVICE as CONFIG_SERVICE } from "./config_service.js"; import { fixBadLinks } from "../../rgthree/common/link_fixer.js"; import { wait } from "../../rgthree/common/shared_utils.js"; import { replaceNode, waitForCanvas, waitForGraph } from "./utils.js";