diff --git a/Data/Server/WebUI/src/nodes/Data Manipulation/Node_Regex_Replace.jsx b/Data/Server/WebUI/src/nodes/Data Manipulation/Node_Regex_Replace.jsx new file mode 100644 index 0000000..2bbbe97 --- /dev/null +++ b/Data/Server/WebUI/src/nodes/Data Manipulation/Node_Regex_Replace.jsx @@ -0,0 +1,173 @@ +////////// PROJECT FILE SEPARATION LINE ////////// CODE AFTER THIS LINE ARE FROM: /Data/WebUI/src/nodes/Data Manipulation/Node_Regex_Replace.jsx +import React, { useEffect, useRef, useState } from "react"; +import { Handle, Position, useReactFlow, useStore } from "reactflow"; + +if (!window.BorealisValueBus) window.BorealisValueBus = {}; +if (!window.BorealisUpdateRate) window.BorealisUpdateRate = 100; + +const RegexReplaceNode = ({ id, data }) => { + const edges = useStore((state) => state.edges); + const { setNodes } = useReactFlow(); + + const [pattern, setPattern] = useState(data?.pattern || ""); + const [replacement, setReplacement] = useState(data?.replacement || ""); + const [result, setResult] = useState(""); + + const valueRef = useRef(""); + + const handlePatternChange = (e) => { + const val = e.target.value; + setPattern(val); + setNodes((nds) => + nds.map((n) => + n.id === id ? { ...n, data: { ...n.data, pattern: val } } : n + ) + ); + }; + + const handleReplacementChange = (e) => { + const val = e.target.value; + setReplacement(val); + setNodes((nds) => + nds.map((n) => + n.id === id ? { ...n, data: { ...n.data, replacement: val } } : n + ) + ); + }; + + useEffect(() => { + let intervalId = null; + let currentRate = window.BorealisUpdateRate; + + const runNodeLogic = () => { + const inputEdge = edges.find((e) => e.target === id); + const inputValue = inputEdge + ? window.BorealisValueBus[inputEdge.source] || "" + : ""; + let newVal = inputValue; + + try { + const regex = new RegExp(pattern, 'g'); + newVal = inputValue.replace(regex, replacement); + } catch (err) { + newVal = `[Error] ${err.message}`; + } + + if (newVal !== valueRef.current) { + valueRef.current = newVal; + setResult(newVal); + window.BorealisValueBus[id] = newVal; + } + }; + + const startInterval = () => { + intervalId = setInterval(runNodeLogic, currentRate); + }; + + startInterval(); + + const monitor = setInterval(() => { + const newRate = window.BorealisUpdateRate; + if (newRate !== currentRate) { + clearInterval(intervalId); + currentRate = newRate; + startInterval(); + } + }, 300); + + return () => { + clearInterval(intervalId); + clearInterval(monitor); + }; + }, [id, edges, pattern, replacement]); + + return ( +
+ + +
Regex Replace
+ +
+
+ Perform regex replacement on upstream string +
+ + + + + + + + +