From 49367cc3a6510c71ea318443817b5eb4f86e4290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Strau=C3=9F?= Date: Sun, 31 Mar 2024 15:57:57 +0200 Subject: [PATCH] Implemented simple DiffViewer with example. --- .../Components/Layout/NavMenu.razor | 1 + .../Components/Pages/DiffViewerExample.razor | 31 +++++ .../Components/Pages/Home.razor | 2 +- .../DiffViewer/DiffViewer.razor | 111 +++++++++++++----- 4 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 samples/DotNetElements.CrudExample/Components/Pages/DiffViewerExample.razor diff --git a/samples/DotNetElements.CrudExample/Components/Layout/NavMenu.razor b/samples/DotNetElements.CrudExample/Components/Layout/NavMenu.razor index c948f8e..168cdc8 100644 --- a/samples/DotNetElements.CrudExample/Components/Layout/NavMenu.razor +++ b/samples/DotNetElements.CrudExample/Components/Layout/NavMenu.razor @@ -1,6 +1,7 @@  Home CRUD + Diff Viewer @* Generic CRUD *@ diff --git a/samples/DotNetElements.CrudExample/Components/Pages/DiffViewerExample.razor b/samples/DotNetElements.CrudExample/Components/Pages/DiffViewerExample.razor new file mode 100644 index 0000000..b02ee77 --- /dev/null +++ b/samples/DotNetElements.CrudExample/Components/Pages/DiffViewerExample.razor @@ -0,0 +1,31 @@ +@page "/diffViewer" +@using DotNetElements.Core.StringDiff +@using DotNetElements.Web.Blazor.DiffViewer + +Diff viewer example + +

Diff viewer example

+ + + +Show Diff + +Diff Result + + +@code +{ + private SideBySideDiffModel? diff; + + private string? textA; + private string? textB; + + private void OnShowDiff() + { + if (string.IsNullOrWhiteSpace(textA) || string.IsNullOrWhiteSpace(textB)) + return; + + SideBySideDiffBuilder diffBuilder = new(); + diff = diffBuilder.Diff(textA, textB); + } +} diff --git a/samples/DotNetElements.CrudExample/Components/Pages/Home.razor b/samples/DotNetElements.CrudExample/Components/Pages/Home.razor index 9001e0b..dfcdf75 100644 --- a/samples/DotNetElements.CrudExample/Components/Pages/Home.razor +++ b/samples/DotNetElements.CrudExample/Components/Pages/Home.razor @@ -4,4 +4,4 @@

Hello, world!

-Welcome to your new app. +Welcome to your new app. \ No newline at end of file diff --git a/src/DotNetElements.Web.Blazor/DiffViewer/DiffViewer.razor b/src/DotNetElements.Web.Blazor/DiffViewer/DiffViewer.razor index 4ad9e7f..508ea42 100644 --- a/src/DotNetElements.Web.Blazor/DiffViewer/DiffViewer.razor +++ b/src/DotNetElements.Web.Blazor/DiffViewer/DiffViewer.razor @@ -1,45 +1,92 @@ @using DotNetElements.Core.StringDiff -@if (Diff?.HasDifferences is true) +@if (Diff?.OldText.HasDifferences is true || Diff?.NewText.HasDifferences is true) { - +
- @foreach (DiffPiece diffLine in Diff.Lines) - { - - - - - } -
- @(diffLine.Position?.ToString() ?? " "); - - - @diffLine.Text - -
+ @foreach ((DiffPiece oldPiece, DiffPiece newPiece) in Diff.OldText.Lines.Zip(Diff.NewText.Lines)) + { + + + @(oldPiece.Position?.ToString() ?? newPiece.Position?.ToString() ?? ""); + + + @if (oldPiece.Type is ChangeType.Unchanged) + { + + @oldPiece.Text + + } + else + { + @if (oldPiece.SubPieces.Count > 0) + { + @foreach (DiffPiece oldSubPiece in oldPiece.SubPieces) + { + + @oldSubPiece.Text + + } + } + else + { + + @oldPiece.Text + + } + } + + + + @if (newPiece.Type is not ChangeType.Unchanged) + { + + + @(newPiece.Position?.ToString() ?? ""); + + + @if (newPiece.SubPieces.Count > 0) + { + @foreach (DiffPiece newSubPiece in newPiece.SubPieces) + { + + @newSubPiece.Text + + } + } + else + { + + @newPiece.Text + + } + + + } + } + } else { - No differences found + No differences found } @code { - [Parameter, EditorRequired] - public InlineDiffModel Diff { get; set; } = default!; + [Parameter, EditorRequired] + public SideBySideDiffModel Diff { get; set; } = default!; - // todo tmp - private string GetDiffLineStyle(DiffPiece diffLine) - { - return diffLine.Type switch - { - ChangeType.Unchanged => "", - ChangeType.Deleted => "background-color: red;", - ChangeType.Inserted => "background-color: green;", - ChangeType.Imaginary => "background-color: gray;", - ChangeType.Modified => "background-color: yellow;", - _ => throw new NotImplementedException(nameof(diffLine.Type)), - }; - } + // todo tmp + private string GetDiffStyle(DiffPiece diffLine) + { + return diffLine.Type switch + { + ChangeType.Unchanged => "", + ChangeType.Deleted => "background-color: #FFB6BA; border-radius: 10%; line-height: 1.5rem; padding: 1px 2px;", + ChangeType.Inserted => "background-color: #97F295; border-radius: 10%; line-height: 1.5rem; padding: 1px 2px;", + ChangeType.Imaginary => "background-color: #F8FAFD; border-radius: 10%; line-height: 1.5rem; padding: 1px 2px;", + ChangeType.Modified => "background-color: #FFB6BA; border-radius: 10%; line-height: 1.5rem; padding: 1px 2px;", + _ => throw new NotImplementedException(nameof(diffLine.Type)), + }; + } }