diff --git a/lib/note-content-editor.tsx b/lib/note-content-editor.tsx index c271e93a7..886e2b6a7 100644 --- a/lib/note-content-editor.tsx +++ b/lib/note-content-editor.tsx @@ -374,6 +374,18 @@ class NoteContentEditor extends Component { this.setDecorators(); } + // If content is changed so that the selected search index is greater then the number of matches + // then select the last match instead + if ( + this.editor && + this.state.editor === 'full' && + (this.props.selectedSearchMatchIndex ?? 0) > this.matchesInNote.length - 1 + ) { + const newIndex = Math.max(this.matchesInNote.length - 1, 0); + this.setSearchSelection(newIndex); + this.props.storeSearchSelection(newIndex); + } + if ( this.editor && this.state.editor === 'full' &&