Skip to content

Commit

Permalink
Migrate domWidget from js to ts (#3)
Browse files Browse the repository at this point in the history
* Rename js to ts

* Migrate domWidget.js

* Fix global declare
  • Loading branch information
huchenlei committed Jun 14, 2024
1 parent 3fbb75c commit 74abc34
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 202 deletions.
2 changes: 1 addition & 1 deletion src/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ComfyUI, $el } from "./ui.js";
import { api } from "./api.js";
import { defaultGraph } from "./defaultGraph.js";
import { getPngMetadata, getWebpMetadata, importA1111, getLatentMetadata } from "./pnginfo.js";
import { addDomClippingSetting } from "./domWidget.js";
import { addDomClippingSetting } from "./domWidget";
import { createImageHost, calculateImageGrid } from "./ui/imagePreview.js"

export const ANIM_PREVIEW_WIDGET = "$$comfy_animation_preview"
Expand Down
53 changes: 41 additions & 12 deletions src/scripts/domWidget.js → src/scripts/domWidget.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { app, ANIM_PREVIEW_WIDGET } from "./app.js";
import type { LGraphNode, Vector4 } from "/types/litegraph";


const SIZE = Symbol();

function intersect(a, b) {

interface Rect {
height: number;
width: number;
x: number;
y: number;
}

interface DOMWidget {
type: string;
name: string;
computedHeight?: number;
element?: HTMLElement;
options: any;
value?: any;
y?: number;
callback?: (value: any) => void;
draw?: (ctx: CanvasRenderingContext2D, node: LGraphNode, widgetWidth: number, y: number, widgetHeight: number) => void;
onRemove?: () => void;
}


function intersect(a: Rect, b: Rect): Vector4 | null {
const x = Math.max(a.x, b.x);
const num1 = Math.min(a.x + a.width, b.x + b.width);
const y = Math.max(a.y, b.y);
Expand All @@ -11,8 +35,8 @@ function intersect(a, b) {
else return null;
}

function getClipPath(node, element) {
const selectedNode = Object.values(app.canvas.selected_nodes)[0];
function getClipPath(node: LGraphNode, element: HTMLElement): string {
const selectedNode: LGraphNode = Object.values(app.canvas.selected_nodes)[0] as LGraphNode;
if (selectedNode && selectedNode !== node) {
const elRect = element.getBoundingClientRect();
const MARGIN = 7;
Expand Down Expand Up @@ -44,7 +68,7 @@ function getClipPath(node, element) {
return "";
}

function computeSize(size) {
function computeSize(size: [number, number]): void {
if (this.widgets?.[0]?.last_y == null) return;

let y = this.widgets[0].last_y;
Expand Down Expand Up @@ -170,7 +194,7 @@ function computeSize(size) {
// Override the compute visible nodes function to allow us to hide/show DOM elements when the node goes offscreen
const elementWidgets = new Set();
const computeVisibleNodes = LGraphCanvas.prototype.computeVisibleNodes;
LGraphCanvas.prototype.computeVisibleNodes = function () {
LGraphCanvas.prototype.computeVisibleNodes = function (): LGraphNode[] {
const visibleNodes = computeVisibleNodes.apply(this, arguments);
for (const node of app.graph._nodes) {
if (elementWidgets.has(node)) {
Expand All @@ -192,7 +216,7 @@ LGraphCanvas.prototype.computeVisibleNodes = function () {

let enableDomClipping = true;

export function addDomClippingSetting() {
export function addDomClippingSetting(): void {
app.ui.settings.addSetting({
id: "Comfy.DOMClippingEnabled",
name: "Enable DOM element clipping (enabling may reduce performance)",
Expand All @@ -204,7 +228,12 @@ export function addDomClippingSetting() {
});
}

LGraphNode.prototype.addDOMWidget = function (name, type, element, options) {
LGraphNode.prototype.addDOMWidget = function (
name: string,
type: string,
element: HTMLElement,
options: Record<string, any>
): DOMWidget {
options = { hideOnZoom: true, selectOn: ["focus", "click"], ...options };

if (!element.parentElement) {
Expand All @@ -221,7 +250,7 @@ LGraphNode.prototype.addDOMWidget = function (name, type, element, options) {
document.addEventListener("mousedown", mouseDownHandler);
}

const widget = {
const widget: DOMWidget = {
type,
name,
get value() {
Expand All @@ -231,7 +260,7 @@ LGraphNode.prototype.addDOMWidget = function (name, type, element, options) {
options.setValue?.(v);
widget.callback?.(widget.value);
},
draw: function (ctx, node, widgetWidth, y, widgetHeight) {
draw: function (ctx: CanvasRenderingContext2D, node: LGraphNode, widgetWidth: number, y: number, widgetHeight: number) {
if (widget.computedHeight == null) {
computeSize.call(node, node.size);
}
Expand All @@ -240,7 +269,7 @@ LGraphNode.prototype.addDOMWidget = function (name, type, element, options) {
node.flags?.collapsed ||
(!!options.hideOnZoom && app.canvas.ds.scale < 0.5) ||
widget.computedHeight <= 0 ||
widget.type === "converted-widget"||
widget.type === "converted-widget" ||
widget.type === "hidden";
element.hidden = hidden;
element.style.display = hidden ? "none" : null;
Expand Down Expand Up @@ -297,9 +326,9 @@ LGraphNode.prototype.addDOMWidget = function (name, type, element, options) {
elementWidgets.add(this);

const collapse = this.collapse;
this.collapse = function() {
this.collapse = function () {
collapse.apply(this, arguments);
if(this.flags?.collapsed) {
if (this.flags?.collapsed) {
element.hidden = true;
element.style.display = "none";
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/widgets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { api } from "./api.js"
import "./domWidget.js";
import "./domWidget";

let controlValueRunBefore = false;
export function updateControlWidgetLabel(widget) {
Expand Down
Loading

0 comments on commit 74abc34

Please sign in to comment.