forked from Ajatt-Tools/Japanese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_toolbar.py
206 lines (176 loc) · 6.93 KB
/
editor_toolbar.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import functools
from collections.abc import Iterable
from typing import Callable, NamedTuple, Optional
import anki.collection
from anki.collection import OpChanges
from anki.notes import Note
from aqt import gui_hooks
from aqt.editor import Editor
from aqt.operations import CollectionOp
from .ajt_common.consts import ADDON_SERIES
from .audio import aud_src_mgr, format_audio_tags
from .config_view import config_view as cfg, ToolbarButtonConfig
from .definitions import sakura_client
from .helpers.profiles import TaskCaller
from .helpers.tokens import clean_furigana
from .reading import generate_furigana
from .tasks import DoTasks
from .widgets.anki_style import fix_default_anki_style
from .widgets.audio_search import AnkiAudioSearchDialog
class ToolbarButton(NamedTuple):
id: str
on_press: Callable[[Editor], None]
tip: str
conf: ToolbarButtonConfig
def carefully_update_note(col: anki.collection.Collection, note: Note) -> OpChanges:
if note.id > 0:
# Anki can't update notes in the "Add" window.
# It can only update notes in the "Browser" and "Edit" windows.
return col.update_note(note)
return OpChanges()
def modify_field(func: Callable[[str], str]) -> Callable[[Editor], None]:
"""
Used to generate or clean furigana in the current field.
"""
def collection_op(col: anki.collection.Collection, note: Note, field_n: int) -> OpChanges:
pos = col.add_custom_undo_entry(f"{ADDON_SERIES}: Modify field {note.keys()[field_n]}.")
note.fields[field_n] = func(note.fields[field_n])
carefully_update_note(col, note)
return col.merge_undo_entries(pos)
@functools.wraps(func)
def decorator(editor: Editor) -> None:
if (note := editor.note) and (field_n := editor.currentField) is not None:
CollectionOp(
parent=editor.widget,
op=lambda col: collection_op(col, note, field_n),
).success(
lambda out: editor.loadNoteKeepingFocus(),
).run_in_background()
return decorator
def modify_note(func: Callable[[Editor], object]) -> Callable[[Editor], None]:
"""
Used to (re)generate all target fields of the current note.
"""
def note_reload(editor: Editor):
return editor.loadNote(focusTo=0) if editor.currentField is None else editor.loadNoteKeepingFocus()
@functools.wraps(func)
def decorator(editor: Editor) -> None:
# Note must be set to proceed.
if editor.note:
func(editor)
CollectionOp(
parent=editor.widget,
op=lambda col: carefully_update_note(col, editor.note),
).success(
lambda out: note_reload(editor),
).run_in_background()
return decorator
def get_note_value(note: Note, field_name: str) -> Optional[str]:
"""
Try to access field with name field_name.
Suppress KeyError and return None if the field with this name doesn't exist.
"""
try:
return note[field_name]
except KeyError:
return None
def search_audio(editor: Editor) -> None:
# the caller should have ensured that editor.note is not None.
assert editor.note is not None
with aud_src_mgr.request_new_session() as session:
dialog = AnkiAudioSearchDialog(session)
fix_default_anki_style(dialog.table)
dialog.set_note_fields(
editor.note.keys(),
selected_src_field_name=cfg.audio_settings.search_dialog_src_field_name,
selected_dest_field_name=cfg.audio_settings.search_dialog_dest_field_name,
)
dialog.search(
editor.web.selectedText()
or get_note_value(note=editor.note, field_name=cfg.audio_settings.search_dialog_src_field_name)
)
if not dialog.exec():
# The user pressed "Cancel". Nothing to do.
return
# remember field names for later calls
cfg.audio_settings.search_dialog_src_field_name = dialog.source_field_name
cfg.audio_settings.search_dialog_dest_field_name = dialog.destination_field_name
cfg.write_config()
# process results
results = dialog.files_to_add()
editor.note[dialog.destination_field_name] += format_audio_tags(results)
session.download_and_save_tags(results)
def query_buttons() -> Iterable[ToolbarButton]:
return (
ToolbarButton(
id="generate_all_button",
on_press=modify_note(
lambda editor: DoTasks(
editor.note,
caller=TaskCaller.toolbar_button,
overwrite=False,
).run()
),
tip="Generate all fields",
conf=cfg.toolbar.generate_all_button,
),
ToolbarButton(
id="regenerate_all_button",
on_press=modify_note(
lambda editor: DoTasks(
editor.note,
caller=TaskCaller.toolbar_button,
overwrite=True,
).run()
),
tip="Regenerate all fields (overwrite existing data)",
conf=cfg.toolbar.regenerate_all_button,
),
ToolbarButton(
id="furigana_button",
on_press=modify_field(generate_furigana),
tip="Generate furigana in the field",
conf=cfg.toolbar.furigana_button,
),
ToolbarButton(
id="hiragana_button",
on_press=modify_field(functools.partial(generate_furigana, full_hiragana=True)),
tip="Reconvert the field as hiragana",
conf=cfg.toolbar.hiragana_button,
),
ToolbarButton(
id="clean_furigana_button",
on_press=modify_field(clean_furigana),
tip="Clean furigana in the field",
conf=cfg.toolbar.clean_furigana_button,
),
ToolbarButton(
id="audio_search_button",
on_press=modify_note(search_audio),
tip="Search audio files to add to note",
conf=cfg.toolbar.audio_search_button,
),
ToolbarButton(
id="add_definition_button",
on_press=modify_note(sakura_client.add_definition),
tip="Add dictionary definition for the target word.",
conf=cfg.toolbar.add_definition_button,
),
)
def add_toolbar_buttons(html_buttons: list[str], editor: Editor) -> None:
html_buttons.extend(
editor.addButton(
icon=None,
cmd=f"ajt__{b.id}",
func=b.on_press,
tip=f"{b.tip} ({b.conf.shortcut})" if b.conf.shortcut else b.tip,
keys=b.conf.shortcut or None,
label=b.conf.text,
)
for b in query_buttons()
if b.conf.enabled
)
def init():
gui_hooks.editor_did_init_buttons.append(add_toolbar_buttons)