Skip to content

Commit

Permalink
Added to remember scroll when reloading files. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
TakamiChie committed Mar 12, 2019
1 parent f569c94 commit f12471a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions html/frame.js
@@ -0,0 +1,9 @@
document.addEventListener("DOMContentLoaded", function (e) {
let h = location.hash;
if(h.substr(0, 4) == "#top"){
window.scroll(0, parseInt(h.substring(4)));
}
})
document.addEventListener("scroll", function (e) {
window.parent.postMessage(document.documentElement.scrollTop, "*");
})
6 changes: 6 additions & 0 deletions html/main.js
Expand Up @@ -4,4 +4,10 @@ document.getElementById("review-file").addEventListener("change", function(e) {

document.getElementById("refresh_view").addEventListener("click", function(e) {
pywebview.api.show_review();
})

var pos = 0;

window.addEventListener("message", function (e) {
pos = e.data;
})
5 changes: 3 additions & 2 deletions src/js_api.py
Expand Up @@ -114,8 +114,9 @@ def show_review(self, filename = None):
it is considered to be a relative path from review_dir.
If the argument is none, only the current file is reloaded.
"""
pos = 0
if filename is None:
pass
pos = webview.evaluate_js("pos")
elif Path(filename).is_absolute():
self._review_file = filename
else:
Expand All @@ -125,7 +126,7 @@ def show_review(self, filename = None):
previewhtml = self._review_file.parent / "preview.html"
with open(previewhtml, mode="w", encoding="utf-8") as f:
f.write(reviewtxt.replace("</body>", "<script src='{0}'></script></body>".format(path_to_url(mypath() / "html" / "frame.js"))))
self._comm.frameurl = path_to_url(previewhtml) + "#{0}".format(self._review_file.stem)
self._comm.frameurl = path_to_url(previewhtml) + "?{0}#top{1}".format(self._review_file.stem, pos)

def path_to_url(path):
"""
Expand Down

0 comments on commit f12471a

Please sign in to comment.