Check for identical templates before saving

This commit is contained in:
RumovZ 2021-05-25 21:57:49 +02:00
parent fa19f590e8
commit 60131eab23
3 changed files with 19 additions and 0 deletions

View file

@ -21,6 +21,7 @@ card-templates-night-mode = Night Mode
card-templates-add-mobile-class = Add Mobile Class
card-templates-preview-settings = Options
card-templates-invalid-template-number = Card template { $number } in notetype '{ $notetype }' has a problem.
card-templates-identical-front = Its front side is identical with the one of card template { $number }.
card-templates-see-preview = See the render preview for more information.
card-templates-changes-saved = Changes saved.
card-templates-discard-changes = Discard changes?

View file

@ -66,6 +66,7 @@ impl AnkiError {
tr.card_templates_invalid_template_number(err.ordinal + 1, &err.notetype);
let details = match err.details {
TemplateSaveErrorDetails::TemplateError => tr.card_templates_see_preview(),
TemplateSaveErrorDetails::Duplicate(i) => tr.card_templates_identical_front(i),
};
format!("{}<br>{}", header, details)
}
@ -145,5 +146,6 @@ pub struct TemplateSaveError {
#[derive(Debug, PartialEq)]
pub enum TemplateSaveErrorDetails {
Duplicate(usize),
TemplateError,
}

View file

@ -272,6 +272,21 @@ impl Notetype {
});
}
fn ensure_template_fronts_unique(&self) -> Result<()> {
for i in 1..self.templates.len() {
for j in 0..i {
if self.templates[i].config.q_format == self.templates[j].config.q_format {
return Err(AnkiError::TemplateSaveError(TemplateSaveError {
notetype: self.name.clone(),
ordinal: i,
details: TemplateSaveErrorDetails::Duplicate(j + 1),
}));
}
}
}
Ok(())
}
pub(crate) fn normalize_names(&mut self) {
ensure_string_in_nfc(&mut self.name);
for f in &mut self.fields {
@ -314,6 +329,7 @@ impl Notetype {
self.fix_template_names()?;
self.ensure_names_unique();
self.reposition_sort_idx();
self.ensure_template_fronts_unique()?;
let parsed_templates = self.parsed_templates();
let invalid_card_idx = parsed_templates