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 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 = 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}"); numChanges++; 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(); } #endregion Protected Methods #region Private Fields private string sepDest = "
"; #endregion Private Fields } }