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 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 = dmp.diff_main(OldText, NewText); //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) { // se fosse testo vuoto --> metto spazio! if (string.IsNullOrEmpty(item.text)) { item.text = " "; } switch (item.operation) { case Operation.DELETE: sbOld.Append($"{HttpUtility.HtmlEncode(item.text)}"); numChanges++; break; case Operation.INSERT: sbNew.Append($"{HttpUtility.HtmlEncode(item.text)}"); numChanges++; break; case Operation.EQUAL: sbNew.Append($"{HttpUtility.HtmlEncode(item.text)}"); sbOld.Append($"{HttpUtility.HtmlEncode(item.text)}"); 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 = "
"; #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 } }