diff --git a/.vs/AIProofread/v17/.suo b/.vs/AIProofread/v17/.suo index fc9e454..d7fc550 100644 Binary files a/.vs/AIProofread/v17/.suo and b/.vs/AIProofread/v17/.suo differ diff --git a/AIProofread/Bridge.cs b/AIProofread/Bridge.cs index 8c2a178..47c2b20 100644 --- a/AIProofread/Bridge.cs +++ b/AIProofread/Bridge.cs @@ -111,7 +111,7 @@ namespace AIProofread Userinfo info = JsonConvert.DeserializeObject(userinfo); // 登录成功 展示 Globals.ThisAddIn.ribbon.ProcessLoginInfo(info); - Globals.ThisAddIn.SendMessageToWeb("async-login-success", null); + //Globals.ThisAddIn.SendMessageToWeb("async-login-success", null); } // 获取文档所有文本数据 @@ -362,58 +362,65 @@ namespace AIProofread public Microsoft.Office.Tools.Word.Bookmark AddBookmark(CorrectedContent item, int findIndex, int offset, int length) { - var document = Globals.ThisAddIn.Application.ActiveDocument; - ControlCollection controls = Globals.Factory.GetVstoObject(document).Controls; - var Start = offset; - var End = offset + length - 1; - // 整体选区的内容 - var fullRange = document.Range(Start, End); - string fullText = fullRange.Text; - End = offset + item.start; - Start = offset + item.end; - Microsoft.Office.Tools.Word.Bookmark bookmark = null; - var r = document.Range(End, Start); - var markName = Config.BuildBookmarkName(item.id); + try + { + var document = Globals.ThisAddIn.Application.ActiveDocument; + ControlCollection controls = Globals.Factory.GetVstoObject(document).Controls; + var Start = offset; + var End = offset + length - 1; + // 整体选区的内容 + var fullRange = document.Range(Start, End); + string fullText = fullRange.Text; + End = offset + item.start; + Start = offset + item.end; - // 判断是否已经存在 - if (controls.Contains(markName)) - { - controls.Remove(markName); - } - // 判断选区是否正确 - if (item.tag == "i" || r.Text == item.origin) - { - bookmark = controls.AddBookmark(r, markName); - bookmark.Tag = "ai_proofread"; - if (item.color != null) + var r = document.Range(End, Start); + var markName = Config.BuildBookmarkName(item.id); + + // 判断是否已经存在 + if (controls.Contains(markName)) { - // 给选区添加背景颜色 - r.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(item.color)); + controls.Remove(markName); } - } - else - { - int startPos = 0, index,findCount = 0; - while ((index = fullText.IndexOf(item.origin, startPos)) != -1) + // 判断选区是否正确 + if (item.tag == "i" || r.Text == item.origin) { - if (findCount == findIndex) + bookmark = controls.AddBookmark(r, markName); + bookmark.Tag = "ai_proofread"; + if (item.color != null) { - r = document.Range(offset + index, offset + index + item.origin.Length); - bookmark = controls.AddBookmark(r, markName); - bookmark.Tag = "ai_proofread"; - if (item.color != null) - { - // 给选区添加背景颜色 - r.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(item.color)); - } - break; + // 给选区添加背景颜色 + r.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(item.color)); + } + } + else + { + int startPos = 0, index, findCount = 0; + while ((index = fullText.IndexOf(item.origin, startPos)) != -1) + { + if (findCount == findIndex) + { + r = document.Range(offset + index, offset + index + item.origin.Length); + bookmark = controls.AddBookmark(r, markName); + bookmark.Tag = "ai_proofread"; + if (item.color != null) + { + // 给选区添加背景颜色 + r.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(item.color)); + } + break; + } + startPos = index; + findCount++; } - startPos = index; - findCount++; } - } + } + catch (Exception ex) + { + Logger.Log("create mark error:" + ex.Message + "\n" + ex.StackTrace + "\n\n"); + } return bookmark; } diff --git a/AIProofread/Config.cs b/AIProofread/Config.cs index 0d6728f..cc1dcf7 100644 --- a/AIProofread/Config.cs +++ b/AIProofread/Config.cs @@ -10,14 +10,10 @@ namespace AIProofread /// /// 网页访问地址 /// - public static readonly string WEB_PATH = "http://localhost:5173/"; + public static readonly string WEB_PATH = "https://gm-plugin.gachafun.com/"; #else - public static readonly string WEB_PATH = "dist/index.html"; + public static readonly string WEB_PATH = "https://gm-plugin.gachafun.com/"; #endif - /// - /// 词库地址 - /// - public static readonly string LEXICON_PATH = "https://gm.gachafun.com/lexicon"; public static readonly string WEB_DATA_PATH = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ai_proofread\\userdata"; /// diff --git a/AIProofread/Controls/BaseWinForm.cs b/AIProofread/Controls/BaseWinForm.cs index b586c2e..154a4ba 100644 --- a/AIProofread/Controls/BaseWinForm.cs +++ b/AIProofread/Controls/BaseWinForm.cs @@ -113,16 +113,23 @@ namespace AIProofread.Controls protected async void InitWebView(WebView2 webView,String url,string name) { //Bridge.InitWebEnvAsync(name, webView); + try + { + if (webView.IsDisposed) return; + var ops = new CoreWebView2EnvironmentOptions("--disable-web-security"); + var env = await CoreWebView2Environment.CreateAsync(null, Config.WEB_DATA_PATH, ops); - var ops = new CoreWebView2EnvironmentOptions("--disable-web-security"); - var env = await CoreWebView2Environment.CreateAsync(null, Config.WEB_DATA_PATH, ops); - await webView.EnsureCoreWebView2Async(env); + await webView.EnsureCoreWebView2Async(env); - var eventForwarder = new EventForwarder(this); - webView.CoreWebView2.AddHostObjectToScript("event", eventForwarder); - webView.CoreWebView2.AddHostObjectToScript("bridge", Bridge.bridge); + var eventForwarder = new EventForwarder(this); + webView.CoreWebView2.AddHostObjectToScript("event", eventForwarder); + webView.CoreWebView2.AddHostObjectToScript("bridge", Bridge.bridge); - webView.Source = new Uri(url); + webView.Source = new Uri(url); + }catch (Exception ex) + { + Logger.Log("\ninit webview error:" + ex.Message + "\n" + ex.StackTrace); + } } } } diff --git a/AIProofread/ProofreadItem.cs b/AIProofread/ProofreadItem.cs index d74c5ca..caa81b1 100644 --- a/AIProofread/ProofreadItem.cs +++ b/AIProofread/ProofreadItem.cs @@ -23,7 +23,13 @@ namespace UtilLib public ProofreadItem(CorrectedContent content, Bookmark bookmark) { this.content = content; - InitBookMark(bookmark); + if (bookmark != null) + { + this.mark = bookmark; + // 记录目前字体 + originSize = bookmark.Range.Font.Size; + } + //InitBookMark(bookmark); } private void InitBookMark(Bookmark bookmark) @@ -59,37 +65,35 @@ namespace UtilLib public void Select() { - if (mark != null) - { - mark.Range.Font.Size = originSize + 2; // 将选中标签文本放大字体 - //Globals.ThisAddIn.Application.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView; - //Globals.ThisAddIn.Application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; - //Globals.ThisAddIn.Application.ActiveWindow.Activate(); - mark.Select(); - } + if (mark == null) return; + mark.Range.Font.Size = originSize + 2; // 将选中标签文本放大字体 + mark.Select(); + } public void UnSelect() { - if (mark != null) - { - mark.Range.Font.Size = originSize; // 还原 - } + if (mark == null) return; + mark.Range.Font.Size = originSize; // 还原 } private void SetMarkStyle() { + if (mark == null) return; mark.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineThick; mark.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(content.color)); } private void ResetMarkStyle() { + if (mark == null) return; mark.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone; mark.Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic; } public void Process(int status) { + + if (mark == null) return; // content.isAccept = status; if (status == AcceptStatus.Accept) diff --git a/AIProofread/Ribbon1.cs b/AIProofread/Ribbon1.cs index 219094a..5b462fb 100644 --- a/AIProofread/Ribbon1.cs +++ b/AIProofread/Ribbon1.cs @@ -66,7 +66,7 @@ namespace AIProofread private void btnOpenLexicon_Click(object sender, RibbonControlEventArgs e) { - Process.Start(Config.LEXICON_PATH); + Globals.ThisAddIn.SendMessageToWeb("show-lexicon", null); } private void btnSetting_Click(object sender, RibbonControlEventArgs e) @@ -94,6 +94,7 @@ namespace AIProofread private void btnClear_Click(object sender, RibbonControlEventArgs e) { DocumentUtil.ClearProofreadMarks(); + Globals.ThisAddIn.SendMessageToWeb("clear-tips",null); } } } diff --git a/AIProofread/core/DocumentText.cs b/AIProofread/core/DocumentText.cs index 440ba89..a431d64 100644 --- a/AIProofread/core/DocumentText.cs +++ b/AIProofread/core/DocumentText.cs @@ -8,6 +8,10 @@ namespace AIProofread public string Text { get; set; } public DocumentText() { } + public DocumentText(string text) + { + this.Text = text; + } public DocumentText(byte[] hash, string text) { this.Hash = BitConverter.ToString(hash).ToLower().Replace("-", ""); diff --git a/AIProofread/core/Tools.cs b/AIProofread/core/Tools.cs index fc4048a..39b4249 100644 --- a/AIProofread/core/Tools.cs +++ b/AIProofread/core/Tools.cs @@ -1,27 +1,41 @@ using Newtonsoft.Json; +using System; using System.Collections.Generic; +using System.Linq; using System.Security.Cryptography; +using System.Security.Policy; using System.Text; +using static System.Net.Mime.MediaTypeNames; namespace AIProofread { public class Tools { + private static readonly string[] paragSplitor = new string[] { "\r", "\n", "\r\n" }; public static Dictionary GetAllText() { // 获取当前文档所有文本 string allText = Globals.ThisAddIn.Application.ActiveDocument.Range().Text; List list = new List(); - + if (allText != null && allText.Trim().Length > 0) { // 开始分割 MD5 md5 = new MD5CryptoServiceProvider(); - List lines = StringUtil.CutTextToSentences(allText); + + List lines = allText.Split(paragSplitor, StringSplitOptions.None).ToList();//StringUtil.CutTextToSentences(allText); foreach (string text in lines) { - byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text)); - list.Add(new DocumentText(hash, text)); + if(text.Trim().Length > 0) + { + byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text)); + list.Add(new DocumentText(hash, text + "\n")); + } + else + { + + list.Add(new DocumentText(text + "\n")); + } } } var map = new Dictionary diff --git a/AIProofread/obj/Debug/AIProofread.csproj.GenerateResource.cache b/AIProofread/obj/Debug/AIProofread.csproj.GenerateResource.cache index 30ba720..bd20f29 100644 Binary files a/AIProofread/obj/Debug/AIProofread.csproj.GenerateResource.cache and b/AIProofread/obj/Debug/AIProofread.csproj.GenerateResource.cache differ diff --git a/AIProofread/obj/Debug/AIProofread.dll b/AIProofread/obj/Debug/AIProofread.dll index 4b19588..dba262f 100644 Binary files a/AIProofread/obj/Debug/AIProofread.dll and b/AIProofread/obj/Debug/AIProofread.dll differ diff --git a/AIProofread/obj/Debug/AIProofread.pdb b/AIProofread/obj/Debug/AIProofread.pdb index 799a10d..4300800 100644 Binary files a/AIProofread/obj/Debug/AIProofread.pdb and b/AIProofread/obj/Debug/AIProofread.pdb differ diff --git a/AIProofread/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/AIProofread/obj/Debug/DesignTimeResolveAssemblyReferences.cache index f877043..fc7c96e 100644 Binary files a/AIProofread/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/AIProofread/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