Skip to content

Commit e05a33c

Browse files
Rename to ts (#92)
Rename Remove ts-nocheck, fix errors Update state when graph cleared via UI (#88) Convert to ts, fix imports Co-authored-by: pythongosssss <[email protected]>
1 parent 84c939c commit e05a33c

21 files changed

+305
-300
lines changed

src/scripts/app.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {
2020
parseComfyWorkflow,
2121
} from "../types/comfyWorkflow";
2222
import { ComfyNodeDef } from "/types/apiTypes";
23-
import { ComfyAppMenu } from "./ui/menu/index.js";
24-
import { getStorageValue, setStorageValue } from "./utils.js";
25-
import { ComfyWorkflowManager } from "./workflows.js";
23+
import { ComfyAppMenu } from "./ui/menu/index";
24+
import { getStorageValue } from "./utils";
25+
import { ComfyWorkflowManager, ComfyWorkflow } from "./workflows";
2626
import {
2727
LGraphCanvas,
2828
LGraph,
@@ -2179,16 +2179,11 @@ export class ComfyApp {
21792179
}
21802180
}
21812181

2182-
/**
2183-
* Populates the graph with the specified workflow data
2184-
* @param {*} graphData A serialized graph object
2185-
* @param { boolean } clean If the graph state, e.g. images, should be cleared
2186-
*/
21872182
async loadGraphData(
21882183
graphData?: ComfyWorkflowJSON,
21892184
clean: boolean = true,
21902185
restore_view: boolean = true,
2191-
workflow: string | null = null
2186+
workflow: string | null | ComfyWorkflow = null
21922187
) {
21932188
if (clean !== false) {
21942189
this.clean();

src/scripts/changeTracker.js renamed to src/scripts/changeTracker.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
// @ts-nocheck
2-
1+
import type { ComfyApp } from "./app";
32
import { api } from "./api";
43
import { clone } from "./utils";
54
import { LGraphCanvas, LiteGraph } from "@comfyorg/litegraph";
5+
import { ComfyWorkflow } from "./workflows";
66

77
export class ChangeTracker {
88
static MAX_HISTORY = 50;
9-
#app;
9+
#app: ComfyApp;
1010
undo = [];
1111
redo = [];
1212
activeState = null;
1313
isOurLoad = false;
14-
/** @type { import("./workflows").ComfyWorkflow | null } */
15-
workflow;
14+
workflow: ComfyWorkflow | null;
1615

17-
ds;
18-
nodeOutputs;
16+
ds: { scale: number; offset: [number, number]; };
17+
nodeOutputs: any;
1918

2019
get app() {
2120
return this.#app ?? this.workflow.manager.app;
2221
}
2322

24-
constructor(workflow) {
23+
constructor(workflow: ComfyWorkflow) {
2524
this.workflow = workflow;
2625
}
2726

@@ -90,8 +89,7 @@ export class ChangeTracker {
9089
}
9190
}
9291

93-
/** @param { import("./app").ComfyApp } app */
94-
static init(app) {
92+
static init(app: ComfyApp) {
9593
const changeTracker = () =>
9694
app.workflowManager.activeWorkflow?.changeTracker ?? globalTracker;
9795
globalTracker.#setApp(app);
@@ -137,7 +135,7 @@ export class ChangeTracker {
137135
if (await changeTracker().undoRedo(e)) return;
138136

139137
// If our active element is some type of input then handle changes after they're done
140-
if (ChangeTracker.bindInput(activeEl)) return;
138+
if (ChangeTracker.bindInput(app, activeEl)) return;
141139
changeTracker().checkState();
142140
});
143141
},
@@ -277,4 +275,4 @@ export class ChangeTracker {
277275
}
278276
}
279277

280-
const globalTracker = new ChangeTracker({});
278+
const globalTracker = new ChangeTracker({} as ComfyWorkflow);

src/scripts/ui.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ type Props = {
2424

2525
type Children = Element[] | Element | string | string[];
2626

27-
export function $el(
28-
tag: string,
27+
type ElementType<K extends string> = K extends keyof HTMLElementTagNameMap
28+
? HTMLElementTagNameMap[K]
29+
: HTMLElement;
30+
31+
export function $el<TTag extends string>(
32+
tag: TTag,
2933
propsOrChildren?: Children | Props,
3034
children?: Children
31-
): HTMLElement {
35+
): ElementType<TTag> {
3236
const split = tag.split(".");
3337
const element = document.createElement(split.shift() as string);
3438
if (split.length > 0) {
@@ -78,10 +82,10 @@ export function $el(
7882
}
7983
}
8084
}
81-
return element;
85+
return element as ElementType<TTag>;
8286
}
8387

84-
function dragElement(dragEl, settings) {
88+
function dragElement(dragEl, settings): () => void {
8589
var posDiffX = 0,
8690
posDiffY = 0,
8791
posStartX = 0,
@@ -340,6 +344,8 @@ export class ComfyUI {
340344
menuHamburger: HTMLDivElement;
341345
menuContainer: HTMLDivElement;
342346
queueSize: Element;
347+
restoreMenuPosition: () => void;
348+
loadFile: () => void;
343349

344350
constructor(app) {
345351
this.app = app;
@@ -425,7 +431,6 @@ export class ComfyUI {
425431
},
426432
}) as HTMLInputElement;
427433

428-
// @ts-ignore
429434
this.loadFile = () => fileInput.click();
430435

431436
const autoQueueModeEl = toggleSwitch(
@@ -740,7 +745,6 @@ export class ComfyUI {
740745
},
741746
});
742747

743-
// @ts-ignore
744748
this.restoreMenuPosition = dragElement(this.menuContainer, this.settings);
745749

746750
this.setStatus({ exec_info: { queue_remaining: "X" } });

src/scripts/ui/components/asyncDialog.js renamed to src/scripts/ui/components/asyncDialog.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { ComfyDialog } from "../dialog";
22
import { $el } from "../../ui";
33

4-
export class ComfyAsyncDialog extends ComfyDialog {
5-
#resolve;
4+
export class ComfyAsyncDialog extends ComfyDialog<HTMLDialogElement> {
5+
#resolve: (value: any) => void;
66

7-
constructor(actions) {
7+
constructor(actions?: Array<string | { value?: any; text: string }>) {
88
super(
99
"dialog.comfy-dialog.comfyui-dialog",
1010
actions?.map((opt) => {
@@ -20,7 +20,7 @@ export class ComfyAsyncDialog extends ComfyDialog {
2020
);
2121
}
2222

23-
show(html) {
23+
show(html: string | HTMLElement | HTMLElement[]) {
2424
this.element.addEventListener("close", () => {
2525
this.close();
2626
});
@@ -32,7 +32,7 @@ export class ComfyAsyncDialog extends ComfyDialog {
3232
});
3333
}
3434

35-
showModal(html) {
35+
showModal(html: string | HTMLElement | HTMLElement[]) {
3636
this.element.addEventListener("close", () => {
3737
this.close();
3838
});

src/scripts/ui/components/button.js renamed to src/scripts/ui/components/button.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
// @ts-nocheck
2-
31
import { $el } from "../../ui";
4-
import { applyClasses, toggleElement } from "../utils";
2+
import { applyClasses, ClassList, toggleElement } from "../utils";
53
import { prop } from "../../utils";
4+
import type { ComfyPopup } from "./popup";
5+
import type { ComfyComponent } from ".";
6+
import type { ComfyApp } from "scripts/app";
7+
8+
type ComfyButtonProps = {
9+
icon?: string;
10+
overIcon?: string;
11+
iconSize?: number;
12+
content?: string | HTMLElement;
13+
tooltip?: string;
14+
enabled?: boolean;
15+
action?: (e: Event, btn: ComfyButton) => void;
16+
classList?: ClassList;
17+
visibilitySetting?: { id: string; showValue: any };
18+
app?: ComfyApp;
19+
};
620

7-
/**
8-
* @typedef {{
9-
* icon?: string;
10-
* overIcon?: string;
11-
* iconSize?: number;
12-
* content?: string | HTMLElement;
13-
* tooltip?: string;
14-
* enabled?: boolean;
15-
* action?: (e: Event, btn: ComfyButton) => void,
16-
* classList?: import("../utils").ClassList,
17-
* visibilitySetting?: { id: string, showValue: any },
18-
* app?: import("../../app").ComfyApp
19-
* }} ComfyButtonProps
20-
*/
21-
export class ComfyButton {
21+
export class ComfyButton implements ComfyComponent<HTMLElement> {
2222
#over = 0;
2323
#popupOpen = false;
2424
isOver = false;
2525
iconElement = $el("i.mdi");
2626
contentElement = $el("span");
27-
/**
28-
* @type {import("./popup").ComfyPopup}
29-
*/
30-
popup;
27+
popup: ComfyPopup;
28+
element: HTMLElement;
29+
overIcon: string;
30+
iconSize: number;
31+
content: string | HTMLElement;
32+
icon: string;
33+
tooltip: string;
34+
classList: ClassList;
35+
hidden: boolean;
36+
enabled: boolean;
37+
action: (e: Event, btn: ComfyButton) => void;
3138

32-
/**
33-
* @param {ComfyButtonProps} opts
34-
*/
3539
constructor({
3640
icon,
3741
overIcon,
@@ -43,7 +47,7 @@ export class ComfyButton {
4347
visibilitySetting,
4448
app,
4549
enabled = true,
46-
}) {
50+
}: ComfyButtonProps) {
4751
this.element = $el(
4852
"button",
4953
{
@@ -101,7 +105,7 @@ export class ComfyButton {
101105
this.hidden = prop(this, "hidden", false, this.updateClasses);
102106
this.enabled = prop(this, "enabled", enabled, () => {
103107
this.updateClasses();
104-
this.element.disabled = !this.enabled;
108+
(this.element as HTMLButtonElement).disabled = !this.enabled;
105109
});
106110
this.action = prop(this, "action", action);
107111
this.element.addEventListener("click", (e) => {
@@ -148,12 +152,7 @@ export class ComfyButton {
148152
applyClasses(this.element, this.classList, ...internalClasses);
149153
};
150154

151-
/**
152-
*
153-
* @param { import("./popup").ComfyPopup } popup
154-
* @param { "click" | "hover" } mode
155-
*/
156-
withPopup(popup, mode = "click") {
155+
withPopup(popup: ComfyPopup, mode: "click" | "hover" = "click") {
157156
this.popup = popup;
158157

159158
if (mode === "hover") {

src/scripts/ui/components/buttonGroup.js renamed to src/scripts/ui/components/buttonGroup.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
// @ts-nocheck
2-
31
import { $el } from "../../ui";
42
import { ComfyButton } from "./button";
53
import { prop } from "../../utils";
64

75
export class ComfyButtonGroup {
86
element = $el("div.comfyui-button-group");
7+
buttons: (HTMLElement | ComfyButton)[];
98

10-
/** @param {Array<ComfyButton | HTMLElement>} buttons */
11-
constructor(...buttons) {
9+
constructor(...buttons: (HTMLElement | ComfyButton)[]) {
1210
this.buttons = prop(this, "buttons", buttons, () => this.update());
1311
}
1412

15-
/**
16-
* @param {ComfyButton} button
17-
* @param {number} index
18-
*/
19-
insert(button, index) {
13+
insert(button: ComfyButton, index: number) {
2014
this.buttons.splice(index, 0, button);
2115
this.update();
2216
}
2317

24-
/** @param {ComfyButton} button */
25-
append(button) {
18+
append(button: ComfyButton) {
2619
this.buttons.push(button);
2720
this.update();
2821
}
2922

30-
/** @param {ComfyButton|number} indexOrButton */
31-
remove(indexOrButton) {
23+
remove(indexOrButton: ComfyButton | number) {
3224
if (typeof indexOrButton !== "number") {
3325
indexOrButton = this.buttons.indexOf(indexOrButton);
3426
}

src/scripts/ui/components/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ComfyComponent<T extends HTMLElement = HTMLElement> {
2+
element: T;
3+
}

src/scripts/ui/components/popup.js renamed to src/scripts/ui/components/popup.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
// @ts-nocheck
2-
31
import { prop } from "../../utils";
42
import { $el } from "../../ui";
5-
import { applyClasses } from "../utils";
3+
import { applyClasses, ClassList } from "../utils";
64

75
export class ComfyPopup extends EventTarget {
86
element = $el("div.comfyui-popup");
7+
open: boolean;
8+
children: HTMLElement[];
9+
target: HTMLElement;
10+
ignoreTarget: boolean;
11+
container: HTMLElement;
12+
position: string;
13+
closeOnEscape: boolean;
14+
horizontal: string;
15+
classList: ClassList;
916

10-
/**
11-
* @param {{
12-
* target: HTMLElement,
13-
* container?: HTMLElement,
14-
* classList?: import("../utils").ClassList,
15-
* ignoreTarget?: boolean,
16-
* closeOnEscape?: boolean,
17-
* position?: "absolute" | "relative",
18-
* horizontal?: "left" | "right"
19-
* }} param0
20-
* @param {...HTMLElement} children
21-
*/
2217
constructor(
2318
{
2419
target,
@@ -28,8 +23,16 @@ export class ComfyPopup extends EventTarget {
2823
closeOnEscape = true,
2924
position = "absolute",
3025
horizontal = "left",
26+
}: {
27+
target: HTMLElement;
28+
container?: HTMLElement;
29+
classList?: ClassList;
30+
ignoreTarget?: boolean;
31+
closeOnEscape?: boolean;
32+
position?: "absolute" | "relative";
33+
horizontal?: "left" | "right";
3134
},
32-
...children
35+
...children: HTMLElement[]
3336
) {
3437
super();
3538
this.target = target;

0 commit comments

Comments
 (0)