export function chainCallback(object, property, callback) { if (object == undefined) { console.error("Tried to add callback to non-existant object") return; } if (property in object) { const callback_orig = object[property] object[property] = function () { const r = callback_orig.apply(this, arguments); callback.apply(this, arguments); return r }; } else { object[property] = callback; } } export function useKVState(nodeType) { chainCallback(nodeType.prototype, "onNodeCreated", function () { chainCallback(this, "onConfigure", function(info) { if (!this.widgets) { //Node has no widgets, there is nothing to restore return } if (typeof(info.widgets_values) != "object") { //widgets_values is in some unknown inactionable format return } let widgetDict = info.widgets_values if (widgetDict.length == undefined) { for (let w of this.widgets) { if (w.name in widgetDict) { w.value = widgetDict[w.name]; if (w.name == "videopreview") { w.updateSource(); } } } } }); chainCallback(this, "onSerialize", function(info) { info.widgets_values = {}; if (!this.widgets) { //object has no widgets, there is nothing to store return; } for (let w of this.widgets) { info.widgets_values[w.name] = w.value; } }); }) } export function fitHeight(node) { node.setSize([node.size[0], node.computeSize([node.size[0], node.size[1]])[1]]) node?.graph?.setDirtyCanvas(true); }