Skip to content

Commit 5bfddf2

Browse files
committed
Improved when create a new item is available
also refactored a bit.
1 parent 53a703e commit 5bfddf2

File tree

1 file changed

+120
-57
lines changed

1 file changed

+120
-57
lines changed

NotebookExplorer.cs

Lines changed: 120 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class NotebookExplorer
1515

1616
private ResultCreator rc;
1717

18-
private List<Result> NoResults => new List<Result>();
1918

2019
public NotebookExplorer(PluginInitContext context, OneNotePlugin oneNotePlugin, ResultCreator resultCreator)
2120
{
@@ -33,77 +32,141 @@ public List<Result> Explore(Query query)
3332
switch (searchStrings.Length)
3433
{
3534
case 2://Full query for notebook not complete e.g. nb\User Noteb
36-
//Get matching notebooks and create results.
37-
searchString = searchStrings[1];
35+
//Get matching notebooks and create results.
36+
return GetNotebooks(searchStrings);
3837

39-
if (string.IsNullOrWhiteSpace(searchString)) // Do a normall notebook search
40-
{
41-
LastSelectedNotebook = null;
42-
return OneNoteProvider.NotebookItems.Select(nb => rc.CreateNotebookResult(nb)).ToList();
43-
}
38+
case 3://Full query for section not complete e.g nb\User Notebook\Happine
39+
return GetSections(searchStrings);
4440

45-
return OneNoteProvider.NotebookItems.Where(nb =>
46-
{
47-
if (LastSelectedNotebook != null && nb.ID == LastSelectedNotebook.ID)
48-
return true;
41+
case 4://Searching pages in a section
42+
return GetPages(searchStrings);
4943

50-
return TreeQuery(nb.Name, searchString, out highlightData);
51-
})
52-
.Select(nb => rc.CreateNotebookResult(nb, highlightData))
53-
.Append(rc.CreateNewNotebookResult(searchString))
54-
.ToList();
44+
default:
45+
return new List<Result>();
46+
}
47+
}
5548

56-
case 3://Full query for section not complete e.g nb\User Notebook\Happine
57-
searchString = searchStrings[2];
49+
private List<Result> GetNotebooks(string[] searchStrings)
50+
{
51+
List<Result> results = new List<Result>();
52+
string query = searchStrings[1];
53+
54+
if (string.IsNullOrWhiteSpace(query)) // Do a normal notebook search
55+
{
56+
LastSelectedNotebook = null;
57+
results = OneNoteProvider.NotebookItems.Select(nb => rc.CreateNotebookResult(nb)).ToList();
58+
return results;
59+
}
60+
List<int> highlightData = null;
5861

59-
if (!ValidateNotebook(searchStrings[1]))
60-
return NoResults;
62+
results = OneNoteProvider.NotebookItems.Where(nb =>
63+
{
64+
if (LastSelectedNotebook != null && nb.ID == LastSelectedNotebook.ID)
65+
return true;
6166

62-
if (string.IsNullOrWhiteSpace(searchString))
63-
{
64-
//TODO: if no sections/page show default result type name to create section/page
65-
LastSelectedSection = null;
66-
return LastSelectedNotebook.Sections.Where(s => !s.Encrypted)
67-
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook))
68-
.ToList();
69-
}
70-
return LastSelectedNotebook.Sections.Where(s =>
71-
{
72-
if(s.Encrypted)
73-
return false;
74-
75-
if (LastSelectedSection != null && s.ID == LastSelectedSection.ID)
76-
return true;
77-
78-
return TreeQuery(s.Name, searchString, out highlightData);
79-
})
80-
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook, highlightData))
81-
.Append(rc.CreateNewSectionResult(LastSelectedNotebook,searchString))
82-
.ToList();
67+
return TreeQuery(nb.Name, query, out highlightData);
68+
})
69+
.Select(nb => rc.CreateNotebookResult(nb, highlightData))
70+
.ToList();
8371

84-
case 4://Searching pages in a section
85-
searchString = searchStrings[3];
72+
if (!results.Any(result => string.Equals(query.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
73+
{
74+
results.Add(rc.CreateNewNotebookResult(query));
75+
}
76+
return results;
77+
}
8678

87-
if (!ValidateNotebook(searchStrings[1]))
88-
return NoResults;
79+
private List<Result> GetSections(string[] searchStrings)
80+
{
81+
List<Result> results = new List<Result>();
8982

90-
if (!ValidateSection(searchStrings[2]))
91-
return NoResults;
83+
string query = searchStrings[2];
9284

93-
if (string.IsNullOrWhiteSpace(searchString))
94-
return LastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg,LastSelectedSection, LastSelectedNotebook)).ToList();
85+
if (!ValidateNotebook(searchStrings[1]))
86+
return results;
9587

96-
return LastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData))
97-
.Select(pg => rc.CreatePageResult(pg, LastSelectedSection, LastSelectedNotebook, highlightData))
98-
.Append(rc.CreateNewPageResult(LastSelectedSection, LastSelectedNotebook, searchString))
88+
if (string.IsNullOrWhiteSpace(query))
89+
{
90+
LastSelectedSection = null;
91+
results = LastSelectedNotebook.Sections.Where(s => !s.Encrypted)
92+
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook))
9993
.ToList();
10094

101-
default:
102-
return NoResults;
95+
//if no sections show ability to create section
96+
if (!results.Any())
97+
{
98+
results.Add(new Result
99+
{
100+
Title = "Create section: \"\"",
101+
SubTitle = "No (unecrypted) sections found. Type a valid title to create one",
102+
IcoPath = Icons.NewSection
103+
});
104+
}
105+
return results;
103106
}
104-
107+
108+
List<int> highlightData = null;
109+
110+
results = LastSelectedNotebook.Sections.Where(s =>
111+
{
112+
if (s.Encrypted)
113+
return false;
114+
115+
if (LastSelectedSection != null && s.ID == LastSelectedSection.ID)
116+
return true;
117+
118+
return TreeQuery(s.Name, query, out highlightData);
119+
})
120+
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook, highlightData))
121+
.ToList();
122+
if (!results.Any(result => string.Equals(query.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
123+
{
124+
results.Add(rc.CreateNewSectionResult(LastSelectedNotebook, query));
125+
}
126+
return results;
127+
}
128+
129+
private List<Result> GetPages(string[] searchStrings)
130+
{
131+
List<Result> results = new List<Result>();
132+
133+
string query = searchStrings[3];
134+
135+
if (!ValidateNotebook(searchStrings[1]))
136+
return results;
137+
138+
if (!ValidateSection(searchStrings[2]))
139+
return results;
140+
141+
if (string.IsNullOrWhiteSpace(query))
142+
{
143+
results = LastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg, LastSelectedSection, LastSelectedNotebook)).ToList();
144+
//if no sections show ability to create section
145+
if (!results.Any())
146+
{
147+
results.Add(new Result
148+
{
149+
Title = "Create page: \"\"",
150+
SubTitle = "No pages found. Type a valid title to create one",
151+
IcoPath = Icons.NewPage
152+
});
153+
}
154+
return results;
155+
}
156+
157+
List<int> highlightData = null;
158+
159+
results = LastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, query, out highlightData))
160+
.Select(pg => rc.CreatePageResult(pg, LastSelectedSection, LastSelectedNotebook, highlightData))
161+
.ToList();
162+
if (!results.Any(result => string.Equals(query.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
163+
{
164+
results.Add(rc.CreateNewPageResult(LastSelectedSection, LastSelectedNotebook, query));
165+
}
166+
return results;
105167
}
106-
168+
169+
107170
private bool ValidateNotebook(string notebookName)
108171
{
109172
if (LastSelectedNotebook == null)

0 commit comments

Comments
 (0)