Skip to content

Commit c4f331e

Browse files
committed
Merge branch 'format-existig-code'
2 parents 1f44b77 + ad06c06 commit c4f331e

File tree

7 files changed

+236
-45
lines changed

7 files changed

+236
-45
lines changed

NoteHighlightAddin/AddIn.cs

+129-34
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,19 @@ private void ShowForm()
139139
//form.ShowDialog();
140140

141141
//TestForm t = new TestForm();
142+
var pageNode = GetPageNode();
143+
string selectedText = "";
144+
XElement outline = null;
142145

143-
MainForm form = new MainForm(tag, outFileName);
146+
if (pageNode != null)
147+
{
148+
var existingPageId = pageNode.Attribute("ID").Value;
149+
selectedText = GetSelectedText(existingPageId);
150+
151+
outline = GetOutline(existingPageId);
152+
}
153+
154+
MainForm form = new MainForm(tag, outFileName, selectedText);
144155

145156
System.Windows.Forms.Application.Run(form);
146157
//}
@@ -154,7 +165,7 @@ private void ShowForm()
154165

155166
if (File.Exists(fileName))
156167
{
157-
InsertHighLightCodeToCurrentSide(fileName, form.Parameters);
168+
InsertHighLightCodeToCurrentSide(fileName, form.Parameters, outline);
158169
}
159170
}
160171

@@ -190,11 +201,31 @@ public IStream GetImage(string imageName)
190201
/// 插入 HighLight Code 至滑鼠游標的位置
191202
/// Insert HighLight Code To Mouse Position
192203
/// </summary>
193-
private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters)
204+
private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters, XElement outline)
194205
{
195206
// Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name);
196207
string htmlContent = File.ReadAllText(fileName, Encoding.UTF8);
197208

209+
var pageNode = GetPageNode();
210+
211+
if (pageNode != null)
212+
{
213+
var existingPageId = pageNode.Attribute("ID").Value;
214+
string[] position=null;
215+
if (outline == null)
216+
{
217+
position = GetMousePointPosition(existingPageId);
218+
}
219+
220+
var page = InsertHighLightCode(htmlContent, position, parameters, outline);
221+
page.Root.SetAttributeValue("ID", existingPageId);
222+
223+
OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue);
224+
}
225+
}
226+
227+
XElement GetPageNode()
228+
{
198229
string notebookXml;
199230
try
200231
{
@@ -203,7 +234,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete
203234
catch (Exception ex)
204235
{
205236
MessageBox.Show("Exception from onApp.GetHierarchy:" + ex.Message);
206-
return;
237+
return null; ;
207238
}
208239

209240
var doc = XDocument.Parse(notebookXml);
@@ -212,18 +243,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete
212243
var pageNode = doc.Descendants(ns + "Page")
213244
.Where(n => n.Attribute("isCurrentlyViewed") != null && n.Attribute("isCurrentlyViewed").Value == "true")
214245
.FirstOrDefault();
215-
216-
if (pageNode != null)
217-
{
218-
var existingPageId = pageNode.Attribute("ID").Value;
219-
220-
string[] position = GetMousePointPosition(existingPageId);
221-
222-
var page = InsertHighLightCode(htmlContent, position, parameters);
223-
page.Root.SetAttributeValue("ID", existingPageId);
224-
225-
OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue);
226-
}
246+
return pageNode;
227247
}
228248

229249
/// <summary>
@@ -251,11 +271,69 @@ private string[] GetMousePointPosition(string pageID)
251271
return null;
252272
}
253273

