preview window is scrolling according to cursor position in markdown buffer

This commit is contained in:
Igor Shymko 2014-10-23 02:14:12 +03:00
parent a9e85581f8
commit 0c53ded3dc
2 changed files with 32 additions and 14 deletions

View File

@ -95,6 +95,14 @@
(mdpm:sent-preview-to mdpm:local-client))
(defun mdpm:sent-preview-to (websocket)
(let ((mark-position-percent
(number-to-string
(truncate
(* 100
(/
(float (line-number-at-pos))
(count-lines (point-min) (point-max))))))))
(when markdown-preview-mode
(markdown markdown-output-buffer-name))
(with-current-buffer (get-buffer markdown-output-buffer-name)
@ -104,11 +112,14 @@
"<span id='style'>"
markdown-preview-style
"</span>"
"<span id='position-percentage'>"
mark-position-percent
"</span>"
"<div id='content'>"
(buffer-substring-no-properties (point-min) (point-max))
"</div>"
"</div>")
)))
))))
(defun mdpm:start ()
(mdpm:start-websocket-server)

View File

@ -5,6 +5,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
<title>Markdown preview</title>
<link href="http://thomasf.github.io/solarized-css/solarized-dark.min.css" rel="stylesheet"></link>
<style>
code, kbd, pre, samp {
line-height: 1.2em;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
(function($, undefined) {
@ -23,6 +28,8 @@
socket.onmessage = function(event) {
loadCSS($(event.data).find("#style").text());
$("#markdown-body").html($(event.data).find("#content").html());
var scroll = $(document).height() * ($(event.data).find("#position-percentage").html() / 100);
$("html, body").animate({ scrollTop: scroll }, 600);
};
socket.onerror = function(error) {
alert("Error: " + error.message);