@@ -139,8 +139,19 @@ private void ShowForm()
139
139
//form.ShowDialog();
140
140
141
141
//TestForm t = new TestForm();
142
+ var pageNode = GetPageNode ( ) ;
143
+ string selectedText = "" ;
144
+ XElement outline = null ;
142
145
143
- MainForm form = new MainForm ( tag , outFileName ) ;
146
+ if ( pageNode != null )
147
+ {
148
+ var existingPageId = pageNode . Attribute ( "ID" ) . Value ;
149
+ selectedText = GetSelectedText ( existingPageId ) ;
150
+
151
+ outline = GetOutline ( existingPageId ) ;
152
+ }
153
+
154
+ MainForm form = new MainForm ( tag , outFileName , selectedText ) ;
144
155
145
156
System . Windows . Forms . Application . Run ( form ) ;
146
157
//}
@@ -154,7 +165,7 @@ private void ShowForm()
154
165
155
166
if ( File . Exists ( fileName ) )
156
167
{
157
- InsertHighLightCodeToCurrentSide ( fileName , form . Parameters ) ;
168
+ InsertHighLightCodeToCurrentSide ( fileName , form . Parameters , outline ) ;
158
169
}
159
170
}
160
171
@@ -190,11 +201,31 @@ public IStream GetImage(string imageName)
190
201
/// 插入 HighLight Code 至滑鼠游標的位置
191
202
/// Insert HighLight Code To Mouse Position
192
203
/// </summary>
193
- private void InsertHighLightCodeToCurrentSide ( string fileName , HighLightParameter parameters )
204
+ private void InsertHighLightCodeToCurrentSide ( string fileName , HighLightParameter parameters , XElement outline )
194
205
{
195
206
// Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name);
196
207
string htmlContent = File . ReadAllText ( fileName , Encoding . UTF8 ) ;
197
208
209
+ var pageNode = GetPageNode ( ) ;
210
+
211
+ if ( pageNode != null )
212
+ {
213
+ var existingPageId = pageNode . Attribute ( "ID" ) . Value ;
214
+ string [ ] position = null ;
215
+ if ( outline == null )
216
+ {
217
+ position = GetMousePointPosition ( existingPageId ) ;
218
+ }
219
+
220
+ var page = InsertHighLightCode ( htmlContent , position , parameters , outline ) ;
221
+ page . Root . SetAttributeValue ( "ID" , existingPageId ) ;
222
+
223
+ OneNoteApplication . UpdatePageContent ( page . ToString ( ) , DateTime . MinValue ) ;
224
+ }
225
+ }
226
+
227
+ XElement GetPageNode ( )
228
+ {
198
229
string notebookXml ;
199
230
try
200
231
{
@@ -203,7 +234,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete
203
234
catch ( Exception ex )
204
235
{
205
236
MessageBox . Show ( "Exception from onApp.GetHierarchy:" + ex . Message ) ;
206
- return ;
237
+ return null ; ;
207
238
}
208
239
209
240
var doc = XDocument . Parse ( notebookXml ) ;
@@ -212,18 +243,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete
212
243
var pageNode = doc . Descendants ( ns + "Page" )
213
244
. Where ( n => n . Attribute ( "isCurrentlyViewed" ) != null && n . Attribute ( "isCurrentlyViewed" ) . Value == "true" )
214
245
. FirstOrDefault ( ) ;
215
-
216
- if ( pageNode != null )
217
- {
218
- var existingPageId = pageNode . Attribute ( "ID" ) . Value ;
219
-
220
- string [ ] position = GetMousePointPosition ( existingPageId ) ;
221
-
222
- var page = InsertHighLightCode ( htmlContent , position , parameters ) ;
223
- page . Root . SetAttributeValue ( "ID" , existingPageId ) ;
224
-
225
- OneNoteApplication . UpdatePageContent ( page . ToString ( ) , DateTime . MinValue ) ;
226
- }
246
+ return pageNode ;
227
247
}
228
248
229
249
/// <summary>
@@ -251,11 +271,69 @@ private string[] GetMousePointPosition(string pageID)
251
271
return null ;
252
272
}
253
273
274
+ private XElement GetOutline ( string pageID )
275
+ {
276
+ string pageXml ;
277
+ OneNoteApplication . GetPageContent ( pageID , out pageXml , PageInfo . piSelection ) ;
278
+
279
+ var node = XDocument . Parse ( pageXml ) . Descendants ( ns + "Outline" )
280
+ . Where ( n => n . Attribute ( "selected" ) != null && n . Attribute ( "selected" ) . Value == "all" )
281
+ . FirstOrDefault ( ) ;
282
+ //if (node != null)
283
+ //{
284
+ // var attrPos = node.Descendants(ns + "Position").FirstOrDefault();
285
+ // if (attrPos != null)
286
+ // {
287
+ // var x = attrPos.Attribute("x").Value;
288
+ // var y = attrPos.Attribute("y").Value;
289
+ // return new string[] { x, y };
290
+ // }
291
+ //}
292
+ //return null;
293
+
294
+ return node ;
295
+ }
296
+
297
+ private string GetSelectedText ( string pageID )
298
+ {
299
+ string pageXml ;
300
+ OneNoteApplication . GetPageContent ( pageID , out pageXml , PageInfo . piSelection ) ;
301
+
302
+ var node = XDocument . Parse ( pageXml ) . Descendants ( ns + "Outline" )
303
+ . Where ( n => n . Attribute ( "selected" ) != null && n . Attribute ( "selected" ) . Value == "all" )
304
+ . FirstOrDefault ( ) ;
305
+
306
+ StringBuilder sb = new StringBuilder ( ) ;
307
+ if ( node != null )
308
+ {
309
+ var table = node . Descendants ( ns + "Table" ) . FirstOrDefault ( ) ;
310
+
311
+ System . Collections . Generic . IEnumerable < XElement > attrPos ;
312
+ if ( table == null )
313
+ {
314
+ attrPos = node . Descendants ( ns + "OEChildren" ) . Descendants ( ns + "T" ) ;
315
+ }
316
+ else
317
+ {
318
+ attrPos = table . Descendants ( ns + "Cell" ) . LastOrDefault ( ) . Descendants ( ns + "T" ) ;
319
+ }
320
+
321
+ foreach ( var line in attrPos )
322
+ {
323
+ var htmlDocument = new HtmlAgilityPack . HtmlDocument ( ) ;
324
+ htmlDocument . LoadHtml ( line . Value ) ;
325
+
326
+ sb . AppendLine ( HttpUtility . HtmlDecode ( htmlDocument . DocumentNode . InnerText ) ) ;
327
+ }
328
+ }
329
+ return sb . ToString ( ) ;
330
+ }
331
+
254
332
/// <summary>
255
333
/// 產生 XML 插入至 OneNote
256
334
/// Generate XML Insert To OneNote
257
335
/// </summary>
258
- public XDocument InsertHighLightCode ( string htmlContent , string [ ] position , HighLightParameter parameters )
336
+ public XDocument InsertHighLightCode ( string htmlContent , string [ ] position , HighLightParameter parameters , XElement outline )
259
337
{
260
338
XElement children = new XElement ( ns + "OEChildren" ) ;
261
339
@@ -364,29 +442,46 @@ public XDocument InsertHighLightCode(string htmlContent, string[] position, High
364
442
children . Add ( new XElement ( ns + "OE" ,
365
443
table ) ) ;
366
444
367
- XElement outline = new XElement ( ns + "Outline" ) ;
445
+ bool update = false ;
446
+ if ( outline == null )
447
+ {
448
+ outline = new XElement ( ns + "Outline" ) ;
368
449
369
- if ( position != null && position . Length == 2 )
450
+ if ( position != null && position . Length == 2 )
451
+ {
452
+ XElement pos = new XElement ( ns + "Position" ) ;
453
+ pos . Add ( new XAttribute ( "x" , position [ 0 ] ) ) ;
454
+ pos . Add ( new XAttribute ( "y" , position [ 1 ] ) ) ;
455
+ outline . Add ( pos ) ;
456
+
457
+ XElement size = new XElement ( ns + "Size" ) ;
458
+ size . Add ( new XAttribute ( "width" , "1600" ) ) ;
459
+ size . Add ( new XAttribute ( "height" , "200" ) ) ;
460
+ outline . Add ( size ) ;
461
+ }
462
+ }
463
+ else
370
464
{
371
- XElement pos = new XElement ( ns + "Position" ) ;
372
- pos . Add ( new XAttribute ( "x" , position [ 0 ] ) ) ;
373
- pos . Add ( new XAttribute ( "y" , position [ 1 ] ) ) ;
374
- outline . Add ( pos ) ;
375
-
376
- XElement size = new XElement ( ns + "Size" ) ;
377
- size . Add ( new XAttribute ( "width" , "1600" ) ) ;
378
- size . Add ( new XAttribute ( "height" , "200" ) ) ;
379
- outline . Add ( size ) ;
465
+ update = true ;
466
+ outline . RemoveNodes ( ) ;
380
467
}
381
- outline . Add ( children ) ;
382
468
383
- XElement page = new XElement ( ns + "Page" ) ;
384
- page . Add ( outline ) ;
469
+ outline . Add ( children ) ;
470
+ if ( update )
471
+ {
472
+ return outline . Parent . Document ;
473
+ }
474
+ else
475
+ {
476
+ XElement page = new XElement ( ns + "Page" ) ;
477
+ page . Add ( outline ) ;
385
478
386
- XDocument doc = new XDocument ( ) ;
387
- doc . Add ( page ) ;
479
+ XDocument doc = new XDocument ( ) ;
480
+ doc . Add ( page ) ;
481
+ return doc ;
482
+ }
388
483
389
- return doc ;
484
+
390
485
}
391
486
392
487
}
0 commit comments