-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.py
69 lines (51 loc) · 2.27 KB
/
editor.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
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from aqt import gui_hooks
from aqt.editor import Editor, EditorWebView
from aqt.qt import *
from aqt.utils import tooltip
from .common import play_text
from .config import config
from .consts import *
def fetch_note_text(editor: Editor) -> str:
selected_text = editor.web.selectedText()
if selected_text:
return selected_text
else:
return "".join(editor.note.fields)
def get_addon_path() -> str:
return os.path.dirname(__file__)
def play_field(editor: Editor) -> None:
if editor.currentField is not None:
field_content = editor.note.fields[editor.currentField]
play_text(field_content)
elif config.show_tooltips is True:
tooltip("No field selected.")
def append_editor_button(buttons: list[str], editor: Editor) -> None:
if config.show_toolbar_button is False:
return
icon_path = os.path.join(get_addon_path(), PLAY_ICON_FILEPATH)
shortcut = config.get("shortcut")
b = editor.addButton(
icon_path,
"play_sound_button",
lambda e: play_text(fetch_note_text(e)),
tip=f"play sound ({shortcut if shortcut else 'no shortcut'})",
keys=shortcut,
)
buttons.append(b)
def add_context_menu_item(webview: EditorWebView, menu: QMenu) -> None:
if config.show_play_field_action is True:
play_field_action: QAction = menu.addAction("Play field")
qconnect(play_field_action.triggered, lambda _=False: play_field(webview.editor))
if config.show_play_selection_action is True:
play_selection_action: QAction = menu.addAction("Play selection")
qconnect(play_selection_action.triggered, lambda _=False: play_text(webview.editor.web.selectedText()))
def on_load_note(editor: Editor) -> None:
if config.autoplay and editor.note and getattr(editor, "last_note_id__", None) != editor.note.id:
play_text(".".join(editor.note.fields), quiet=True)
setattr(editor, "last_note_id__", editor.note.id)
def init():
gui_hooks.editor_did_init_buttons.append(append_editor_button)
gui_hooks.editor_will_show_context_menu.append(add_context_menu_item)
gui_hooks.editor_did_load_note.append(on_load_note)