Skip to content

Instantly share code, notes, and snippets.

@hikaru-y
Last active June 21, 2020 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hikaru-y/737e60d5ff58a124ffe7e2a78a385831 to your computer and use it in GitHub Desktop.
Save hikaru-y/737e60d5ff58a124ffe7e2a78a385831 to your computer and use it in GitHub Desktop.
# 2020/06/22 Fix to work on Anki 2.1.28+
from typing import Optional, Any, List
# import sys
import anki
import aqt
TARGET_TAG = 'disable_night_mode'
TMP_NORMAL_CLASS = 'tmp_normal_mode'
night_mode_classes = "'nightMode', 'night_mode'"
ADD_NM = f'document.body.classList.add({night_mode_classes});'
REMOVE_NM = f'document.body.classList.remove({night_mode_classes});'
ON_SHOWN = '''
<script>
onShownHook.push(() => {{
{operation}
}});
</script>
'''
TAGS_ATTR = f'tags_for_{__name__}'
def get_normal_mode_window_color():
''' Based on: aqt.webview.AnkiWebView._getWindowColor() '''
qcolor: aqt.qt.QColor
if anki.utils.isMac:
# standard palette does not return correct window color on macOS
qcolor = aqt.qt.QColor('#ececec')
else:
qcolor = aqt.mw.web.style().standardPalette().color(
aqt.qt.QPalette.Window)
return qcolor.name()
EDITOR_TMP_NORMAL_MODE_STYLE = f'''
<style>
.{TMP_NORMAL_CLASS},
.{TMP_NORMAL_CLASS} body,
.{TMP_NORMAL_CLASS} #topbutsOuter {{
background-color: {get_normal_mode_window_color()};
}}
</style>
'''
def toggle_editor_night_mode(tags_text, editor):
js: str
if TARGET_TAG in tags_text.split():
js = REMOVE_NM
js += f'document.documentElement.classList.add("{TMP_NORMAL_CLASS}");'
else:
js = ADD_NM
js += f'document.documentElement.classList.remove("{TMP_NORMAL_CLASS}");'
editor.web.eval(js)
def on_editor_did_init(editor: aqt.editor.Editor):
editor.tags.textChanged.connect(
lambda text: toggle_editor_night_mode(text, editor))
def on_webview_will_set_content(
web_content: aqt.webview.WebContent, context: Optional[Any]):
if isinstance(context, aqt.editor.Editor):
web_content.head += EDITOR_TMP_NORMAL_MODE_STYLE
def on_card_will_show(text: str, card: anki.cards.Card, kind: str):
js_to_append: str = ''
tags: List[str] = []
try:
tags = card.note().tags
except TypeError:
# print(sys.exc_info())
# for Anki 2.1.28+
if kind in ('clayoutQuestion', 'clayoutAnswer'):
tags = getattr(card._render_output, TAGS_ATTR)
if TARGET_TAG in tags:
js_to_append = ON_SHOWN.format(operation=REMOVE_NM)
return text + js_to_append
def TemplateRenderContext_render_wrapper(
self: anki.template.TemplateRenderContext, _old):
'''
for Anki 2.1.28+
store tags of note for card layout editor
'''
output = _old(self)
setattr(output, TAGS_ATTR, self._note.tags)
return output
if aqt.mw.pm.night_mode():
aqt.gui_hooks.editor_did_init.append(on_editor_did_init)
aqt.gui_hooks.webview_will_set_content.append(on_webview_will_set_content)
aqt.gui_hooks.card_will_show.append(on_card_will_show)
try:
anki.template.TemplateRenderContext.render = anki.hooks.wrap(
anki.template.TemplateRenderContext.render,
TemplateRenderContext_render_wrapper,
'around'
)
except BaseException:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment