ai_office_plugin/AIProofread/ProofreadItem.cs
callmeyan b4c4af198f 1.优化面板尺寸监听;
2.校对标签颜色;
3.添加文档id存储避免网页刷新;
2024-06-02 17:42:32 +08:00

152 lines
4.9 KiB
C#

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
{
public class ProofreadItem
{
public Bookmark mark;
public CorrectedContent content;
private float originSize;
private WdColor originColor;
private WdColor originBackgroundColor;
public string Name { get; set; }
public ProofreadItem(CorrectedContent content)
{
this.content = content;
InitBookMark(null);
SetMarkName();
}
private void SetMarkName()
{
this.Name = this.mark != null ? mark.Name : Config.BuildBookmarkName(content.id);
}
public ProofreadItem(CorrectedContent content, Bookmark bookmark)
{
this.content = content;
if (bookmark != null)
{
this.mark = bookmark;
// 记录目前字体
originSize = bookmark.Range.Font.Size;
originBackgroundColor = bookmark.Shading.BackgroundPatternColor;
originColor = bookmark.Shading.ForegroundPatternColor;
mark.Selected += OnMarkSelected;
}
SetMarkName();
//InitBookMark(bookmark);
}
private void InitBookMark(Bookmark bookmark)
{
// 创建mark
this.mark = bookmark != null ? bookmark
: DocumentUtil.AddBookmark(content.tag == "i" ? null : content.color, content.start, content.end);
if (mark != null)
{
// 记录目前字体
originSize = mark.Range.Font.Size;
// 设置名称
mark.Name = Config.BuildBookmarkName(content.id);
}
}
private void OnMarkSelectionChange(object sender, Microsoft.Office.Tools.Word.SelectionEventArgs e)
{
//throw new System.NotImplementedException();
//mark.Range.Font.Size = originSize;
//Console.WriteLine("xxx");
}
private void OnMarkSelected(object sender, Microsoft.Office.Tools.Word.SelectionEventArgs e)
{
//throw new System.NotImplementedException();
Bridge.bridge.SelectMarkById(content.id);
}
public void Select()
{
if (mark == null) return;
mark.Range.Font.Size = originSize + 2; // 将选中标签文本放大字体
mark.Select();
}
public void UnSelect()
{
if (mark == null) return;
mark.Range.Font.Size = originSize; // 还原
mark.Shading.ForegroundPatternColor = originColor;
}
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));
}
public void ResetMarkStyle()
{
if (mark == null) return;
if (mark.Range == null) return;
try
{
mark.Range.Font.Size = originSize; // 还原
//mark.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
//originBackgroundColor = bookmark.Shading.BackgroundPatternColor;
mark.Shading.ForegroundPatternColor = originColor;
mark.Range.Shading.BackgroundPatternColor = originColor;// Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
}
catch (Exception) { }
}
public void Process(int status)
{
if (mark == null) return;
//
content.isAccept = status;
if (status == AcceptStatus.Accept)
{
if (content.tag == "r" || content.tag == "i")
{
mark.Text = content.text;
}
else if (content.tag == "d")
{
mark.Text = "";
}
ResetMarkStyle();
}
else if (status == AcceptStatus.Review || status == AcceptStatus.Ignore)
{
ResetMarkStyle();
// 新增添加了空格 所以当忽略时还原
if (content.tag == "i")
{
mark.Text = "";
}
}
else if (status == AcceptStatus.Default)
{
if (content.tag == "r" || content.tag == "d")
{
mark.Text = content.origin;
}
else if (content.tag == "i")
{
mark.Text = ToolUtil.GetBlankText(content.text.Length);
}
SetMarkStyle();
}
}
}
}