Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Bugfix: Allow simultaneously adding cards and browsing cards #179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions chinese/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@
# You should have received a copy of the GNU General Public License along with
# Chinese Support Redux. If not, see <https://www.gnu.org/licenses/>.

from anki.hooks import addHook
from aqt import mw
from aqt import mw, gui_hooks

from .behavior import update_fields
from .main import config


class EditManager:
def __init__(self):
addHook('setupEditorButtons', self.setupButton)
addHook('loadNote', self.updateButton)
addHook('editFocusLost', self.onFocusLost)
gui_hooks.editor_did_init_buttons.append(self.setupButton)
gui_hooks.editor_did_load_note.append(self.updateButton)
gui_hooks.editor_did_unfocus_field.append(self.onFocusLost)
self.editors = []

def setupButton(self, buttons, editor):
self.editor = editor
self.editors.append(editor)
self.buttonOn = False
editor._links['chineseSupport'] = self.onToggle

button = editor._addButton(
button = editor.addButton(
icon=None,
func=self.onToggle,
cmd='chineseSupport',
tip='Chinese Support',
label='<b>汉字</b>',
id='chineseSupport',
toggleable=True)

return buttons + [button]
buttons.append(button)

def onToggle(self, editor):
self.buttonOn = not self.buttonOn
Expand All @@ -64,6 +64,10 @@ def updateButton(self, editor):
editor.web.eval('toggleEditorButton(chineseSupport);')
self.buttonOn = not self.buttonOn

def _refreshAllEditors(self, focusTo):
for editor in self.editors:
editor.loadNote(focusTo=focusTo)

def onFocusLost(self, _, note, index):
if not self.buttonOn:
return False
Expand All @@ -72,10 +76,8 @@ def onFocusLost(self, _, note, index):
field = allFields[index]

if update_fields(note, field, allFields):
if index == len(allFields) - 1:
self.editor.loadNote(focusTo=index)
else:
self.editor.loadNote(focusTo=index+1)
focusTo = (index + 1) % len(allFields)
self._refreshAllEditors(focusTo)

return False

Expand Down
17 changes: 8 additions & 9 deletions chinese/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
# You should have received a copy of the GNU General Public License along with
# Chinese Support Redux. If not, see <https://www.gnu.org/licenses/>.

from anki.hooks import addHook, wrap
from anki.lang import _
from anki.hooks import wrap
from anki.stats import CollectionStats
from anki.stdmodels import models
from aqt import mw
from aqt import mw, gui_hooks

from .config import ConfigManager
from .database import Dictionary
Expand All @@ -42,12 +41,12 @@
def load():
ruby.install()
chinese.install()
addHook('profileLoaded', load_menu)
addHook('profileLoaded', add_models)
addHook('loadNote', append_tone_styling)
addHook('unloadProfile', config.save)
addHook('unloadProfile', dictionary.conn.close)
addHook('unloadProfile', unload_menu)
gui_hooks.profile_did_open.append(load_menu)
gui_hooks.profile_did_open.append(add_models)
gui_hooks.editor_did_load_note.append(append_tone_styling)
gui_hooks.profile_will_close.append(config.save)
gui_hooks.profile_will_close.append(dictionary.conn.close)
gui_hooks.profile_will_close.append(unload_menu)
CollectionStats.todayStats = wrap(
CollectionStats.todayStats, todayStats, 'around'
)
Expand Down