From 8893e9d4d5ee442551f32ce0d1f75fff28d39d8b Mon Sep 17 00:00:00 2001 From: SiriusXT <1160925501@qq.com> Date: Mon, 11 Nov 2024 22:57:24 +0800 Subject: [PATCH] add replacement feature for code note --- src/public/app/widgets/find.js | 2 +- src/public/app/widgets/find_in_code.js | 44 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/public/app/widgets/find.js b/src/public/app/widgets/find.js index 93527ff85..5e0258998 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 40308bae6..66052ee02 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 = []; + } }