-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0d7d3c
commit 0ef02e7
Showing
4 changed files
with
166 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,120 @@ | ||
//************************************************************************************************ | ||
// Copyright © 2021 Steven M Cohn. All rights reserved. | ||
// Copyright © 2021 Steven M Cohn. All rights reserved. | ||
//************************************************************************************************ | ||
|
||
namespace River.OneMoreAddIn.Commands | ||
{ | ||
using River.OneMoreAddIn.Models; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
|
||
/// <summary> | ||
/// Removes all ink drawings and annotations from the current page. | ||
/// </summary> | ||
internal class RemoveInkCommand : Command | ||
{ | ||
private readonly PageSchema schema; | ||
private Page page; | ||
private XNamespace ns; | ||
|
||
|
||
public RemoveInkCommand() | ||
{ | ||
schema = new PageSchema(); | ||
} | ||
|
||
|
||
public override async Task Execute(params object[] args) | ||
{ | ||
await using var one = new OneNote(out var page, out var ns); | ||
var ink = page.Root.Descendants(ns + "InkDrawing"); | ||
await using var one = new OneNote(out page, out ns); | ||
|
||
PageNamespace.Set(ns); | ||
|
||
var ink = page.Root.Descendants(ns + "InkDrawing"); | ||
if (ink.Any()) | ||
{ | ||
ink.ForEach(e => | ||
one.DeleteContent(page.PageId, e.Attribute("objectID").Value)); | ||
ink.ForEach(e => one.DeleteContent(page.PageId, e.Attribute("objectID").Value)); | ||
} | ||
|
||
await Task.Yield(); | ||
var updated = false; | ||
|
||
ink = page.Root.Descendants(ns + "InkWord").Reverse().ToList(); | ||
if (ink is not null && ink.Any()) | ||
{ | ||
foreach (var word in ink) | ||
{ | ||
var parent = word.Parent; | ||
word.Remove(); | ||
updated = true; | ||
|
||
RemoveEmptyContainers(one, parent); | ||
} | ||
} | ||
|
||
ink = page.Root.Descendants(ns + "InkParagraph").Reverse().ToList(); | ||
if (ink is not null && ink.Any()) | ||
{ | ||
foreach (var paragraph in ink) | ||
{ | ||
var parent = paragraph.Parent; | ||
paragraph.Remove(); | ||
updated = true; | ||
|
||
RemoveEmptyContainers(one, parent); | ||
} | ||
} | ||
|
||
if (updated) | ||
{ | ||
await one.Update(page); | ||
} | ||
} | ||
|
||
|
||
// InkParagraph can only exist inside of an OE | ||
// InkWord can existing in an InkParagraph or an OE | ||
private void RemoveEmptyContainers(OneNote one, XElement node) | ||
{ | ||
var parent = node.Parent; | ||
|
||
while (parent is not null) | ||
{ | ||
// OEChildren can only be inside a Cell and an Outline | ||
if (node.Name.LocalName == "Cell") | ||
{ | ||
if (node.Element(ns + "OEChildren") is null) | ||
{ | ||
// don't leave the cell empty | ||
node.Add(new Paragraph(string.Empty)); | ||
} | ||
|
||
break; | ||
} | ||
|
||
// remove empty OE.. OEChildren.. Outline.. | ||
|
||
var names = schema.GetContentNames(node.Name.LocalName); | ||
|
||
if (node.Elements().Any(e => e.Name.LocalName.In(names))) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
node.Remove(); | ||
|
||
if (node.Name.LocalName == "Outline") | ||
{ | ||
one.DeleteContent(page.PageId, node.Attribute("objectID").Value); | ||
break; | ||
} | ||
|
||
node = parent; | ||
parent = node.Parent; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//************************************************************************************************ | ||
// Copyright © 2016 Steven M Cohn. All rights reserved. | ||
//************************************************************************************************ | ||
|
||
namespace River.OneMoreAddIn.Models | ||
{ | ||
|
||
/// <summary> | ||
/// Provides an on-demand instantiation of schema arrays. | ||
/// </summary> | ||
internal sealed class PageSchema | ||
{ | ||
private string[] outHeaders; // Outline info elements | ||
private string[] outContent; // Outline content elements | ||
private string[] oecHeaders; // OEChildren info elements | ||
private string[] oecContent; // OEChildren content elements | ||
private string[] oeHeaders; // OE info elements | ||
private string[] oeContent; // OE content elements | ||
|
||
|
||
// Outline Schema... | ||
|
||
public string[] OutHeaders => outHeaders ??= new string[] | ||
{ | ||
"Postion", "Size", "Meta", "Indents" | ||
}; | ||
|
||
public string[] OutContent => outContent ?? new string[] | ||
{ | ||
"OEChildren" | ||
}; | ||
|
||
|
||
// OEChildren Schema... | ||
|
||
public string[] OecHeaders => oecHeaders ??= new string[] | ||
{ | ||
"ChildOELayout" | ||
}; | ||
|
||
public string[] OecContent => oecContent ??= new string[] | ||
{ | ||
"OE", "HTMLBlock" | ||
}; | ||
|
||
|
||
// OE Schema... | ||
|
||
public string[] OeHeaders => oeHeaders ??= new string[] | ||
{ | ||
"MediaIndex", "Tag", "OutlookTask"/*+Tag?*/, "Meta", "List" | ||
}; | ||
|
||
public string[] OeContent => oeContent ??= new string[] | ||
{ | ||
"Image", "Table", "InkDrawing", "InsertedFile", | ||
"MediaFile", "InkParagraph", "FutureObject", | ||
"T", "InkWord", | ||
"OEChildren", | ||
"LinkedNote" | ||
}; | ||
|
||
|
||
public string[] GetContentNames(string containerName) | ||
{ | ||
if (containerName == "OE") return OeContent; | ||
if (containerName == "OEChildren") return OecContent; | ||
if (containerName == "Outline") return OutContent; | ||
return new string[0]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters