add replacement feature for code note

This commit is contained in:
SiriusXT 2024-11-11 22:57:24 +08:00
parent 2d9376a05c
commit 8893e9d4d5
2 changed files with 45 additions and 1 deletions

View File

@ -164,7 +164,7 @@ export default class FindWidget extends NoteContextAwareWidget {
this.$widget.show(); this.$widget.show();
this.$input.focus(); this.$input.focus();
const isReadOnly = await this.noteContext.isReadOnly(); const isReadOnly = await this.noteContext.isReadOnly();
if (this.note.type === 'text' && !isReadOnly) { if (['text', 'code'].includes(this.note.type) && !isReadOnly) {
this.$replaceWidgetBox.show(); this.$replaceWidgetBox.show();
}else{ }else{
this.$replaceWidgetBox.hide(); this.$replaceWidgetBox.hide();

View File

@ -170,4 +170,48 @@ export default class FindInCode {
codeEditor.focus(); 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 = [];
}
} }