Files
mapo-core/MP.Prog/Components/DiffView.razor
T

122 lines
3.1 KiB
Plaintext

@using MP.Prog.Data
@using System.Text
@using DiffMatchPatch
<div class="row">
<div class="col-6">
<h4 class="text-success">Archiviato</h4>
<div class="border border-success p-2">
@((MarkupString)oldResult)
</div>
</div>
<div class="col-6">
<div class="row">
<div class="col-4 text-danger">
<h4>Attuale</h4>
</div>
<div class="col-8 text-right">
<span class="border border-danger table-danger px-2"><b>@numChanges</b> modifiche</span>
</div>
</div>
<div class="border border-danger p-2">
<p>@((MarkupString)newResult)"></p>
</div>
</div>
</div>
@code {
string sepDest = "<br />";
protected string oldResult = "";
protected string newResult = "";
protected string _oldText = "";
protected string _newText = "";
protected int numChanges = 0;
[Parameter]
public string oldText
{
get
{
return _oldText;
}
set
{
_oldText = value;
ReloadData();
}
}
protected string oldTextFix
{
get
{
return _oldText.Replace(Environment.NewLine, sepDest).Replace("\n", sepDest).Replace("\r", sepDest);
}
}
protected string newTextFix
{
get
{
return _newText.Replace(Environment.NewLine, sepDest).Replace("\n", sepDest).Replace("\r", sepDest);
}
}
[Parameter]
public string newText
{
get
{
return _newText;
}
set
{
_newText = value;
ReloadData();
}
}
protected void ReloadData()
{
numChanges = 0;
// calcolo diff
diff_match_patch dmp = new diff_match_patch();
List<Diff> diff = dmp.diff_main(oldTextFix, newTextFix);
dmp.diff_cleanupSemantic(diff);
// predispongo la stringa secondo l'elenco dei diff....
StringBuilder sbNew = new StringBuilder();
StringBuilder sbOld = new StringBuilder();
foreach (var item in diff)
{
switch (item.operation)
{
case Operation.DELETE:
sbOld.Append($"<span class=\"border border-success table-success\">{item.text}</span>");
break;
case Operation.INSERT:
sbNew.Append($"<span class=\"border border-danger table-danger\">{item.text}</span>");
numChanges++;
break;
case Operation.EQUAL:
sbNew.Append($"<span class=\"text-dark\">{item.text}</span>");
sbOld.Append($"<span class=\"text-dark\">{item.text}</span>");
break;
default:
break;
}
}
newResult = sbNew.ToString().Trim();
oldResult = sbOld.ToString().Trim();
}
protected override Task OnInitializedAsync()
{
ReloadData();
return base.OnInitializedAsync();
}
}