Skip to content

Commit 61d6b2d

Browse files
committed
fix: defer AI context chip updates when sidebar tab is inactive
Reading editor selection data (getSelection/getSelectedText) from both activeEditorChange and cursorActivity handlers during inline editor operations interferes with the inline editor's cursor position tracking. Guard _updateSelectionChip and _updateLivePreviewChip with an AI tab visibility check, and refresh when the tab becomes active. Also pass the editor instance directly from event handlers to avoid redundant getActiveEditor() calls.
1 parent 8a84144 commit 61d6b2d

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

src/core-ai/AIChatPanel.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,21 @@ define(function (require, exports, module) {
227227
}
228228
if (newEditor) {
229229
newEditor.off("cursorActivity.aiContext");
230-
newEditor.on("cursorActivity.aiContext", _updateSelectionChip);
230+
newEditor.on("cursorActivity.aiContext", function (_evt, editor) {
231+
_updateSelectionChip(editor);
232+
});
231233
}
232-
_updateSelectionChip();
234+
_updateSelectionChip(newEditor);
233235
});
234236
// Bind to current editor if already active
235237
const currentEditor = EditorManager.getActiveEditor();
236238
if (currentEditor) {
237239
currentEditor.off("cursorActivity.aiContext");
238-
currentEditor.on("cursorActivity.aiContext", _updateSelectionChip);
240+
currentEditor.on("cursorActivity.aiContext", function (_evt, editor) {
241+
_updateSelectionChip(editor);
242+
});
239243
}
240-
_updateSelectionChip();
244+
_updateSelectionChip(currentEditor);
241245

242246
// Track live preview status — listen to both LiveDev status changes
243247
// and panel show/hide events so the chip updates when the panel is closed
@@ -254,6 +258,16 @@ define(function (require, exports, module) {
254258
WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_HIDDEN + ".aiChat", _updateLivePreviewChip);
255259
_updateLivePreviewChip();
256260

261+
// Refresh context bar when the AI tab becomes active (DOM updates
262+
// are deferred while the tab is hidden to avoid layout interference)
263+
SidebarTabs.off("tabChanged.aiChat");
264+
SidebarTabs.on("tabChanged.aiChat", function (_event, tabId) {
265+
if (tabId === "ai") {
266+
_updateSelectionChip();
267+
_updateLivePreviewChip();
268+
}
269+
});
270+
257271
// When a screenshot is captured, attach the image to the awaiting tool indicator
258272
PhoenixConnectors.off("screenshotCaptured.aiChat");
259273
PhoenixConnectors.on("screenshotCaptured.aiChat", function (_event, base64) {
@@ -301,9 +315,17 @@ define(function (require, exports, module) {
301315

302316
/**
303317
* Update the selection/cursor chip based on the active editor state.
318+
* Skipped when the AI tab isn't active — calling getSelection()/getSelectedText()
319+
* from both activeEditorChange and cursorActivity during inline editor operations
320+
* interferes with the inline editor's cursor position tracking.
304321
*/
305-
function _updateSelectionChip() {
306-
const editor = EditorManager.getActiveEditor();
322+
function _updateSelectionChip(editor) {
323+
if (SidebarTabs.getActiveTab() !== "ai") {
324+
return;
325+
}
326+
if (!editor) {
327+
editor = EditorManager.getActiveEditor();
328+
}
307329
if (!editor) {
308330
_lastSelectionInfo = null;
309331
_lastCursorLine = null;
@@ -360,6 +382,9 @@ define(function (require, exports, module) {
360382
* Update the live preview chip based on panel visibility.
361383
*/
362384
function _updateLivePreviewChip() {
385+
if (SidebarTabs.getActiveTab() !== "ai") {
386+
return;
387+
}
363388
const panel = WorkspaceManager.getPanelForID("live-preview-panel");
364389
const wasActive = _livePreviewActive;
365390
_livePreviewActive = !!(panel && panel.isVisible());

0 commit comments

Comments
 (0)