Skip to content

Commit ef9f4b7

Browse files
committed
Added ability to create pages, sections and notebooks
1 parent da7735a commit ef9f4b7

File tree

5 files changed

+170
-12
lines changed

5 files changed

+170
-12
lines changed

Main.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public List<Result> Query(Query query)
8282
Score = int.MinValue,
8383
Action = c =>
8484
{
85+
//TODO Create method OpenAndSync
8586
OneNoteProvider.PageItems.First().OpenInOneNote();
8687
OneNoteProvider.NotebookItems.Sync();
8788
return false;
@@ -143,6 +144,7 @@ public List<Result> Query(Query query)
143144

144145
public List<Result> LoadContextMenus(Result selectedResult)
145146
{
147+
//TODO: Clean up
146148
switch (selectedResult.ContextData)
147149
{
148150
case IOneNoteExtNotebook notebook:
@@ -206,6 +208,7 @@ private static string GetLastEdited(TimeSpan diff)
206208
return lastEdited;
207209
else
208210
return lastEdited += "Now.";
211+
209212
bool PluralCheck(double totalTime, string timeType, ref string lastEdited)
210213
{
211214
var roundedTime = (int)Math.Round(totalTime);

NotebookExplorer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public List<Result> Explore(Query query)
5050
return TreeQuery(nb.Name, searchString, out highlightData);
5151
})
5252
.Select(nb => rc.CreateNotebookResult(nb, highlightData))
53+
.Append(rc.CreateNewNotebookResult(searchString))
5354
.ToList();
5455

5556
case 3://Full query for section not complete e.g nb\User Notebook\Happine
@@ -60,6 +61,7 @@ public List<Result> Explore(Query query)
6061

6162
if (string.IsNullOrWhiteSpace(searchString))
6263
{
64+
//TODO: if no sections/page show default result type name to create section/page
6365
LastSelectedSection = null;
6466
return LastSelectedNotebook.Sections.Where(s => !s.Encrypted)
6567
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook))
@@ -76,6 +78,7 @@ public List<Result> Explore(Query query)
7678
return TreeQuery(s.Name, searchString, out highlightData);
7779
})
7880
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook, highlightData))
81+
.Append(rc.CreateNewSectionResult(LastSelectedNotebook,searchString))
7982
.ToList();
8083

8184
case 4://Searching pages in a section
@@ -91,7 +94,8 @@ public List<Result> Explore(Query query)
9194
return LastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg,LastSelectedSection, LastSelectedNotebook)).ToList();
9295

9396
return LastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData))
94-
.Select(pg => rc.CreatePageResult(pg,LastSelectedSection, LastSelectedNotebook, highlightData))
97+
.Select(pg => rc.CreatePageResult(pg, LastSelectedSection, LastSelectedNotebook, highlightData))
98+
.Append(rc.CreateNewPageResult(LastSelectedSection, LastSelectedNotebook, searchString))
9599
.ToList();
96100

97101
default:

ResultCreator.cs

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
using System;
23
using System.Collections.Generic;
34
using ScipBe.Common.Office.OneNote;
45

@@ -22,18 +23,29 @@ public ResultCreator(PluginInitContext context, OneNotePlugin oneNotePlugin)
2223
notebookInfo = new OneNoteItemInfo("NotebookIcons", "notebook.png", context);
2324
sectionInfo = new OneNoteItemInfo("SectionIcons", "section.png", context);
2425
}
26+
27+
private string GetNicePath(IOneNoteSection section, IOneNoteNotebook notebook, bool isPage)
28+
{
29+
int offset = isPage
30+
? 4 //"4" is to remove the ".one" from the path
31+
: section.Name.Length + 5; //The "+5" is to remove the ".one" and "/" from the path
32+
var sectionPath = section.Path;
33+
var index = sectionPath.IndexOf(notebook.Name);
34+
var path = sectionPath[index..^offset]
35+
.Replace("/", " > ")
36+
.Replace("\\", " > ");
37+
return path;
38+
}
39+
40+
2541
public Result CreatePageResult(IOneNoteExtPage page, List<int> highlightingData = null)
2642
{
2743
return CreatePageResult(page, page.Section, page.Notebook, highlightingData);
2844
}
2945

