-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTextDiffLine.razor
More file actions
103 lines (91 loc) · 2.84 KB
/
Copy pathTextDiffLine.razor
File metadata and controls
103 lines (91 loc) · 2.84 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
@using DiffPlex.DiffBuilder.Model;
@if (!string.IsNullOrEmpty(Model?.Text))
{
@if (Model.Type == ChangeType.Unchanged)
{
@Model.Text
}
else if (Model.SubPieces is not null && Model.SubPieces.Count > 0)
{
@foreach (var word in GetWords())
{
@if (!word.HasChanges)
{
@word.Text
}
else if (word.IsFullyChanged)
{
<span class="@word.CharacterCss">@word.Text</span>
}
else
{
<span class="@word.WordCss">@foreach (var ch in word.Characters)
{
@if (ch.IsChanged)
{<span class="@ch.CssClass">@ch.Text</span>}
else
{@ch.Text}
}</span>
}
}
}
else
{
@Model.Text
}
}
@code {
[Parameter, EditorRequired] public DiffPiece Model { get; set; } = default!;
private sealed record CharInfo(string Text, bool IsChanged, string CssClass);
private sealed record WordGroup(
string Text,
List<CharInfo> Characters,
bool HasChanges,
bool IsFullyChanged,
string CharacterCss,
string WordCss);
private List<WordGroup> GetWords()
{
var words = new List<WordGroup>();
var currentChars = new List<CharInfo>();
foreach (var sub in Model.SubPieces)
{
if (sub.Type == ChangeType.Imaginary) continue;
var text = sub.Text ?? "";
if (text.Length == 0) continue;
var isChanged = sub.Type != ChangeType.Unchanged;
var info = new CharInfo(text, isChanged, $"{sub.Type.ToString().ToLower()}-character");
if (text.All(char.IsWhiteSpace))
{
if (currentChars.Count > 0)
{
words.Add(BuildWord(currentChars));
currentChars = [];
}
words.Add(BuildWord([info]));
}
else
{
currentChars.Add(info);
}
}
if (currentChars.Count > 0)
words.Add(BuildWord(currentChars));
return words;
}
private static WordGroup BuildWord(List<CharInfo> chars)
{
var text = string.Concat(chars.Select(c => c.Text));
var changed = chars.Where(c => c.IsChanged).ToList();
var hasChanges = changed.Count > 0;
var isFullyChanged = changed.Count == chars.Count;
var dominantType = changed.FirstOrDefault()?.CssClass.Replace("-character", "") ?? "unchanged";
return new WordGroup(
text,
chars,
hasChanges,
isFullyChanged,
$"{dominantType}-character",
$"{dominantType}-word");
}
}