diff --git a/.vs/AIProofread/v17/.suo b/.vs/AIProofread/v17/.suo index f38da7f..6d3ee23 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 619de96..d762260 100644 --- a/AIProofread/Bridge.cs +++ b/AIProofread/Bridge.cs @@ -576,6 +576,7 @@ namespace AIProofread webViewDict.Add(name, webView); } + Logger.Error("初始化Main Pane Web环境 开始"); // 禁用web安全,允许跨域 否则需要web编译为umd加载模式 var ops = new CoreWebView2EnvironmentOptions("--disable-web-security"); var env = await CoreWebView2Environment.CreateAsync(null, Config.WEB_DATA_PATH, ops); @@ -586,12 +587,13 @@ namespace AIProofread // 添加 js与客户端代理 webView.CoreWebView2.AddHostObjectToScript("bridge", bridge); + Logger.Error("初始化Main Pane Web环境 结束"); } catch (Exception ex) { - + Debug.WriteLine("初始化web环境失败" + ex.Message); Logger.Error("初始化web环境失败", ex); - LogHelper.Log(ex); + //LogHelper.Log(ex); } } diff --git a/AIProofread/Controls/ProofreadMainControl.cs b/AIProofread/Controls/ProofreadMainControl.cs index 4be59e2..7ed3bf9 100644 --- a/AIProofread/Controls/ProofreadMainControl.cs +++ b/AIProofread/Controls/ProofreadMainControl.cs @@ -57,7 +57,7 @@ namespace AIProofread.Controls if (ex.TryGetWebMessageAsString() == "reload") { isShowingErrorPage = false; - web.Source = new Uri(Config.WebPath("correct")); + //web.Source = new Uri(Config.WebPath("correct")); } }; this.web.Source = new Uri(Config.WebPath("correct" )); diff --git a/AIProofread/Model/DocumentInfo.cs b/AIProofread/Model/DocumentInfo.cs index 54204a4..bd2fc76 100644 --- a/AIProofread/Model/DocumentInfo.cs +++ b/AIProofread/Model/DocumentInfo.cs @@ -8,6 +8,7 @@ using System.Windows.Forms; using AIProofread.Controls; using AIProofread.core; using AIProofread.Util; +using log4net; using Microsoft.Office.Interop.Word; using Newtonsoft.Json; using UtilLib; @@ -20,6 +21,7 @@ namespace AIProofread.Model /// public class DocumentInfo { + public ILog Logger = LogHelper.GetLogger(typeof(DocumentInfo)); public static readonly int MIN_WIDTH = 420; private static char[] ArticleSpecialChars = new char[4] { '\a', '\r', '\v', '\f' }; @@ -74,8 +76,8 @@ namespace AIProofread.Model } } - public bool IsActive { get; internal set; } - public bool PaneVisible { get; set; } + public bool IsActive { get; internal set; } = false; + public bool PaneVisible { get; set; } = false; public int Id { get; set; } public CustomTaskPane TaskPane { get; set; } @@ -102,26 +104,33 @@ namespace AIProofread.Model /// public void ShowPane() { - if (null != TaskPane) - { - TaskPane.Visible = PaneVisible = true; - } - else + if(null == TaskPane) { CreateTaskPane(); } + Logger.Debug("TaskPane.Visible {"+TaskPane==null?"null":(TaskPane.Visible?"true":"false")+" => true"); + TaskPane.Visible = PaneVisible = true; + } + + /// + /// 隐藏面板 + /// + public void HidePane() + { + if (!PaneVisible) return; + ShowDocumentStatus("HidePane"); + Logger.Debug($"TaskPane.Visible {TaskPane.Visible} => false"); + TaskPane.Visible = PaneVisible = false; } public void RunInMainThread(Action action) { - if (null != TaskPane) - { - TaskPane.Control.BeginInvoke(action); - } - else + Logger.Debug($"RunInMainThread {action}"); + if (null == TaskPane) { CreateTaskPane(); } + TaskPane.Control.BeginInvoke(action); } public void ShowDialog(string message, string confirmText, string confirmAction) @@ -167,16 +176,7 @@ namespace AIProofread.Model private void ShowDocumentStatus(string tag) { - // Logger.Log($"{fileName} {tag} PaneVisible is {PaneVisible} Poofread is {Proofread} Proofreading is {Proofreading}"); - } - - /// - /// 隐藏面板 - /// - public void HidePane() - { - ShowDocumentStatus(" HidePane"); - if (null != TaskPane) TaskPane.Visible = PaneVisible = false; + // Logger.Log($"{fileName} {tag} PaneVisible is {PaneVisible} Poofread is {Proofread} Proofreading is {Proofreading}"); } /// @@ -184,7 +184,7 @@ namespace AIProofread.Model /// public void Active() { - ShowDocumentStatus(" Active"); + ShowDocumentStatus("Active"); IsActive = true; if (Config.IS_WPS && null != TaskPane && PaneVisible) { @@ -194,7 +194,7 @@ namespace AIProofread.Model public void Deactive() { - ShowDocumentStatus(" Deactive"); + ShowDocumentStatus("Deactive"); IsActive = false; if (Config.IS_WPS) { @@ -203,7 +203,7 @@ namespace AIProofread.Model // 异步等待一段时间 重新设置为true,防止面板关闭时,触发事件 ThreadPool.QueueUserWorkItem(state => { - Thread.Sleep(1000); + Thread.Sleep(500); PaneVisible = true; }); } @@ -270,6 +270,7 @@ namespace AIProofread.Model // 创建pane 并初始化 public void CreateTaskPane() { + Logger.Debug("CreateTaskPane"); var control = new ProofreadMainControl(); if (MinWidth < 10) { @@ -312,7 +313,12 @@ namespace AIProofread.Model this.fileName = CurrentDocument.FullName; ranges.Clear(); ComputeUniqueId(); - if (TaskPane == null) CreateTaskPane(); + if (TaskPane == null) + { + Logger.Debug("init document Initialize(318) and CreateTaskPane"); + CreateTaskPane(); + + } } diff --git a/AIProofread/Model/DocumentList.cs b/AIProofread/Model/DocumentList.cs index 214d0d1..3418a07 100644 --- a/AIProofread/Model/DocumentList.cs +++ b/AIProofread/Model/DocumentList.cs @@ -1,9 +1,9 @@ -using Microsoft.Office.Interop.Word; +using log4net; +using log4net.Repository.Hierarchy; +using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace AIProofread.Model { @@ -12,6 +12,7 @@ namespace AIProofread.Model /// public class DocumentList { + public ILog Logger = LogHelper.GetLogger(typeof(DocumentList)); public List documentList = new List(); public DocumentInfo ActiveDocument { get; set; } public int Count => documentList.Count; @@ -109,11 +110,17 @@ namespace AIProofread.Model /// public DocumentInfo SetActiveDocument(Document originDocument) { + Logger.Debug("SetActiveDocument(113): " + originDocument.Name); if (originDocument == null) return null; var document = InitDocument(originDocument); - + if (document == null) + { + Logger.Error("Document not exists SetActiveDocument"); + return null; + } if (ActiveDocument == document) return document; - else if (Globals.ThisAddIn.IsWPS) + + if (Globals.ThisAddIn.IsWPS) { // WPS 只有一个窗口 所以需要先关闭之前文档的面板 ActiveDocument?.Deactive(); @@ -137,6 +144,7 @@ namespace AIProofread.Model // 如果不存在,则添加 if (document == null) { + Logger.Debug("Document not exists,InitDocument: " + originDocument.Name); document = new DocumentInfo(originDocument); Add(document); } diff --git a/AIProofread/Ribbon1.Designer.cs b/AIProofread/Ribbon1.Designer.cs index 71b31ea..44c4699 100644 --- a/AIProofread/Ribbon1.Designer.cs +++ b/AIProofread/Ribbon1.Designer.cs @@ -107,7 +107,6 @@ namespace AIProofread // menuProofreadList // this.menuProofreadList.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge; - this.menuProofreadList.Enabled = false; this.menuProofreadList.Image = global::AIProofread.Properties.Resources.icon_proofread; this.menuProofreadList.Items.Add(this.BtnProofreadExact); this.menuProofreadList.Items.Add(this.BtnProofreadFull); @@ -290,6 +289,7 @@ namespace AIProofread // BtnShowPanel // this.BtnShowPanel.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge; + this.BtnShowPanel.Enabled = false; this.BtnShowPanel.Image = global::AIProofread.Properties.Resources.icon_panel; this.BtnShowPanel.Label = "显示面板\r\n"; this.BtnShowPanel.Name = "BtnShowPanel"; diff --git a/AIProofread/ThisAddIn.cs b/AIProofread/ThisAddIn.cs index d0d6486..b6b2df7 100644 --- a/AIProofread/ThisAddIn.cs +++ b/AIProofread/ThisAddIn.cs @@ -9,6 +9,8 @@ using UtilLib; using AIProofread.Model; using System.Collections.Generic; using log4net; +using System.Threading.Tasks; +using DocumentFormat.OpenXml.EMMA; namespace AIProofread @@ -130,10 +132,28 @@ namespace AIProofread // CheckDocumentClosedTick(); // 定时检测文档是否关闭 - _timer = new System.Timers.Timer(10000); - _timer.Elapsed += CheckDocumentClosed; - _timer.AutoReset = true; - _timer.Enabled = true; + //_timer = new System.Timers.Timer(10000); + //_timer.Elapsed += CheckDocumentClosed; + //_timer.AutoReset = true; + //_timer.Enabled = true; + + try + { + // 默认已经打开了文档 直接初始化 + if (Application.Documents.Count > 0 && Application.ActiveDocument != null) + { + Logger.Debug("ThisAddIn_Startup 开始初始化当前文档"); + ActiveDocument = documentList.InitDocument(Application.ActiveDocument); + + // 直接初始化面板 + // info.CheckPanel(); + Logger.Debug("ThisAddIn_Startup 结束初始化当前文档"); + } + } + catch (Exception ex) + { + Logger.Error("Initialize documentlist Error ", ex); + } } catch (Exception ex1) { @@ -184,6 +204,12 @@ namespace AIProofread for (int i = documentList.documentList.Count - 1; i >= 0; i--) { var item = documentList.documentList[i]; + // 判断文档对象是否已经被移除 + if (item.CurrentDocument == null) + { + documentList.Remove(item); + continue; + } // 可能出现另存问题 所以需要更新文件名称 var oldName = item.fileName; var currentName = item.CurrentDocument.FullName; @@ -274,6 +300,7 @@ namespace AIProofread // 启动地址 applicationStartupPath = System.Windows.Forms.Application.StartupPath; + Logger.Debug("applicationStartupPath --> " + applicationStartupPath); // 判断是否是WPS if (applicationStartupPath.Contains("WPS")) { @@ -289,22 +316,6 @@ namespace AIProofread Logger.Error("Init WPS Error ", ex); } } - try - { - // 默认已经打开了文档 直接初始化 - if(CurrentWordApplication.Documents.Count > 0){ - foreach (Document item in CurrentWordApplication.Documents) - { - var info = documentList.InitDocument(item); - // 直接初始化面板 - info.CheckPanel(); - } - } - } - catch (Exception ex) - { - Logger.Error("Initialize documentlist Error ", ex); - } string verTextFile = Config.APP_BASE_DIR + Path.GetFileName("app_version.txt"); try { @@ -351,21 +362,27 @@ namespace AIProofread private void Application_DocumentChange() { + // 检测是否存在打开的文档 if (CurrentWordApplication.Documents.Count == 0) { return; } - //var document = CurrentWordApplication.ActiveDocument; + var document = CurrentWordApplication.ActiveDocument; + Logger.Debug("Application_DocumentChange -- " + document.Name + " 修订模式: is " + document.TrackRevisions); // 设置当前文档 - ActiveDocument = documentList.SetActiveDocument(CurrentWordApplication.ActiveDocument); + ActiveDocument = documentList.SetActiveDocument(document); + if (ActiveDocument == null) + { + return; + } ActiveDocument.CheckBtnStatus(); CheckDocumentClosed(null, null); if (formCommonsenseDetection != null) { formCommonsenseDetection.SendMessageToWeb("document-change", null); } - //LogHelper.Log("Application_DocumentChange -- " + ActiveDocument.fileName + " track is " + ActiveDocument.CurrentDocument.TrackRevisions); + // } public void SetActiveDocument(Document doc) @@ -390,10 +407,10 @@ namespace AIProofread /// private void Application_WindowActivate(Document activeDoc, Window Wn) { + Logger.Debug("Application_WindowActivate -- " + activeDoc.Name); if (activeDoc != null && (ActiveDocument == null || activeDoc != ActiveDocument.CurrentDocument)) { - ActiveDocument = documentList.SetActiveDocument(activeDoc); - Logger.Info("Application_WindowActivate -- " + ActiveDocument.fileName); + documentList.SetActiveDocument(activeDoc); } //// 当前文档添加书签集合 //if (!allMarks.ContainsKey(activeDoc)) @@ -472,16 +489,6 @@ namespace AIProofread FmainThreadContext.Send(d, null); } - //private void ProofreadPanel_SizeChanged(object sender, EventArgs e) - //{ - // // 处理最小宽度 - // if (customTaskPane != null && customTaskPane.Width < MinWidth && customTaskPane.Visible) - // { - // SendKeys.Send("{ESC}"); - // customTaskPane.Width = MinWidth; - // } - //} - private void Application_WindowSelectionChange(Selection s) { ribbon.ParseSelectionChange(s); @@ -529,6 +536,7 @@ namespace AIProofread // 显示面板 public void ShowPanel() { + Logger.Debug("ShowPanel"); ActiveDocument?.ShowPane(); }