274+
private XElement GetOutline(string pageID)
275+
{
276+
string pageXml;
277+
OneNoteApplication.GetPageContent(pageID, out pageXml, PageInfo.piSelection);
278+
279+
var node = XDocument.Parse(pageXml).Descendants(ns + "Outline")
280+
.Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")
281+
.FirstOrDefault();
282+
//if (node != null)
283+
//{
284+
// var attrPos = node.Descendants(ns + "Position").FirstOrDefault();
285+
// if (attrPos != null)
286+
// {
287+
// var x = attrPos.Attribute("x").Value;
288+
// var y = attrPos.Attribute("y").Value;
289+
// return new string[] { x, y };
290+
// }
291+
//}
292+
//return null;
293+
294+
return node;
295+
}
296+
297+
private string GetSelectedText(string pageID)
298+
{
299+
string pageXml;
300+
OneNoteApplication.GetPageContent(pageID, out pageXml, PageInfo.piSelection);
301+
302+
var node = XDocument.Parse(pageXml).Descendants(ns + "Outline")
303+
.Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")
304+
.FirstOrDefault();
305+
306+
StringBuilder sb = new StringBuilder();
307+
if (node != null)
308+
{
309+
var table = node.Descendants(ns + "Table").FirstOrDefault();
310+
311+
System.Collections.Generic.IEnumerable<XElement> attrPos;
312+
if (table == null)
313+
{
314+
attrPos = node.Descendants(ns + "OEChildren").Descendants(ns + "T");
315+
}
316+
else
317+
{
318+
attrPos = table.Descendants(ns + "Cell").LastOrDefault().Descendants(ns + "T");
319+
}
320+
321+
foreach (var line in attrPos)
322+
{
323+
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
324+
htmlDocument.LoadHtml(line.Value);
325+
326+
sb.AppendLine(HttpUtility.HtmlDecode(htmlDocument.DocumentNode.InnerText));
327+
}
328+
}
329+
return sb.ToString();
330+
}
331+
254332
/// <summary>
255333
/// 產生 XML 插入至 OneNote
256334
/// Generate XML Insert To OneNote
257335
/// </summary>
258-
public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters)
336+
public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters, XElement outline)
259337
{
260338
XElement children = new XElement(ns + "OEChildren");
261339

@@ -364,29 +442,46 @@ public XDocument InsertHighLightCode(string htmlContent, string[] position, High
364442
children.Add(new XElement(ns + "OE",
365443
table));
366444

367-
XElement outline = new XElement(ns + "Outline");
445+
bool update = false;
446+
if (outline == null)
447+
{
448+
outline = new XElement(ns + "Outline");
368449

369-
if (position != null && position.Length == 2)
450+
if (position != null && position.Length == 2)
451+
{
452+
XElement pos = new XElement(ns + "Position");
453+
pos.Add(new XAttribute("x", position[0]));
454+
pos.Add(new XAttribute("y", position[1]));
455+
outline.Add(pos);
456+
457+
XElement size = new XElement(ns + "Size");
458+
size.Add(new XAttribute("width", "1600"));
459+
size.Add(new XAttribute("height", "200"));
460+
outline.Add(size);
461+
}
462+
}
463+
else
370464
{
371-
XElement pos = new XElement(ns + "Position");
372-
pos.Add(new XAttribute("x", position[0]));
373-
pos.Add(new XAttribute("y", position[1]));
374-
outline.Add(pos);
375-
376-
XElement size = new XElement(ns + "Size");
377-
size.Add(new XAttribute("width", "1600"));
378-
size.Add(new XAttribute("height", "200"));
379-
outline.Add(size);
465+
update = true;
466+
outline.RemoveNodes();
380467
}
381-
outline.Add(children);
382468

383-
XElement page = new XElement(ns + "Page");
384-
page.Add(outline);
469+
outline.Add(children);
470+
if (update)
471+
{
472+
return outline.Parent.Document;
473+
}
474+
else
475+
{
476+
XElement page = new XElement(ns + "Page");
477+
page.Add(outline);
385478

386-
XDocument doc = new XDocument();
387-
doc.Add(page);
479+
XDocument doc = new XDocument();
480+
doc.Add(page);
481+
return doc;
482+
}
388483

389-
return doc;
484+
390485
}
391486

392487
}

NoteHighlightAddin/MainForm.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ public partial class MainForm : Form
5050

5151
#region -- Constructor --
5252

53-
public MainForm(string codeType, string fileName)
53+
public MainForm(string codeType, string fileName, string selectedText)
5454
{
5555
_codeType = codeType;
5656
_fileName = fileName;
5757
InitializeComponent();
5858
LoadThemes();
59+
txtCode.Text = selectedText;
5960

6061
}
6162

NoteHighlightAddin/NoteHighlightAddin.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
<Reference Include="extensibility, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
7676
<EmbedInteropTypes>True</EmbedInteropTypes>
7777
</Reference>
78+
<Reference Include="HtmlAgilityPack, Version=1.5.1.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
79+
<HintPath>..\packages\HtmlAgilityPack.1.5.1\lib\Net45\HtmlAgilityPack.dll</HintPath>
80+
<Private>True</Private>
81+
</Reference>
7882
<Reference Include="ICSharpCode.TextEditor, Version=3.0.0.3437, Culture=neutral, PublicKeyToken=4d61825e8dd49f1a, processorArchitecture=MSIL">
7983
<SpecificVersion>False</SpecificVersion>
8084
<HintPath>Lib\ICSharpCode.TextEditor.dll</HintPath>
@@ -159,6 +163,7 @@
159163
<Content Include="Resources\SQL.png" />
160164
<Content Include="Resources\XHTML.png" />
161165
<Content Include="Resources\XML.png" />
166+
<None Include="packages.config" />
162167
<None Include="ribbon.xml">
163168
<SubType>Designer</SubType>
164169
</None>

NoteHighlightAddin/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333
//
3434
// You can specify all the values or you can default the Build and Revision Numbers
3535
// by using the '*' as shown below:
36-
[assembly: AssemblyVersion("2.3.*")]
37-
[assembly: AssemblyFileVersion("2.3.*")]
36+
[assembly: AssemblyVersion("2.4.*")]
37+
[assembly: AssemblyFileVersion("2.4.*")]

