Annotate settings.ts (#134)

* Annotate settings.ts

* nit
This commit is contained in:
Chenlei Hu
2024-07-16 11:21:23 -04:00
committed by GitHub
parent e216fa82c5
commit 13cda7de41
4 changed files with 53 additions and 41 deletions

View File

@@ -30,6 +30,7 @@ import {
LGraphNode, LGraphNode,
LiteGraph, LiteGraph,
} from "@comfyorg/litegraph"; } from "@comfyorg/litegraph";
import { StorageLocation } from "@/types/settingTypes";
// CSS imports. style.css must be imported later as it overwrites some litegraph styles. // CSS imports. style.css must be imported later as it overwrites some litegraph styles.
import "@comfyorg/litegraph/css/litegraph.css"; import "@comfyorg/litegraph/css/litegraph.css";
@@ -104,8 +105,7 @@ export class ComfyApp {
progress: { value: number; max: number } | null; progress: { value: number; max: number } | null;
configuringGraph: boolean; configuringGraph: boolean;
isNewUserSession: boolean; isNewUserSession: boolean;
// Are there any other options than "server"? storageLocation: StorageLocation;
storageLocation: string;
multiUserServer: boolean; multiUserServer: boolean;
ctx: CanvasRenderingContext2D; ctx: CanvasRenderingContext2D;
widgets: Record<string, ComfyWidgetConstructor>; widgets: Record<string, ComfyWidgetConstructor>;

View File

@@ -233,7 +233,10 @@ export class ComfyWorkflowsMenu {
) { ) {
const r = getExtraMenuOptions?.apply?.(this, arguments); const r = getExtraMenuOptions?.apply?.(this, arguments);
if ( if (
app.ui.settings.getSettingValue("Comfy.UseNewMenu", false) === true app.ui.settings.getSettingValue<boolean>(
"Comfy.UseNewMenu",
false
) === true
) { ) {
const t = this; const t = this;
let img; let img;

View File

@@ -2,44 +2,14 @@ import { $el } from "../ui";
import { api } from "../api"; import { api } from "../api";
import { ComfyDialog } from "./dialog"; import { ComfyDialog } from "./dialog";
import type { ComfyApp } from "../app"; import type { ComfyApp } from "../app";
import type { Setting, SettingParams } from "@/types/settingTypes";
/* The Setting entry stored in `ComfySettingsDialog` */
interface Setting {
id: string;
onChange?: (value: any, oldValue?: any) => void;
name: string;
render: () => HTMLElement;
}
interface SettingOption {
text: string;
value?: string;
}
interface SettingParams {
id: string;
name: string;
type:
| string
| ((
name: string,
setter: (v: any) => void,
value: any,
attrs: any
) => HTMLElement);
defaultValue: any;
onChange?: (newValue: any, oldValue?: any) => void;
attrs?: any;
tooltip?: string;
options?: Array<string | SettingOption> | ((value: any) => SettingOption[]);
}
export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> { export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
app: ComfyApp; app: ComfyApp;
settingsValues: any; settingsValues: any;
settingsLookup: Record<string, Setting>; settingsLookup: Record<string, Setting>;
constructor(app) { constructor(app: ComfyApp) {
super(); super();
this.app = app; this.app = app;
this.settingsValues = {}; this.settingsValues = {};
@@ -83,7 +53,7 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
return Object.values(this.settingsLookup); return Object.values(this.settingsLookup);
} }
#dispatchChange(id, value, oldValue?) { #dispatchChange<T>(id: string, value: T, oldValue?: T) {
this.dispatchEvent( this.dispatchEvent(
new CustomEvent(id + ".change", { new CustomEvent(id + ".change", {
detail: { detail: {
@@ -109,26 +79,26 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
} }
} }
getId(id) { getId(id: string) {
if (this.app.storageLocation === "browser") { if (this.app.storageLocation === "browser") {
id = "Comfy.Settings." + id; id = "Comfy.Settings." + id;
} }
return id; return id;
} }
getSettingValue(id, defaultValue?) { getSettingValue<T>(id: string, defaultValue?: T): T {
let value = this.settingsValues[this.getId(id)]; let value = this.settingsValues[this.getId(id)];
if (value != null) { if (value != null) {
if (this.app.storageLocation === "browser") { if (this.app.storageLocation === "browser") {
try { try {
value = JSON.parse(value); value = JSON.parse(value) as T;
} catch (error) {} } catch (error) {}
} }
} }
return value ?? defaultValue; return value ?? defaultValue;
} }
async setSettingValueAsync(id, value) { async setSettingValueAsync(id: string, value: any) {
const json = JSON.stringify(value); const json = JSON.stringify(value);
localStorage["Comfy.Settings." + id] = json; // backwards compatibility for extensions keep setting in storage localStorage["Comfy.Settings." + id] = json; // backwards compatibility for extensions keep setting in storage
@@ -143,7 +113,7 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
await api.storeSetting(id, value); await api.storeSetting(id, value);
} }
setSettingValue(id, value) { setSettingValue(id: string, value: any) {
this.setSettingValueAsync(id, value).catch((err) => { this.setSettingValueAsync(id, value).catch((err) => {
alert(`Error saving setting '${id}'`); alert(`Error saving setting '${id}'`);
console.error(err); console.error(err);

39
src/types/settingTypes.ts Normal file
View File

@@ -0,0 +1,39 @@
export type StorageLocation = "browser" | "server";
export type SettingInputType =
| "boolean"
| "number"
| "slider"
| "combo"
| "text"
| "hidden";
export type SettingCustomRenderer = (
name: string,
setter: (v: any) => void,
value: any,
attrs: any
) => HTMLElement;
export interface SettingOption {
text: string;
value?: string;
}
export interface Setting {
id: string;
onChange?: (value: any, oldValue?: any) => void;
name: string;
render: () => HTMLElement;
}
export interface SettingParams {
id: string;
name: string;
type: SettingInputType | SettingCustomRenderer;
defaultValue: any;
onChange?: (newValue: any, oldValue?: any) => void;
attrs?: any;
tooltip?: string;
options?: Array<string | SettingOption> | ((value: any) => SettingOption[]);
}