5cea4857dd
- fix display size - fix display status - review modulo DiffView
128 lines
3.4 KiB
C#
128 lines
3.4 KiB
C#
using DiffMatchPatch;
|
|
using Microsoft.AspNetCore.Components;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Prog.Components
|
|
{
|
|
public partial class DiffView
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public EventCallback<int> diffDone { get; set; }
|
|
|
|
[Parameter]
|
|
public string newText
|
|
{
|
|
get => _newText;
|
|
set => _newText = value;
|
|
}
|
|
|
|
[Parameter]
|
|
public string oldText
|
|
{
|
|
get => _oldText;
|
|
set => _oldText = value;
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Fields
|
|
|
|
protected string _newText = "";
|
|
|
|
protected string _oldText = "";
|
|
|
|
protected string newResult = "";
|
|
|
|
protected string oldResult = "";
|
|
|
|
protected int pHeight = 25;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
protected int numChanges { get; set; } = 0;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected override Task OnInitializedAsync()
|
|
{
|
|
ReloadData();
|
|
return base.OnInitializedAsync();
|
|
}
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
ReloadData();
|
|
}
|
|
|
|
protected void ReloadData()
|
|
{
|
|
numChanges = 0;
|
|
// calcolo diff
|
|
diff_match_patch dmp = new diff_match_patch();
|
|
List<Diff> diff = dmp.diff_main(oldText, newText);
|
|
//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>");
|
|
numChanges++;
|
|
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();
|
|
var pUpd = Task.Run(async () =>
|
|
{
|
|
await diffDone.InvokeAsync(numChanges);
|
|
});
|
|
pUpd.Wait();
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private string sepDest = "<br />";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
private string StrFix(string origVal)
|
|
{
|
|
return origVal.Replace(" ", " ").Replace(Environment.NewLine, sepDest).Replace("\n", sepDest).Replace("\r", sepDest);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |