fix: dismiss stepper on script edit and improve function brace tracking
Stepper now auto-dismisses when the user edits the script body during detection, preventing stale matchedLine replacements. Function-level param() block detection uses brace depth counting instead of simple equality check, correctly handling nested braces in function bodies. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -67,6 +67,16 @@ export function ScriptTemplateEditor({ templateId, onBack, onSaved }: Props) {
|
|||||||
|
|
||||||
const { canShareScriptTemplate } = usePermissions()
|
const { canShareScriptTemplate } = usePermissions()
|
||||||
|
|
||||||
|
// Dismiss stepper if user edits the script body during detection
|
||||||
|
const scriptBodyRef = form.script_body
|
||||||
|
useEffect(() => {
|
||||||
|
if (showStepper) {
|
||||||
|
setShowStepper(false)
|
||||||
|
setDetectedCandidates([])
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [scriptBodyRef])
|
||||||
|
|
||||||
// Load categories + template detail (if editing)
|
// Load categories + template detail (if editing)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
|
|||||||
@@ -127,19 +127,33 @@ function parseDefault(value: string | null, type: ScriptParameter['type']): stri
|
|||||||
*/
|
*/
|
||||||
function findScriptLevelParamBlock(script: string): { start: number; end: number } | null {
|
function findScriptLevelParamBlock(script: string): { start: number; end: number } | null {
|
||||||
const lines = script.split('\n')
|
const lines = script.split('\n')
|
||||||
let inFunction = false
|
let functionBraceDepth = 0
|
||||||
let paramStart = -1
|
let paramStart = -1
|
||||||
let parenDepth = 0
|
let parenDepth = 0
|
||||||
|
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
const trimmed = lines[i].trim()
|
const trimmed = lines[i].trim()
|
||||||
|
|
||||||
|
// Track function blocks with brace depth to handle nested braces
|
||||||
if (/^function\s+/i.test(trimmed)) {
|
if (/^function\s+/i.test(trimmed)) {
|
||||||
inFunction = true
|
// Count braces on this line and subsequent lines
|
||||||
|
for (const ch of lines[i]) {
|
||||||
|
if (ch === '{') functionBraceDepth++
|
||||||
|
if (ch === '}') functionBraceDepth--
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!inFunction && /^param\s*\(/i.test(trimmed) && paramStart === -1) {
|
// Track braces while inside a function
|
||||||
|
if (functionBraceDepth > 0) {
|
||||||
|
for (const ch of lines[i]) {
|
||||||
|
if (ch === '{') functionBraceDepth++
|
||||||
|
if (ch === '}') functionBraceDepth--
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (functionBraceDepth === 0 && /^param\s*\(/i.test(trimmed) && paramStart === -1) {
|
||||||
paramStart = i
|
paramStart = i
|
||||||
for (let j = i; j < lines.length; j++) {
|
for (let j = i; j < lines.length; j++) {
|
||||||
for (const ch of lines[j]) {
|
for (const ch of lines[j]) {
|
||||||
@@ -151,10 +165,6 @@ function findScriptLevelParamBlock(script: string): { start: number; end: number
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inFunction && trimmed === '}') {
|
|
||||||
inFunction = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
|||||||
Reference in New Issue
Block a user