Skip to content

Commit

Permalink
Type domWidget computeSize (#2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei authored Feb 10, 2025
1 parent aaca519 commit 9051ab8
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/scripts/domWidget.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @ts-strict-ignore
import { LGraphCanvas, LGraphNode, LiteGraph } from '@comfyorg/litegraph'
import type { Vector4 } from '@comfyorg/litegraph'
import {
import type {
ICustomWidget,
IWidget,
IWidgetOptions
} from '@comfyorg/litegraph/dist/types/widgets'

Expand Down Expand Up @@ -120,7 +121,13 @@ function getClipPath(
return ''
}

function computeSize(size: [number, number]): void {
function isDomWidget(
widget: IWidget
): widget is DOMWidget<HTMLElement, object> {
return !!widget.element
}

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

let y = this.widgets[0].last_y
Expand All @@ -129,12 +136,14 @@ function computeSize(size: [number, number]): void {
let widgetHeight = 0
let dom = []
for (const w of this.widgets) {
// @ts-expect-error custom widget type
if (w.type === 'converted-widget') {
// Ignore
// @ts-expect-error custom widget type
delete w.computedHeight
} else if (w.computeSize) {
widgetHeight += w.computeSize()[1] + 4
} else if (w.element) {
} else if (isDomWidget(w)) {
// Extract DOM widget size info
const styles = getComputedStyle(w.element)
let minHeight =
Expand All @@ -144,14 +153,17 @@ function computeSize(size: [number, number]): void {
w.options.getMaxHeight?.() ??
parseInt(styles.getPropertyValue('--comfy-widget-max-height'))

let prefHeight =
let prefHeight: string | number =
w.options.getHeight?.() ??
styles.getPropertyValue('--comfy-widget-height')
// @ts-expect-error number has no endsWith
if (prefHeight.endsWith?.('%')) {
prefHeight =
size[1] *
// @ts-expect-error number has no substring
(parseFloat(prefHeight.substring(0, prefHeight.length - 1)) / 100)
} else {
// @ts-expect-error number is not assignable to param of type string
prefHeight = parseInt(prefHeight)
if (isNaN(minHeight)) {
minHeight = prefHeight
Expand Down Expand Up @@ -241,7 +253,9 @@ function computeSize(size: [number, number]): void {
// Position each of the widgets
for (const w of this.widgets) {
w.y = y
// @ts-expect-error custom widget type
if (w.computedHeight) {
// @ts-expect-error custom widget type
y += w.computedHeight
} else if (w.computeSize) {
y += w.computeSize()[1] + 4
Expand All @@ -253,9 +267,7 @@ function computeSize(size: [number, number]): void {

// Override the compute visible nodes function to allow us to hide/show DOM elements when the node goes offscreen
const elementWidgets = new Set()
//@ts-ignore
const computeVisibleNodes = LGraphCanvas.prototype.computeVisibleNodes
//@ts-ignore
LGraphCanvas.prototype.computeVisibleNodes = function (): LGraphNode[] {
const visibleNodes = computeVisibleNodes.apply(this, arguments)

Expand Down

0 comments on commit 9051ab8

Please sign in to comment.