diff --git a/NoteHighlightAddin/AddIn.cs b/NoteHighlightAddin/AddIn.cs index b341050..e9229f8 100644 --- a/NoteHighlightAddin/AddIn.cs +++ b/NoteHighlightAddin/AddIn.cs @@ -139,8 +139,19 @@ private void ShowForm() //form.ShowDialog(); //TestForm t = new TestForm(); + var pageNode = GetPageNode(); + string selectedText = ""; + XElement outline = null; - MainForm form = new MainForm(tag, outFileName); + if (pageNode != null) + { + var existingPageId = pageNode.Attribute("ID").Value; + selectedText = GetSelectedText(existingPageId); + + outline = GetOutline(existingPageId); + } + + MainForm form = new MainForm(tag, outFileName, selectedText); System.Windows.Forms.Application.Run(form); //} @@ -154,7 +165,7 @@ private void ShowForm() if (File.Exists(fileName)) { - InsertHighLightCodeToCurrentSide(fileName, form.Parameters); + InsertHighLightCodeToCurrentSide(fileName, form.Parameters, outline); } } @@ -190,11 +201,31 @@ public IStream GetImage(string imageName) /// 插入 HighLight Code 至滑鼠游標的位置 /// Insert HighLight Code To Mouse Position /// - private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters) + private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters, XElement outline) { // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); string htmlContent = File.ReadAllText(fileName, Encoding.UTF8); + var pageNode = GetPageNode(); + + if (pageNode != null) + { + var existingPageId = pageNode.Attribute("ID").Value; + string[] position=null; + if (outline == null) + { + position = GetMousePointPosition(existingPageId); + } + + var page = InsertHighLightCode(htmlContent, position, parameters, outline); + page.Root.SetAttributeValue("ID", existingPageId); + + OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); + } + } + + XElement GetPageNode() + { string notebookXml; try { @@ -203,7 +234,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete catch (Exception ex) { MessageBox.Show("Exception from onApp.GetHierarchy:" + ex.Message); - return; + return null; ; } var doc = XDocument.Parse(notebookXml); @@ -212,18 +243,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete var pageNode = doc.Descendants(ns + "Page") .Where(n => n.Attribute("isCurrentlyViewed") != null && n.Attribute("isCurrentlyViewed").Value == "true") .FirstOrDefault(); - - if (pageNode != null) - { - var existingPageId = pageNode.Attribute("ID").Value; - - string[] position = GetMousePointPosition(existingPageId); - - var page = InsertHighLightCode(htmlContent, position, parameters); - page.Root.SetAttributeValue("ID", existingPageId); - - OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); - } + return pageNode; } /// @@ -251,11 +271,69 @@ private string[] GetMousePointPosition(string pageID) return null; } + private XElement GetOutline(string pageID) + { + string pageXml; + OneNoteApplication.GetPageContent(pageID, out pageXml, PageInfo.piSelection); + + var node = XDocument.Parse(pageXml).Descendants(ns + "Outline") + .Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all") + .FirstOrDefault(); + //if (node != null) + //{ + // var attrPos = node.Descendants(ns + "Position").FirstOrDefault(); + // if (attrPos != null) + // { + // var x = attrPos.Attribute("x").Value; + // var y = attrPos.Attribute("y").Value; + // return new string[] { x, y }; + // } + //} + //return null; + + return node; + } + + private string GetSelectedText(string pageID) + { + string pageXml; + OneNoteApplication.GetPageContent(pageID, out pageXml, PageInfo.piSelection); + + var node = XDocument.Parse(pageXml).Descendants(ns + "Outline") + .Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all") + .FirstOrDefault(); + + StringBuilder sb = new StringBuilder(); + if (node != null) + { + var table = node.Descendants(ns + "Table").FirstOrDefault(); + + System.Collections.Generic.IEnumerable attrPos; + if (table == null) + { + attrPos = node.Descendants(ns + "OEChildren").Descendants(ns + "T"); + } + else + { + attrPos = table.Descendants(ns + "Cell").LastOrDefault().Descendants(ns + "T"); + } + + foreach (var line in attrPos) + { + var htmlDocument = new HtmlAgilityPack.HtmlDocument(); + htmlDocument.LoadHtml(line.Value); + + sb.AppendLine(HttpUtility.HtmlDecode(htmlDocument.DocumentNode.InnerText)); + } + } + return sb.ToString(); + } + /// /// 產生 XML 插入至 OneNote /// Generate XML Insert To OneNote /// - public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters) + public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters, XElement outline) { XElement children = new XElement(ns + "OEChildren"); @@ -364,29 +442,46 @@ public XDocument InsertHighLightCode(string htmlContent, string[] position, High children.Add(new XElement(ns + "OE", table)); - XElement outline = new XElement(ns + "Outline"); + bool update = false; + if (outline == null) + { + outline = new XElement(ns + "Outline"); - if (position != null && position.Length == 2) + if (position != null && position.Length == 2) + { + XElement pos = new XElement(ns + "Position"); + pos.Add(new XAttribute("x", position[0])); + pos.Add(new XAttribute("y", position[1])); + outline.Add(pos); + + XElement size = new XElement(ns + "Size"); + size.Add(new XAttribute("width", "1600")); + size.Add(new XAttribute("height", "200")); + outline.Add(size); + } + } + else { - XElement pos = new XElement(ns + "Position"); - pos.Add(new XAttribute("x", position[0])); - pos.Add(new XAttribute("y", position[1])); - outline.Add(pos); - - XElement size = new XElement(ns + "Size"); - size.Add(new XAttribute("width", "1600")); - size.Add(new XAttribute("height", "200")); - outline.Add(size); + update = true; + outline.RemoveNodes(); } - outline.Add(children); - XElement page = new XElement(ns + "Page"); - page.Add(outline); + outline.Add(children); + if (update) + { + return outline.Parent.Document; + } + else + { + XElement page = new XElement(ns + "Page"); + page.Add(outline); - XDocument doc = new XDocument(); - doc.Add(page); + XDocument doc = new XDocument(); + doc.Add(page); + return doc; + } - return doc; + } } diff --git a/NoteHighlightAddin/MainForm.cs b/NoteHighlightAddin/MainForm.cs index 757b8e2..6cd4196 100644 --- a/NoteHighlightAddin/MainForm.cs +++ b/NoteHighlightAddin/MainForm.cs @@ -50,12 +50,13 @@ public partial class MainForm : Form #region -- Constructor -- - public MainForm(string codeType, string fileName) + public MainForm(string codeType, string fileName, string selectedText) { _codeType = codeType; _fileName = fileName; InitializeComponent(); LoadThemes(); + txtCode.Text = selectedText; } diff --git a/NoteHighlightAddin/NoteHighlightAddin.csproj b/NoteHighlightAddin/NoteHighlightAddin.csproj index 4486d79..b1d1c16 100644 --- a/NoteHighlightAddin/NoteHighlightAddin.csproj +++ b/NoteHighlightAddin/NoteHighlightAddin.csproj @@ -75,6 +75,10 @@ True + + ..\packages\HtmlAgilityPack.1.5.1\lib\Net45\HtmlAgilityPack.dll + True + False Lib\ICSharpCode.TextEditor.dll @@ -159,6 +163,7 @@ + Designer diff --git a/NoteHighlightAddin/Properties/AssemblyInfo.cs b/NoteHighlightAddin/Properties/AssemblyInfo.cs index e897066..f481f36 100644 --- a/NoteHighlightAddin/Properties/AssemblyInfo.cs +++ b/NoteHighlightAddin/Properties/AssemblyInfo.cs @@ -33,5 +33,5 @@ // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("2.3.*")] -[assembly: AssemblyFileVersion("2.3.*")] +[assembly: AssemblyVersion("2.4.*")] +[assembly: AssemblyFileVersion("2.4.*")] diff --git a/NoteHighlightAddin/packages.config b/NoteHighlightAddin/packages.config new file mode 100644 index 0000000..d7a3447 --- /dev/null +++ b/NoteHighlightAddin/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Setup/Setup.vdproj b/Setup/Setup.vdproj index adc8e09..252a8bf 100644 --- a/Setup/Setup.vdproj +++ b/Setup/Setup.vdproj @@ -39,6 +39,12 @@ } "Entry" { + "MsmKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5" + "OwnerKey" = "8:_692D7E391D074D36AD2530720FB8D6C4" + "MsmSig" = "8:_UNDEFINED" + } + "Entry" + { "MsmKey" = "8:_692D7E391D074D36AD2530720FB8D6C4" "OwnerKey" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED" @@ -94,6 +100,12 @@ "Entry" { "MsmKey" = "8:_UNDEFINED" + "OwnerKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5" + "MsmSig" = "8:_UNDEFINED" + } + "Entry" + { + "MsmKey" = "8:_UNDEFINED" "OwnerKey" = "8:_CBF51C0C38434D629FC3271C5390E247" "MsmSig" = "8:_UNDEFINED" } @@ -278,7 +290,7 @@ { "AssemblyRegister" = "3:1" "AssemblyIsInGAC" = "11:FALSE" - "AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.0.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL" + "AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.3.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL" "ScatterAssemblies" { "_0FFDD9474C2B804E2E2B2C7C6360577B" @@ -345,6 +357,37 @@ "IsDependency" = "11:FALSE" "IsolateTo" = "8:" } + "{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_5F7DADAECA95B8A8EECB662A9FCF5BB5" + { + "AssemblyRegister" = "3:1" + "AssemblyIsInGAC" = "11:FALSE" + "AssemblyAsmDisplayName" = "8:HtmlAgilityPack, Version=1.5.1.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL" + "ScatterAssemblies" + { + "_5F7DADAECA95B8A8EECB662A9FCF5BB5" + { + "Name" = "8:HtmlAgilityPack.dll" + "Attributes" = "3:512" + } + } + "SourcePath" = "8:HtmlAgilityPack.dll" + "TargetName" = "8:" + "Tag" = "8:" + "Folder" = "8:_76DE0C3E4C3C41398EBF2960C9F81C36" + "Condition" = "8:" + "Transitive" = "11:FALSE" + "Vital" = "11:TRUE" + "ReadOnly" = "11:FALSE" + "Hidden" = "11:FALSE" + "System" = "11:FALSE" + "Permanent" = "11:FALSE" + "SharedLegacy" = "11:FALSE" + "PackageAs" = "3:1" + "Register" = "3:1" + "Exclude" = "11:FALSE" + "IsDependency" = "11:TRUE" + "IsolateTo" = "8:" + } "{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_805E0317B9E0B1539EEBB800C533393B" { "AssemblyRegister" = "3:1" @@ -571,15 +614,15 @@ { "Name" = "8:NoteHighlightAddin" "ProductName" = "8:NoteHighlight2016" - "ProductCode" = "8:{60F24B03-C32C-4E84-8E4C-DF696910E5C2}" - "PackageCode" = "8:{17A94C9D-5142-4CC1-93D4-3C8896C40A95}" + "ProductCode" = "8:{C35EA8DF-ED92-4011-B998-B66F28C01EAF}" + "PackageCode" = "8:{CB51AF31-9D66-464C-8F45-70CE7425C909}" "UpgradeCode" = "8:{0025873C-20C5-48D6-A93A-FBD3891A9233}" "AspNetVersion" = "8:4.0.30319.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:TRUE" - "ProductVersion" = "8:2.3" + "ProductVersion" = "8:2.4" "Manufacturer" = "8:CodingRoad" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:" diff --git a/SetupX86/SetupX86.vdproj b/SetupX86/SetupX86.vdproj index 46c147a..b582cf9 100644 --- a/SetupX86/SetupX86.vdproj +++ b/SetupX86/SetupX86.vdproj @@ -39,6 +39,12 @@ } "Entry" { + "MsmKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5" + "OwnerKey" = "8:_692D7E391D074D36AD2530720FB8D6C4" + "MsmSig" = "8:_UNDEFINED" + } + "Entry" + { "MsmKey" = "8:_692D7E391D074D36AD2530720FB8D6C4" "OwnerKey" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED" @@ -94,6 +100,12 @@ "Entry" { "MsmKey" = "8:_UNDEFINED" + "OwnerKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5" + "MsmSig" = "8:_UNDEFINED" + } + "Entry" + { + "MsmKey" = "8:_UNDEFINED" "OwnerKey" = "8:_CBF51C0C38434D629FC3271C5390E247" "MsmSig" = "8:_UNDEFINED" } @@ -278,7 +290,7 @@ { "AssemblyRegister" = "3:1" "AssemblyIsInGAC" = "11:FALSE" - "AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.0.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL" + "AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.3.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL" "ScatterAssemblies" { "_0FFDD9474C2B804E2E2B2C7C6360577B" @@ -345,6 +357,37 @@ "IsDependency" = "11:FALSE" "IsolateTo" = "8:" } + "{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_5F7DADAECA95B8A8EECB662A9FCF5BB5" + { + "AssemblyRegister" = "3:1" + "AssemblyIsInGAC" = "11:FALSE" + "AssemblyAsmDisplayName" = "8:HtmlAgilityPack, Version=1.5.1.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL" + "ScatterAssemblies" + { + "_5F7DADAECA95B8A8EECB662A9FCF5BB5" + { + "Name" = "8:HtmlAgilityPack.dll" + "Attributes" = "3:512" + } + } + "SourcePath" = "8:HtmlAgilityPack.dll" + "TargetName" = "8:" + "Tag" = "8:" + "Folder" = "8:_76DE0C3E4C3C41398EBF2960C9F81C36" + "Condition" = "8:" + "Transitive" = "11:FALSE" + "Vital" = "11:TRUE" + "ReadOnly" = "11:FALSE" + "Hidden" = "11:FALSE" + "System" = "11:FALSE" + "Permanent" = "11:FALSE" + "SharedLegacy" = "11:FALSE" + "PackageAs" = "3:1" + "Register" = "3:1" + "Exclude" = "11:FALSE" + "IsDependency" = "11:TRUE" + "IsolateTo" = "8:" + } "{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_805E0317B9E0B1539EEBB800C533393B" { "AssemblyRegister" = "3:1" @@ -571,15 +614,15 @@ { "Name" = "8:NoteHighlightAddin" "ProductName" = "8:NoteHighlight2016" - "ProductCode" = "8:{0320059E-5CCF-4D53-B546-1AC788EEFFF2}" - "PackageCode" = "8:{2BF3BB79-955B-46E2-B3D5-C5B8566F417D}" + "ProductCode" = "8:{1862FC60-5BBC-45DD-BE18-0F98044FB64A}" + "PackageCode" = "8:{B193D63A-E356-46B1-8B65-F53A5A95D116}" "UpgradeCode" = "8:{0025873C-20C5-48D6-A93A-FBD3891A9233}" "AspNetVersion" = "8:4.0.30319.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:TRUE" - "ProductVersion" = "8:2.3" + "ProductVersion" = "8:2.4" "Manufacturer" = "8:CodingRoad" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:"