Skip to content

Commit

Permalink
Remove divider line along with last removed foonote
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmcohn committed Apr 3, 2020
1 parent 4f072f8 commit 4b00764
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions OneMore/Commands/Footnotes/FootnoteEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ private bool WriteFootnoteRef(string label)

//=======================================================================================

/// <summary>
/// Refreshes the label numbers so that all references are sequentially ordered starting
/// at 1 from the top of the page. This is needed when adding a new reference prior to
/// existing ones or deleting a reference.
/// </summary>
private void RefreshLabels()
{
var refs = FindSelectedReferences(page.Descendants(ns + "T"), true);
Expand Down Expand Up @@ -340,9 +345,6 @@ private void RefreshLabels()
note.Element.Remove();
}

// make sure divider is set
EnsureFootnoteFooter();

var previous = divider;
foreach (var note in notes)
{
Expand All @@ -359,6 +361,7 @@ private List<FootnoteReference> FindSelectedReferences(IEnumerable<XElement> roo
? @"vertical-align:super[;'""].*>\[(\d+)\]</span>"
: @"\[(\d+)\]";

// find selected "[\d]" labels
var list = roots.DescendantNodes().OfType<XCData>()
.Select(CData => new
{
Expand All @@ -375,6 +378,7 @@ private List<FootnoteReference> FindSelectedReferences(IEnumerable<XElement> roo
})
.ToList();

// find selected footnote text lines
foreach (var root in roots)
{
var meta = root.Parent.Elements(ns + "Meta")
Expand All @@ -386,15 +390,19 @@ private List<FootnoteReference> FindSelectedReferences(IEnumerable<XElement> roo
})
.FirstOrDefault();

if (!list.Any(e => e.Label == meta.Label))
if ((meta != null) && !list.Any(e => e.Label == meta.Label))
{
list.Add(new FootnoteReference
var match = Regex.Match(meta.CData.Value, @"\[(\d+)\]");
if (match.Success)
{
CData = meta.CData,
Label = meta.Label,
Index = 0,
Length = 0
});
list.Add(new FootnoteReference
{
CData = meta.CData,
Label = meta.Label,
Index = match.Groups[1].Index,
Length = match.Groups[1].Length
});
}
}
}

Expand All @@ -413,10 +421,6 @@ private List<FootnoteReference> FindSelectedReferences(IEnumerable<XElement> roo
/// </remarks>
public void RemoveFootnote()
{

System.Diagnostics.Debugger.Launch();


// find all selected paragraph
var elements = page.Elements(ns + "Outline")
.Where(e => e.Attributes("selected").Any())
Expand Down Expand Up @@ -485,7 +489,23 @@ public void RemoveFootnote()
}
}

RefreshLabels();
// make sure divider is set
EnsureFootnoteFooter();

var remaining = divider.NodesAfterSelf().OfType<XElement>().Elements(ns + "Meta")
.Any(e => e.Attribute("name").Value.Equals("omfootnote"));

if (remaining)
{
// must be some footnotes so resequence them
RefreshLabels();
}
else
{
// no footnotes left so remove divider line
divider.Remove();
}

manager.UpdatePageContent(page);
}

Expand Down

0 comments on commit 4b00764

Please sign in to comment.