@using MP.Land.Data
@using System.Text
@using DiffMatchPatch
@if (numChanges > 0)
{
Attuale
@numChanges modifiche
}
@((MarkupString)oldResult)
@if (numChanges > 0)
{
@((MarkupString)newResult)
}
@code {
string sepDest = "
";
protected int pHeight = 25;
protected string oldResult = "";
protected string newResult = "";
protected string _oldText = "";
protected string _newText = "";
[Parameter]
public EventCallback diffDone { get; set; }
protected int numChanges { get; set; } = 0;
[Parameter]
public string oldText
{
get
{
return _oldText;
}
set
{
_oldText = value;
}
}
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 = 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($"{item.text}");
break;
case Operation.INSERT:
sbNew.Append($"{item.text}");
numChanges++;
break;
case Operation.EQUAL:
sbNew.Append($"{item.text}");
sbOld.Append($"{item.text}");
break;
default:
break;
}
}
newResult = sbNew.ToString().Trim();
oldResult = sbOld.ToString().Trim();
var pUpd = Task.Run(async () =>
{
await diffDone.InvokeAsync(numChanges);
});
pUpd.Wait();
}
protected override Task OnInitializedAsync()
{
ReloadData();
return base.OnInitializedAsync();
}
}