fix(widgets): reuse orphaned DOM containers after undo/redo in Vue render mode

In ComfyUI Vue render mode, WidgetDOM.vue reuses its component instance
during undo/redo without re-calling mountWidgetElement(), leaving newly
created widget containers detached from the DOM.

- AutocompleteTextWidget: scan for empty containers by ID prefix and reuse
- Loras widget: scan for empty .lm-loras-container elements and reuse
- Prevent duplicate event listeners by guarding listener setup on new
  containers only
- Keep container in DOM on cleanup (clearChildren instead of remove)
  so it can be found and reused by the next factory invocation
This commit is contained in:
Will Miao
2026-07-16 18:18:33 +08:00
parent 5d50ddb5d4
commit a9dc4d7b9d
4 changed files with 87 additions and 45 deletions

View File

@@ -74,7 +74,7 @@ function forwardMiddleMouseToCanvas(container: HTMLElement) {
})
}
const vueApps = new Map<number, VueApp>()
const vueApps = new Map<number | string, VueApp>()
let autocompleteTextWidgetInstanceId = 0
export function createAutocompleteTextWidgetInstanceId() {
@@ -405,7 +405,6 @@ function createJsonDisplayWidget(node) {
return { widget }
}
// Store nodeData options per widget type for autocomplete widgets
const widgetInputOptions: Map<string, { placeholder?: string }> = new Map()
function getSerializableWidgetNames(node: any): string[] {
@@ -722,16 +721,30 @@ function createAutocompleteTextWidgetFactory(
inputOptions: { placeholder?: string } = {}
) {
const metadataWidgetName = `__lm_autocomplete_meta_${widgetName}`
const instanceId = createAutocompleteTextWidgetInstanceId()
const container = document.createElement('div')
container.id = `autocomplete-text-widget-${instanceId}`
container.style.width = '100%'
container.style.height = '100%'
container.style.display = 'flex'
container.style.flexDirection = 'column'
container.style.overflow = 'hidden'
forwardMiddleMouseToCanvas(container)
let container: HTMLElement | null = null
const existingContainers = document.querySelectorAll<HTMLElement>(
'[id^="autocomplete-text-widget-"]'
)
for (const el of existingContainers) {
if (el.children.length === 0) {
container = el
break
}
}
if (!container) {
const instanceId = String(createAutocompleteTextWidgetInstanceId())
container = document.createElement('div')
container.id = `autocomplete-text-widget-${instanceId}`
container.style.width = '100%'
container.style.height = '100%'
container.style.display = 'flex'
container.style.flexDirection = 'column'
container.style.overflow = 'hidden'
forwardMiddleMouseToCanvas(container)
}
// Store textarea reference on the container element so cloned widgets can access it
// This is necessary because when widgets are promoted to subgraph nodes,
@@ -810,15 +823,10 @@ function createAutocompleteTextWidgetFactory(
})
vueApp.mount(container)
const appKey = instanceId
const appKey = container.id
vueApps.set(appKey, vueApp)
if (maxHeight) {
// Set only minHeight as a true minimum — remove maxHeight so the
// textarea can grow when the user resizes it in app mode (where
// [&_textarea]:resize-y applies). Graph mode (canvas & Vue render)
// is unaffected because LiteGraph's layout system still governs
// the widget area size.
container.style.minHeight = `${AUTOCOMPLETE_TEXT_WIDGET_MIN_HEIGHT}px`
}
@@ -830,10 +838,14 @@ function createAutocompleteTextWidgetFactory(
)
}
widget.onRemove = createVueWidgetCleanup(vueApp, () => {
const vueCleanup = createVueWidgetCleanup(vueApp, () => {
vueApps.delete(appKey)
})
widget.onRemove = () => {
vueCleanup()
}
// Return minWidth/minHeight hints so ComfyUI's _initialMinSize mechanism
// sets a sensible initial node width (and height for prompt/embeddings).
// loras modelType retains its existing height constraints (getMaxHeight: 100).
@@ -1007,9 +1019,7 @@ app.registerExtension({
info.widgets_values = [...(info.widgets_values ?? []), null]
}
const result = originalConfigure?.apply(this, arguments)
return result
return originalConfigure?.apply(this, arguments)
}
}