Saved changes to Note overwritten at click 'Save'

Check this simple demo addon:

def process_note(note: Any) -> Any:
    note_type = mw.col.models.by_name(note.note_type()['name'])

    note_type['tmpls'][0]['afmt'] += "aaaaaaaaaaaaaaaaaaa"

    mw.col.models.save(note_type)

hooks.note_will_flush.append(process_note)

You may think the backTemplate will have “aaaaaaaaa” when the user saves the note type But no. The change is only carried when the user DOESN’T click ‘Save’, but instead closes wihtout saving.
This is because the ‘Save’ button overrides the DB with the content of the editor.

I tried finding a hook for the ‘Save’ button, but couldn’t find any. I also tried reloading the editor to show the changes made to the DB, but couldn’t find how to get the current editor instance.
I have already spent a few hours on this. So I just want to know if it’s possible or should I find another way.

I have faced this problem before. In some scenarios calling editor.set_note(), editor.loadNote() or editor.loadNoteKeepingFocus() helped me, but I guess there is no universal solution.

You can use this to get the instance of the editor. Then call function such as set_note(note).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.