优化找不到标记异常;优化对象查找

This commit is contained in:
LittleBoy 2024-05-24 17:54:50 +08:00
parent 0c1fd59146
commit a5ed094f2d
14 changed files with 84 additions and 21 deletions

Binary file not shown.

View File

@ -233,13 +233,17 @@ namespace AIProofread
item.ResetMarkStyle(); item.ResetMarkStyle();
} }
} }
marks.Clear();
DocumentUtil.ClearProofreadMarks(); DocumentUtil.ClearProofreadMarks();
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Log("ClearAllProofreadMark", ex); Logger.Log("ClearAllProofreadMark", ex);
} }
try
{
// 清空marks
marks.Clear();
}catch (Exception) { }
} }
public void removeBookmark(string markId) public void removeBookmark(string markId)
@ -416,7 +420,7 @@ namespace AIProofread
// 先滚动到可视区域 // 先滚动到可视区域
//doc.ActiveWindow.ScrollIntoView(mark.Range); //doc.ActiveWindow.ScrollIntoView(mark.Range);
marks[proofreadId].Select(); marks[proofreadId].Select();
Globals.ThisAddIn.SendMessageToWeb("select", proofreadId); //Globals.ThisAddIn.SendMessageToWeb("select", proofreadId);
} }
Globals.ThisAddIn.SendMessageToWeb("select_proofread", proofreadId); Globals.ThisAddIn.SendMessageToWeb("select_proofread", proofreadId);
} }
@ -438,7 +442,6 @@ namespace AIProofread
//var app = Globals.ThisAddIn.Application; //var app = Globals.ThisAddIn.Application;
//var cur = app.Selection; //var cur = app.Selection;
// //
int aIndex = 0;
foreach (var correct in list) foreach (var correct in list)
{ {
//cur.TypeText(correct.Insert); //cur.TypeText(correct.Insert);
@ -451,16 +454,8 @@ namespace AIProofread
// return it.start + correct.Insert_len; // return it.start + correct.Insert_len;
//}).ToList(); //}).ToList();
int index = 0; int index = 0;
if (aIndex++ > 20)
{
Console.WriteLine(aIndex);
}
foreach (var item in correct.Diffs) foreach (var item in correct.Diffs)
{ {
if(item.id == 109288)
{
Console.WriteLine("xxx");
}
var mark = AddBookmark(item, index, correct.Sentence_offset, correct.Insert_len, correct.Paragraph_offset); var mark = AddBookmark(item, index, correct.Sentence_offset, correct.Insert_len, correct.Paragraph_offset);
if (item.tag != "i") index++; if (item.tag != "i") index++;
if (mark != null) if (mark != null)
@ -522,7 +517,10 @@ namespace AIProofread
// 判断是否已经存在 // 判断是否已经存在
if (controls.Contains(markName)) if (controls.Contains(markName))
{ {
controls.Remove(markName); try
{
controls.Remove(markName);
}catch (Exception) { }
} }
Range findRange = null; Range findRange = null;
if (paragraphIndex > document.Paragraphs.Count) return null; if (paragraphIndex > document.Paragraphs.Count) return null;

View File

@ -8,15 +8,17 @@ namespace AIProofread
public static readonly string APP_NAME = "AI校对王"; public static readonly string APP_NAME = "AI校对王";
public static readonly string APP_VERSION = "1.0.4"; public static readonly string APP_VERSION = "1.0.4";
public static bool IS_WPS = false; public static bool IS_WPS = false;
public static readonly string CONFIG_FILE = AppDomain.CurrentDomain.BaseDirectory + "app.json";
#if DEBUG #if DEBUG
/// <summary> /// <summary>
/// 网页访问地址 /// 网页访问地址
/// </summary> /// </summary>
public static readonly string WEB_PATH = "http://192.168.10.100:5173/"; public static string WEB_PATH = "http://192.168.10.100:5173/";
public static readonly bool RUN_IN_DEBUG = true; public static bool RUN_IN_DEBUG = true;
#else #else
public static readonly string WEB_PATH = "https://gm-plugin.gachafun.com/"; public static string WEB_PATH = "https://gm-plugin.gachafun.com/";
public static readonly bool RUN_IN_DEBUG = false; public static bool RUN_IN_DEBUG = false;
#endif #endif
public static readonly string APP_DATA_PATH = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ai_proofread"; public static readonly string APP_DATA_PATH = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ai_proofread";
public static readonly string APP_LOG_PATH = APP_DATA_PATH + "\\logs\\"; public static readonly string APP_LOG_PATH = APP_DATA_PATH + "\\logs\\";

View File

@ -36,5 +36,13 @@ namespace AIProofread.Controls
{ {
return label1.Width; return label1.Width;
} }
public void ResetWeb()
{
if (!this.web.IsDisposed)
{
this.web.Stop();
this.web.Dispose();
}
}
} }
} }

