Files
2023-09-11 14:20:29 +02:00

136 lines
3.5 KiB
C#

using DiffMatchPatch;
using global::Microsoft.AspNetCore.Components;
using System.Text;
namespace WebDoorCreator.UI.Components.FilesMan
{
public partial class DiffView
{
#region Public Properties
[Parameter]
public EventCallback<int> diffDone { get; set; }
[Parameter]
public string newText
{
get
{
return _newText;
}
set
{
_newText = value;
ReloadData();
}
}
[Parameter]
public string oldText
{
get
{
return _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 = 45;
#endregion Protected Fields
#region Protected Properties
protected string newTextFix
{
get
{
return _newText.Replace(Environment.NewLine, sepDest).Replace("\n", sepDest).Replace("\r", sepDest);
}
}
protected int numChanges { get; set; } = 0;
protected string oldTextFix
{
get
{
return _oldText.Replace(Environment.NewLine, sepDest).Replace("\n", sepDest).Replace("\r", sepDest);
}
}
#endregion Protected Properties
#region Protected Methods
protected override Task OnInitializedAsync()
{
ReloadData();
return base.OnInitializedAsync();
}
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>");
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
}
}