mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-07-27 08:28:28 -06:00
Fixed Issues & Enhanced RegEx Replacer Node
This commit is contained in:
@ -11,26 +11,17 @@ const RegexReplaceNode = ({ id, data }) => {
|
|||||||
|
|
||||||
const [pattern, setPattern] = useState(data?.pattern || "");
|
const [pattern, setPattern] = useState(data?.pattern || "");
|
||||||
const [replacement, setReplacement] = useState(data?.replacement || "");
|
const [replacement, setReplacement] = useState(data?.replacement || "");
|
||||||
|
const [flags, setFlags] = useState(data?.flags || "g");
|
||||||
|
const [enabled, setEnabled] = useState(data?.enabled ?? true);
|
||||||
const [result, setResult] = useState("");
|
const [result, setResult] = useState("");
|
||||||
|
const [original, setOriginal] = useState("");
|
||||||
|
|
||||||
const valueRef = useRef("");
|
const valueRef = useRef("");
|
||||||
|
|
||||||
const handlePatternChange = (e) => {
|
const updateNodeData = (key, val) => {
|
||||||
const val = e.target.value;
|
setNodes(nds =>
|
||||||
setPattern(val);
|
nds.map(n =>
|
||||||
setNodes((nds) =>
|
n.id === id ? { ...n, data: { ...n.data, [key]: val } } : n
|
||||||
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
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -44,11 +35,23 @@ const RegexReplaceNode = ({ id, data }) => {
|
|||||||
const inputValue = inputEdge
|
const inputValue = inputEdge
|
||||||
? window.BorealisValueBus[inputEdge.source] || ""
|
? window.BorealisValueBus[inputEdge.source] || ""
|
||||||
: "";
|
: "";
|
||||||
|
|
||||||
|
setOriginal(inputValue);
|
||||||
|
|
||||||
let newVal = inputValue;
|
let newVal = inputValue;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const regex = new RegExp(pattern, 'g');
|
if (enabled && pattern) {
|
||||||
newVal = inputValue.replace(regex, replacement);
|
const regex = new RegExp(pattern, flags);
|
||||||
|
let safeReplacement = replacement.trim();
|
||||||
|
if (
|
||||||
|
safeReplacement.startsWith('"') &&
|
||||||
|
safeReplacement.endsWith('"')
|
||||||
|
) {
|
||||||
|
safeReplacement = safeReplacement.slice(1, -1);
|
||||||
|
}
|
||||||
|
newVal = inputValue.replace(regex, safeReplacement);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
newVal = `[Error] ${err.message}`;
|
newVal = `[Error] ${err.message}`;
|
||||||
}
|
}
|
||||||
@ -60,18 +63,14 @@ const RegexReplaceNode = ({ id, data }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const startInterval = () => {
|
|
||||||
intervalId = setInterval(runNodeLogic, currentRate);
|
intervalId = setInterval(runNodeLogic, currentRate);
|
||||||
};
|
|
||||||
|
|
||||||
startInterval();
|
|
||||||
|
|
||||||
const monitor = setInterval(() => {
|
const monitor = setInterval(() => {
|
||||||
const newRate = window.BorealisUpdateRate;
|
const newRate = window.BorealisUpdateRate;
|
||||||
if (newRate !== currentRate) {
|
if (newRate !== currentRate) {
|
||||||
clearInterval(intervalId);
|
clearInterval(intervalId);
|
||||||
|
intervalId = setInterval(runNodeLogic, newRate);
|
||||||
currentRate = newRate;
|
currentRate = newRate;
|
||||||
startInterval();
|
|
||||||
}
|
}
|
||||||
}, 300);
|
}, 300);
|
||||||
|
|
||||||
@ -79,7 +78,7 @@ const RegexReplaceNode = ({ id, data }) => {
|
|||||||
clearInterval(intervalId);
|
clearInterval(intervalId);
|
||||||
clearInterval(monitor);
|
clearInterval(monitor);
|
||||||
};
|
};
|
||||||
}, [id, edges, pattern, replacement]);
|
}, [id, edges, pattern, replacement, flags, enabled]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="borealis-node">
|
<div className="borealis-node">
|
||||||
@ -92,64 +91,71 @@ const RegexReplaceNode = ({ id, data }) => {
|
|||||||
Perform regex replacement on upstream string
|
Perform regex replacement on upstream string
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<label style={{ display: "block", marginBottom: "2px" }}>
|
<label>Regex Pattern:</label>
|
||||||
Regular Expression:
|
|
||||||
</label>
|
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={pattern}
|
value={pattern}
|
||||||
onChange={handlePatternChange}
|
onChange={(e) => {
|
||||||
placeholder="e.g. \\d+"
|
setPattern(e.target.value);
|
||||||
style={{
|
updateNodeData("pattern", e.target.value);
|
||||||
width: "100%",
|
|
||||||
fontSize: "9px",
|
|
||||||
background: "#1e1e1e",
|
|
||||||
color: "#ccc",
|
|
||||||
border: "1px solid #444",
|
|
||||||
borderRadius: "2px",
|
|
||||||
padding: "3px",
|
|
||||||
marginBottom: "6px"
|
|
||||||
}}
|
}}
|
||||||
|
placeholder="e.g. \\d+"
|
||||||
|
style={inputStyle}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<label style={{ display: "block", marginBottom: "2px" }}>
|
<label>Replacement:</label>
|
||||||
Replacement:
|
|
||||||
</label>
|
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={replacement}
|
value={replacement}
|
||||||
onChange={handleReplacementChange}
|
onChange={(e) => {
|
||||||
placeholder="e.g. '#'
|
setReplacement(e.target.value);
|
||||||
"
|
updateNodeData("replacement", e.target.value);
|
||||||
style={{
|
|
||||||
width: "100%",
|
|
||||||
fontSize: "9px",
|
|
||||||
background: "#1e1e1e",
|
|
||||||
color: "#ccc",
|
|
||||||
border: "1px solid #444",
|
|
||||||
borderRadius: "2px",
|
|
||||||
padding: "3px",
|
|
||||||
marginBottom: "6px"
|
|
||||||
}}
|
}}
|
||||||
|
placeholder="e.g. $1"
|
||||||
|
style={inputStyle}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<label style={{ display: "block", marginBottom: "2px" }}>
|
<label>Regex Flags:</label>
|
||||||
Output:
|
<input
|
||||||
|
type="text"
|
||||||
|
value={flags}
|
||||||
|
onChange={(e) => {
|
||||||
|
setFlags(e.target.value);
|
||||||
|
updateNodeData("flags", e.target.value);
|
||||||
|
}}
|
||||||
|
placeholder="e.g. gi"
|
||||||
|
style={inputStyle}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div style={{ margin: "6px 0" }}>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={enabled}
|
||||||
|
onChange={(e) => {
|
||||||
|
setEnabled(e.target.checked);
|
||||||
|
updateNodeData("enabled", e.target.checked);
|
||||||
|
}}
|
||||||
|
style={{ marginRight: "6px" }}
|
||||||
|
/>
|
||||||
|
Enable Replacement
|
||||||
</label>
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label>Original Input:</label>
|
||||||
|
<textarea
|
||||||
|
readOnly
|
||||||
|
value={original}
|
||||||
|
rows={2}
|
||||||
|
style={textAreaStyle}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label>Output:</label>
|
||||||
<textarea
|
<textarea
|
||||||
readOnly
|
readOnly
|
||||||
value={result}
|
value={result}
|
||||||
rows={3}
|
rows={2}
|
||||||
style={{
|
style={textAreaStyle}
|
||||||
width: "100%",
|
|
||||||
fontSize: "9px",
|
|
||||||
background: "#2a2a2a",
|
|
||||||
color: "#ccc",
|
|
||||||
border: "1px solid #444",
|
|
||||||
borderRadius: "2px",
|
|
||||||
padding: "3px",
|
|
||||||
resize: "vertical"
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -158,15 +164,37 @@ const RegexReplaceNode = ({ id, data }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const inputStyle = {
|
||||||
|
width: "100%",
|
||||||
|
fontSize: "9px",
|
||||||
|
background: "#1e1e1e",
|
||||||
|
color: "#ccc",
|
||||||
|
border: "1px solid #444",
|
||||||
|
borderRadius: "2px",
|
||||||
|
padding: "3px",
|
||||||
|
marginBottom: "6px"
|
||||||
|
};
|
||||||
|
|
||||||
|
const textAreaStyle = {
|
||||||
|
width: "100%",
|
||||||
|
fontSize: "9px",
|
||||||
|
background: "#2a2a2a",
|
||||||
|
color: "#ccc",
|
||||||
|
border: "1px solid #444",
|
||||||
|
borderRadius: "2px",
|
||||||
|
padding: "3px",
|
||||||
|
resize: "vertical",
|
||||||
|
marginBottom: "6px"
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
type: "RegexReplace",
|
type: "RegexReplace",
|
||||||
label: "Regex Replacer",
|
label: "Regex Replacer",
|
||||||
description: `
|
description: `
|
||||||
Perform a user-specified regular expression replacement on an input string.
|
Enhanced Regex Replacer:
|
||||||
|
- Add regex flags (g, i, m, etc)
|
||||||
- User enters a regex pattern
|
- Live preview of input vs output
|
||||||
- User enters a replacement string
|
- Optional enable toggle for replacement logic
|
||||||
- Outputs the transformed string to downstream nodes
|
|
||||||
`.trim(),
|
`.trim(),
|
||||||
content: "Perform regex replacement on upstream string",
|
content: "Perform regex replacement on upstream string",
|
||||||
component: RegexReplaceNode
|
component: RegexReplaceNode
|
||||||
|
Reference in New Issue
Block a user