Revert Editor.set_note's signature change with an alternative approach for #3730 (#3736)

* remove orig_note_id param

* add and use Note.orig_note_id instead

* add and use Editor.orig_note_id instead
This commit is contained in:
llama 2025-01-18 14:14:09 +08:00 committed by GitHub
parent d2ced60f7c
commit 430d5f5639
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 12 deletions

View file

@ -78,7 +78,8 @@ class AddCards(QMainWindow):
new_note.fields = note.fields[:]
new_note.tags = note.tags[:]
self.setAndFocusNote(new_note, orig_note_id=note.id)
self.editor.orig_note_id = note.id
self.setAndFocusNote(new_note)
def setupEditor(self) -> None:
self.editor = aqt.editor.Editor(
@ -143,8 +144,8 @@ class AddCards(QMainWindow):
b.setEnabled(False)
self.historyButton = b
def setAndFocusNote(self, note: Note, orig_note_id: NoteId | None = None) -> None:
self.editor.set_note(note, focusTo=0, orig_note_id=orig_note_id)
def setAndFocusNote(self, note: Note) -> None:
self.editor.set_note(note, focusTo=0)
def show_notetype_selector(self) -> None:
self.editor.call_after_note_saved(self.notetype_chooser.choose_notetype)

View file

@ -139,6 +139,8 @@ class Editor:
# Similar to currentField, but not set to None on a blur. May be
# outside the bounds of the current notetype.
self.last_field_index: int | None = None
# used when creating a copy of an existing note
self.orig_note_id: NoteId | None = None
# current card, for card layout
self.card: Card | None = None
self.state: EditorState = EditorState.INITIAL
@ -528,22 +530,19 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
note: Note | None,
hide: bool = True,
focusTo: int | None = None,
orig_note_id: NoteId | None = None,
) -> None:
"Make NOTE the current note."
self.note = note
self.currentField = None
if self.note:
self.loadNote(focusTo=focusTo, orig_note_id=orig_note_id)
self.loadNote(focusTo=focusTo)
elif hide:
self.widget.hide()
def loadNoteKeepingFocus(self) -> None:
self.loadNote(self.currentField)
def loadNote(
self, focusTo: int | None = None, orig_note_id: NoteId | None = None
) -> None:
def loadNote(self, focusTo: int | None = None) -> None:
if not self.note:
return
@ -606,8 +605,9 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
if self.editorMode is not EditorMode.ADD_CARDS:
io_options = self._create_edit_io_options(note_id=self.note.id)
js += " setupMaskEditor(%s);" % json.dumps(io_options)
elif orig_note_id:
io_options = self._create_clone_io_options(cloned_note_id=orig_note_id)
elif orig_note_id := self.orig_note_id:
self.orig_note_id = None
io_options = self._create_clone_io_options(orig_note_id)
js += " setupMaskEditor(%s);" % json.dumps(io_options)
js = gui_hooks.editor_will_load_note(js, self.note, self)
@ -1186,9 +1186,9 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
}
@staticmethod
def _create_clone_io_options(cloned_note_id: NoteId) -> dict:
def _create_clone_io_options(orig_note_id: NoteId) -> dict:
return {
"mode": {"kind": "add", "clonedNoteId": cloned_note_id},
"mode": {"kind": "add", "clonedNoteId": orig_note_id},
}
@staticmethod