forked from FDscend/fdscend_word_addin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighlightForm.cs
114 lines (98 loc) · 4.21 KB
/
HighlightForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using Microsoft.Office.Interop.Word;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WordAddIn1
{
public partial class HighlightForm : UserControl
{
string FDscendHome = Ribbon1.FDscendHome;
string highlight_index = Ribbon1.highlight_index;
string online_url = @"https://highlightjs.org/demo";
bool mobileMod = false;
public static JObject ImportJSON(string jsonfile)
{
StreamReader reader = File.OpenText(jsonfile);
JsonTextReader jsonTextReader = new JsonTextReader(reader);
JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
reader.Close();
return jsonObject;
}
public HighlightForm()
{
InitializeComponent();
InitializeWebView2Async();
JObject js = ImportJSON(Ribbon1.PresetHighlight);
online_url = js["onlineMod"]["url"].ToString();
mobileMod = (bool)js["onlineMod"]["mobileMod"];
#if DEBUG
webView21.Location = new System.Drawing.Point(0, 80);
#endif
#if !DEBUG
webView21.Location = new System.Drawing.Point(0, 0);
#endif
insertCode.Width = 0;
onlineMod.Left = insertCode.Left;
onlineMod.Text = "切换离线模式";
this.Resize += new System.EventHandler(this.Form_Resize);
}
private async void InitializeWebView2Async()
{
var env = await CoreWebView2Environment.CreateAsync(null, FDscendHome);
await webView21.EnsureCoreWebView2Async(env);
if (mobileMod)
{
webView21.CoreWebView2.Settings.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148";
}
webView21.CoreWebView2.Navigate(online_url);
}
private void Form_Resize(object sender, EventArgs e)
{
webView21.Size = this.ClientSize - new System.Drawing.Size(webView21.Location);
onlineMod.Width = this.ClientSize.Width - insertCode.Width + insertCode.Left;
}
private void insertCode_Click(object sender, EventArgs e)
{
webView21.CoreWebView2.ExecuteScriptAsync($"document.getElementById(\"codeContent\").innerHTML=\"\"");
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in Globals.ThisAddIn.Application.Selection.Paragraphs)
{
string codeContent = paragraph.Range.Text;
codeContent = codeContent.Substring(0, codeContent.Length - 1);
webView21.CoreWebView2.ExecuteScriptAsync($"document.getElementById(\"codeContent\").innerHTML+=\"{codeContent}\"\n");
}
//string codeContent = Globals.ThisAddIn.Application.Selection.Text;
//codeContent = codeContent.Substring(0, codeContent.Length - 1);
//MessageBox.Show(codeContent);
//webView21.CoreWebView2.ExecuteScriptAsync($"document.getElementById(\"codeContent\").textContent=unescape(\"{codeContent}\")");
//webView21.CoreWebView2.ExecuteScriptAsync("hljs.highlightAll()");
}
private void onlineMod_Click(object sender, EventArgs e)
{
if (insertCode.Width != 0)
{
insertCode.Width = 0;
onlineMod.Width = this.ClientSize.Width;
onlineMod.Left = insertCode.Left;
onlineMod.Text = "切换离线模式";
webView21.CoreWebView2.Navigate(online_url);
}
else
{
insertCode.Width = 122;
onlineMod.Left = insertCode.Left + insertCode.Width;
onlineMod.Text = "切换在线模式";
webView21.CoreWebView2.Navigate(highlight_index);
}
}
}
}