From d273ea29233606b3e150bb20788a11848cbabdc8 Mon Sep 17 00:00:00 2001 From: Matthias M <62722460+kleinerpirat@users.noreply.github.com> Date: Mon, 15 Nov 2021 11:34:52 +0100 Subject: [PATCH] Enable access to old notetype name --- proto/anki/notetypes.proto | 2 ++ pylib/anki/models.py | 1 + rslib/src/backend/notetypes.rs | 3 +++ rslib/src/notetype/notetypechange.rs | 6 +++++- ts/change-notetype/lib.test.ts | 2 ++ ts/change-notetype/lib.ts | 7 ++++++- 6 files changed, 19 insertions(+), 2 deletions(-) diff --git a/proto/anki/notetypes.proto b/proto/anki/notetypes.proto index 2b8dc9146..85592cbc0 100644 --- a/proto/anki/notetypes.proto +++ b/proto/anki/notetypes.proto @@ -167,6 +167,7 @@ message ChangeNotetypeRequest { int64 old_notetype_id = 4; int64 new_notetype_id = 5; int64 current_schema = 6; + string old_notetype_name = 7; } message ChangeNotetypeInfo { @@ -175,4 +176,5 @@ message ChangeNotetypeInfo { repeated string new_field_names = 3; repeated string new_template_names = 4; ChangeNotetypeRequest input = 5; + string old_notetype_name = 6; } diff --git a/pylib/anki/models.py b/pylib/anki/models.py index 02e39a1b3..71c2c75af 100644 --- a/pylib/anki/models.py +++ b/pylib/anki/models.py @@ -415,6 +415,7 @@ and notes.mid = ? and cards.ord = ?""", note_ids=nids, new_fields=field_map, new_templates=template_map, + old_notetype_name=notetype["name"], old_notetype_id=notetype["id"], new_notetype_id=newModel["id"], current_schema=self.col.db.scalar("select scm from col"), diff --git a/rslib/src/backend/notetypes.rs b/rslib/src/backend/notetypes.rs index 3e2e85047..21c3b9eb6 100644 --- a/rslib/src/backend/notetypes.rs +++ b/rslib/src/backend/notetypes.rs @@ -190,6 +190,7 @@ impl From for Notetype { impl From for pb::ChangeNotetypeInfo { fn from(i: NotetypeChangeInfo) -> Self { pb::ChangeNotetypeInfo { + old_notetype_name: i.old_notetype_name, old_field_names: i.old_field_names, old_template_names: i.old_template_names, new_field_names: i.new_field_names, @@ -204,6 +205,7 @@ impl From for ChangeNotetypeInput { ChangeNotetypeInput { current_schema: i.current_schema.into(), note_ids: i.note_ids.into_newtype(NoteId), + old_notetype_name: i.old_notetype_name.into(), old_notetype_id: i.old_notetype_id.into(), new_notetype_id: i.new_notetype_id.into(), new_fields: i @@ -232,6 +234,7 @@ impl From for pb::ChangeNotetypeRequest { pb::ChangeNotetypeRequest { current_schema: i.current_schema.into(), note_ids: i.note_ids.into_iter().map(Into::into).collect(), + old_notetype_name: i.old_notetype_name.into(), old_notetype_id: i.old_notetype_id.into(), new_notetype_id: i.new_notetype_id.into(), new_fields: i diff --git a/rslib/src/notetype/notetypechange.rs b/rslib/src/notetype/notetypechange.rs index 50cbf627a..0c341a608 100644 --- a/rslib/src/notetype/notetypechange.rs +++ b/rslib/src/notetype/notetypechange.rs @@ -16,6 +16,7 @@ use crate::{ pub struct ChangeNotetypeInput { pub current_schema: TimestampMillis, pub note_ids: Vec, + pub old_notetype_name: String, pub old_notetype_id: NotetypeId, pub new_notetype_id: NotetypeId, pub new_fields: Vec>, @@ -25,6 +26,7 @@ pub struct ChangeNotetypeInput { #[derive(Debug)] pub struct NotetypeChangeInfo { pub input: ChangeNotetypeInput, + pub old_notetype_name: String, pub old_field_names: Vec, pub old_template_names: Vec, pub new_field_names: Vec, @@ -77,18 +79,20 @@ impl Collection { .ok_or(AnkiError::NotFound)?; let current_schema = self.storage.get_collection_timestamps()?.schema_change; + let old_notetype_name = &old_notetype.name.to_string(); let new_fields = default_field_map(&old_notetype, &new_notetype); let new_templates = default_template_map(&old_notetype, &new_notetype); - Ok(NotetypeChangeInfo { input: ChangeNotetypeInput { current_schema, note_ids: vec![], + old_notetype_name: old_notetype_name.to_string(), old_notetype_id, new_notetype_id, new_fields, new_templates, }, + old_notetype_name: old_notetype_name.to_string(), old_field_names: old_notetype.fields.iter().map(|f| f.name.clone()).collect(), old_template_names: old_notetype .templates diff --git a/ts/change-notetype/lib.test.ts b/ts/change-notetype/lib.test.ts index 3fe91cd8e..be047d23d 100644 --- a/ts/change-notetype/lib.test.ts +++ b/ts/change-notetype/lib.test.ts @@ -45,6 +45,7 @@ const exampleInfoDifferent = { oldNotetypeId: "1623289129847", newNotetypeId: "1623289129849", currentSchema: "1623302002316", + oldNotetypeName: "Basic", }, }; @@ -59,6 +60,7 @@ const exampleInfoSame = { oldNotetypeId: "1623289129847", newNotetypeId: "1623289129847", currentSchema: "1623302002316", + oldNotetypeName: "Basic", }, }; diff --git a/ts/change-notetype/lib.ts b/ts/change-notetype/lib.ts index 37a97c5bc..8921a3706 100644 --- a/ts/change-notetype/lib.ts +++ b/ts/change-notetype/lib.ts @@ -45,6 +45,7 @@ export function negativeOneToNull(list: number[]): (number | null)[] { export class ChangeNotetypeInfoWrapper { fields: (number | null)[]; templates?: (number | null)[]; + oldNotetypeName: String; readonly info: Notetypes.ChangeNotetypeInfo; constructor(info: Notetypes.ChangeNotetypeInfo) { @@ -54,6 +55,7 @@ export class ChangeNotetypeInfoWrapper { this.templates = negativeOneToNull(templates); } this.fields = negativeOneToNull(info.input!.newFields!); + this.oldNotetypeName = info.oldNotetypeName; } /// A list with an entry for each field/template in the new notetype, with @@ -82,6 +84,10 @@ export class ChangeNotetypeInfoWrapper { : this.info.oldFieldNames; } + getOldNotetypeName(): string { + return this.info.oldNotetypeName; + } + getNewName(ctx: MapContext, idx: number): string { return ( ctx == MapContext.Template @@ -97,7 +103,6 @@ export class ChangeNotetypeInfoWrapper { (idx) => !usedEntries.has(idx), ); const unusedNames = unusedIdxs.map((idx) => oldNames[idx]); - unusedNames.sort(); return unusedNames; }