mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
Check for identical templates before saving
This commit is contained in:
parent
fa19f590e8
commit
60131eab23
3 changed files with 19 additions and 0 deletions
|
@ -21,6 +21,7 @@ card-templates-night-mode = Night Mode
|
||||||
card-templates-add-mobile-class = Add Mobile Class
|
card-templates-add-mobile-class = Add Mobile Class
|
||||||
card-templates-preview-settings = Options
|
card-templates-preview-settings = Options
|
||||||
card-templates-invalid-template-number = Card template { $number } in notetype '{ $notetype }' has a problem.
|
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-see-preview = See the render preview for more information.
|
||||||
card-templates-changes-saved = Changes saved.
|
card-templates-changes-saved = Changes saved.
|
||||||
card-templates-discard-changes = Discard changes?
|
card-templates-discard-changes = Discard changes?
|
||||||
|
|
|
@ -66,6 +66,7 @@ impl AnkiError {
|
||||||
tr.card_templates_invalid_template_number(err.ordinal + 1, &err.notetype);
|
tr.card_templates_invalid_template_number(err.ordinal + 1, &err.notetype);
|
||||||
let details = match err.details {
|
let details = match err.details {
|
||||||
TemplateSaveErrorDetails::TemplateError => tr.card_templates_see_preview(),
|
TemplateSaveErrorDetails::TemplateError => tr.card_templates_see_preview(),
|
||||||
|
TemplateSaveErrorDetails::Duplicate(i) => tr.card_templates_identical_front(i),
|
||||||
};
|
};
|
||||||
format!("{}<br>{}", header, details)
|
format!("{}<br>{}", header, details)
|
||||||
}
|
}
|
||||||
|
@ -145,5 +146,6 @@ pub struct TemplateSaveError {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum TemplateSaveErrorDetails {
|
pub enum TemplateSaveErrorDetails {
|
||||||
|
Duplicate(usize),
|
||||||
TemplateError,
|
TemplateError,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
pub(crate) fn normalize_names(&mut self) {
|
||||||
ensure_string_in_nfc(&mut self.name);
|
ensure_string_in_nfc(&mut self.name);
|
||||||
for f in &mut self.fields {
|
for f in &mut self.fields {
|
||||||
|
@ -314,6 +329,7 @@ impl Notetype {
|
||||||
self.fix_template_names()?;
|
self.fix_template_names()?;
|
||||||
self.ensure_names_unique();
|
self.ensure_names_unique();
|
||||||
self.reposition_sort_idx();
|
self.reposition_sort_idx();
|
||||||
|
self.ensure_template_fronts_unique()?;
|
||||||
|
|
||||||
let parsed_templates = self.parsed_templates();
|
let parsed_templates = self.parsed_templates();
|
||||||
let invalid_card_idx = parsed_templates
|
let invalid_card_idx = parsed_templates
|
||||||
|
|
Loading…
Reference in a new issue