forked from Ajatt-Tools/mergenotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicate_notes.py
61 lines (46 loc) · 2.02 KB
/
duplicate_notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from gettext import ngettext
from typing import Sequence
from anki.collection import Collection, OpChanges
from anki.notes import Note
from aqt import gui_hooks
from aqt import mw
from aqt.browser import Browser
from aqt.operations import CollectionOp
from aqt.qt import *
from aqt.utils import tooltip
from .config import config
LIMIT = 30
def duplicate_notes_op(col: Collection, notes: Sequence[Note]) -> OpChanges:
pos = col.add_custom_undo_entry(ngettext("Duplicate %d note", "Duplicate %d notes", len(notes)) % len(notes))
for ref_note in notes:
new_note = Note(col, ref_note.note_type())
first_card = ref_note.cards()[0]
for key in ref_note.keys():
new_note[key] = ref_note[key]
new_note.tags = [tag for tag in ref_note.tags if tag != 'leech' and tag != 'marked']
col.add_note(new_note, deck_id=(first_card.odid or first_card.did))
return col.merge_undo_entries(pos)
def duplicate_notes(browser: Browser) -> None:
notes = [mw.col.get_note(note_id) for note_id in browser.selected_notes()]
if 1 <= len(notes) <= LIMIT:
CollectionOp(
browser, lambda col: duplicate_notes_op(col, notes)
).success(
lambda out: tooltip(
ngettext("%d note duplicated.", "%d notes duplicated.", len(notes)) % len(notes),
parent=browser
)
).run_in_background()
else:
tooltip(f"Please select at most {LIMIT} notes.")
def setup_context_menu(browser: Browser) -> None:
if config['show_duplicate_notes_button']:
menu = browser.form.menu_Cards
action = menu.addAction("Duplicate notes")
if shortcut := config['duplicate_notes_shortcut']:
action.setShortcut(QKeySequence(shortcut))
qconnect(action.triggered, lambda: duplicate_notes(browser))
def init():
gui_hooks.browser_menus_did_init.append(setup_context_menu)