HEL-892: pin bounded VHS combine and video load reference contracts
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
+137
@@ -0,0 +1,137 @@
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import { useSettingStore } from '@/platform/settings/settingStore'
|
||||
import { isStringInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import { app } from '@/scripts/app'
|
||||
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgets'
|
||||
|
||||
const TRACKPAD_DETECTION_THRESHOLD = 50
|
||||
|
||||
function addMultilineWidget(
|
||||
node: LGraphNode,
|
||||
name: string,
|
||||
opts: { defaultVal: string; placeholder?: string }
|
||||
) {
|
||||
const inputEl = document.createElement('textarea')
|
||||
inputEl.className = 'comfy-multiline-input'
|
||||
inputEl.value = opts.defaultVal
|
||||
inputEl.placeholder = opts.placeholder || name
|
||||
inputEl.spellcheck = useSettingStore().get('Comfy.TextareaWidget.Spellcheck')
|
||||
|
||||
const widget = node.addDOMWidget(name, 'customtext', inputEl, {
|
||||
getValue(): string {
|
||||
return inputEl.value
|
||||
},
|
||||
setValue(v: string) {
|
||||
inputEl.value = v
|
||||
}
|
||||
})
|
||||
|
||||
widget.inputEl = inputEl
|
||||
widget.options.minNodeSize = [400, 200]
|
||||
|
||||
inputEl.addEventListener('input', () => {
|
||||
widget.callback?.(widget.value)
|
||||
})
|
||||
|
||||
// Allow middle mouse button panning
|
||||
inputEl.addEventListener('pointerdown', (event: PointerEvent) => {
|
||||
if (event.button === 1) {
|
||||
app.canvas.processMouseDown(event)
|
||||
}
|
||||
})
|
||||
|
||||
inputEl.addEventListener('pointermove', (event: PointerEvent) => {
|
||||
if ((event.buttons & 4) === 4) {
|
||||
app.canvas.processMouseMove(event)
|
||||
}
|
||||
})
|
||||
|
||||
inputEl.addEventListener('pointerup', (event: PointerEvent) => {
|
||||
if (event.button === 1) {
|
||||
app.canvas.processMouseUp(event)
|
||||
}
|
||||
})
|
||||
|
||||
inputEl.addEventListener('wheel', (event: WheelEvent) => {
|
||||
const gesturesEnabled = useSettingStore().get(
|
||||
'LiteGraph.Pointer.TrackpadGestures'
|
||||
)
|
||||
const deltaX = event.deltaX
|
||||
const deltaY = event.deltaY
|
||||
|
||||
const canScrollY = inputEl.scrollHeight > inputEl.clientHeight
|
||||
const isHorizontal = Math.abs(deltaX) > Math.abs(deltaY)
|
||||
|
||||
// Prevent pinch zoom from zooming the page
|
||||
if (event.ctrlKey) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
app.canvas.processMouseWheel(event)
|
||||
return
|
||||
}
|
||||
|
||||
// Detect if this is likely a trackpad gesture vs mouse wheel
|
||||
// Trackpads usually have deltaX or smaller deltaY values (< TRACKPAD_DETECTION_THRESHOLD)
|
||||
// Mouse wheels typically have larger discrete deltaY values (>= TRACKPAD_DETECTION_THRESHOLD)
|
||||
const isLikelyTrackpad =
|
||||
Math.abs(deltaX) > 0 || Math.abs(deltaY) < TRACKPAD_DETECTION_THRESHOLD
|
||||
|
||||
// Trackpad gestures: when enabled, trackpad panning goes to canvas
|
||||
if (gesturesEnabled && isLikelyTrackpad) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
app.canvas.processMouseWheel(event)
|
||||
return
|
||||
}
|
||||
|
||||
// When gestures disabled: horizontal always goes to canvas (no horizontal scroll in textarea)
|
||||
if (isHorizontal) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
app.canvas.processMouseWheel(event)
|
||||
return
|
||||
}
|
||||
|
||||
// Vertical scrolling when gestures disabled: let textarea scroll if scrollable
|
||||
if (canScrollY) {
|
||||
event.stopPropagation()
|
||||
return
|
||||
}
|
||||
|
||||
// If textarea can't scroll vertically, pass to canvas
|
||||
event.preventDefault()
|
||||
app.canvas.processMouseWheel(event)
|
||||
})
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
export const useStringWidget = () => {
|
||||
const widgetConstructor: ComfyWidgetConstructorV2 = (
|
||||
node: LGraphNode,
|
||||
inputSpec: InputSpec
|
||||
) => {
|
||||
if (!isStringInputSpec(inputSpec)) {
|
||||
throw new Error(`Invalid input data: ${inputSpec}`)
|
||||
}
|
||||
|
||||
const defaultVal = inputSpec.default ?? ''
|
||||
const multiline = inputSpec.multiline
|
||||
|
||||
const widget = multiline
|
||||
? addMultilineWidget(node, inputSpec.name, {
|
||||
defaultVal,
|
||||
placeholder: inputSpec.placeholder
|
||||
})
|
||||
: node.addWidget('text', inputSpec.name, defaultVal, () => {}, {})
|
||||
|
||||
if (typeof inputSpec.dynamicPrompts === 'boolean') {
|
||||
widget.dynamicPrompts = inputSpec.dynamicPrompts
|
||||
}
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
return widgetConstructor
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { applyTextReplacements as _applyTextReplacements } from '@/utils/searchAndReplace'
|
||||
|
||||
import { api } from './api'
|
||||
import type { ComfyApp } from './app'
|
||||
import { $el } from './ui'
|
||||
|
||||
export function clone<T>(obj: T): T {
|
||||
try {
|
||||
if (typeof structuredClone !== 'undefined') {
|
||||
return structuredClone(obj)
|
||||
}
|
||||
} catch (error) {
|
||||
// structuredClone is stricter than using JSON.parse/stringify so fallback to that
|
||||
}
|
||||
|
||||
return JSON.parse(JSON.stringify(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `applyTextReplacements` from `@/utils/searchAndReplace` instead
|
||||
* There are external callers to this function, so we need to keep it for now
|
||||
*/
|
||||
export function applyTextReplacements(app: ComfyApp, value: string): string {
|
||||
return _applyTextReplacements(app.rootGraph, value)
|
||||
}
|
||||
|
||||
export async function addStylesheet(
|
||||
urlOrFile: string,
|
||||
relativeTo?: string
|
||||
): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
let url
|
||||
if (urlOrFile.endsWith('.js')) {
|
||||
url = urlOrFile.substr(0, urlOrFile.length - 2) + 'css'
|
||||
} else {
|
||||
url = new URL(
|
||||
urlOrFile,
|
||||
relativeTo ?? `${window.location.protocol}//${window.location.host}`
|
||||
).toString()
|
||||
}
|
||||
$el('link', {
|
||||
parent: document.head,
|
||||
rel: 'stylesheet',
|
||||
type: 'text/css',
|
||||
href: url,
|
||||
onload: res,
|
||||
onerror: rej
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export { downloadBlob } from '@/base/common/downloadUtil'
|
||||
|
||||
export function uploadFile(accept: string) {
|
||||
return new Promise<File>((resolve, reject) => {
|
||||
const input = document.createElement('input')
|
||||
input.type = 'file'
|
||||
input.accept = accept
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0]
|
||||
if (!file) return reject(new Error('No file selected'))
|
||||
resolve(file)
|
||||
}
|
||||
input.click()
|
||||
})
|
||||
}
|
||||
|
||||
export function prop<T>(
|
||||
target: object,
|
||||
name: string,
|
||||
defaultValue: T,
|
||||
onChanged?: (
|
||||
currentValue: T,
|
||||
previousValue: T,
|
||||
target: object,
|
||||
name: string
|
||||
) => void
|
||||
): T {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
let currentValue
|
||||
Object.defineProperty(target, name, {
|
||||
get() {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
return currentValue
|
||||
},
|
||||
set(newValue) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const prevValue = currentValue
|
||||
currentValue = newValue
|
||||
onChanged?.(currentValue, prevValue, target, name)
|
||||
}
|
||||
})
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
export function getStorageValue(id: string) {
|
||||
const clientId = api.clientId ?? api.initialClientId
|
||||
return (
|
||||
(clientId && sessionStorage.getItem(`${id}:${clientId}`)) ??
|
||||
localStorage.getItem(id)
|
||||
)
|
||||
}
|
||||
|
||||
export function setStorageValue(id: string, value: string) {
|
||||
const clientId = api.clientId ?? api.initialClientId
|
||||
if (clientId) {
|
||||
sessionStorage.setItem(`${id}:${clientId}`, value)
|
||||
}
|
||||
localStorage.setItem(id, value)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import type { LGraph, Subgraph } from '@/lib/litegraph/src/litegraph'
|
||||
import { formatDate } from '@/utils/formatUtil'
|
||||
import { collectAllNodes } from '@/utils/graphTraversalUtil'
|
||||
|
||||
export function applyTextReplacements(
|
||||
graph: LGraph | Subgraph,
|
||||
value: string
|
||||
): string {
|
||||
const allNodes = collectAllNodes(graph)
|
||||
|
||||
return value.replace(/%([^%]+)%/g, function (match, text) {
|
||||
const split = text.split('.')
|
||||
if (split.length !== 2) {
|
||||
// Special handling for dates
|
||||
if (split[0].startsWith('date:')) {
|
||||
return formatDate(split[0].substring(5), new Date())
|
||||
}
|
||||
|
||||
if (text !== 'width' && text !== 'height') {
|
||||
// Dont warn on standard replacements
|
||||
console.warn('Invalid replacement pattern', text)
|
||||
}
|
||||
return match
|
||||
}
|
||||
|
||||
// Find node with matching S&R property name
|
||||
let nodes = allNodes.filter(
|
||||
(n) => n.properties?.['Node name for S&R'] === split[0]
|
||||
)
|
||||
// If we can't, see if there is a node with that title
|
||||
if (!nodes.length) {
|
||||
nodes = allNodes.filter((n) => n.title === split[0])
|
||||
}
|
||||
if (!nodes.length) {
|
||||
console.warn('Unable to find node', split[0])
|
||||
return match
|
||||
}
|
||||
|
||||
if (nodes.length > 1) {
|
||||
console.warn('Multiple nodes matched', split[0], 'using first match')
|
||||
}
|
||||
|
||||
const node = nodes[0]
|
||||
|
||||
const widget = node.widgets?.find((w) => w.name === split[1])
|
||||
if (!widget) {
|
||||
console.warn('Unable to find widget', split[1], 'on node', split[0], node)
|
||||
return match
|
||||
}
|
||||
|
||||
return ((widget.value ?? '') + '').replaceAll(
|
||||
// eslint-disable-next-line no-control-regex
|
||||
/[/?<>\\:*|"\x00-\x1F\x7F]/g,
|
||||
'_'
|
||||
)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user