Skip to content

Build your personal knowledge base with Trilium Notes

add replacement feature for code note

SiriusXT8893e9d4d5ee
diff --git a/src/public/app/widgets/find.js b/src/public/app/widgets/find.js
index 93527ff851..5e0258998c 100644
--- a/src/public/app/widgets/find.js
+++ b/src/public/app/widgets/find.js
@@ -164,7 +164,7 @@ export default class FindWidget extends NoteContextAwareWidget {
this.$widget.show();
this.$input.focus();
const isReadOnly = await this.noteContext.isReadOnly();
- if (this.note.type === 'text' && !isReadOnly) {
+ if (['text', 'code'].includes(this.note.type) && !isReadOnly) {
this.$replaceWidgetBox.show();
}else{
this.$replaceWidgetBox.hide();
diff --git a/src/public/app/widgets/find_in_code.js b/src/public/app/widgets/find_in_code.js
index 40308bae6b..66052ee026 100644
--- a/src/public/app/widgets/find_in_code.js
+++ b/src/public/app/widgets/find_in_code.js
@@ -170,4 +170,48 @@ export default class FindInCode {
codeEditor.focus();
}
+ async replace(replaceText) {
+ 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;
+ 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 replaceAll(replaceText) {
+ const codeEditor = await this.getCodeEditor();
+ const doc = codeEditor.doc;
+ codeEditor.operation(() => {
+ 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 = [];
+ }
}