Skip to content

Commit

Permalink
Fix duplicate page with hidden title
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Jul 14, 2024
1 parent 93891dd commit 4717eea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
16 changes: 16 additions & 0 deletions OneMore/Commands/Page/DuplicatePageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,33 @@ public override async Task Execute(params object[] args)

// set the page ID to the new page's ID
page.Root.Attribute("ID").Value = newId;

// remove all objectID values and let OneNote generate new IDs
page.Root.Descendants().Attributes("objectID").Remove();
page = new Page(page.Root); // reparse to refresh PageId

var section = await one.GetSection(sectionId);

var editor = new SectionEditor(section);

// restore Title if it's hidden; the Interop API doesn't let us delete Title!
if (page.Title is null)
{
page.SetTitle(page.Root.Attribute("name").Value);
}

editor.SetUniquePageTitle(page);

await one.Update(page);

// FOLLOWING DOESN'T WORK; INTEROP API DOESN'T LET US HIDE (DELETE) Title
//if (hiddenTitle)
//{
// page = await one.GetPage(page.PageId);
// page.Root.Elements(ns + "Title").Remove();
// await one.Update(page);
//}

if (editor.MovePageAfterAnchor(page.PageId, originalId))
{
one.UpdateHierarchy(section);
Expand Down
6 changes: 6 additions & 0 deletions OneMore/Commands/Page/ExtractContentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public override async Task Execute(params object[] args)
return;
}

// restore Title if it's hidden; the Interop API doesn't let us delete Title!
if (page.Title is null)
{
page.SetTitle(page.Root.Attribute("name").Value);
}

var editor = new PageEditor(page);
var content = await editor.ExtractSelectedContent();

Expand Down
17 changes: 11 additions & 6 deletions OneMore/Models/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,12 +1366,17 @@ public void SetTitle(string title)
block = new XElement(ns + "Title",
new Paragraph(title).SetQuickStyle(style.Index));

var outline = Root.Elements(ns + "Outline")
.FirstOrDefault(e => !e.Elements(ns + "Meta")
.Any(m => m.Attribute("name").Value.Equals(MetaNames.TaggingBank)));

outline ??= EnsureContentContainer();
outline.AddBeforeSelf(block);
var anchor = Root.Elements(ns + "PageSettings").FirstOrDefault();
if (anchor is not null)
{
anchor.AddAfterSelf(block);
}
else
{
anchor = Root.Elements(ns + "Outline").FirstOrDefault();
anchor ??= EnsureContentContainer();
anchor.AddBeforeSelf(block);
}
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions OneMore/Models/SectionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public bool SetUniquePageTitle(Page page)
{
// shouldn't happen?
page.Title = $"{page.Title} ({index})";
page.Root.SetAttributeValue("name", page.Title);
return false;
}

Expand Down Expand Up @@ -222,6 +223,7 @@ public bool SetUniquePageTitle(Page page)
}

page.Title = title;
page.Root.SetAttributeValue("name", title);
return true;
}
}
Expand Down

0 comments on commit 4717eea

Please sign in to comment.