1- using System ;
1+ using System ;
2+ using System . Windows ;
23using Dynamo . Controls ;
34using Dynamo . Core ;
45using Dynamo . Graph . Nodes ;
@@ -19,6 +20,11 @@ internal enum State
1920
2021 private State state ;
2122
23+ /// <summary>
24+ /// Current editor state.
25+ /// </summary>
26+ internal State CurrentState => state ;
27+
2228 /// <summary>
2329 /// Event handler for editor state changed.
2430 /// </summary>
@@ -84,6 +90,22 @@ public CodeBlockEditor(NodeView nodeView): base(nodeView)
8490
8591 WatermarkLabel . Text = Properties . Resources . WatermarkLabelText ;
8692 stateMachine . OnStateChanged += OnEditorStateChanged ;
93+ nodeView . Unloaded += OnNodeViewUnloaded ;
94+ }
95+
96+ /// <summary>
97+ /// Ensures graph shortcuts are restored if this editor is unloaded while still
98+ /// in edit mode (e.g. node removal or workspace close). LostFocus may not commit
99+ /// when <see cref="CodeCompletionEditor.IsDisposed"/> is already true.
100+ /// Only clears the shared flag when this editor was the active one, so unloading
101+ /// another Code Block does not re-enable shortcuts mid-edit.
102+ /// </summary>
103+ private void OnNodeViewUnloaded ( object sender , System . Windows . RoutedEventArgs e )
104+ {
105+ if ( stateMachine . CurrentState == EditorStateMachine . State . Editing )
106+ {
107+ nodeViewModel . DynamoViewModel . IsCodeBlockEditorActive = false ;
108+ }
87109 }
88110
89111 protected override void OnEscape ( )
@@ -116,6 +138,21 @@ protected override void OnCommitChange()
116138 protected override void OnTextAreaGotFocus ( object sender , System . Windows . RoutedEventArgs e )
117139 {
118140 stateMachine . Transit ( EditorStateMachine . State . Editing ) ;
141+ // Ensure the flag is set even if the state machine was already Editing
142+ // (e.g. keyboard focus returned after a modal dialog closed).
143+ nodeViewModel . DynamoViewModel . IsCodeBlockEditorActive = true ;
144+ }
145+
146+ /// <summary>
147+ /// Keeps the shared edit flag in sync with keyboard focus. Clears it when focus
148+ /// leaves (e.g. dialog or AA assistant chat) and sets it again when focus returns,
149+ /// including cases where TextArea GotFocus does not fire reliably.
150+ /// </summary>
151+ protected override void OnIsKeyboardFocusWithinChanged ( DependencyPropertyChangedEventArgs e )
152+ {
153+ base . OnIsKeyboardFocusWithinChanged ( e ) ;
154+
155+ nodeViewModel . DynamoViewModel . IsCodeBlockEditorActive = IsKeyboardFocusWithin ;
119156 }
120157
121158 private void OnEditorStateChanged ( EditorStateMachine . State state )
0 commit comments