Skip to content

Commit 8e56809

Browse files
authored
Merge pull request #6 from Odotocodot/dev
- **Added ability to create notebooks, sections and pages!** - Improved readme. - Fixed typos. - Refactored code.
2 parents 58fb275 + f1dc78d commit 8e56809

18 files changed

+425
-184
lines changed

Constants.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
namespace Flow.Launcher.Plugin.OneNote
22
{
3-
public static class Constants
3+
public static class Icons
44
{
5-
public const string LogoIconPath = "Images/logo.png";
6-
public const string UnavailableIconPath = "Images/unavailable.png";
7-
public const string SyncIconPath = "Images/refresh.png";
8-
public const string RecentIconPath = "Images/recent.png";
9-
public const string RecentPageIconPath = "Images/recent_page.png";
10-
public const string WarningLogoPath = "Images/warning.png";
11-
12-
public const string StructureKeyword = "nb:\\";
13-
public const string RecentKeyword = "rcntpgs:";
5+
6+
public const string Logo = "Images/logo.png";
7+
public const string Unavailable = "Images/unavailable.png";
8+
public const string Sync = "Images/refresh.png";
9+
public const string Recent = "Images/recent.png";
10+
public const string RecentPage = "Images/recent_page.png";
11+
public const string Warning = "Images/warning.png";
12+
13+
public const string Section = "Images/section.png";
14+
public const string Notebook = "Images/notebook.png";
15+
public const string NewPage = "Images/new_page.png";
16+
public const string NewSection = "Images/new_section.png";
17+
public const string NewNotebook = "Images/new_notebook.png";
18+
}
19+
public static class Keywords
20+
{
21+
public const string NotebookExplorer = "nb:\\";
22+
public const string RecentPages = "rcntpgs:";
1423
}
1524
}

Images/new_notebook.png

2.67 KB
Loading

Images/new_page.png

7.85 KB
Loading

Images/new_section.png

2.83 KB
Loading

Main.cs

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -42,57 +42,68 @@ public List<Result> Query(Query query)
4242
new Result
4343
{
4444
Title = "OneNote is not installed.",
45-
IcoPath = Constants.UnavailableIconPath
45+
IcoPath = Icons.Unavailable
4646
}
4747
};
4848
}
4949
if (string.IsNullOrEmpty(query.Search))
5050
{
51-
var results = new List<Result>();
52-
results.Add(new Result
51+
return new List<Result>()
5352
{
54-
Title = "Search OneNote pages",
55-
SubTitle = $"Type \"{Constants.StructureKeyword}\" to search by notebook structure or select this option",
56-
AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}",
57-
IcoPath = Constants.LogoIconPath,
58-
Score = 2000,
59-
Action = c =>
53+
new Result
6054
{
61-
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}");
62-
return false;
55+
Title = "Search OneNote pages",
56+
SubTitle = $"Type \"{Keywords.NotebookExplorer}\" or select this option to search by notebook structure ",
57+
AutoCompleteText = $"{query.ActionKeyword} {Keywords.NotebookExplorer}",
58+
IcoPath = Icons.Logo,
59+
Score = 2000,
60+
Action = c =>
61+
{
62+
context.API.ChangeQuery($"{query.ActionKeyword} {Keywords.NotebookExplorer}");
63+
return false;
64+
},
6365
},
64-
});
65-
results.Add(new Result
66-
{
67-
Title = "See recent pages",
68-
SubTitle = $"Type \"{Constants.RecentKeyword}\" to see last modified pages or select this option",
69-
AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}",
70-
IcoPath = Constants.RecentIconPath,
71-
Score = -1000,
72-
Action = c =>
66+
new Result
7367
{
74-
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.RecentKeyword}");
75-
return false;
68+
Title = "See recent pages",
69+
SubTitle = $"Type \"{Keywords.RecentPages}\" or select this option to see recently modified pages",
70+
AutoCompleteText = $"{query.ActionKeyword} {Keywords.RecentPages}",
71+
IcoPath = Icons.Recent,
72+
Score = -1000,
73+
Action = c =>
74+
{
75+
context.API.ChangeQuery($"{query.ActionKeyword} {Keywords.RecentPages}");
76+
return false;
77+
},
7678
},
77-
});
78-
results.Add(new Result
79-
{
80-
Title = "Open and sync notebooks",
81-
IcoPath = Constants.SyncIconPath,
82-
Score = int.MinValue,
83-
Action = c =>
79+
new Result
8480
{
85-
OneNoteProvider.PageItems.First().OpenInOneNote();
86-
OneNoteProvider.NotebookItems.Sync();
87-
return false;
88-
}
89-
});
90-
return results;
81+
Title = "New quick note",
82+
IcoPath = Icons.NewPage,
83+
Score = -4000,
84+
Action = c =>
85+
{
86+
ScipBeExtensions.CreateAndOpenPage();
87+
return true;
88+
}
89+
},
90+
new Result
91+
{
92+
Title = "Open and sync notebooks",
93+
IcoPath = Icons.Sync,
94+
Score = int.MinValue,
95+
Action = c =>
96+
{
97+
OneNoteProvider.NotebookItems.OpenAndSync(OneNoteProvider.PageItems.First());
98+
return false;
99+
}
100+
},
101+
};
91102
}
92-
if (query.FirstSearch.StartsWith(Constants.RecentKeyword))
103+
if (query.FirstSearch.StartsWith(Keywords.RecentPages))
93104
{
94105
int count = recentPagesCount;
95-
if (query.FirstSearch.Length > Constants.RecentKeyword.Length && int.TryParse(query.FirstSearch[Constants.RecentKeyword.Length..], out int userChosenCount))
106+
if (query.FirstSearch.Length > Keywords.RecentPages.Length && int.TryParse(query.FirstSearch[Keywords.RecentPages.Length..], out int userChosenCount))
96107
count = userChosenCount;
97108

98109
return OneNoteProvider.PageItems.OrderByDescending(pg => pg.LastModified)
@@ -101,15 +112,15 @@ public List<Result> Query(Query query)
101112
{
102113
Result result = rc.CreatePageResult(pg);
103114
result.SubTitle = $"{GetLastEdited(DateTime.Now - pg.LastModified)}\t{result.SubTitle}";
104-
result.IcoPath = Constants.RecentPageIconPath;
115+
result.IcoPath = Icons.RecentPage;
105116
return result;
106117
})
107118
.ToList();
108119
}
109120

