-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathVersionDiffDialog.razor
More file actions
398 lines (358 loc) · 16.8 KB
/
Copy pathVersionDiffDialog.razor
File metadata and controls
398 lines (358 loc) · 16.8 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
@using DiffPlex
@using DiffPlex.DiffBuilder
@using DiffPlex.DiffBuilder.Model
@using LinkDotNet.Blog.Domain
@using System.Collections.Generic
@using System.Net
@using System.Text
<div class="modal @modalClass overflow-auto" tabindex="-1" role="dialog" style="display:@modalDisplay;">
<div class="modal-dialog modal-xl modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header py-2">
<div>
<h5 class="modal-title mb-0">Version Diff</h5>
@if (version is not null)
{
<small class="text-muted">
v@(version.VersionNumber)
<span class="opacity-50">(@version.CreatedAt.ToLocalTime().ToString("MMM dd, yyyy HH:mm"))</span>
→ Current
</small>
}
</div>
<button type="button" class="btn-close ms-3" @onclick="Close" aria-label="Close"></button>
</div>
@if (version is not null && current is not null)
{
<div class="modal-body p-0 d-flex flex-column" style="min-height: 0;">
<ul class="nav nav-tabs bg-light border-bottom px-3 pt-2 flex-shrink-0">
<li class="nav-item">
<button type="button" class="nav-link @(activeTab == DiffTab.Fields ? "active" : "")"
@onclick='() => activeTab = DiffTab.Fields'>
Fields
@if (changedFieldCount > 0)
{
<span class="badge rounded-pill bg-warning text-dark ms-1" style="font-size: 0.7rem;">
@changedFieldCount changed
</span>
}
</button>
</li>
<li class="nav-item">
<button type="button" class="nav-link @(activeTab == DiffTab.Content ? "active" : "")"
@onclick='() => activeTab = DiffTab.Content'>
Content
@if (insertedLines > 0 || deletedLines > 0)
{
<span class="badge rounded-pill bg-success ms-1" style="font-size: 0.7rem;">+@insertedLines</span>
<span class="badge rounded-pill bg-danger ms-1" style="font-size: 0.7rem;">-@deletedLines</span>
}
</button>
</li>
</ul>
@if (activeTab == DiffTab.Fields)
{
<div class="overflow-auto p-3 flex-grow-1" style="min-height: 0;">
<table class="table table-sm table-bordered align-middle mb-0">
<thead class="table-light sticky-top">
<tr>
<th style="width: 14%">Field</th>
<th style="width: 43%">
v@(version.VersionNumber)
<small class="text-muted fw-normal ms-1">
@version.CreatedAt.ToLocalTime().ToString("MMM dd HH:mm")
</small>
</th>
<th style="width: 43%">Current</th>
</tr>
</thead>
<tbody>
@FieldRow("Title", version.Title, current.Title)
@FieldRow("Author", version.AuthorName ?? "", current.AuthorName ?? "")
@FieldRow("Short Description", version.ShortDescription, current.ShortDescription)
@FieldRow("Image URL", version.PreviewImageUrl, current.PreviewImageUrl)
@FieldRow("Fallback Image URL", version.PreviewImageUrlFallback ?? "", current.PreviewImageUrlFallback ?? "")
@FieldRow("Tags", version.TagsAsString, current.TagsAsString)
@FieldRow("Published", version.IsPublished ? "Yes" : "No", current.IsPublished ? "Yes" : "No")
@FieldRow("Updated Date",
version.UpdatedDate.ToString("yyyy-MM-dd HH:mm:ss"),
current.UpdatedDate.ToString("yyyy-MM-dd HH:mm:ss"))
</tbody>
</table>
</div>
}
@if (activeTab == DiffTab.Content)
{
<div class="overflow-auto p-3 flex-grow-1" style="min-height: 0;">
@if (contentLines.Count > 0)
{
<div class="mb-2 d-flex gap-2 align-items-center flex-wrap">
<span class="badge bg-success">+@insertedLines added</span>
<span class="badge bg-danger">-@deletedLines removed</span>
<span class="text-muted small">@unchangedLines unchanged lines</span>
</div>
<div class="border rounded" style="font-family: 'SFMono-Regular', Consolas, monospace; font-size: 0.8rem; overflow-x: auto;">
@foreach (var line in contentLines)
{
@if (line.IsCollapse)
{
<div class="d-flex align-items-center py-1 px-2 text-muted"
style="background: #f6f8fa; border-top: 1px solid #e1e4e8; user-select: none;">
<span style="min-width: 3.5em;" class="me-1 opacity-50 text-end">···</span>
<span style="min-width: 3.5em;" class="me-2 opacity-50 text-end">···</span>
<span class="ms-1 small fst-italic">@line.CollapseCount unchanged lines</span>
</div>
}
else
{
<div class="d-flex @line.CssClass"
style="line-height: 1.6; border-top: 1px solid rgba(0,0,0,0.04);">
<span class="text-end px-2 flex-shrink-0 border-end"
style="min-width: 3.5em; color: #767676; background: rgba(0,0,0,0.03); user-select: none;">
@line.OldNum
</span>
<span class="text-end px-2 flex-shrink-0 border-end"
style="min-width: 3.5em; color: #767676; background: rgba(0,0,0,0.03); user-select: none;">
@line.NewNum
</span>
<span class="px-2 flex-shrink-0 fw-bold"
style="min-width: 1.5em; user-select: none; opacity: 0.7;">
@line.Prefix
</span>
<span class="px-1 flex-grow-1"
style="white-space: pre-wrap; word-break: break-all; padding-top: 1px;">
@line.Text
</span>
</div>
}
}
</div>
}
else
{
<div class="alert alert-success mb-0">Content is identical.</div>
}
</div>
}
</div>
}
<div class="modal-footer py-2">
<button type="button" class="btn btn-secondary btn-sm" @onclick="Close">Close</button>
</div>
</div>
</div>
</div>
@if (showBackdrop)
{
<div class="modal-backdrop fade show"></div>
}
@code {
private sealed record DiffDisplayLine(
string OldNum,
string NewNum,
string Prefix,
string Text,
string CssClass,
bool IsCollapse = false,
int CollapseCount = 0);
private enum DiffTab { Fields, Content }
private const int ContextLines = 3;
private string modalDisplay = "none;";
private string modalClass = string.Empty;
private bool showBackdrop;
private DiffTab activeTab = DiffTab.Fields;
private BlogPostVersion? version;
private BlogPost? current;
private IReadOnlyList<DiffDisplayLine> contentLines = [];
private int insertedLines;
private int deletedLines;
private int unchangedLines;
private int changedFieldCount;
public void Open(BlogPostVersion v, BlogPost currentPost)
{
version = v;
current = currentPost;
activeTab = DiffTab.Fields;
Compute();
modalDisplay = "block;";
modalClass = "show";
showBackdrop = true;
StateHasChanged();
}
public void Close()
{
modalDisplay = "none;";
modalClass = string.Empty;
showBackdrop = false;
StateHasChanged();
}
private void Compute()
{
if (version is null || current is null)
{
return;
}
changedFieldCount = CountChangedFields();
contentLines = BuildContentDiff(version.Content, current.Content);
insertedLines = contentLines.Count(l => l.Prefix == "+");
deletedLines = contentLines.Count(l => l.Prefix == "-");
unchangedLines = contentLines.Count(l => l.Prefix == " " && !l.IsCollapse);
}
private int CountChangedFields()
{
var count = 0;
if (version!.Title != current!.Title) count++;
if ((version.AuthorName ?? "") != (current.AuthorName ?? "")) count++;
if (version.ShortDescription != current.ShortDescription) count++;
if (version.PreviewImageUrl != current.PreviewImageUrl) count++;
if ((version.PreviewImageUrlFallback ?? "") != (current.PreviewImageUrlFallback ?? "")) count++;
if (version.TagsAsString != current.TagsAsString) count++;
if (version.IsPublished != current.IsPublished) count++;
if (version.UpdatedDate != current.UpdatedDate) count++;
return count;
}
private static IReadOnlyList<DiffDisplayLine> BuildContentDiff(string oldText, string newText)
{
var rawLines = InlineDiffBuilder.Diff(oldText, newText).Lines;
var visible = new bool[rawLines.Count];
for (var i = 0; i < rawLines.Count; i++)
{
if (rawLines[i].Type == ChangeType.Unchanged)
{
continue;
}
var from = Math.Max(0, i - ContextLines);
var to = Math.Min(rawLines.Count - 1, i + ContextLines);
for (var j = from; j <= to; j++)
{
visible[j] = true;
}
}
var result = new List<DiffDisplayLine>(rawLines.Count);
var oldLine = 0;
var newLine = 0;
var pendingCollapse = 0;
for (var i = 0; i < rawLines.Count; i++)
{
var piece = rawLines[i];
if (!visible[i] && piece.Type == ChangeType.Unchanged)
{
oldLine++;
newLine++;
pendingCollapse++;
continue;
}
if (pendingCollapse > 0)
{
result.Add(new DiffDisplayLine("", "", " ", "", "", IsCollapse: true, CollapseCount: pendingCollapse));
pendingCollapse = 0;
}
string oldNum, newNum, prefix, cssClass;
switch (piece.Type)
{
case ChangeType.Deleted:
oldLine++;
oldNum = oldLine.ToString();
newNum = "";
prefix = "-";
cssClass = "bg-danger-subtle";
break;
case ChangeType.Inserted:
newLine++;
oldNum = "";
newNum = newLine.ToString();
prefix = "+";
cssClass = "bg-success-subtle";
break;
case ChangeType.Modified:
oldLine++;
newLine++;
oldNum = oldLine.ToString();
newNum = newLine.ToString();
prefix = "~";
cssClass = "bg-warning-subtle";
break;
default:
if (piece.Type == ChangeType.Unchanged)
{
oldLine++;
newLine++;
}
oldNum = piece.Type == ChangeType.Unchanged ? oldLine.ToString() : "";
newNum = piece.Type == ChangeType.Unchanged ? newLine.ToString() : "";
prefix = " ";
cssClass = "";
break;
}
result.Add(new DiffDisplayLine(oldNum, newNum, prefix, piece.Text ?? "", cssClass));
}
if (pendingCollapse > 0)
{
result.Add(new DiffDisplayLine("", "", " ", "", "", IsCollapse: true, CollapseCount: pendingCollapse));
}
return result;
}
private static RenderFragment FieldRow(string fieldName, string old, string @new) => builder =>
{
var changed = old != @new;
builder.OpenElement(0, "tr");
builder.OpenElement(1, "td");
builder.AddAttribute(2, "class", "fw-semibold text-nowrap align-top");
builder.AddContent(3, fieldName);
builder.CloseElement();
builder.OpenElement(4, "td");
builder.AddAttribute(5, "class", changed ? "align-top bg-danger-subtle" : "align-top");
builder.AddMarkupContent(6, BuildWordDiff(old, @new, isOldSide: true).Value);
builder.CloseElement();
builder.OpenElement(7, "td");
builder.AddAttribute(8, "class", changed ? "align-top bg-success-subtle" : "align-top");
builder.AddMarkupContent(9, BuildWordDiff(old, @new, isOldSide: false).Value);
builder.CloseElement();
builder.CloseElement();
};
private static MarkupString BuildWordDiff(string old, string @new, bool isOldSide)
{
if (old == @new)
{
return new MarkupString(WebUtility.HtmlEncode(isOldSide ? old : @new));
}
var differ = new Differ();
var diff = differ.CreateWordDiffs(old, @new, ignoreWhitespace: false, [' ']);
var pieces = isOldSide ? diff.PiecesOld : diff.PiecesNew;
var changedIndices = new HashSet<int>();
foreach (var block in diff.DiffBlocks)
{
if (isOldSide)
{
for (var i = block.DeleteStartA; i < block.DeleteStartA + block.DeleteCountA; i++)
{
changedIndices.Add(i);
}
}
else
{
for (var i = block.InsertStartB; i < block.InsertStartB + block.InsertCountB; i++)
{
changedIndices.Add(i);
}
}
}
var sb = new StringBuilder();
for (var i = 0; i < pieces.Count; i++)
{
var encoded = WebUtility.HtmlEncode(pieces[i]);
if (changedIndices.Contains(i))
{
var style = isOldSide
? "background:#ffcdd2; border-radius:2px; padding:0 2px;"
: "background:#c8e6c9; border-radius:2px; padding:0 2px;";
sb.Append($"<mark style=\"{style}\">{encoded}</mark>");
}
else
{
sb.Append(encoded);
}
}
return new MarkupString(sb.ToString());
}
}