|
6 | 6 | using System.Windows.Controls;
|
7 | 7 | using MadsKristensen.EditorExtensions.Settings;
|
8 | 8 | using Microsoft.VisualStudio.Text;
|
| 9 | +using mshtml; |
9 | 10 |
|
10 | 11 | namespace MadsKristensen.EditorExtensions.Markdown
|
11 | 12 | {
|
12 | 13 | internal class MarkdownMargin : CompilingMarginBase
|
13 | 14 | {
|
| 15 | + private HTMLDocument _document; |
14 | 16 | private WebBrowser _browser;
|
15 | 17 | private const string _stylesheet = "WE-Markdown.css";
|
| 18 | + private double _cachedPosition = 0, |
| 19 | + _cachedHeight = 0, |
| 20 | + _positionPercentage = 0; |
16 | 21 |
|
17 | 22 | public MarkdownMargin(ITextDocument document)
|
18 | 23 | : base(WESettings.Instance.Markdown, document)
|
19 | 24 | { }
|
20 | 25 |
|
21 |
| - |
22 | 26 | public static string GetStylesheet()
|
23 | 27 | {
|
24 | 28 | string file = GetCustomStylesheetFilePath();
|
@@ -62,13 +66,31 @@ protected override void UpdateMargin(CompilerResult result)
|
62 | 66 | </head>
|
63 | 67 | <body>{1}</body></html>", GetStylesheet(), result.Result);
|
64 | 68 |
|
| 69 | + if (_document == null) |
| 70 | + { |
| 71 | + _browser.NavigateToString(html); |
| 72 | + |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + _cachedPosition = _document.documentElement.getAttribute("scrollTop"); |
| 77 | + _cachedHeight = Math.Max(1.0, _document.body.offsetHeight); |
| 78 | + _positionPercentage = _cachedPosition * 100 / _cachedHeight; |
| 79 | + |
65 | 80 | _browser.NavigateToString(html);
|
66 | 81 | }
|
67 | 82 |
|
68 | 83 | protected override FrameworkElement CreatePreviewControl()
|
69 | 84 | {
|
70 | 85 | _browser = new WebBrowser();
|
71 | 86 | _browser.HorizontalAlignment = HorizontalAlignment.Stretch;
|
| 87 | + _browser.Navigated += (s, e) => |
| 88 | + { |
| 89 | + _document = _browser.Document as HTMLDocument; |
| 90 | + _cachedHeight = _document.body.offsetHeight; |
| 91 | + _document.documentElement.setAttribute("scrollTop", _positionPercentage * _cachedHeight / 100); |
| 92 | + }; |
| 93 | + |
72 | 94 | return _browser;
|
73 | 95 | }
|
74 | 96 | }
|
|
0 commit comments