fixed position error

This commit is contained in:
LittleBoy 2024-03-29 02:42:25 +08:00
parent 59aacb27a1
commit aba0247d14
8 changed files with 134 additions and 12 deletions

Binary file not shown.

View File

@ -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<DocumentCorrectItem> list = JsonConvert.DeserializeObject<List<DocumentCorrectItem>>(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;
}
}
}

View File

@ -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();
}

View File

@ -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<CorrectedContent> Diffs { get; set; }
}
}

View File

@ -45,6 +45,7 @@
<Compile Include="AcceptEnum.cs" />
<Compile Include="Colors.cs" />
<Compile Include="CorrectedContent.cs" />
<Compile Include="DocumentCorrectItem.cs" />
<Compile Include="ProofreadType.cs" />
<Compile Include="Userinfo.cs" />
<Compile Include="UtilLib.cs" />