Skip to content

Build your personal knowledge base with Trilium Notes

chore(code/find): reimplement replace all

Elian Doran3ace3d9ed914
diff --git a/apps/client/src/widgets/find_in_code.ts b/apps/client/src/widgets/find_in_code.ts
index df413fb301..a66683eeeb 100644
--- a/apps/client/src/widgets/find_in_code.ts
+++ b/apps/client/src/widgets/find_in_code.ts
@@ -61,23 +61,7 @@ export default class FindInCode {
}
async replaceAll(replaceText: string) {
- if (!this.findResult || this.findResult.length === 0) {
- return;
- }
const codeEditor = await this.getCodeEditor();
- const doc = codeEditor?.doc;
- codeEditor?.operation(() => {
- if (!this.findResult) {
- return;
- }
-
- for (let currentFound = 0; currentFound < this.findResult.length; currentFound++) {
- let marker = this.findResult[currentFound];
- let pos = marker.find();
- doc?.replaceRange(replaceText, pos.from, pos.to);
- marker.clear();
- }
- });
- this.findResult = [];
+ codeEditor?.replaceAll(replaceText);
}
}
diff --git a/packages/codemirror/src/find_replace.ts b/packages/codemirror/src/find_replace.ts
index 139691e467..493b9e0088 100644
--- a/packages/codemirror/src/find_replace.ts
+++ b/packages/codemirror/src/find_replace.ts
@@ -57,6 +57,18 @@ export class SearchHighlighter {
});
}
+ replaceAll(replacementText: string) {
+ if (!this.parsedMatches.length) return;
+
+ this.view.dispatch({
+ changes: this.parsedMatches.map(change => ({
+ from: change.from,
+ to: change.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.ts
index a9310a2209..af1c5ce7e5 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -198,6 +198,10 @@ export default class CodeMirror extends EditorView {
this.searchPlugin?.replaceActiveMatch(replaceText);
}
+ async replaceAll(replaceText: string) {
+ this.searchPlugin?.replaceAll(replaceText);
+ }
+
cleanSearch() {
if (this.searchPlugin) {
this.dispatch({