Skip to content

Commit

Permalink
Update previewer HTML incrementally when editing single notes
Browse files Browse the repository at this point in the history
  • Loading branch information
glutanimate committed Jun 3, 2017
1 parent d1c1fe8 commit 3dced4f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ For further information on the use of this add-on please check out [the original

This add-on was originally developed on a commission by [BB on the Anki support forums](https://anki.tenderapp.com/discussions/add-ons/8504-100-for-add-on-developer). The card reviewing feature introduced in the latest update was commissioned by another fellow Anki user who would like to remain anonymous. I would like to thank both of them for their great ideas and generous support in writing this add-on.

If you have an idea for an add-on or new feature, please feel free to reach out to me on [Twitter](https://twitter.com/glutanimate), or at <em>ankiglutanimate [αt] gmail . com</em>
If you'd like to hire my services to work an add-on or new feature, please feel free to reach out to me on [Twitter](https://twitter.com/glutanimate), or at <em>ankiglutanimate [αt] gmail . com</em>

Licensed under the [GNU AGPL v3](https://www.gnu.org/licenses/agpl.html).
4 changes: 4 additions & 0 deletions advanced_previewer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def setupAddon():
Browser._updatePreviewButtons = updatePreviewButtons
Browser._onPreviewPrev = onPreviewPrev
Browser._onPreviewNext = onPreviewNext
Browser.refreshCurrentCard = refreshCurrentCard
# new methods:
Browser.scrollToPreview = scrollToPreview
Browser._previewLinkHandler = previewLinkHandler
Expand All @@ -55,5 +56,8 @@ def setupAddon():
Browser._onPreviewAnswer = onPreviewAnswer
Browser._updatePreviewAnswers = updatePreviewAnswers
Browser._onPreviewMove = onPreviewMove
Browser._selectiveCardRender = selectiveCardRender
Browser.updatePreviewHtml = updatePreviewHtml


Scheduler._nextRevIvl = wrap(Scheduler._nextRevIvl, nextRevIvl, "around")
30 changes: 30 additions & 0 deletions advanced_previewer/previewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from anki.hooks import wrap
from anki.sound import clearAudioQueue, playFromText, play
from anki.js import browserSel
from anki.utils import json

from .html import *
from .config import loadConfig
Expand Down Expand Up @@ -365,3 +366,32 @@ def renderPreview(self, cardChanged=False):
if not multi and self.mw.reviewer.autoplay(c):
playFromText(txt)


def updatePreviewHtml(self, note):
replacements = self._selectiveCardRender(note)
for cid, html in replacements.items():
self._previewWeb.eval(
js_replace.format(str(cid), json.dumps(html)))
self.scrollToPreview(cid)


def selectiveCardRender(self, note):
cards = note.cards()
replacements = {}
for card in cards:
cid = card.id
if self._previewState == "answer":
inner_html = card.a()
else:
inner_html = card.q()
replacements[cid] = inner_html
return replacements

js_replace = u"""
const elm = document.getElementById('{}');
elm.innerHTML = {}
"""

def refreshCurrentCard(self, note):
self.model.refreshNote(note)
self.updatePreviewHtml(note)
2 changes: 1 addition & 1 deletion designer/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ p, li { white-space: pre-wrap; }
&lt;p style=&quot; margin-top:0px; margin-bottom:12px; margin-left:10px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Credits and License&lt;/span&gt; &lt;/p&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:10px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-style:italic;&quot;&gt;Advanced Previewer&lt;/span&gt; is copyright © 2016-2017 &lt;a href=&quot;https://github.com/Glutanimate&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Aristotelis P.&lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:10px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Licensed under the GNU AGPLv3. &lt;/p&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:10px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;This add-on was developed on a commission. If you have an idea for an add-on or a new feature, feel free to reach out to me on &lt;a href=&quot;https://twitter.com/glutanimate&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Twitter&lt;/span&gt;&lt;/a&gt; or at ankiglutanimate [αt] gmail . com.&lt;/p&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:10px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;This add-on was developed on a commission. If you'd like to hire my services to work an add-on or new feature, feel free to reach out to me on &lt;a href=&quot;https://twitter.com/glutanimate&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Twitter&lt;/span&gt;&lt;/a&gt; or at ankiglutanimate [αt] gmail . com.&lt;/p&gt;
&lt;p style=&quot; margin-top:14px; margin-bottom:12px; margin-left:10px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;More Information&lt;/span&gt; &lt;/p&gt;
&lt;ul style=&quot;margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 0;&quot;&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:10px; margin-left:35px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;a href=&quot;https://github.com/Glutanimate/advanced-previewer&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Project page on GitHub&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li style=&quot; margin-top:12px; margin-bottom:10px; margin-left:35px; margin-right:10px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;a href=&quot;https://ankiweb.net/shared/info/544521385&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Add-on description on AnkiWeb&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</string>
Expand Down
2 changes: 1 addition & 1 deletion docs/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ When reviewing cards in advance, please be mindful of the following:

This add-on was originally developed on a commission by [BB on the Anki support forums](https://anki.tenderapp.com/discussions/add-ons/8504-100-for-add-on-developer). The card reviewing feature introduced in the latest update was commissioned by another fellow Anki user who would like to remain anonymous. I would like to thank both of them for their great ideas and generous support in writing this add-on.

I'm always happy for new add-on commissions. If you have an idea for an add-on or new feature, please feel free to reach out to me on [Twitter](https://twitter.com/glutanimate), or at <em>ankiglutanimate [αt] gmail . com</em>
I'm always happy for new add-on commissions. If you'd like to hire my services to work an add-on or new feature, please feel free to reach out to me on [Twitter](https://twitter.com/glutanimate), or at <em>ankiglutanimate [αt] gmail . com</em>

Licensed under the [GNU AGPL v3](https://www.gnu.org/licenses/agpl.html). The source code for this add-on is available on [GitHub](https://github.com/Glutanimate/advanced-previewer).

0 comments on commit 3dced4f

Please sign in to comment.