-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
250 lines (195 loc) · 7.81 KB
/
__init__.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# anki-webbrowse
# Copyright (C) 2020 Tom Z.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import re
from aqt import mw, gui_hooks
from aqt.utils import showInfo
from aqt.qt import *
from aqt.editor import Editor
from anki.hooks import wrap, addHook
from aqt.addcards import AddCards
from anki.utils import isMac
sys.path.insert(0, os.path.dirname(__file__))
from .browser import AddCardsTabbedBrowser
browser_displayed = False
browser = None
menu = None
shortcut_set = False
nightmode = False
def display_browser(web):
global browser_displayed, browser, menu
# first init
if browser is None:
config = mw.addonManager.getConfig(__name__)
addcards = web.parent().parent()
if not isinstance(addcards, AddCards):
addcards = mw.app.activeWindow()
assert(isinstance(addcards, AddCards))
layout = addcards.layout()
hbox = QHBoxLayout()
vbox = QVBoxLayout()
while layout.count() > 0:
item = layout.takeAt(0)
if item.widget() is None or isinstance(item, QBoxLayout):
vbox.addLayout(item.layout())
else:
vbox.addWidget(item.widget())
temp = QWidget()
temp.setLayout(layout)
flds = QWidget()
flds.setLayout(vbox)
hbox.setSpacing(0)
hbox.setContentsMargins(0, 0, 0, 0)
menu = QWidget()
menu_vbox = QVBoxLayout()
# build bookmarks list
btree = QTreeWidget()
btree.itemClicked.connect(bookmark_clicked)
bookmarks = config["bookmarks"]
expand = config["expand_bookmark_folders_by_default"]
for k, blist in bookmarks.items():
folder = QTreeWidgetItem(btree, [k])
folder.setExpanded(expand)
for b in blist:
b = re.sub("(https?://)?(www2?\\.)?", "", b)
if b.endswith("/"):
b = b[:-1]
item = QTreeWidgetItem([b])
folder.addChild(item)
btree.setHeaderLabels(["Bookmarks"])
btree.setIconSize(QSize(0,0))
menu_vbox.addWidget(btree, 2)
menu.setLayout(menu_vbox)
menu.setVisible(False)
browser = AddCardsTabbedBrowser(flds, menu, web, nightmode)
flds_width = config["fields_area_width_in_percent"]
flds_width = min(95, max(1, flds_width))
bookmarks_width = config["bookmarks_area_width_in_percent"]
if not config["switch_fields_area_to_right"]:
hbox.addWidget(flds, flds_width)
hbox.addWidget(browser, 100-flds_width-bookmarks_width)
hbox.addWidget(menu, bookmarks_width)
else:
hbox.addWidget(menu, bookmarks_width)
hbox.addWidget(browser, 100-flds_width-bookmarks_width)
hbox.addWidget(flds, flds_width)
addcards.setLayout(hbox)
shortcut = QShortcut(QKeySequence(config["toggle_fields_shortcut"]), browser)
shortcut.activated.connect(toggle_sidebar)
shortcut = QShortcut(QKeySequence(config["zoom_in_shortcut"]), browser)
shortcut.activated.connect(zoom_in)
shortcut = QShortcut(QKeySequence(config["zoom_out_shortcut"]), browser)
shortcut.activated.connect(zoom_out)
addcards.form.deckArea.setMinimumWidth(35)
addcards.form.modelArea.setMinimumWidth(35)
# addcards.form.deckArea.setMaximumWidth(125)
# addcards.form.modelArea.setMaximumWidth(125)
addcards.helpButton.setVisible(False)
else:
browser.setVisible(True)
web.eval("""
document.getElementById('toggleBrowserBtn').style.color = `#2496dc`;
if (document.getElementById('switchBtn')) {
document.getElementById('switchBtn').setAttribute('style', 'display:none !important');
}
""")
browser_displayed = True
def hide_browser(web):
global browser_displayed, browser, menu
browser.setVisible(False)
menu.setVisible(False)
browser_displayed = False
addcards = web.parent().parent().parent()
# bad but works
addcards.form.deckArea.setMaximumWidth(3000)
addcards.form.modelArea.setMaximumWidth(3000)
web.eval("""
document.getElementById('toggleBrowserBtn').style.color = `inherit`;
if (document.getElementById('switchBtn') ) {
document.getElementById('switchBtn').setAttribute('style', 'display:inline-block');
}
""")
def bookmark_clicked(item):
if item.parent() is None:
return
browser.load(item.text(0))
def on_editor_did_init(editor):
global shortcut_set
if editor.addMode:
if not shortcut_set:
config = mw.addonManager.getConfig(__name__)
shortcut = QShortcut(QKeySequence(config["toggle_browser_shortcut"]), mw.app.activeWindow())
shortcut.activated.connect(toggle_browser)
shortcut_set = True
def cb(info):
global browser, nightmode
rendered = info[0]
nightmode = info[1]
if not rendered:
browser = None
if browser_displayed:
display_browser(editor.web)
if isMac:
btn_text = "www"
else:
btn_text = "🌐"
editor.web.evalWithCallback("""
(() => {
let rendered = false;
if (!document.getElementById('toggleBrowserBtn')) {
document.getElementById('topbutsleft').innerHTML += `<button id='toggleBrowserBtn' onclick='pycmd("toggle_browser")'>%s</button> `;
} else {
rendered = true;
}
let nightmode = document.body.classList.contains("nightMode");
return [rendered, nightmode];
})();
""" % btn_text, cb)
def toggle_sidebar():
if not browser_displayed:
return
browser.toggle()
def toggle_browser():
win = mw.app.activeWindow()
if hasattr(win, "editor"):
web = win.editor.web
if browser_displayed:
hide_browser(web)
else:
display_browser(web)
def zoom_out():
if not browser_displayed:
return
browser.zoom_out()
def zoom_in():
if not browser_displayed:
return
browser.zoom_in()
def expanded_on_bridge(self, cmd, _old):
if cmd == "toggle_browser":
if browser_displayed:
hide_browser(self.web)
else:
display_browser(self.web)
else:
return _old(self, cmd)
def on_keypress(addcards, evt, _old):
if evt.key() in (Qt.Key_Enter, Qt.Key_Return) and browser.url.hasFocus():
return
return _old(addcards, evt)
def init_addon():
gui_hooks.editor_did_load_note.append(on_editor_did_init)
Editor.onBridgeCmd = wrap(Editor.onBridgeCmd, expanded_on_bridge, "around")
init_addon()