@@ -5,6 +5,7 @@ import { CodeHighlight } from "@mantine/code-highlight";
55import type { NodeData } from "../../../types/graph" ;
66import useGraph from "../../editor/views/GraphView/stores/useGraph" ;
77import useJson from "../../../store/useJson" ;
8+ import useFile from "../../../store/useFile" ;
89
910// return object from json removing array and object fields
1011const normalizeNodeData = ( nodeRows : NodeData [ "text" ] ) => {
@@ -45,7 +46,27 @@ export const NodeModal = ({ opened, onClose }: ModalProps) => {
4546
4647 const enterEditMode = ( ) => {
4748 setError ( null ) ;
48- setEditedContent ( normalizeNodeData ( nodeData ?. text ?? [ ] ) ) ;
49+ try {
50+ const raw = getJson ( ) ;
51+ const parsed = JSON . parse ( raw ) ;
52+
53+ const getValueAtPath = ( obj : any , path : NodeData [ "path" ] | undefined ) => {
54+ if ( ! path || path . length === 0 ) return obj ;
55+ let curr = obj ;
56+ for ( let i = 0 ; i < path . length ; i ++ ) {
57+ const seg = path [ i ] as string | number ;
58+ if ( curr === undefined || curr === null ) return undefined ;
59+ curr = curr [ seg ] ;
60+ }
61+ return curr ;
62+ } ;
63+
64+ const value = getValueAtPath ( parsed , nodeData ?. path ) ;
65+ setEditedContent ( JSON . stringify ( value , null , 2 ) ) ;
66+ } catch ( err ) {
67+ // fallback to previous behavior
68+ setEditedContent ( normalizeNodeData ( nodeData ?. text ?? [ ] ) ) ;
69+ }
4970 setIsEditing ( true ) ;
5071 } ;
5172
@@ -76,16 +97,18 @@ export const NodeModal = ({ opened, onClose }: ModalProps) => {
7697 try {
7798 const newValue = JSON . parse ( editedContent ) ;
7899
79- // get current app JSON
80- const raw = getJson ( ) ;
81- const parsed = JSON . parse ( raw ) ;
100+ // get current app JSON
101+ const raw = getJson ( ) ;
102+ const parsed = JSON . parse ( raw ) ;
82103
83- const updated = setValueAtPath ( parsed , nodeData ?. path , newValue ) ;
104+ const updated = setValueAtPath ( parsed , nodeData ?. path , newValue ) ;
84105
85- setJson ( JSON . stringify ( updated , null , 2 ) ) ;
106+ // Update the editor/file contents so Export and other file-level state reflect the change
107+ useFile . getState ( ) . setContents ( { contents : JSON . stringify ( updated , null , 2 ) , hasChanges : true } ) ;
86108
87- setIsEditing ( false ) ;
88- onClose ?.( ) ;
109+ // debouncedUpdateJson inside useFile will update useJson / graph; close modal
110+ setIsEditing ( false ) ;
111+ onClose ?.( ) ;
89112 } catch ( err : any ) {
90113 setError ( err ?. message ?? String ( err ) ) ;
91114 }
0 commit comments