diff --git a/js/metahub_save_cta.js b/js/metahub_save_cta.js index 141a45f..43f897e 100644 --- a/js/metahub_save_cta.js +++ b/js/metahub_save_cta.js @@ -8,6 +8,11 @@ const METAHUB_SAVE_NODE_CLASSES = new Set([ const IMAGE_METAHUB_URL = "https://www.imagemetahub.com/"; const IMAGE_METAHUB_DEEPLINK = "imagemetahub://open"; +const HIDE_CTA_SETTING_ID = "ImageMetaHub.SaveNode.hideCTAs"; +const CTA_WIDGET_NAMES = new Set([ + "Open in Image MetaHub", + "Get Image MetaHub", +]); function buildDeepLink(node) { const savedPath = node.__imageMetaHubLastSavedPath; @@ -24,6 +29,18 @@ function openUrl(url) { window.open(url, "_blank", "noopener,noreferrer"); } +function shouldHideCTA() { + if (app.extensionManager?.setting?.get) { + return app.extensionManager.setting.get(HIDE_CTA_SETTING_ID) === true; + } + + if (app.ui?.settings?.getSettingValue) { + return app.ui.settings.getSettingValue(HIDE_CTA_SETTING_ID) === true; + } + + return false; +} + function getSavedPaths(message) { const candidates = [ message?.imagemetahub_files, @@ -55,11 +72,14 @@ function addMetaHubCTA(node) { return; } - if (node.__imageMetaHubCTAAdded) { + if (shouldHideCTA()) { + removeMetaHubCTA(node); return; } - node.__imageMetaHubCTAAdded = true; + if (hasMetaHubCTA(node)) { + return; + } node.addWidget( "button", @@ -82,8 +102,82 @@ function addMetaHubCTA(node) { ); } +function hasMetaHubCTA(node) { + return node.widgets?.some((widget) => CTA_WIDGET_NAMES.has(widget.name)) === true; +} + +function removeMetaHubCTA(node) { + if (!Array.isArray(node.widgets)) { + return; + } + + const nextWidgets = node.widgets.filter( + (widget) => !CTA_WIDGET_NAMES.has(widget.name) + ); + + if (nextWidgets.length !== node.widgets.length) { + node.widgets = nextWidgets; + node.setSize?.(node.computeSize?.() ?? node.size); + } +} + +function syncMetaHubCTA(node) { + if (!METAHUB_SAVE_NODE_CLASSES.has(node.comfyClass)) { + return; + } + + if (shouldHideCTA()) { + removeMetaHubCTA(node); + } else { + addMetaHubCTA(node); + } +} + +function getGraphNodes(graph) { + if (Array.isArray(graph?._nodes)) { + return graph._nodes; + } + + if (Array.isArray(graph?.nodes)) { + return graph.nodes; + } + + return []; +} + +function walkGraphNodes(graph, visitor, visitedGraphs = new Set()) { + if (!graph || visitedGraphs.has(graph)) { + return; + } + + visitedGraphs.add(graph); + + for (const node of getGraphNodes(graph)) { + visitor(node); + walkGraphNodes(node?.subgraph, visitor, visitedGraphs); + } +} + +function syncAllMetaHubCTAs() { + walkGraphNodes(app.graph, syncMetaHubCTA); + + app.graph?.setDirtyCanvas?.(true, true); +} + app.registerExtension({ name: "ImageMetaHub.SaveNodeCTA", + settings: [ + { + id: HIDE_CTA_SETTING_ID, + name: "Hide Open/Get Image MetaHub buttons", + type: "boolean", + defaultValue: false, + category: ["Image MetaHub", "Save Node", "Hide Open/Get Image MetaHub buttons"], + tooltip: + 'Hide the "Open in Image MetaHub" and "Get Image MetaHub" buttons on MetaHub Save nodes.', + onChange: syncAllMetaHubCTAs, + }, + ], async beforeRegisterNodeDef(nodeType, nodeData) { if (!METAHUB_SAVE_NODE_CLASSES.has(nodeData.name)) { @@ -98,6 +192,6 @@ app.registerExtension({ }, async nodeCreated(node) { - addMetaHubCTA(node); + syncMetaHubCTA(node); }, });