Skip to content

Commit

Permalink
Merge pull request #1198 from stevencohn/1197-apply-spacing
Browse files Browse the repository at this point in the history
Apply line spacing with entire theme
  • Loading branch information
stevencohn authored Dec 1, 2023
2 parents 68eae13 + 5ba196a commit 4a2dd34
Showing 1 changed file with 64 additions and 39 deletions.
103 changes: 64 additions & 39 deletions OneMore/Commands/Styles/ApplyStylesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,56 +103,70 @@ private bool ApplyPageColor(string color)

private bool ApplyStyles(List<Style> styles)
{
var applied = false;

var quickStyles = page.Root.Elements(ns + "QuickStyleDef");
if (quickStyles?.Any() == true)
if (quickStyles == null || !quickStyles.Any())
{
return false;
}

string spacing = null;
if (FindStyle(styles, "p") is Style normal)
{
foreach (var quick in quickStyles)
if (double.TryParse(normal.Spacing, out var spc) && spc > 0.0)
{
var name = quick.Attribute("name").Value;
spacing = normal.Spacing;
}
}

// NOTE previously, affected only the first occurance of the "p" quick style
// and ignore all additional "p" occurances defined, QuickStyleDef[@name='p']
// I don't remember why but pulled out that logic Feb 7 2022
var applied = false;
foreach (var quick in quickStyles)
{
var name = quick.Attribute("name").Value;

if (FindStyle(styles, name) is Style style)
{
//logger.WriteLine(
// $"~ name:{quick.Attribute("name").Value} style:{style.Name}");
// NOTE previously, affected only the first occurance of the "p" quick style
// and ignore all additional "p" occurances defined, QuickStyleDef[@name='p']
// I don't remember why but pulled out that logic Feb 7 2022

// could use QuickStyleDef class here but this is faster
// than replacing the element...
if (FindStyle(styles, name) is Style style)
{
//logger.WriteLine(
// $"~ name:{quick.Attribute("name").Value} style:{style.Name}");

quick.Attribute("font").Value = style.FontFamily;
// could use QuickStyleDef class here but this is faster
// than replacing the element...

quick.Attribute("spaceBefore").Value = style.SpaceBefore;
quick.Attribute("spaceAfter").Value = style.SpaceAfter;
// must also apply to paragraphs otherwise OneNote applies x10 values!
SetSpacing(quick.Attribute("index").Value, style.SpaceBefore, style.SpaceAfter);
quick.Attribute("font").Value = style.FontFamily;

quick.Attribute("fontColor").Value = style.Color;
quick.Attribute("highlightColor").Value = style.Highlight;
quick.Attribute("spaceBefore").Value = style.SpaceBefore;
quick.Attribute("spaceAfter").Value = style.SpaceAfter;

quick.SetAttributeValue("italic", style.IsItalic.ToString().ToLower());
quick.SetAttributeValue("bold", style.IsBold.ToString().ToLower());
quick.SetAttributeValue("underline", style.IsUnderline.ToString().ToLower());
// must also apply to paragraphs otherwise OneNote applies x10 values!
SetSpacing(quick.Attribute("index").Value,
style.SpaceBefore, style.SpaceAfter,
// spaceBetween should only apply to normal paragraphs
name == "p" ? spacing : null);

quick.Attribute("fontSize").Value = style.FontSize;
quick.Attribute("fontColor").Value = style.Color;
quick.Attribute("highlightColor").Value = style.Highlight;

applied = true;
}
quick.SetAttributeValue("italic", style.IsItalic.ToString().ToLower());
quick.SetAttributeValue("bold", style.IsBold.ToString().ToLower());
quick.SetAttributeValue("underline", style.IsUnderline.ToString().ToLower());

var index = quick.Attribute("index").Value;
ClearInlineStyles(index, name == "p");
quick.Attribute("fontSize").Value = style.FontSize;

applied = true;
}

var index = quick.Attribute("index").Value;
ClearInlineStyles(index, name == "p");
}

return applied;
}


private static Style FindStyle(List<Style> styles, string name)
private static Style FindStyle(IEnumerable<Style> styles, string name)
{
Style style = null;

Expand Down Expand Up @@ -203,18 +217,26 @@ private static Style FindStyle(List<Style> styles, string name)
}


private void SetSpacing(string index, string spaceBefore, string spaceAfter)
private void SetSpacing(string index, string spaceBefore, string spaceAfter, string spacing)
{
var paragraphs = page.Root.Descendants(ns + "OE")
var paragraphs = page.Root
.Elements(ns + "Outline")
.Descendants(ns + "OE")
.Where(e => e.Attribute("quickStyleIndex")?.Value == index &&
!e.Elements(ns + "Meta")
.Any(a => a.Attribute("name").Value.StartsWith("omfootnote"))
!e.Elements(ns + "Meta").Any(a =>
a.Attribute("name").Value.StartsWith("omfootnote") ||
a.Attribute("name").Value.StartsWith("omtaggingbank"))
);

foreach (var paragraph in paragraphs)
{
paragraph.SetAttributeValue("spaceBefore", spaceBefore);
paragraph.SetAttributeValue("spaceAfter", spaceAfter);

if (spacing != null)
{
paragraph.SetAttributeValue("spaceBetween", spacing);
}
}
}

Expand All @@ -237,20 +259,22 @@ private void ClearInlineStyles(string index, bool paragraph)
// filter out omfootnote paragraphs
!(o.Meta.Attribute("name").Value.StartsWith("omfootnote") ||
// filter out omStyleHint=skip paragraphs; use in InfoBox symbol cells
(o.Meta.Attribute("name").Value == Style.HintMeta && o.Meta.Attribute("content").Value == "skip")))
(o.Meta.Attribute("name").Value == Style.HintMeta &&
o.Meta.Attribute("content").Value == "skip")))
.Select(o => o.Element);

if (elements != null)
if (elements.Any())
{
foreach (var element in elements)
{
stylizer.Clear(element, paragraph ? Stylizer.Clearing.Gray : Stylizer.Clearing.All);
stylizer.Clear(element,
paragraph ? Stylizer.Clearing.Gray : Stylizer.Clearing.All);
}
}
}


private void ApplyToLists(List<Style> styles)
private void ApplyToLists(IEnumerable<Style> styles)
{
var style = styles.SingleOrDefault(s =>
s.Name.ToLower() == "normal" ||
Expand All @@ -275,6 +299,7 @@ private void ApplyToLists(List<Style> styles)
}
}


private static void ApplyToListItems(IEnumerable<XElement> elements, Style style, bool withFamily)
{
foreach (var element in elements)
Expand Down Expand Up @@ -305,7 +330,7 @@ private void ApplyToHyperlinks()
.Select(e => e.Parent)
.ToList();

if (elements?.Count > 0)
if (elements.Count > 0)
{
foreach (var element in elements)
{
Expand Down

0 comments on commit 4a2dd34

Please sign in to comment.