File size: 6,199 Bytes
8fb6433 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import {app} from "/scripts/app.js";
import {
createLegacyWidgetWidthController,
graphNodes,
} from "./h3_legacy_widget_width_fix_core.mjs?v=0.6.2";
const SETTING_ID = "MiniMaxH3ContexLoop.legacyWidgetWidthFix";
const LEGACY_FIX_NODE = "LegacyWidgetWidthFix";
const REPAIR_BUTTON = "Repair widget widths now";
const HOST_LIFECYCLE_PATCH = "__h3_lwwf_host_lifecycle";
const LEGACY_LIFECYCLE_PATCH = "__h3_lwwf_legacy_lifecycle";
const LEGACY_DISPLAY_PREFIX = "MiniMax H3 Contex Loop";
const CURRENT_DISPLAY_PREFIX = "MiniMax H3 Context Loop";
const H3_NODE_TYPES = new Set([
"MiniMaxH3LoopTrim",
"MiniMaxH3ContexLoopSeamProbe",
"MiniMaxH3ChainPlan",
"MiniMaxH3ChainPlanModern",
"MiniMaxH3ChainCheckpointManager",
"MiniMaxH3ChainScenePromptEditor",
"MiniMaxH3ChainRunManager",
"MiniMaxH3ReferenceVideoPrepare",
"MiniMaxH3ScheduledPictureReference",
"MiniMaxH3ScheduledVideoReference",
"MiniMaxH3ScheduledAudioReference",
"MiniMaxH3ScheduledReferenceToVideo",
"MiniMaxH3TaggedPictureReference",
"MiniMaxH3TaggedVideoReference",
"MiniMaxH3TaggedMotionReference",
"MiniMaxH3LazyMotionAVLoader",
"MiniMaxH3TaggedMotionReferencePath",
"MiniMaxH3LazyMotionScenePreview",
"MiniMaxH3TaggedAudioReference",
"MiniMaxH3TaggedReferenceToVideo",
"MiniMaxH3ChainExternalVideo",
"MiniMaxH3ChainLoopStart",
"MiniMaxH3ChainCurrent",
"MiniMaxH3ChainContext",
"MiniMaxH3ChainSegmentSave",
"MiniMaxH3ChainReview",
"MiniMaxH3ChainLoopEnd",
"MiniMaxH3ChainManifestLoad",
"MiniMaxH3ChainExportPNG",
"MiniMaxH3ChainLatentVideoAdapter",
"MiniMaxH3ChainAssemble",
]);
function nodeType(node) {
return node?.comfyClass ?? node?.constructor?.comfyClass ?? node?.type ?? null;
}
function isH3Host(node) {
return H3_NODE_TYPES.has(nodeType(node));
}
function correctLegacyNodeTitle(node) {
if (!isH3Host(node) || typeof node?.title !== "string"
|| !node.title.includes(LEGACY_DISPLAY_PREFIX)) return false;
node.title = node.title.replaceAll(
LEGACY_DISPLAY_PREFIX, CURRENT_DISPLAY_PREFIX);
return true;
}
function correctLegacyGraphTitles(graph) {
for (const node of graphNodes(graph)) correctLegacyNodeTitle(node);
}
function schedule(callback) {
if (typeof globalThis.requestAnimationFrame === "function") {
globalThis.requestAnimationFrame(callback);
} else {
globalThis.setTimeout?.(callback, 0);
}
}
function askStandaloneFixToRepair() {
schedule(() => {
for (const node of graphNodes(app.graph)) {
if (nodeType(node) !== LEGACY_FIX_NODE) continue;
const button = node.widgets?.find((widget) =>
widget.name === REPAIR_BUTTON);
button?.callback?.();
}
});
}
const controller = createLegacyWidgetWidthController({
getGraph: () => app.graph,
requestFrame: schedule,
onRelinquish: askStandaloneFixToRepair,
});
function patchHostLifecycle(nodeTypeDefinition) {
const prototype = nodeTypeDefinition?.prototype;
if (!prototype || prototype[HOST_LIFECYCLE_PATCH]) return;
Object.defineProperty(prototype, HOST_LIFECYCLE_PATCH, {value: true});
const originalAdded = prototype.onAdded;
prototype.onAdded = function h3WidthFixHostAdded(...args) {
const result = originalAdded?.apply(this, args);
controller.addHost(this);
return result;
};
const originalRemoved = prototype.onRemoved;
prototype.onRemoved = function h3WidthFixHostRemoved(...args) {
try {
return originalRemoved?.apply(this, args);
} finally {
controller.removeHost(this);
}
};
}
function patchStandaloneRemoval(nodeTypeDefinition) {
const prototype = nodeTypeDefinition?.prototype;
if (!prototype || prototype[LEGACY_LIFECYCLE_PATCH]) return;
Object.defineProperty(prototype, LEGACY_LIFECYCLE_PATCH, {value: true});
const originalRemoved = prototype.onRemoved;
prototype.onRemoved = function h3WidthFixStandaloneRemoved(...args) {
try {
return originalRemoved?.apply(this, args);
} finally {
controller.scheduleRepairAll();
}
};
}
function settingEnabled() {
return app.ui?.settings?.getSettingValue?.(SETTING_ID) !== false;
}
app.registerExtension({
name: "MiniMaxH3ContexLoop.LegacyWidgetWidthFix",
init() {
controller.installHooks(globalThis.LGraphNode?.prototype);
app.ui?.settings?.addSetting?.({
id: SETTING_ID,
category: ["MiniMax H3 Context Loop", "Compatibility", "Widget widths"],
name: "Repair legacy widget widths while H3 nodes are present",
tooltip: "Works around ComfyUI frontend issue #12443 across the whole canvas. Disable only if your frontend has fixed the LiteGraph widget-width regression.",
type: "boolean",
defaultValue: true,
onChange(value) {
controller.setAllowed(value !== false);
},
});
controller.setAllowed(settingEnabled());
},
beforeRegisterNodeDef(nodeTypeDefinition, nodeData) {
controller.installHooks(globalThis.LGraphNode?.prototype);
if (H3_NODE_TYPES.has(nodeData?.name)) {
patchHostLifecycle(nodeTypeDefinition);
} else if (nodeData?.name === LEGACY_FIX_NODE) {
patchStandaloneRemoval(nodeTypeDefinition);
}
},
nodeCreated(node) {
controller.installHooks(globalThis.LGraphNode?.prototype);
correctLegacyNodeTitle(node);
if (isH3Host(node)) controller.addHost(node);
controller.nodeCreated(node);
},
afterConfigureGraph() {
correctLegacyGraphTitles(app.graph);
controller.syncHosts(graphNodes(app.graph), isH3Host);
controller.scheduleRepairAll();
},
setup() {
controller.installHooks(globalThis.LGraphNode?.prototype);
controller.setAllowed(settingEnabled());
correctLegacyGraphTitles(app.graph);
controller.syncHosts(graphNodes(app.graph), isH3Host);
controller.scheduleRepairAll();
},
});
|