View File

@ -93,9 +93,14 @@ namespace UtilLib
public void ResetMarkStyle() public void ResetMarkStyle()
{ {
if (mark == null) return; if (mark == null) return;
mark.Range.Font.Size = originSize; // 还原 if (mark.Range == null) return;
//mark.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone; try
mark.Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic; {
mark.Range.Font.Size = originSize; // 还原
//mark.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
mark.Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
}
catch (Exception) { }
} }
public void Process(int status) public void Process(int status)

View File

@ -19,6 +19,11 @@ namespace AIProofread
//btnLogout.Label = "退出\n登录"; //btnLogout.Label = "退出\n登录";
Globals.ThisAddIn.ribbon = this; Globals.ThisAddIn.ribbon = this;
ShowDebug();
}
public void ShowDebug()
{
if (Config.RUN_IN_DEBUG) if (Config.RUN_IN_DEBUG)
{ {
grpDebug.Visible = true; grpDebug.Visible = true;
@ -118,7 +123,8 @@ namespace AIProofread
private void btnClear_Click(object sender, RibbonControlEventArgs e) private void btnClear_Click(object sender, RibbonControlEventArgs e)
{ {
DocumentUtil.ClearProofreadMarks(); //DocumentUtil.ClearProofreadMarks();
Bridge.bridge.clearAllProofreadMark();
Globals.ThisAddIn.SendMessageToWeb("clear-tips", null); Globals.ThisAddIn.SendMessageToWeb("clear-tips", null);
} }

View File

@ -10,6 +10,7 @@ using System.Threading;
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Word;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO;
namespace AIProofread namespace AIProofread
{ {
@ -50,8 +51,12 @@ namespace AIProofread
private void Application_WindowDeactivate(Word.Document doc, Window Wn) private void Application_WindowDeactivate(Word.Document doc, Window Wn)
{ {
Logger.Log("Application_WindowDeactivate -- " + doc.FullName + " visible:" + currentDocumentTaskPane.Visible); Logger.Log("Application_WindowDeactivate -- " + doc.FullName + " visible:");
//HidePanel(Doc); //HidePanel(Doc);
// 处理wps直接资源管理器新开文档面板闪烁问题
//if (IsWPS) {
// this.currentDocumentTaskPane.Visible = false;
//}
} }
private void Application_WindowActivate(Word.Document activeDoc, Window Wn) private void Application_WindowActivate(Word.Document activeDoc, Window Wn)
@ -122,6 +127,8 @@ namespace AIProofread
} }
if (taskPanels.ContainsKey(doc)) if (taskPanels.ContainsKey(doc))
{ {
var control = (ProofreadMainControl)taskPanels[doc].Control;
control.ResetWeb();
taskPanels[doc].Visible = false; taskPanels[doc].Visible = false;
taskPanels[doc].Dispose(); taskPanels[doc].Dispose();
taskPanels.Remove(doc); taskPanels.Remove(doc);
@ -227,10 +234,37 @@ namespace AIProofread
// } // }
//} //}
private void InitAppByConfig()
{
try
{
if (File.Exists(Config.CONFIG_FILE))
{
string content = File.ReadAllText(Config.CONFIG_FILE);
Logger.Log("Found app.json " + content);
if (content == null || content.Length == 0) return;
AppConfig config = JsonConvert.DeserializeObject<AppConfig>(content);
if (config.AppUrl?.Length > 0)
{
Config.WEB_PATH = config.AppUrl;
}
if (config.AppRunInDebug)
{
Config.RUN_IN_DEBUG = config.AppRunInDebug;
if(this.ribbon != null)
{
this.ribbon.ShowDebug();
}
}
}
}catch (Exception) { }
}
private void ThisAddIn_Startup(object sender, System.EventArgs e) private void ThisAddIn_Startup(object sender, System.EventArgs e)
{ {
try try
{ {
InitAppByConfig();
FmainThreadContext = SynchronizationContext.Current; FmainThreadContext = SynchronizationContext.Current;
// 启动地址 // 启动地址
applicationStartupPath = System.Windows.Forms.Application.StartupPath; applicationStartupPath = System.Windows.Forms.Application.StartupPath;

9
util-lib/AppConfig.cs Normal file
View File

@ -0,0 +1,9 @@

namespace UtilLib
{
public class AppConfig
{
public string AppUrl { get; set; }
public bool AppRunInDebug { get; set; }
}
}

View File

@ -52,6 +52,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AcceptEnum.cs" /> <Compile Include="AcceptEnum.cs" />
<Compile Include="AppConfig.cs" />
<Compile Include="Colors.cs" /> <Compile Include="Colors.cs" />
<Compile Include="CorrectedContent.cs" /> <Compile Include="CorrectedContent.cs" />
<Compile Include="DocumentCorrectItem.cs" /> <Compile Include="DocumentCorrectItem.cs" />