Skip to content

Commit

Permalink
1730 remove inkwords (#1731)
Browse files Browse the repository at this point in the history
* Remove 2016 style InkWord and InkParagraph
#1730

* #1730
  • Loading branch information
stevencohn authored Dec 26, 2024
1 parent e0d7d3c commit 0ef02e7
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 60 deletions.
97 changes: 91 additions & 6 deletions OneMore/Commands/Clean/RemoveInkCommand.cs
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;
}
}
}
}
}
56 changes: 2 additions & 54 deletions OneMore/Models/PageEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,61 +26,9 @@ private sealed class Snippet
}


#region Schema
/// <summary>
/// Provides an on-demand instantiation of schema arrays.
/// </summary>
private sealed class Schema
{
private string[] outHeaders; // Outline info 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 static string[] OutContent => ...

// 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"
};
}
#endregion Schema


private readonly Page page;
private readonly XNamespace ns;
private Schema schema;
private PageSchema schema;


public PageEditor(Page page)
Expand Down Expand Up @@ -692,7 +640,7 @@ public bool EditNode(XElement cursor, Func<XNode, XNode> edit)
/// <returns>An OEChildren XElement</returns>
public async Task<XElement> ExtractSelectedContent(XElement targetOutline = null)
{
schema = new Schema();
schema = new PageSchema();

var content = new XElement(ns + "OEChildren");

Expand Down
72 changes: 72 additions & 0 deletions OneMore/Models/PageSchema.cs
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];
}
}
}
1 change: 1 addition & 0 deletions OneMore/OneMore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@
<Compile Include="Models\PageEditor.cs" />
<Compile Include="AddInHotkeys.cs" />
<Compile Include="Models\PageReader.cs" />
<Compile Include="Models\PageSchema.cs" />
<Compile Include="Models\SectionEditor.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down

0 comments on commit 0ef02e7

Please sign in to comment.