Rework Find & Replace dialog:
- Add option to affect whole collection - Allow to open without selection - Add parameter for presetting field
This commit is contained in:
parent
b148b7b5e0
commit
a5193339e7
@ -92,6 +92,7 @@ browsing-reschedule = Reschedule
|
||||
browsing-search-bar-hint = Search cards/notes (type text, then press Enter)
|
||||
browsing-search-in = Search in:
|
||||
browsing-search-within-formatting-slow = Search within formatting (slow)
|
||||
browsing-selected-notes-only = Selected notes only
|
||||
browsing-shift-position-of-existing-cards = Shift position of existing cards
|
||||
browsing-sidebar = Sidebar
|
||||
browsing-sidebar-filter = Sidebar filter
|
||||
|
@ -812,7 +812,6 @@ class Browser(QMainWindow):
|
||||
######################################################################
|
||||
|
||||
@no_arg_trigger
|
||||
@skip_if_selection_is_empty
|
||||
@ensure_editor_saved
|
||||
def onFindReplace(self) -> None:
|
||||
FindAndReplaceDialog(self, mw=self.mw, note_ids=self.selected_notes())
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Sequence
|
||||
from typing import List, Optional, Sequence
|
||||
|
||||
import aqt
|
||||
from anki.notes import NoteId
|
||||
@ -25,6 +25,7 @@ from aqt.utils import (
|
||||
save_combo_index_for_session,
|
||||
save_is_checked,
|
||||
saveGeom,
|
||||
tooltip,
|
||||
tr,
|
||||
)
|
||||
|
||||
@ -33,19 +34,34 @@ class FindAndReplaceDialog(QDialog):
|
||||
COMBO_NAME = "BrowserFindAndReplace"
|
||||
|
||||
def __init__(
|
||||
self, parent: QWidget, *, mw: AnkiQt, note_ids: Sequence[NoteId]
|
||||
self,
|
||||
parent: QWidget,
|
||||
*,
|
||||
mw: AnkiQt,
|
||||
note_ids: Sequence[NoteId],
|
||||
field: Optional[str] = None,
|
||||
) -> None:
|
||||
"""
|
||||
If 'field' is passed, only this is added to the field selector.
|
||||
Otherwise, the fields belonging to the 'note_ids' are added.
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.mw = mw
|
||||
self.note_ids = note_ids
|
||||
self.field_names: List[str] = []
|
||||
self._field = field
|
||||
|
||||
# fetch field names and then show
|
||||
QueryOp(
|
||||
parent=mw,
|
||||
op=lambda col: col.field_names_for_note_ids(note_ids),
|
||||
success=self._show,
|
||||
).run_in_background()
|
||||
if field:
|
||||
self._show([field])
|
||||
elif note_ids:
|
||||
# fetch field names and then show
|
||||
QueryOp(
|
||||
parent=mw,
|
||||
op=lambda col: col.field_names_for_note_ids(note_ids),
|
||||
success=self._show,
|
||||
).run_in_background()
|
||||
else:
|
||||
self._show([])
|
||||
|
||||
def _show(self, field_names: Sequence[str]) -> None:
|
||||
# add "all fields" and "tags" to the top of the list
|
||||
@ -68,13 +84,23 @@ class FindAndReplaceDialog(QDialog):
|
||||
)
|
||||
self.form.replace.completer().setCaseSensitivity(Qt.CaseSensitive)
|
||||
|
||||
if not self.note_ids:
|
||||
# no selected notes to affect
|
||||
self.form.selected_notes.setChecked(False)
|
||||
self.form.selected_notes.setEnabled(False)
|
||||
elif self._field:
|
||||
self.form.selected_notes.setChecked(False)
|
||||
|
||||
restore_is_checked(self.form.re, self.COMBO_NAME + "Regex")
|
||||
restore_is_checked(self.form.ignoreCase, self.COMBO_NAME + "ignoreCase")
|
||||
|
||||
self.form.field.addItems(self.field_names)
|
||||
restore_combo_index_for_session(
|
||||
self.form.field, self.field_names, self.COMBO_NAME + "Field"
|
||||
)
|
||||
if self._field:
|
||||
self.form.field.setCurrentIndex(self.field_names.index(self._field))
|
||||
else:
|
||||
restore_combo_index_for_session(
|
||||
self.form.field, self.field_names, self.COMBO_NAME + "Field"
|
||||
)
|
||||
|
||||
qconnect(self.form.buttonBox.helpRequested, self.show_help)
|
||||
|
||||
@ -97,16 +123,20 @@ class FindAndReplaceDialog(QDialog):
|
||||
save_is_checked(self.form.re, self.COMBO_NAME + "Regex")
|
||||
save_is_checked(self.form.ignoreCase, self.COMBO_NAME + "ignoreCase")
|
||||
|
||||
if not self.form.selected_notes.isChecked():
|
||||
# an empty list means *all* notes
|
||||
self.note_ids = []
|
||||
|
||||
# tags?
|
||||
if self.form.field.currentIndex() == 1:
|
||||
find_and_replace_tag(
|
||||
op = find_and_replace_tag(
|
||||
parent=self.parentWidget(),
|
||||
note_ids=self.note_ids,
|
||||
search=search,
|
||||
replacement=replace,
|
||||
regex=regex,
|
||||
match_case=match_case,
|
||||
).run_in_background()
|
||||
)
|
||||
else:
|
||||
# fields
|
||||
if self.form.field.currentIndex() == 0:
|
||||
@ -114,7 +144,7 @@ class FindAndReplaceDialog(QDialog):
|
||||
else:
|
||||
field = self.field_names[self.form.field.currentIndex()]
|
||||
|
||||
find_and_replace(
|
||||
op = find_and_replace(
|
||||
parent=self.parentWidget(),
|
||||
note_ids=self.note_ids,
|
||||
search=search,
|
||||
@ -122,7 +152,16 @@ class FindAndReplaceDialog(QDialog):
|
||||
regex=regex,
|
||||
field_name=field,
|
||||
match_case=match_case,
|
||||
).run_in_background()
|
||||
)
|
||||
|
||||
if not self.note_ids:
|
||||
op.success(
|
||||
lambda out: tooltip(
|
||||
tr.browsing_notes_updated(count=out.count),
|
||||
parent=self.parentWidget(),
|
||||
)
|
||||
)
|
||||
op.run_in_background()
|
||||
|
||||
super().accept()
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>367</width>
|
||||
<width>377</width>
|
||||
<height>224</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -16,15 +16,8 @@
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>browsing_find</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="find">
|
||||
<item row="1" column="1">
|
||||
<widget class="QComboBox" name="replace">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>9</horstretch>
|
||||
@ -49,8 +42,46 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QComboBox" name="replace">
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>browsing_in</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>browsing_find</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QComboBox" name="field">
|
||||
<property name="sizeAdjustPolicy">
|
||||
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="1">
|
||||
<widget class="QCheckBox" name="re">
|
||||
<property name="text">
|
||||
<string>browsing_treat_input_as_regular_expression</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QCheckBox" name="ignoreCase">
|
||||
<property name="text">
|
||||
<string>browsing_ignore_case</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="find">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>9</horstretch>
|
||||
@ -68,31 +99,10 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>browsing_in</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QComboBox" name="field">
|
||||
<property name="sizeAdjustPolicy">
|
||||
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QCheckBox" name="re">
|
||||
<property name="text">
|
||||
<string>browsing_treat_input_as_regular_expression</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QCheckBox" name="ignoreCase">
|
||||
<widget class="QCheckBox" name="selected_notes">
|
||||
<property name="text">
|
||||
<string>browsing_ignore_case</string>
|
||||
<string>browsing_selected_notes_only</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
|
@ -6,7 +6,7 @@ mod search_node;
|
||||
|
||||
use std::{convert::TryInto, str::FromStr, sync::Arc};
|
||||
|
||||
use super::Backend;
|
||||
use super::{notes::to_note_ids, Backend};
|
||||
pub(super) use crate::backend_proto::search_service::Service as SearchService;
|
||||
use crate::{
|
||||
backend_proto as pb,
|
||||
@ -74,7 +74,7 @@ impl SearchService for Backend {
|
||||
if !input.match_case {
|
||||
search = format!("(?i){}", search);
|
||||
}
|
||||
let nids = input.nids.into_iter().map(NoteId).collect();
|
||||
let mut nids = to_note_ids(input.nids);
|
||||
let field_name = if input.field_name.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@ -82,6 +82,9 @@ impl SearchService for Backend {
|
||||
};
|
||||
let repl = input.replacement;
|
||||
self.with_col(|col| {
|
||||
if nids.is_empty() {
|
||||
nids = col.search_notes_unordered("")?
|
||||
};
|
||||
col.find_and_replace(nids, &search, &repl, field_name)
|
||||
.map(Into::into)
|
||||
})
|
||||
|
@ -73,8 +73,13 @@ impl TagsService for Backend {
|
||||
input: pb::FindAndReplaceTagRequest,
|
||||
) -> Result<pb::OpChangesWithCount> {
|
||||
self.with_col(|col| {
|
||||
let note_ids = if input.note_ids.is_empty() {
|
||||
col.search_notes_unordered("")?
|
||||
} else {
|
||||
to_note_ids(input.note_ids)
|
||||
};
|
||||
col.find_and_replace_tag(
|
||||
&to_note_ids(input.note_ids),
|
||||
¬e_ids,
|
||||
&input.search,
|
||||
&input.replacement,
|
||||
input.regex,
|
||||
|
Loading…
Reference in New Issue
Block a user