3046
public Result CreatePageResult(IOneNotePage page, IOneNoteSection section, IOneNoteNotebook notebook, List<int> highlightingData = null)
3147
{
32-
var sectionPath = section.Path;
33-
var index = sectionPath.IndexOf(notebook.Name);
34-
var path = sectionPath[index..^4] //"+4" is to remove the ".one" from the path
35-
.Replace("/", " > ")
36-
.Replace("\\", " > ");
48+
var path = GetNicePath(section, notebook, true);
3749
return new Result
3850
{
3951
Title = page.Name,
@@ -55,12 +67,7 @@ public Result CreatePageResult(IOneNotePage page, IOneNoteSection section, IOneN
5567

5668
public Result CreateSectionResult(IOneNoteExtSection section, IOneNoteExtNotebook notebook, List<int> highlightData = null)
5769
{
58-
var sectionPath = section.Path;
59-
var index = sectionPath.IndexOf(notebook.Name);
60-
var path = sectionPath[index..^(section.Name.Length + 5)] //The "+5" is to remove the ".one" and "/" from the path
61-
.Replace("/", " > ")
62-
.Replace("\\", " > ");
63-
70+
var path = GetNicePath(section, notebook, false);
6471
return new Result
6572
{
6673
Title = section.Name,
@@ -93,5 +100,61 @@ public Result CreateNotebookResult(IOneNoteExtNotebook notebook, List<int> highl
93100
},
94101
};
95102
}
103+
public Result CreateNewPageResult(IOneNoteSection section, IOneNoteNotebook notebook, string pageTitle)
104+
{
105+
pageTitle = pageTitle.Trim();
106+
return new Result
107+
{
108+
Title = $"Create page: \"{pageTitle}\"",
109+
SubTitle = $"Path: {GetNicePath(section,notebook,true)}",
110+
//IcoPath = Constants.LogoIconPath,
111+
Action = c =>
112+
{
113+
ScipBeExtensions.CreateAndOpenPage(LastSelectedSection, pageTitle);
114+
LastSelectedNotebook = null;
115+
LastSelectedSection = null;
116+
return true;
117+
}
118+
};
119+
}
120+
121+
public Result CreateNewSectionResult(IOneNoteNotebook notebook, string sectionTitle)
122+
{
123+
sectionTitle = sectionTitle.Trim();
124+
return new Result
125+
{
126+
Title = $"Create section: \"{sectionTitle}\"",
127+
SubTitle = $"Path: {notebook.Name}",
128+
Action = c =>
129+
{
130+
ScipBeExtensions.CreateAndOpenSection(LastSelectedNotebook,sectionTitle);
131+
context.API.ChangeQuery(context.CurrentPluginMetadata.ActionKeyword);
132+
LastSelectedNotebook = null;
133+
LastSelectedSection = null;
134+
return true;
135+
}
136+
};
137+
}
138+
139+
public Result CreateNewNotebookResult(string notebookTitle)
140+
{
141+
notebookTitle = notebookTitle.Trim();
142+
return new Result
143+
{
144+
Title = $"Create notebook: \"{notebookTitle}\"",
145+
//TitleHighlightData = context.API.FuzzySearch(notebookTitle,title).MatchData,
146+
SubTitle = $"Location: {ScipBeExtensions.GetDefaultNotebookLocation()}",
147+
//IcoPath =
148+
Action = c =>
149+
{
150+
ScipBeExtensions.CreateAndOpenNotebook(context,notebookTitle);
151+
context.API.ChangeQuery(context.CurrentPluginMetadata.ActionKeyword);
152+
LastSelectedNotebook = null;
153+
LastSelectedSection = null;
154+
return true;
155+
}
156+
};
157+
}
158+
96159
}
97160
}

ScipBeExtensions.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Xml.Linq;
4+
using Microsoft.Office.Interop.OneNote;
25
using ScipBe.Common.Office.OneNote;
36
using static Flow.Launcher.Plugin.OneNote.ScipBeUtils.Utils;
47

@@ -51,5 +54,70 @@ public static void Sync(this IEnumerable<IOneNoteNotebook> items)
5154
}
5255
});
5356
}
57+
58+
public static string GetDefaultNotebookLocation()
59+
{
60+
return CallOneNoteSafely(onenote =>
61+
{
62+
onenote.GetSpecialLocation(SpecialLocation.slDefaultNotebookFolder, out string path);
63+
return path;
64+
});
65+
}
66+
67+
public static string GetDefaultPageLocation()
68+
{
69+
return CallOneNoteSafely(onenote =>
70+
{
71+
onenote.GetSpecialLocation(SpecialLocation.slUnfiledNotesSection, out string path);
72+
return path;
73+
});
74+
}
75+
76+
public static void CreateAndOpenPage(IOneNoteSection section, string pageTitle)
77+
{
78+
CallOneNoteSafely(onenote =>
79+
{
80+
onenote.GetHierarchy(null, HierarchyScope.hsNotebooks, out string xmlNb);
81+
82+
XNamespace ns = XDocument.Parse(xmlNb).Root.Name.Namespace;
83+
84+
onenote.CreateNewPage(section.ID, out string pageID, NewPageStyle.npsBlankPageWithTitle);
85+
86+
onenote.GetPageContent(pageID, out string xml, PageInfo.piBasic);
87+
var doc = XDocument.Parse(xml);
88+
var Xtitle = doc.Descendants(ns + "T").First();
89+
Xtitle.Value = pageTitle;
90+
91+
onenote.UpdatePageContent(doc.ToString());
92+
93+
onenote.SyncHierarchy(pageID);
94+
onenote.NavigateTo(pageID);
95+
});
96+
}
97+
98+
public static void CreateAndOpenSection(this IOneNoteNotebook notebook, string title)
99+
{
100+
CallOneNoteSafely(onenote =>
101+
{
102+
onenote.OpenHierarchy(title + ".one", notebook.ID, out string sectionID, CreateFileType.cftSection);
103+
104+
onenote.SyncHierarchy(sectionID);
105+
onenote.NavigateTo(sectionID);
106+
});
107+
}
108+
109+
public static void CreateAndOpenNotebook(PluginInitContext context,string title)
110+
{
111+
CallOneNoteSafely(onenote =>
112+
{
113+
onenote.GetSpecialLocation(SpecialLocation.slDefaultNotebookFolder, out string path);
114+
115+
onenote.OpenHierarchy($"{path}\\{title}", null, out string notebookID, CreateFileType.cftNotebook);
116+
117+
onenote.SyncHierarchy(notebookID);
118+
onenote.NavigateTo(notebookID);
119+
});
120+
}
121+
54122
}
55123
}

ScipBeUtils.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,25 @@ internal static void CallOneNoteSafely(Action<Application> action)
8686
}
8787
}
8888
}
89+
internal static T CallOneNoteSafely<T>(Func<Application, T> action)
90+
{
91+
Application oneNote = null;
92+
try
93+
{
94+
oneNote = TryCatchAndRetry<Application, COMException>(
95+
() => new Application(),
96+
TimeSpan.FromMilliseconds(100),
97+
3,
98+
ex => System.Diagnostics.Trace.TraceError(ex.Message));
99+
return action(oneNote);
100+
}
101+
finally
102+
{
103+
if (oneNote != null)
104+
{
105+
Marshal.ReleaseComObject(oneNote);
106+
}
107+
}
108+
}
89109
}
90110
}

0 commit comments

Comments
 (0)