This commit is contained in:
2026-03-13 00:19:14 +01:00
parent 2ff7e1f5f2
commit 2d8f13aebb
5 changed files with 58 additions and 39 deletions

View File

@@ -4,7 +4,7 @@
* Wherever this editor is used, it provides the same features (syntax highlighting + Nunjucks).
* The parent (node) provides the expected base language for highlighting.
*/
import React, { useCallback, useRef } from 'react'
import React, { useCallback, useLayoutEffect, useRef } from 'react'
import Editor from 'react-simple-code-editor'
import { highlight, type HighlightLanguage } from '@/lib/syntaxHighlight'
@@ -53,6 +53,7 @@ export function CodeEditor({
minHeight = 120,
}: CodeEditorProps) {
const containerRef = useRef<HTMLDivElement | null>(null)
const selectionRestoreRef = useRef<{ start: number; end: number } | null>(null)
const highlightCode = useCallback(
(code: string) => highlight(code, language),
@@ -69,17 +70,27 @@ export function CodeEditor({
(textareaId ? document.getElementById(textareaId) : containerRef.current?.querySelector('textarea')) as HTMLTextAreaElement | null
const start = ta?.selectionStart ?? newValue.length
const end = ta?.selectionEnd ?? newValue.length
selectionRestoreRef.current = { start, end }
onValueChange(newValue)
requestAnimationFrame(() => {
const el = (textareaId
? document.getElementById(textareaId)
: containerRef.current?.querySelector('textarea')) as HTMLTextAreaElement | null
if (el) el.setSelectionRange(start, end)
})
},
[onValueChange, readOnly, textareaId]
)
useLayoutEffect(() => {
const pending = selectionRestoreRef.current
if (pending == null) return
selectionRestoreRef.current = null
const el = (textareaId
? document.getElementById(textareaId)
: containerRef.current?.querySelector('textarea')) as HTMLTextAreaElement | null
if (el) {
const { start, end } = pending
const safeEnd = Math.min(end, el.value.length)
const safeStart = Math.min(start, safeEnd)
el.setSelectionRange(safeStart, safeEnd)
}
}, [value, textareaId])
return (
<div ref={containerRef} className="code-editor" style={{ minHeight: 0 }}>
<Editor

View File

@@ -190,7 +190,16 @@ export function useRenderingNodeState(
const manualRunTriggerSyncedRef = useRef(false)
const pathCtx = useContext(ConnectionPathContext)
const pathCtxRef = useRef(pathCtx)
pathCtxRef.current = pathCtx
const triggerNodeIds = pathCtx?.connectionPathTriggerNodeIds ?? []
const updateDataRef = useRef(updateData)
updateDataRef.current = updateData
const setNodesRef = useRef(setNodes)
setNodesRef.current = setNodes
const aiConnectionRef = useRef(aiConnection)
aiConnectionRef.current = aiConnection
const hasPendingInputs =
effectiveUpdateMode === 'manual' &&
!loading &&
@@ -256,6 +265,7 @@ export function useRenderingNodeState(
let cancelled = false
const run = async () => {
pathCtxRef.current?.addConnectionPathTrigger?.(id)
loadingStartedAtRef.current = Date.now()
setLoading(true)
setError(null)
@@ -263,7 +273,7 @@ export function useRenderingNodeState(
setResolvedContent(null)
setStreamingMarkdown(null)
setReasoningContent('')
updateData({
updateDataRef.current({
cachedRenderedContent: undefined,
cachedResolvedContent: undefined,
cachedReasoningContent: undefined,
@@ -278,8 +288,8 @@ export function useRenderingNodeState(
renderNodeId: id,
viewportWidth,
viewportHeight,
setNodes: setNodes ?? undefined,
aiConnection,
setNodes: setNodesRef.current ?? undefined,
aiConnection: aiConnectionRef.current,
...(isAgentSource && {
onStreamingStart: () => setStreamingMarkdown(''),
onStreamingChunk: (chunk: string) =>
@@ -297,7 +307,7 @@ export function useRenderingNodeState(
if (thisRunId !== runIdRef.current) return
setRenderedContent(htmlOrSvg)
setError(null)
updateData({
updateDataRef.current({
cachedRenderedContent: htmlOrSvg,
cachedResolvedContent: resolved,
cachedReasoningContent: reasoning ?? '',
@@ -351,26 +361,18 @@ export function useRenderingNodeState(
minLoadingTimeoutRef.current = null
}
}
// Content updates only when connected-node data changes (sourceSignature) or explicit run/viewport.
// React Flow updates (position, selection, context ref churn) do not trigger re-runs.
}, [
id,
srcId,
srcNode?.type,
effectiveUpdateMode,
runTrigger,
sourceContent,
sourceSignature,
configSignature,
edgesSignature,
variablesSignature,
functionsSignature,
dataSignature,
viewportWidth,
viewportHeight,
retryCount,
updateData,
setNodes,
aiConnection,
isAgentSource,
incomingIds.length,
])