From b1cbca0d08ad22019304903354e9cbe9ca4486f1 Mon Sep 17 00:00:00 2001 From: RyanOnTheInside <7623207+ryanontheinside@users.noreply.github.com> Date: Sun, 22 Dec 2024 10:01:34 -0500 Subject: [PATCH] js validation cleanup --- web/js/widget_validation.js | 106 +++--------------------------------- 1 file changed, 9 insertions(+), 97 deletions(-) diff --git a/web/js/widget_validation.js b/web/js/widget_validation.js index d66d22a..c686d8c 100644 --- a/web/js/widget_validation.js +++ b/web/js/widget_validation.js @@ -1,16 +1,16 @@ // Widget validation for RealTime nodes import { app } from "../../../scripts/app.js"; +// TODO: Add validation for step values to ensure they match target widget constraints + + // Register validation behavior when nodes are connected app.registerExtension({ name: "RealTime.WidgetValidation", async beforeRegisterNodeDef(nodeType, nodeData, app) { - // Update category check to include sequence nodes if (!nodeData.category?.startsWith("real-time/control/")) { return; } - - console.log("Registering validation for control node:", nodeData.name); // Store constraints from connected widgets nodeType.prototype.targetConstraints = null; @@ -25,20 +25,10 @@ app.registerExtension({ if (valuesWidget) { const originalCallback = valuesWidget.callback; valuesWidget.callback = (value) => { - console.log("Values widget direct callback:", { - before: value, - hasConstraints: !!this.targetConstraints - }); - if (this.targetConstraints) { this.validateSequenceValues(valuesWidget); - value = valuesWidget.value; // Use validated value + value = valuesWidget.value; } - - console.log("After validation:", { - after: value - }); - return originalCallback?.call(valuesWidget, value); }; } @@ -46,7 +36,6 @@ app.registerExtension({ // Function to check connections const checkConnections = () => { const outputLinks = this.outputs[0]?.links || []; - console.log("Checking existing connections:", outputLinks); for (const linkId of outputLinks) { const link = app.graph.links[linkId]; @@ -60,14 +49,12 @@ app.registerExtension({ const targetWidget = targetNode.widgets.find(w => w.name === inputName); if (targetWidget?.options || (targetWidget?.type === "converted-widget" && targetWidget.options)) { - console.log("Found existing connection to widget:", targetWidget); this.targetConstraints = { min: targetWidget.options.min, max: targetWidget.options.max, step: targetWidget.options.step }; - // For sequence nodes, validate immediately if (nodeData.category?.includes("/sequence")) { const valuesWidget = this.widgets.find(w => w.name === "values"); if (valuesWidget) { @@ -81,13 +68,9 @@ app.registerExtension({ } }; - // Check immediately + // Check connections at different intervals to ensure graph is loaded checkConnections(); - - // Also check after a delay to ensure graph is fully loaded setTimeout(checkConnections, 100); - - // And check once more after a longer delay setTimeout(checkConnections, 1000); return result; @@ -95,19 +78,13 @@ app.registerExtension({ const originalOnConnectOutput = nodeType.prototype.onConnectOutput; nodeType.prototype.onConnectOutput = function (slot, type, input, targetNode, targetSlot) { - console.log("Output connection made from control node:", this.title); - - // Call original connection handler if it exists const result = originalOnConnectOutput?.apply(this, arguments); if (targetNode?.widgets) { const inputName = targetNode.inputs[targetSlot]?.name; const targetWidget = targetNode.widgets.find(w => w.name === inputName); - console.log("Found target widget:", targetWidget); - if (targetWidget?.options || (targetWidget?.type === "converted-widget" && targetWidget.options)) { - // Handle both regular and converted widgets const options = targetWidget.options; this.targetConstraints = { min: options.min, @@ -115,16 +92,12 @@ app.registerExtension({ step: options.step }; - console.log("Set constraints:", this.targetConstraints); - - // For sequence nodes, validate the values string if (nodeData.category?.includes("/sequence")) { const valuesWidget = this.widgets.find(w => w.name === "values"); if (valuesWidget) { this.validateSequenceValues(valuesWidget); } } else { - // Existing validation for non-sequence nodes this.updateWidgetConstraints(); } } @@ -150,7 +123,6 @@ app.registerExtension({ } }); - // Clamp current values to new constraints this.clampWidgetValues(); }; @@ -161,7 +133,6 @@ app.registerExtension({ const startWidget = this.widgets.find(w => w.name === "starting_value"); if (minWidget && maxWidget) { - // Ensure min <= start <= max and all within target constraints const targetMin = this.targetConstraints?.min ?? minWidget.options.min; const targetMax = this.targetConstraints?.max ?? maxWidget.options.max; @@ -176,103 +147,44 @@ app.registerExtension({ // Add method to validate sequence values nodeType.prototype.validateSequenceValues = function(widget) { - if (!this.targetConstraints) { - console.log("No target constraints found for validation"); - return; - } - - console.log("Validating sequence values:", { - widget: widget, - value: widget.value, - constraints: this.targetConstraints - }); + if (!this.targetConstraints) return; const separator = widget.options?.multiline ? "\n" : ","; const values = widget.value.split(separator) .map(v => v.trim()) .filter(v => v.length > 0); - // Convert and validate values based on node type const isFloat = this.type === "FloatSequence"; const isInt = this.type === "IntSequence"; - console.log("Processing values:", { - nodeType: this.type, - isFloat: isFloat, - isInt: isInt, - values: values - }); - if (isFloat || isInt) { const validValues = values.map(v => { let num = isFloat ? parseFloat(v) : parseInt(v); if (isNaN(num)) num = this.targetConstraints.min; const clamped = Math.max(this.targetConstraints.min, Math.min(num, this.targetConstraints.max)); - console.log(`Validating value: ${v} -> ${num} -> ${clamped}`); return isFloat ? clamped.toString() : Math.round(clamped).toString(); }); - // Update the widget value with validated values - const newValue = validValues.join(separator); - console.log("Setting new value:", newValue); - widget.value = newValue; + widget.value = validValues.join(separator); } }; // Override the widget's callback to enforce constraints const originalWidgetCallback = nodeType.prototype.onWidgetChanged; - console.log("Setting up widget change handler for:", nodeData.name, { - hasOriginalCallback: !!originalWidgetCallback - }); - nodeType.prototype.onWidgetChanged = function(widget, value) { - console.log("Widget changed - BEFORE validation:", { - nodeName: this.title, - widgetName: widget.name, - originalValue: value, - widgetValue: widget.value, - hasConstraints: !!this.targetConstraints, - nodeType: this.type, - constraints: this.targetConstraints - }); - - // For sequence nodes, validate before calling original callback if (widget.name === "values" && this.targetConstraints) { - console.log("About to validate sequence values"); this.validateSequenceValues(widget); - // Important: Update the value parameter to match the validated widget value - value = widget.value; // Use the validated value + value = widget.value; - // Force widget update to ensure UI reflects validated value widget.value = value; if (widget.callback) { widget.callback(value); } - - console.log("After validation:", { - newValue: value, - widgetValue: widget.value, - originalValue: value - }); } else if (["maximum_value", "minimum_value", "starting_value"].includes(widget.name)) { - console.log("Clamping widget values"); this.clampWidgetValues(); } - console.log("Before calling original callback:", { - finalValue: value, - widgetValue: widget.value - }); - - // Call original callback with validated value - const result = originalWidgetCallback?.apply(this, [widget, value]); - - console.log("After original callback:", { - result: result, - finalWidgetValue: widget.value - }); - - return result; + return originalWidgetCallback?.apply(this, [widget, value]); }; } }); \ No newline at end of file