using Microsoft.Office.Tools.Ribbon; using System; using System.Diagnostics; using AIProofread.Controls; using UtilLib; using System.Drawing; using System.Security.Cryptography; using Microsoft.Office.Interop.Word; using System.IO; using System.Threading.Tasks; using AIProofread.Util; using Microsoft.Office.Tools.Word; using System.Collections.Generic; using System.Linq; using System.Text; using AIProofread.core; using System.Windows.Forms; namespace AIProofread { public partial class Ribbon1 { private bool IS_LOGIN = false; private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { //btnLogin.Label = "用户\n登录"; //btnLogout.Label = "退出\n登录"; Globals.ThisAddIn.ribbon = this; ShowDebug(); } /// /// 用于调试或测试 /// public void ShowDebug() { if (Config.RUN_IN_DEBUG) { grpDebug.Visible = true; } } public void InitWPS() { BtnProofreadAll.Image = AIProofread.Properties.Resources.icon_proofread_wps; btnClear.Image = AIProofread.Properties.Resources.icon_clear_wps; btnOpenLexicon.Image = AIProofread.Properties.Resources.icon_book_wps; btnSetting.Image = AIProofread.Properties.Resources.icon_setting_wps; BtnGetContact.Image = AIProofread.Properties.Resources.icon_phone_wps; BtnUpdate.Image = AIProofread.Properties.Resources.icon_update_wps; btnLogin.Image = AIProofread.Properties.Resources.icon_user_wps; btnLogout.Image = AIProofread.Properties.Resources.icon_logout_wps; } public void ProcessLoginInfo(Userinfo userinfo) { if (userinfo == null) return; IS_LOGIN = true; ToggleLogin(); LblNickname.Label = userinfo.nickname; LblDate.Label = userinfo.expiration; // 关闭所有登录窗口 foreach (var item in Globals.ThisAddIn.LoginFormList) { if (!item.IsDisposed) { item.Close(); } } } public void ProcessLogout() { IS_LOGIN = false; ToggleLogin(); } // 处理是否登录的展示 private void ToggleLogin() { btnLogin.Visible = !IS_LOGIN; btnLogout.Visible = IS_LOGIN; //LblNickname.Visible = IS_LOGIN; //LblDate.Visible = IS_LOGIN; } // 弹出登录窗口 private void btnLogin_Click(object sender, RibbonControlEventArgs e) { Bridge.bridge.ShowLoginForm(null); } // 注销登录 private void btnLogout_Click(object sender, RibbonControlEventArgs e) { // 注销吧 Bridge.bridge.Logout(null); Globals.ThisAddIn.HideAllPanel(); } private void btnOpenLexicon_Click(object sender, RibbonControlEventArgs e) { Globals.ThisAddIn.SendMessageToWeb("show-lexicon", null); } private void btnSetting_Click(object sender, RibbonControlEventArgs e) { Globals.ThisAddIn.SendMessageToWeb("show-setting", null); } private void BtnGetContact_Click(object sender, RibbonControlEventArgs e) { (new FormContact()).ShowDialog(); } private void BtnUpdate_Click(object sender, RibbonControlEventArgs e) { //System.Windows.Forms.MessageBox.Show("当前插件是最新版本"); //Globals.ThisAddIn.SendMessageToWeb("upgrade", Config.APP_VERSION); Bridge.bridge.ShowUpgradeView(); } private void BtnProofreadAll_Click(object sender, RibbonControlEventArgs e) { string time = DateTime.Now.ToString("yyyy - MM - dd HH: mm:ss: fff:ffffff"); Globals.ThisAddIn.SendMessageToWeb("start", time); } private void btnClear_Click(object sender, RibbonControlEventArgs e) { //DocumentUtil.ClearProofreadMarks(); var result = MessageBox.Show("请确认是否清除此文档的所有校对标注?", "提示", MessageBoxButtons.OKCancel); if(result == DialogResult.OK) { Bridge.bridge.clearAllProofreadMark(null); Globals.ThisAddIn.SendMessageToWeb("clear-tips", null); } } private void btnShowPane_Click(object sender, RibbonControlEventArgs e) { Globals.ThisAddIn.ShowPanel(); } private void btnHidePane_Click(object sender, RibbonControlEventArgs e) { Globals.ThisAddIn.HidePanel(); } private Microsoft.Office.Interop.Word.Document CurrentDocument; private void TestSplit() { //try //{ // this.CurrentDocument = Globals.ThisAddIn.Application.ActiveDocument; // string rangeText = GetRangeText(CurrentDocument.Content); // // 获取所有数学公式吗 // foreach (OMath oMath in CurrentDocument.OMaths) // { // Console.WriteLine("xx"); // } // // 形状哟 // int count = CurrentDocument.Shapes.Count; // System.Windows.Forms.MessageBox.Show(string.Format("Shapes总计:{0}", count)); // foreach (Microsoft.Office.Interop.Word.Shape shape in CurrentDocument.Shapes) // { // } // int count2 = CurrentDocument.InlineShapes.Count; // System.Windows.Forms.MessageBox.Show(string.Format("InlineShapes总计:{0}", count2)); // foreach (InlineShape inlineShape in CurrentDocument.InlineShapes) // { // } // string text5 = rangeText; // Logger.Log($"Removed unuseable section article: {text5}"); // string text6 = HostHelper.ReplaceSpecialChars(rangeText, isReplaceMultSpaceLine: true); // Logger.Log($"Removed unuseable section upload: {text6}"); // int end = CurrentDocument.Content.End; // // 分割 // string[] separator = new string[5] { "\r\a", "\a", "\r", "\v", "\f" }; // string[] array4 = text5.Split(separator, StringSplitOptions.None); // string[] array5 = text6.Split('\n'); // object Start = 0; // object End = 1; // //int pagenumber = GetIndexPageNumber(CurrentDocument.Range(ref Start, ref End)); // int start = 0; // int start2 = 0; // int start3 = 0; // string empty = string.Empty; // string empty2 = string.Empty; // string empty3 = string.Empty; // Range lastRange = null; // for (int j = 0; j < array5.Length; j++) // { // empty3 = string.Empty; // List characters = new List(); // CorrectionFragmentAbstract correctionFragmentAbstract = null; // //Logger.Instance.Info($"Handling {j + 1} upload part"); // empty2 = array5[j]; // if (string.IsNullOrEmpty(empty2)) // { // continue; // } // int num4 = start + empty2.Length + 1; // for (; string.IsNullOrEmpty(HostHelper.RemoveInvisibleCharas(array4[i])); i++) // { // num4 += array4[i].Length + 1; // } // empty = array4[i]; // //Logger.Instance.Info($"Upload part is {empty2}"); // End = start; // Start = num4; // Range range3 = CurrentDocument.Range(ref End, ref Start); // string rangeText3 = GetRangeText(range3); // //Logger.Instance.Info($"Text is {rangeText3}"); // StringBuilder stringBuilder = new StringBuilder(); // string text7 = (string.IsNullOrEmpty(rangeText3) ? "" : rangeText3.Last().ToString()); // if (HostHelper.RemoveInvisibleCharas(rangeText3) != empty2 || (text7 != "\r" && text7 != "\r\a" && text7 != "\a" && text7 != "\f" && text7 != "\v")) // { // int articleIndex = start; // Start = articleIndex; // End = articleIndex + 1; // Range range4 = CurrentDocument.Range(ref Start, ref End); // text7 = GetRangeText(range4); // bool flag = true; // while ((text7 != "\r" && text7 != "\r\a" && text7 != "\a" && text7 != "\f" && text7 != "\v") || flag) // { // if (flag) // { // //Logger.Instance.Info(string.Format("Special text default logic.", rangeText3)); // GetInvisibleCharacters(ref articleIndex, empty2, end, stringBuilder, characters, flag); // } // else // { // //Logger.Instance.Info(string.Format("Text not contains \\r.", rangeText3)); // GetInvisibleCharacters(ref articleIndex, "\r", end, stringBuilder, characters, flag); // } // End = articleIndex; // Start = articleIndex + 1; // range4 = CurrentDocument.Range(ref End, ref Start); // text7 = GetRangeText(range4); // flag = false; // } // stringBuilder.Append(text7); // string text8 = stringBuilder.ToString(); // empty = text8.Replace("\a", ""); // num4 = range4.End; // //Logger./*Instance*/.Info($"Article part is {empty}"); // if (empty.ToUpper().Contains("PAGEREF _TOC")) // { // start = num4; // //Logger.Instance.Info("String Contains 'PAGEREF _TOC' Struct."); // continue; // } // } // else // { // empty = rangeText3.Replace("\a", ""); // int tempIndex = start; // empty.ToCharArray().ToList().ForEach(delegate (char x) // { // characters.Add(new CorrectionCharacter(x, _v: true, tempIndex)); // tempIndex++; // }); // } // } //} //catch (Exception ex) //{ // System.Windows.Forms.MessageBox.Show("出现了异常:" + ex.Message); //} } private string GetInvisibleCharacters(ref int articleIndex, string articlePart, int end, StringBuilder stringBuilder, List characters, bool isFirst = true) { int num = articleIndex; if (!isFirst) { articlePart = HostHelper.RemoveInvisibleCharas(articlePart); } int num2 = ((articlePart.Length + articleIndex < end) ? articlePart.Length : (end - articleIndex)); Range range = null; string empty = string.Empty; if (string.IsNullOrEmpty(articlePart)) { string[] source = new string[4] { "\r", "\r\a", "\v", "\f" }; empty = string.Empty; while (range == null || range.Text == null || !source.Contains(empty)) { if (!string.IsNullOrEmpty(empty)) { stringBuilder.Append(empty); num++; } if (num >= end) { //Logger.Instance.Error($"Correction dictionary over length:{stringBuilder.ToString()}"); throw new ArgumentOutOfRangeException($"Not pair character: \\r \\f \\v \\r\\a, position {articleIndex}"); } object Start = num; object End = num + 1; range = CurrentDocument.Range(ref Start, ref End); empty = GetRangeText(range); string text = empty; string text2 = text; if (text2 != null && (text2 == null || text2.Length != 0)) { if (text2 == "\u001e") { empty = "-"; } } else { empty = "\n"; } characters.Add(new CorrectionCharacter(empty.First(), source.Contains(empty), range.Start)); } } else { for (int i = 0; i < num2; i++) { string text3 = articlePart[i].ToString(); empty = string.Empty; while (range == null || range.Text == null || !empty.Contains(text3)) { if (num >= end) { //Logger.Instance.Error($"Correction dictionary over length:{stringBuilder.ToString()}"); throw new ArgumentOutOfRangeException($"Not pair character: {text3}, position {articleIndex}"); } object End = num; object Start = num + 1; range = CurrentDocument.Range(ref End, ref Start); empty = GetRangeText(range); string text4 = empty; string text5 = text4; if (text5 != null && (text5 == null || text5.Length != 0)) { if (text5 == "\u001e") { empty = "-"; } } else { empty = "\n"; } stringBuilder.Append(empty); num++; characters.Add(new CorrectionCharacter(empty.First(), empty.Contains(text3), range.Start)); } } } articleIndex = num; return stringBuilder.ToString(); } public static int GetIndexPageNumber(Range range) { int result = -1; try { result = int.Parse(range.get_Information(WdInformation.wdActiveEndPageNumber).ToString()); } catch (Exception exception) { Logger.Log(exception); } return result; } protected string GetRangeText(Range range) { return string.IsNullOrEmpty(range.Text) ? "" : range.Text; } private void BtnOpenLog_Click(object sender, RibbonControlEventArgs e) { // 打开日志目录 Process.Start(Config.APP_LOG_PATH); } private void button1_Click(object sender, RibbonControlEventArgs e) { Bridge.StartUpgradeProcess(); } } }