Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(code/find): reimplement single replace
Elian Doranb64647501831
diff --git a/apps/client/src/widgets/find_in_code.ts b/apps/client/src/widgets/find_in_code.tsindex 46131bd4ee..df413fb301 100644--- a/apps/client/src/widgets/find_in_code.ts+++ b/apps/client/src/widgets/find_in_code.ts@@ -54,43 +54,12 @@ export default class FindInCode {codeEditor?.cleanSearch();codeEditor?.focus();}- async replace(replaceText: string) {- // this.findResult may be undefined and null- if (!this.findResult || this.findResult.length === 0) {- return;- }- let currentFound = -1;- this.findResult.forEach((marker, index) => {- const pos = marker.find();- if (pos) {- if (marker.className === FIND_RESULT_SELECTED_CSS_CLASSNAME) {- currentFound = index;- return;- }- }- });- if (currentFound >= 0) {- let marker = this.findResult[currentFound];- let pos = marker.find();- const codeEditor = await this.getCodeEditor();- const doc = codeEditor?.doc;- if (doc) {- doc.replaceRange(replaceText, pos.from, pos.to);- }- marker.clear();- let nextFound;- if (currentFound === this.findResult.length - 1) {- nextFound = 0;- } else {- nextFound = currentFound;- }- this.findResult.splice(currentFound, 1);- if (this.findResult.length > 0) {- this.findNext(0, nextFound, nextFound);- }- }+ async replace(replaceText: string) {+ const codeEditor = await this.getCodeEditor();+ codeEditor?.replace(replaceText);}+async replaceAll(replaceText: string) {if (!this.findResult || this.findResult.length === 0) {return;diff --git a/packages/codemirror/src/find_replace.ts b/packages/codemirror/src/find_replace.tsindex 687e55218d..139691e467 100644--- a/packages/codemirror/src/find_replace.ts+++ b/packages/codemirror/src/find_replace.ts@@ -46,6 +46,17 @@ export class SearchHighlighter {this.#scrollToMatchNearestSelection();}+ replaceActiveMatch(replacementText: string) {+ if (!this.parsedMatches.length || this.currentFound === 0) return;++ const matchIndex = this.currentFound - 1;+ const match = this.parsedMatches[matchIndex];++ this.view.dispatch({+ changes: { from: match.from, to: match.to, insert: replacementText }+ });+ }+scrollToMatch(matchIndex: number) {if (this.parsedMatches.length <= matchIndex) {return;diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.tsindex f41472d257..a9310a2209 100644--- a/packages/codemirror/src/index.ts+++ b/packages/codemirror/src/index.ts@@ -194,6 +194,10 @@ export default class CodeMirror extends EditorView {this.searchPlugin?.scrollToMatch(nextFound);}+ async replace(replaceText: string) {+ this.searchPlugin?.replaceActiveMatch(replaceText);+ }+cleanSearch() {if (this.searchPlugin) {this.dispatch({