-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTedApp.MarkdownPreview.cs
More file actions
257 lines (214 loc) · 8.11 KB
/
Copy pathTedApp.MarkdownPreview.cs
File metadata and controls
257 lines (214 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Terminal.Gui.Drawing;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
namespace Ted;
public sealed partial class TedApp
{
private MarkdownPreview? _markdownPreview;
private bool _syncingScroll;
/// <summary>Toggle state used by the View menu item that shows or hides the Markdown preview pane.</summary>
public CheckBox PreviewCheckBox { get; } = new ()
{
AllowCheckStateNone = false,
CanFocus = false,
Text = "_Preview",
Value = CheckState.UnChecked,
Visible = false
};
/// <summary>Gets whether the current file is a Markdown file.</summary>
private bool IsMarkdownFile =>
CurrentFilePath is not null
&& Path.GetExtension (CurrentFilePath).Equals (".md", StringComparison.OrdinalIgnoreCase);
/// <summary>Shows or hides the <see cref="PreviewCheckBox" /> based on the current file extension.</summary>
internal void UpdatePreviewVisibility ()
{
var isMd = IsMarkdownFile;
PreviewCheckBox.Visible = isMd;
_previewMarkdownMenuItem.Enabled = isMd;
if (!isMd && _markdownPreview is not null)
{
HideMarkdownPreview ();
PreviewCheckBox.Value = CheckState.UnChecked;
}
else if (isMd && _markdownPreview is not null)
{
// Document was swapped while preview was open — refresh content and re-hook.
RefreshPreviewDocument ();
}
_previewMarkdownMenuItem.Title = ToggleTitle (
PreviewCheckBox.Value == CheckState.Checked,
"_Preview Markdown");
}
private void ToggleMarkdownPreview ()
{
if (PreviewCheckBox.Value == CheckState.Checked)
{
ShowMarkdownPreview ();
}
else
{
HideMarkdownPreview ();
}
}
private void ShowMarkdownPreview ()
{
if (_markdownPreview is not null)
{
return;
}
_markdownPreview = new MarkdownPreview
{
X = Pos.Right (Editor),
Y = Editor.Y,
Width = Dim.Fill (),
Height = Editor.Height,
Text = Editor.Document?.Text ?? string.Empty,
TotalSourceLines = Editor.Document?.LineCount ?? 1,
ViewportSettings = ViewportSettingsFlags.HasScrollBars,
SyntaxHighlighter = new TextMateSyntaxHighlighter ()
};
// Editor takes the left half, preview takes the right half.
Editor.Width = Dim.Percent (50);
Add (_markdownPreview);
// Sync scrolling bidirectionally.
Editor.ViewportChanged += OnEditorViewportChanged;
_markdownPreview.ViewportChanged += OnPreviewViewportChanged;
// Update preview when document content changes.
if (Editor.Document is not null)
{
Editor.Document.Changed += OnDocumentChangedForPreview;
}
// Track caret movement to highlight the corresponding line in the preview.
Editor.CaretChanged += OnEditorCaretChangedForPreview;
UpdatePreviewHighlight ();
// Click in preview navigates the editor caret.
_markdownPreview.SourceLineClicked += OnPreviewSourceLineClicked;
}
private void HideMarkdownPreview ()
{
if (_markdownPreview is null)
{
return;
}
Editor.ViewportChanged -= OnEditorViewportChanged;
_markdownPreview.ViewportChanged -= OnPreviewViewportChanged;
Editor.CaretChanged -= OnEditorCaretChangedForPreview;
_markdownPreview.SourceLineClicked -= OnPreviewSourceLineClicked;
if (Editor.Document is not null)
{
Editor.Document.Changed -= OnDocumentChangedForPreview;
}
Remove (_markdownPreview);
_markdownPreview.Dispose ();
_markdownPreview = null;
// Restore editor to full width.
Editor.Width = Dim.Fill ();
}
private void OnEditorViewportChanged (object? sender, DrawEventArgs e)
{
if (_markdownPreview is null || _syncingScroll)
{
return;
}
_syncingScroll = true;
try
{
// Synchronise vertical scroll position. The Markdown view's content size may differ
// from the editor's, so we use a proportional mapping.
var editorContentHeight = Editor.GetContentSize ().Height;
var editorViewportHeight = Editor.Viewport.Height;
var maxEditorY = Math.Max (0, editorContentHeight - editorViewportHeight);
var editorY = Editor.Viewport.Y;
var previewContentHeight = _markdownPreview.GetContentSize ().Height;
var previewViewportHeight = _markdownPreview.Viewport.Height;
var maxPreviewY = Math.Max (0, previewContentHeight - previewViewportHeight);
var newY = maxEditorY > 0
? (int)((long)editorY * maxPreviewY / maxEditorY)
: 0;
_markdownPreview.Viewport = _markdownPreview.Viewport with { Y = Math.Clamp (newY, 0, maxPreviewY) };
}
finally
{
_syncingScroll = false;
}
}
private void OnPreviewViewportChanged (object? sender, DrawEventArgs e)
{
if (_markdownPreview is null || _syncingScroll)
{
return;
}
_syncingScroll = true;
try
{
var previewContentHeight = _markdownPreview.GetContentSize ().Height;
var previewViewportHeight = _markdownPreview.Viewport.Height;
var maxPreviewY = Math.Max (0, previewContentHeight - previewViewportHeight);
var previewY = _markdownPreview.Viewport.Y;
var editorContentHeight = Editor.GetContentSize ().Height;
var editorViewportHeight = Editor.Viewport.Height;
var maxEditorY = Math.Max (0, editorContentHeight - editorViewportHeight);
var newY = maxPreviewY > 0
? (int)((long)previewY * maxEditorY / maxPreviewY)
: 0;
Editor.Viewport = Editor.Viewport with { Y = Math.Clamp (newY, 0, maxEditorY) };
}
finally
{
_syncingScroll = false;
}
}
private void OnDocumentChangedForPreview (object? sender, EventArgs e)
{
if (_markdownPreview is null)
{
return;
}
_markdownPreview.Text = Editor.Document?.Text ?? string.Empty;
_markdownPreview.TotalSourceLines = Editor.Document?.LineCount ?? 1;
}
private void OnEditorCaretChangedForPreview (object? sender, EventArgs e)
{
UpdatePreviewHighlight ();
}
private void UpdatePreviewHighlight ()
{
if (_markdownPreview is null || Editor.Document is null)
{
return;
}
var sourceLine = Editor.Document.GetLineByOffset (Editor.CaretOffset).LineNumber - 1;
_markdownPreview.HighlightSourceLine = sourceLine;
}
private void OnPreviewSourceLineClicked (object? sender, SourceLineClickedEventArgs e)
{
if (Editor.Document is null)
{
return;
}
var lineNumber = Math.Clamp (e.SourceLine + 1, 1, Editor.Document.LineCount);
Editor.CaretOffset = Editor.Document.GetLineByNumber (lineNumber).Offset;
Editor.SetFocus ();
}
/// <summary>
/// Re-hooks the document change handler and refreshes the preview content after a document swap.
/// </summary>
private void RefreshPreviewDocument ()
{
if (_markdownPreview is null)
{
return;
}
// Detach from the current document first to avoid duplicate subscriptions when
// RefreshPreviewDocument is called multiple times for the same document instance
// (e.g. Save As that keeps the .md extension).
if (Editor.Document is not null)
{
Editor.Document.Changed -= OnDocumentChangedForPreview;
Editor.Document.Changed += OnDocumentChangedForPreview;
}
_markdownPreview.Text = Editor.Document?.Text ?? string.Empty;
_markdownPreview.TotalSourceLines = Editor.Document?.LineCount ?? 1;
UpdatePreviewHighlight ();
}
}