From 20016b101ea04b3c32b09bca26a26f022c7a3dc4 Mon Sep 17 00:00:00 2001 From: Kevin Nadro Date: Thu, 23 Jan 2025 09:45:45 -0500 Subject: [PATCH] Fix: Properly setting selection range when KCL editor is not mounted. (#4960) * fix: fixed selection range issue when doing a constraint when the KCL editor is closed * fix: linter and tsc errors * fix: trying to reuse logic instead? * fix: removed console log --- src/components/ModelingMachineProvider.tsx | 11 ++++++++- src/editor/manager.ts | 28 ++++++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/components/ModelingMachineProvider.tsx b/src/components/ModelingMachineProvider.tsx index 09f8480ee..fe1b20a5a 100644 --- a/src/components/ModelingMachineProvider.tsx +++ b/src/components/ModelingMachineProvider.tsx @@ -388,7 +388,16 @@ export const ModelingMachineProvider = ({ } if (setSelections.selectionType === 'completeSelection') { - editorManager.selectRange(setSelections.selection) + const codeMirrorSelection = editorManager.createEditorSelection( + setSelections.selection + ) + kclEditorActor.send({ + type: 'setLastSelectionEvent', + data: { + codeMirrorSelection, + scrollIntoView: false, + }, + }) if (!sketchDetails) return { selectionRanges: setSelections.selection, diff --git a/src/editor/manager.ts b/src/editor/manager.ts index 2e8de8637..87d86259f 100644 --- a/src/editor/manager.ts +++ b/src/editor/manager.ts @@ -315,6 +315,21 @@ export default class EditorManager { if (selections?.graphSelections?.length === 0) { return } + + if (!this._editorView) { + return + } + const codeBaseSelections = this.createEditorSelection(selections) + this._editorView.dispatch({ + selection: codeBaseSelections, + annotations: [ + updateOutsideEditorEvent, + Transaction.addToHistory.of(false), + ], + }) + } + + createEditorSelection(selections: Selections) { let codeBasedSelections = [] for (const selection of selections.graphSelections) { const safeEnd = Math.min( @@ -331,18 +346,7 @@ export default class EditorManager { .range[1] const safeEnd = Math.min(end, this._editorView?.state.doc.length || end) codeBasedSelections.push(EditorSelection.cursor(safeEnd)) - - if (!this._editorView) { - return - } - - this._editorView.dispatch({ - selection: EditorSelection.create(codeBasedSelections, 1), - annotations: [ - updateOutsideEditorEvent, - Transaction.addToHistory.of(false), - ], - }) + return EditorSelection.create(codeBasedSelections, 1) } // We will ONLY get here if the user called a select event.