110121
//Search via notebook structure
111122
//NOTE: There is no nested sections i.e. there is nothing for the Section Group in the structure
112-
if (query.FirstSearch.StartsWith(Constants.StructureKeyword))
123+
if (query.FirstSearch.StartsWith(Keywords.NotebookExplorer))
113124
return notebookExplorer.Explore(query);
114125

115126
//Check for invalid start of query i.e. symbols
@@ -120,7 +131,7 @@ public List<Result> Query(Query query)
120131
{
121132
Title = "Invalid query",
122133
SubTitle = "The first character of the search must be a letter or a digit",
123-
IcoPath = Constants.WarningLogoPath,
134+
IcoPath = Icons.Warning,
124135
}
125136
};
126137
//Default search
@@ -136,13 +147,14 @@ public List<Result> Query(Query query)
136147
{
137148
Title = "No matches found",
138149
SubTitle = "Try searching something else, or syncing your notebooks.",
139-
IcoPath = Constants.LogoIconPath,
150+
IcoPath = Icons.Logo,
140151
}
141152
};
142153
}
143154

144155
public List<Result> LoadContextMenus(Result selectedResult)
145156
{
157+
List<Result> results = new List<Result>();
146158
switch (selectedResult.ContextData)
147159
{
148160
case IOneNoteExtNotebook notebook:
@@ -152,26 +164,20 @@ public List<Result> LoadContextMenus(Result selectedResult)
152164
result.ContextData = null;
153165
result.Action = c =>
154166
{
155-
notebook.Sections.First().Pages
156-
.OrderByDescending(pg => pg.LastModified)
157-
.First()
158-
.OpenInOneNote();
159-
notebook.Sync();
167+
notebook.OpenAndSync();
160168
lastSelectedNotebook = null;
161169
return true;
162170
};
163-
return new List<Result> { result };
171+
results.Add(result);
172+
break;
164173
case IOneNoteExtSection section:
165174
Result sResult = rc.CreateSectionResult(section, lastSelectedNotebook);
166175
sResult.Title = "Open and sync section";
167176
sResult.SubTitle = section.Name;
168177
sResult.ContextData = null;
169178
sResult.Action = c =>
170179
{
171-
section.Pages.OrderByDescending(pg => pg.LastModified)
172-
.First()
173-
.OpenInOneNote();
174-
section.Sync();
180+
section.OpenAndSync();
175181
lastSelectedNotebook = null;
176182
lastSelectedSection = null;
177183
return true;
@@ -181,31 +187,29 @@ public List<Result> LoadContextMenus(Result selectedResult)
181187
nbResult.SubTitle = lastSelectedNotebook.Name;
182188
nbResult.Action = c =>
183189
{
184-
lastSelectedNotebook.Sections.First().Pages
185-
.OrderByDescending(pg => pg.LastModified)
186-
.First()
187-
.OpenInOneNote();
188-
lastSelectedNotebook.Sync();
190+
lastSelectedNotebook.OpenAndSync();
189191
lastSelectedNotebook = null;
190192
lastSelectedSection = null;
191193
return true;
192194
};
193-
return new List<Result> { sResult, nbResult, };
194-
default:
195-
return new List<Result>();
195+
results.Add(sResult);
196+
results.Add(nbResult);
197+
break;
196198
}
199+
return results;
197200
}
198201

199202
private static string GetLastEdited(TimeSpan diff)
200203
{
201-
string lastEdited = "Last editied ";
204+
string lastEdited = "Last edited ";
202205
if (PluralCheck(diff.TotalDays, "day", ref lastEdited)
203206
|| PluralCheck(diff.TotalHours, "hour", ref lastEdited)
204207
|| PluralCheck(diff.TotalMinutes, "min", ref lastEdited)
205208
|| PluralCheck(diff.TotalSeconds, "sec", ref lastEdited))
206209
return lastEdited;
207210
else
208211
return lastEdited += "Now.";
212+
209213
bool PluralCheck(double totalTime, string timeType, ref string lastEdited)
210214
{
211215
var roundedTime = (int)Math.Round(totalTime);

0 commit comments

Comments
 (0)