| | | 1 | | @using System.IO |
| | | 2 | | @using System.Text |
| | | 3 | | |
| | | 4 | | <div class="diff-view"> |
| | 0 | 5 | | @if (!string.IsNullOrEmpty(FilePath)) |
| | | 6 | | { |
| | 0 | 7 | | <div @key="@FilePath" class="tool-file-header">@FilePath</div> |
| | | 8 | | } |
| | | 9 | | |
| | | 10 | | <div class="diff-block"> |
| | | 11 | | <Virtualize Items="@_blocks" Context="block" ItemSize="16" >@* Обязательно все элементы 16px *@ |
| | | 12 | | <DiffLine Model="block" /> |
| | | 13 | | </Virtualize> |
| | | 14 | | </div> |
| | | 15 | | </div> |
| | | 16 | | |
| | | 17 | | @code { |
| | 0 | 18 | | [Parameter] public List<string> Lines { get; set; } = []; |
| | | 19 | | |
| | 0 | 20 | | private string? FilePath { get; set; } |
| | 0 | 21 | | private List<DiffBlock> _blocks = []; |
| | | 22 | | |
| | | 23 | | protected override void OnParametersSet() |
| | | 24 | | { |
| | 0 | 25 | | ParseDiff(); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | private void ParseDiff() |
| | | 29 | | { |
| | 0 | 30 | | bool inSearch = false; |
| | 0 | 31 | | bool inReplace = false; |
| | 0 | 32 | | int blockIndex = 0; |
| | | 33 | | |
| | | 34 | | // Читаем первую строку для FilePath |
| | 0 | 35 | | FilePath = Lines[0]; |
| | 0 | 36 | | foreach (var line in Lines) |
| | | 37 | | { |
| | 0 | 38 | | UpdateOrAddBlock(blockIndex++, line, ref inSearch, ref inReplace); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Удаляем лишние блоки, если текст сократился |
| | 0 | 42 | | if (_blocks.Count > blockIndex) |
| | 0 | 43 | | _blocks.RemoveRange(blockIndex, _blocks.Count - blockIndex); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private void UpdateOrAddBlock(int index, string line, ref bool inSearch, ref bool inReplace) |
| | | 47 | | { |
| | 0 | 48 | | var type = DetermineType(line, ref inSearch, ref inReplace); |
| | | 49 | | |
| | 0 | 50 | | if (index < _blocks.Count) |
| | | 51 | | { |
| | | 52 | | // ВАЖНО: Обновляем свойства существующего объекта. |
| | | 53 | | // Blazor увидит, что экземпляр тот же, и проверит только Content. |
| | 0 | 54 | | var block = _blocks[index]; |
| | 0 | 55 | | if (block.Type != type || block.Content.Length < line.Length) |
| | | 56 | | { |
| | 0 | 57 | | block.Type = type; |
| | 0 | 58 | | block.Content = line; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | else |
| | | 62 | | { |
| | 0 | 63 | | _blocks.Add(new DiffBlock { Type = type, Content = line }); |
| | | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | private DiffBlockType DetermineType(string line, ref bool inSearch, ref bool inReplace) |
| | | 68 | | { |
| | 0 | 69 | | var trimmed = line.Trim(); |
| | | 70 | | |
| | 0 | 71 | | if (trimmed.StartsWith("<<<<<<< SEARCH")) |
| | | 72 | | { |
| | 0 | 73 | | inSearch = true; |
| | 0 | 74 | | inReplace = false; // Сброс на всякий случай |
| | 0 | 75 | | return DiffBlockType.SearchHeader; |
| | | 76 | | } |
| | 0 | 77 | | if (trimmed.StartsWith("=======")) |
| | | 78 | | { |
| | 0 | 79 | | inSearch = false; |
| | 0 | 80 | | inReplace = true; |
| | 0 | 81 | | return DiffBlockType.Separator; |
| | | 82 | | } |
| | 0 | 83 | | if (trimmed.StartsWith(">>>>>>> REPLACE")) |
| | | 84 | | { |
| | 0 | 85 | | inSearch = false; |
| | 0 | 86 | | inReplace = false; |
| | 0 | 87 | | return DiffBlockType.ReplaceHeader; |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | if (inSearch) return DiffBlockType.Removed; |
| | 0 | 91 | | if (inReplace) return DiffBlockType.Added; |
| | | 92 | | |
| | 0 | 93 | | return DiffBlockType.Context; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public enum DiffBlockType |
| | | 97 | | { |
| | | 98 | | SearchHeader, |
| | | 99 | | Separator, |
| | | 100 | | ReplaceHeader, |
| | | 101 | | Removed, |
| | | 102 | | Added, |
| | | 103 | | Context |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public class DiffBlock |
| | | 107 | | { |
| | 0 | 108 | | public DiffBlockType Type { get; set; } |
| | 0 | 109 | | public string Content { get; set; } = string.Empty; |
| | | 110 | | } |
| | | 111 | | } |