417aaa9d28
- fix gestione display versioni fix update in selezione
146 lines
4.0 KiB
C#
146 lines
4.0 KiB
C#
using DiffMatchPatch;
|
|
using Microsoft.AspNetCore.Components;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace MP.Prog.Components
|
|
{
|
|
public partial class DiffView
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public bool CompareVers { get; set; } = false;
|
|
|
|
[Parameter]
|
|
public EventCallback<int> diffDone { get; set; }
|
|
|
|
[Parameter]
|
|
public string NewText
|
|
{
|
|
get => _newText;
|
|
set => _newText = value;
|
|
}
|
|
|
|
[Parameter]
|
|
public string NewTitle { get; set; } = "Attuale";
|
|
|
|
[Parameter]
|
|
public string OldText
|
|
{
|
|
get => _oldText;
|
|
set => _oldText = value;
|
|
}
|
|
|
|
[Parameter]
|
|
public string OldTitle { get; set; } = "Archivio";
|
|
|
|
#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)
|
|
{
|
|
// se fosse testo vuoto --> metto spazio!
|
|
if (string.IsNullOrEmpty(item.text))
|
|
{
|
|
item.text = " ";
|
|
}
|
|
switch (item.operation)
|
|
{
|
|
case Operation.DELETE:
|
|
sbOld.Append($"<span class=\"border border-success bg-success bg-opacity-25\">{HttpUtility.HtmlEncode(item.text)}</span>");
|
|
numChanges++;
|
|
break;
|
|
|
|
case Operation.INSERT:
|
|
sbNew.Append($"<span class=\"border border-danger bg-danger bg-opacity-25\">{HttpUtility.HtmlEncode(item.text)}</span>");
|
|
numChanges++;
|
|
break;
|
|
|
|
case Operation.EQUAL:
|
|
sbNew.Append($"<span class=\"text-dark\">{HttpUtility.HtmlEncode(item.text)}</span>");
|
|
sbOld.Append($"<span class=\"text-dark\">{HttpUtility.HtmlEncode(item.text)}</span>");
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
newResult = sbNew.ToString();
|
|
oldResult = sbOld.ToString();
|
|
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 MarkupString StrFix(string origVal)
|
|
{
|
|
string fixVal = origVal.Trim()
|
|
.Replace(" ", " ")
|
|
.Replace(Environment.NewLine, sepDest);
|
|
return new MarkupString(fixVal);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |