@@ -15,7 +15,6 @@ public class NotebookExplorer
15
15
16
16
private ResultCreator rc ;
17
17
18
- private List < Result > NoResults => new List < Result > ( ) ;
19
18
20
19
public NotebookExplorer ( PluginInitContext context , OneNotePlugin oneNotePlugin , ResultCreator resultCreator )
21
20
{
@@ -33,77 +32,141 @@ public List<Result> Explore(Query query)
33
32
switch ( searchStrings . Length )
34
33
{
35
34
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 ) ;
38
37
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 ) ;
44
40
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 ) ;
49
43
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
+ }
55
48
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 ;
58
61
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 ;
61
66
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 ( ) ;
83
71
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
+ }
86
78
87
- if ( ! ValidateNotebook ( searchStrings [ 1 ] ) )
88
- return NoResults ;
79
+ private List < Result > GetSections ( string [ ] searchStrings )
80
+ {
81
+ List < Result > results = new List < Result > ( ) ;
89
82
90
- if ( ! ValidateSection ( searchStrings [ 2 ] ) )
91
- return NoResults ;
83
+ string query = searchStrings [ 2 ] ;
92
84
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 ;
95
87
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 ) )
99
93
. ToList ( ) ;
100
94
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 ;
103
106
}
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 ;
105
167
}
106
-
168
+
169
+
107
170
private bool ValidateNotebook ( string notebookName )
108
171
{
109
172
if ( LastSelectedNotebook == null )
0 commit comments