* Updates the visual highlighting of results based on the current line selection in the editor. * * @function updateSelection * @requires editor, processedExpressions * * @description * 1. Determines the current line number in the editor's selection. * 2. Finds the corresponding result (processed ex
()
| 163 | * - Scrolls the newly selected result into view. |
| 164 | */ |
| 165 | function updateSelection() { |
| 166 | const selectedLine = editor.state.doc.lineAt( |
| 167 | editor.state.selection.ranges[editor.state.selection.mainIndex].from |
| 168 | ).number - 1; |
| 169 | |
| 170 | let selectedExpressionIndex; |
| 171 | |
| 172 | processedExpressions.forEach((result, index) => { |
| 173 | if ((selectedLine >= result.from) && (selectedLine <= result.to)) { |
| 174 | selectedExpressionIndex = index; |
| 175 | } |
| 176 | }); |
| 177 | |
| 178 | if (selectedExpressionIndex !== previousSelectedExpressionIndex) { |
| 179 | const previouslyHighlightedResult = document.querySelector('#result').children[previousSelectedExpressionIndex]; |
| 180 | if (previouslyHighlightedResult !== undefined) { |
| 181 | previouslyHighlightedResult.className = null; |
| 182 | } |
| 183 | |
| 184 | const currentlySelectedResult = document.querySelector('#result').children[selectedExpressionIndex]; |
| 185 | if (currentlySelectedResult !== undefined) { |
| 186 | currentlySelectedResult.className = 'highlighted'; |
| 187 | currentlySelectedResult.scrollIntoView({ block: 'nearest', inline: 'start' }); |
| 188 | } |
| 189 | |
| 190 | previousSelectedExpressionIndex = selectedExpressionIndex; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Converts an array of processed results into HTML elements for display. |