NoteHighlightAddin/packages.config

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="HtmlAgilityPack" version="1.5.1" targetFramework="net45" />
4+
</packages>

Setup/Setup.vdproj

+47-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
}
4040
"Entry"
4141
{
42+
"MsmKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5"
43+
"OwnerKey" = "8:_692D7E391D074D36AD2530720FB8D6C4"
44+
"MsmSig" = "8:_UNDEFINED"
45+
}
46+
"Entry"
47+
{
4248
"MsmKey" = "8:_692D7E391D074D36AD2530720FB8D6C4"
4349
"OwnerKey" = "8:_UNDEFINED"
4450
"MsmSig" = "8:_UNDEFINED"
@@ -94,6 +100,12 @@
94100
"Entry"
95101
{
96102
"MsmKey" = "8:_UNDEFINED"
103+
"OwnerKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5"
104+
"MsmSig" = "8:_UNDEFINED"
105+
}
106+
"Entry"
107+
{
108+
"MsmKey" = "8:_UNDEFINED"
97109
"OwnerKey" = "8:_CBF51C0C38434D629FC3271C5390E247"
98110
"MsmSig" = "8:_UNDEFINED"
99111
}
@@ -278,7 +290,7 @@
278290
{
279291
"AssemblyRegister" = "3:1"
280292
"AssemblyIsInGAC" = "11:FALSE"
281-
"AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.0.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL"
293+
"AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.3.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL"
282294
"ScatterAssemblies"
283295
{
284296
"_0FFDD9474C2B804E2E2B2C7C6360577B"
@@ -345,6 +357,37 @@
345357
"IsDependency" = "11:FALSE"
346358
"IsolateTo" = "8:"
347359
}
360+
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_5F7DADAECA95B8A8EECB662A9FCF5BB5"
361+
{
362+
"AssemblyRegister" = "3:1"
363+
"AssemblyIsInGAC" = "11:FALSE"
364+
"AssemblyAsmDisplayName" = "8:HtmlAgilityPack, Version=1.5.1.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL"
365+
"ScatterAssemblies"
366+
{
367+
"_5F7DADAECA95B8A8EECB662A9FCF5BB5"
368+
{
369+
"Name" = "8:HtmlAgilityPack.dll"
370+
"Attributes" = "3:512"
371+
}
372+
}
373+
"SourcePath" = "8:HtmlAgilityPack.dll"
374+
"TargetName" = "8:"
375+
"Tag" = "8:"
376+
"Folder" = "8:_76DE0C3E4C3C41398EBF2960C9F81C36"
377+
"Condition" = "8:"
378+
"Transitive" = "11:FALSE"
379+
"Vital" = "11:TRUE"
380+
"ReadOnly" = "11:FALSE"
381+
"Hidden" = "11:FALSE"
382+
"System" = "11:FALSE"
383+
"Permanent" = "11:FALSE"
384+
"SharedLegacy" = "11:FALSE"
385+
"PackageAs" = "3:1"
386+
"Register" = "3:1"
387+
"Exclude" = "11:FALSE"
388+
"IsDependency" = "11:TRUE"
389+
"IsolateTo" = "8:"
390+
}
348391
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_805E0317B9E0B1539EEBB800C533393B"
349392
{
350393
"AssemblyRegister" = "3:1"
@@ -571,15 +614,15 @@
571614
{
572615
"Name" = "8:NoteHighlightAddin"
573616
"ProductName" = "8:NoteHighlight2016"
574-
"ProductCode" = "8:{60F24B03-C32C-4E84-8E4C-DF696910E5C2}"
575-
"PackageCode" = "8:{17A94C9D-5142-4CC1-93D4-3C8896C40A95}"
617+
"ProductCode" = "8:{C35EA8DF-ED92-4011-B998-B66F28C01EAF}"
618+
"PackageCode" = "8:{CB51AF31-9D66-464C-8F45-70CE7425C909}"
576619
"UpgradeCode" = "8:{0025873C-20C5-48D6-A93A-FBD3891A9233}"
577620
"AspNetVersion" = "8:4.0.30319.0"
578621
"RestartWWWService" = "11:FALSE"
579622
"RemovePreviousVersions" = "11:TRUE"
580623
"DetectNewerInstalledVersion" = "11:TRUE"
581624
"InstallAllUsers" = "11:TRUE"
582-
"ProductVersion" = "8:2.3"
625+
"ProductVersion" = "8:2.4"
583626
"Manufacturer" = "8:CodingRoad"
584627
"ARPHELPTELEPHONE" = "8:"
585628
"ARPHELPLINK" = "8:"

0 commit comments

Comments
 (0)