mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-29 00:11:32 +08:00
add replacement feature for code note
This commit is contained in:
parent
2d9376a05c
commit
8893e9d4d5
@ -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();
|
||||
|
@ -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 = [];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user