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)
}
}

View File

@@ -14,12 +14,31 @@ import { getStrengthStepPreference } from "./settings.js";
export function addLorasWidget(node, name, opts, callback) {
ensureLmStyles();
// Create container for loras
const container = document.createElement("div");
container.className = "lm-loras-container";
// Create container for loras — search for an empty container already
// in the DOM first. During undo/redo in ComfyUI Vue render mode,
// WidgetDOM.vue reuses its component without re-calling
// mountWidgetElement(), so we must reuse the existing DOM element
// instead of creating an orphaned replacement.
let container = null;
let reuseExisting = false;
const existingContainers = document.querySelectorAll('.lm-loras-container');
for (const el of existingContainers) {
if (el.children.length === 0) {
container = el;
reuseExisting = true;
break;
}
}
forwardMiddleMouseToCanvas(container);
forwardWheelToCanvas(container);
if (!container) {
container = document.createElement("div");
container.className = "lm-loras-container";
}
if (!reuseExisting) {
forwardMiddleMouseToCanvas(container);
forwardWheelToCanvas(container);
}
// Set initial height using CSS variables approach
const defaultHeight = 200;
@@ -29,10 +48,8 @@ export function addLorasWidget(node, name, opts, callback) {
// scrolls when content exceeds the allocated space.
container.style.setProperty('--comfy-widget-min-height', `${defaultHeight}px`);
if (typeof LiteGraph !== 'undefined' && LiteGraph.vueNodesMode) {
if (!reuseExisting && typeof LiteGraph !== 'undefined' && LiteGraph.vueNodesMode) {
container.classList.add('lm-vue-node');
// Window capture-phase hook: scroll the widget instead of zooming the canvas
// when the wheel is over a scrollable loras list.
enableListWheelScroll(container);
}
@@ -732,9 +749,10 @@ export function addLorasWidget(node, name, opts, callback) {
widget.callback = callback;
widget.onRemove = () => {
container.remove();
while (container.firstChild) {
container.removeChild(container.firstChild);
}
previewTooltip.cleanup();
// Remove keyboard event listener
container.removeEventListener('keydown', handleKeyboardNavigation);
};

View File

@@ -16081,15 +16081,27 @@ function createLoraInfoWidget(node) {
function createAutocompleteTextWidgetFactory(node, widgetName, modelType, inputOptions = {}) {
var _a2, _b, _c;
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 = null;
const existingContainers = document.querySelectorAll(
'[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);
}
const widgetElementRef = { inputEl: void 0 };
container.__widgetInputEl = widgetElementRef;
const metadataWidget = node.addWidget("text", metadataWidgetName, {
@@ -16154,7 +16166,7 @@ function createAutocompleteTextWidgetFactory(node, widgetName, modelType, inputO
ripple: false
});
vueApp.mount(container);
const appKey = instanceId;
const appKey = container.id;
vueApps.set(appKey, vueApp);
if (maxHeight) {
container.style.minHeight = `${AUTOCOMPLETE_TEXT_WIDGET_MIN_HEIGHT}px`;
@@ -16166,9 +16178,12 @@ function createAutocompleteTextWidgetFactory(node, widgetName, modelType, inputO
typeof LiteGraph !== "undefined" && LiteGraph.vueNodesMode
);
}
widget.onRemove = createVueWidgetCleanup(vueApp, () => {
const vueCleanup = createVueWidgetCleanup(vueApp, () => {
vueApps.delete(appKey);
});
widget.onRemove = () => {
vueCleanup();
};
const minWidth = AUTOCOMPLETE_TEXT_MIN_WIDTH_DEFAULT;
const minHeight = modelType === "loras" ? void 0 : AUTOCOMPLETE_TEXT_MIN_HEIGHT_DEFAULT;
return { widget, minWidth, minHeight };
@@ -16315,8 +16330,7 @@ app$1.registerExtension({
if (bypassResult) {
info.widgets_values = [...info.widgets_values ?? [], null];
}
const result = originalConfigure == null ? void 0 : originalConfigure.apply(this, arguments);
return result;
return originalConfigure == null ? void 0 : originalConfigure.apply(this, arguments);
};
}
if (LORA_CHAIN_NODE_TYPES$1.includes(comfyClass)) {

File diff suppressed because one or more lines are too long