|
| 1 | +import { app } from "../../scripts/app.js"; |
| 2 | + |
| 3 | +import './bizyair_frontend.js' |
| 4 | +import { hideWidget } from './subassembly/tools.js' |
| 5 | + |
| 6 | +function createSetWidgetCallback() { |
| 7 | + return function setWidgetCallback() { |
| 8 | + const targetWidget = this.widgets.find(widget => widget.name == "dataset_path"); |
| 9 | + if (targetWidget) { |
| 10 | + targetWidget.value = targetWidget.value || "to choose" |
| 11 | + targetWidget.mouse = function(e, pos, canvas) { |
| 12 | + try { |
| 13 | + if (e.type === "pointerdown" || e.type === "mousedown" || e.type === "click" || e.type === "pointerup") { |
| 14 | + e.preventDefault(); |
| 15 | + e.stopPropagation(); |
| 16 | + e.widgetClick = true; |
| 17 | + const currentNode = this.node; |
| 18 | + |
| 19 | + if (!currentNode || !currentNode.widgets) { |
| 20 | + console.warn("Node or widgets not available"); |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + if (typeof bizyAirLib !== 'undefined' && typeof bizyAirLib.showDatasetSelect === 'function') { |
| 25 | + bizyAirLib.showDatasetSelect({ |
| 26 | + showDatasetSelect: true, |
| 27 | + isNodeSelect: true, |
| 28 | + onApply: (versionId, name) => { |
| 29 | + console.log("onApply", versionId, name) |
| 30 | + if (!currentNode || !currentNode.widgets) return; |
| 31 | + |
| 32 | + const datasetPath = currentNode.widgets.find(widget => widget.name == "dataset_path"); |
| 33 | + const datasetVersionId = currentNode.widgets.find(w => w.name === "dataset_version_id"); |
| 34 | + if (datasetPath && datasetVersionId) { |
| 35 | + datasetPath.value = name; |
| 36 | + datasetVersionId.value = versionId; |
| 37 | + currentNode.setDirtyCanvas(true); |
| 38 | + } |
| 39 | + } |
| 40 | + }) |
| 41 | + } else { |
| 42 | + console.error("bizyAirLib not available"); |
| 43 | + } |
| 44 | + return false; |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + console.error("Error handling mouse event:", error); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + targetWidget.node = this; |
| 52 | + targetWidget.options = targetWidget.options || {}; |
| 53 | + targetWidget.options.values = () => []; |
| 54 | + targetWidget.options.editable = false; |
| 55 | + targetWidget.clickable = true; |
| 56 | + targetWidget.processMouse = true; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function setupNodeMouseBehavior(node) { |
| 62 | + hideWidget(node, "dataset_version_id"); |
| 63 | + |
| 64 | + if (!node._bizyairState) { |
| 65 | + node._bizyairState = { |
| 66 | + lastClickTime: 0, |
| 67 | + DEBOUNCE_DELAY: 300, |
| 68 | + original_onMouseDown: node.onMouseDown |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + node.onMouseDown = function(e, pos, canvas) { |
| 73 | + if (e.widgetClick) { |
| 74 | + return this._bizyairState.original_onMouseDown?.apply(this, arguments); |
| 75 | + } |
| 76 | + const targetWidget = this.widgets.find(widget => widget.name == "dataset_path"); |
| 77 | + if (targetWidget && pos[1] - targetWidget.last_y > 0 && pos[1] - targetWidget.last_y < 20) { |
| 78 | + const litecontextmenu = document.querySelector('.litegraph.litecontextmenu') |
| 79 | + if (litecontextmenu) { |
| 80 | + litecontextmenu.style.display = 'none' |
| 81 | + } |
| 82 | + e.stopImmediatePropagation(); |
| 83 | + e.preventDefault(); |
| 84 | + if (e.button !== 0) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + const currentTime = new Date().getTime(); |
| 89 | + if (currentTime - this._bizyairState.lastClickTime < this._bizyairState.DEBOUNCE_DELAY) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + this._bizyairState.lastClickTime = currentTime; |
| 93 | + bizyAirLib.showDatasetSelect({ |
| 94 | + showDatasetSelect: true, |
| 95 | + isNodeSelect: true, |
| 96 | + onApply: (versionId, name) => { |
| 97 | + console.log("onApply", versionId, name) |
| 98 | + if (!currentNode || !currentNode.widgets) return; |
| 99 | + |
| 100 | + const datasetPath = currentNode.widgets.find(widget => widget.name == "dataset_path"); |
| 101 | + const datasetVersionId = currentNode.widgets.find(w => w.name === "dataset_version_id"); |
| 102 | + if (datasetPath && datasetVersionId) { |
| 103 | + datasetPath.value = name; |
| 104 | + datasetVersionId.value = versionId; |
| 105 | + currentNode.setDirtyCanvas(true); |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + return false; |
| 110 | + } else { |
| 111 | + return this._bizyairState.original_onMouseDown?.apply(this, arguments); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +app.registerExtension({ |
| 117 | + name: "bizyair.siliconcloud.share.dataset.loader.train", |
| 118 | + async beforeRegisterNodeDef(nodeType, nodeData, app) { |
| 119 | + if (nodeData.name === "BizyAir_TrainDatasetAdd") { |
| 120 | + const onNodeCreated = nodeType.prototype.onNodeCreated; |
| 121 | + nodeType.prototype.onNodeCreated = function() { |
| 122 | + try { |
| 123 | + const result = onNodeCreated?.apply(this, arguments); |
| 124 | + createSetWidgetCallback().call(this); |
| 125 | + return result; |
| 126 | + } catch (error) { |
| 127 | + console.error("Error in node creation:", error); |
| 128 | + } |
| 129 | + }; |
| 130 | + } |
| 131 | + }, |
| 132 | + |
| 133 | + async nodeCreated(node) { |
| 134 | + if (node?.comfyClass === "BizyAir_TrainDatasetAdd") { |
| 135 | + setupNodeMouseBehavior(node); |
| 136 | + } |
| 137 | + } |
| 138 | +}) |
0 commit comments