diff --git a/.vs/AIProofread/v17/.suo b/.vs/AIProofread/v17/.suo index ba807cf..fc9e454 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 a457976..8c2a178 100644 --- a/AIProofread/Bridge.cs +++ b/AIProofread/Bridge.cs @@ -1,15 +1,21 @@ using AIProofread.Controls; using Microsoft.Office.Interop.Word; +using Microsoft.Office.Tools.Word; using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; +using System.Linq; using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; using System.Text; using System.Text.RegularExpressions; +using System.Xml.Linq; using UtilLib; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock; using Document = Microsoft.Office.Interop.Word.Document; @@ -311,14 +317,105 @@ namespace AIProofread Globals.ThisAddIn.SendMessageToWeb("select", proofreadId); marks[proofreadId].Select(); } + Globals.ThisAddIn.SendMessageToWeb("select_proofread", proofreadId); } - public void processMark(int proofreadId,int status) + public void processMark(int proofreadId, int status) { if (proofreadId > 0 && marks.ContainsKey(proofreadId)) { marks[proofreadId].Process(status); } } + + public void InitContent(string content) + { + List list = JsonConvert.DeserializeObject>(content); + // + clearAllProofreadMark(); + + //var app = Globals.ThisAddIn.Application; + //var cur = app.Selection; + // + foreach (var correct in list) + { + //cur.TypeText(correct.Insert); + //var rng = cur.Range; + if (correct.Diffs != null && correct.Diffs.Count > 0) + { + //var diffs = correct.Diffs.OrderBy(it => + //{ + // if (it.tag != "i") return it.start; + // return it.start + correct.Insert_len; + //}).ToList(); + int index = 0; + foreach (var item in correct.Diffs) + { + var mark = AddBookmark(item, index, correct.Offset, correct.Insert_len); + if (item.tag != "i") index++; + marks.Add(item.id, new ProofreadItem(item, mark)); + } + } + } + } + + + 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); + + // 判断是否已经存在 + 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) + { + // 给选区添加背景颜色 + 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++; + } + } + + return bookmark; + } + } } diff --git a/AIProofread/ProofreadItem.cs b/AIProofread/ProofreadItem.cs index 3747a01..d74c5ca 100644 --- a/AIProofread/ProofreadItem.cs +++ b/AIProofread/ProofreadItem.cs @@ -3,6 +3,7 @@ using System; using System.Drawing; using AIProofread; using Microsoft.Office.Interop.Word; +using Microsoft.Office.Tools.Word; using Bookmark = Microsoft.Office.Tools.Word.Bookmark; namespace UtilLib @@ -16,16 +17,20 @@ namespace UtilLib public ProofreadItem(CorrectedContent content) { this.content = content; - InitBookMark(); + InitBookMark(null); } - private void InitBookMark() + public ProofreadItem(CorrectedContent content, Bookmark bookmark) + { + this.content = content; + InitBookMark(bookmark); + } + + private void InitBookMark(Bookmark bookmark) { // 创建mark - this.mark = DocumentUtil.AddBookmark( - content.tag == "i" ? null : content.color, - content.start, content.end - ); + this.mark = bookmark != null ? bookmark + : DocumentUtil.AddBookmark(content.tag == "i" ? null : content.color, content.start, content.end); if (mark != null) { // 记录目前字体 @@ -57,6 +62,10 @@ namespace UtilLib 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(); } } public void UnSelect() @@ -85,12 +94,13 @@ namespace UtilLib content.isAccept = status; if (status == AcceptStatus.Accept) { - if(content.tag == "r" || content.tag == "i") + if (content.tag == "r" || content.tag == "i") { mark.Text = content.text; - }else if (content.tag == "d") + } + else if (content.tag == "d") { - mark.Text = "\u200C"; + mark.Text = ""; } ResetMarkStyle(); } @@ -104,9 +114,9 @@ namespace UtilLib { mark.Text = content.origin; } - else if(content.tag == "i") + else if (content.tag == "i") { - mark.Text = "\u200C"; + mark.Text = ""; } SetMarkStyle(); } diff --git a/AIProofread/obj/Debug/AIProofread.csproj.AssemblyReference.cache b/AIProofread/obj/Debug/AIProofread.csproj.AssemblyReference.cache index 46fb2a4..8a2ed3c 100644 Binary files a/AIProofread/obj/Debug/AIProofread.csproj.AssemblyReference.cache and b/AIProofread/obj/Debug/AIProofread.csproj.AssemblyReference.cache differ diff --git a/AIProofread/obj/Debug/AIProofread.dll b/AIProofread/obj/Debug/AIProofread.dll index 7ba0dbc..4b19588 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 5a44544..799a10d 100644 Binary files a/AIProofread/obj/Debug/AIProofread.pdb and b/AIProofread/obj/Debug/AIProofread.pdb differ diff --git a/util-lib/DocumentCorrectItem.cs b/util-lib/DocumentCorrectItem.cs new file mode 100644 index 0000000..274695c --- /dev/null +++ b/util-lib/DocumentCorrectItem.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace UtilLib +{ + public class DocumentCorrectItem + { + public string Key { get; set; } + public string Insert { get; set; } + public string New_text { get; set; } + public int Insert_len { get; set; } + public int Offset { get; set; } + public List Diffs { get; set; } + } +} diff --git a/util-lib/util-lib.csproj b/util-lib/util-lib.csproj index ddd5e78..5c5c213 100644 --- a/util-lib/util-lib.csproj +++ b/util-lib/util-lib.csproj @@ -45,6 +45,7 @@ +