Skip to content

Commit

Permalink
#1618 - Handle Section Background image in Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaor committed Feb 28, 2025
1 parent ae8cf11 commit d8af571
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ internal CanvasColumn(CanvasSection section)
LayoutIndex = 1;
}

internal CanvasColumn(CanvasSection section, int order, int? sectionFactor)
internal CanvasColumn(CanvasSection section, int order, int? sectionFactor, string zoneId = null)
{
Section = section ?? throw new ArgumentNullException(nameof(section));
Order = order;
ColumnFactor = sectionFactor ?? 12;
LayoutIndex = 1;
ZoneId = !string.IsNullOrEmpty(zoneId) ? zoneId : null;
}

internal CanvasColumn(CanvasSection section, int order, int? sectionFactor, int? layoutIndex)
internal CanvasColumn(CanvasSection section, int order, int? sectionFactor, int? layoutIndex, string zoneId = null)
{
Section = section ?? throw new ArgumentNullException(nameof(section));
Order = order;
ColumnFactor = sectionFactor ?? 12;
LayoutIndex = layoutIndex ?? 1;
ZoneId = !string.IsNullOrEmpty(zoneId) ? zoneId : null;
}
#endregion

Expand All @@ -61,6 +63,8 @@ internal CanvasColumn(CanvasSection section, int order, int? sectionFactor, int?
/// </summary>
public int LayoutIndex { get; }

public string ZoneId { get; private set; }

/// <summary>
/// List of <see cref="ICanvasControl"/> instances that are hosted in this section
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ internal static Type GetType(string controlDataJson)
{
return typeof(CanvasColumn);
}
else if (controlData.ControlType == 14) // Special Control Type used for section background image
{
return typeof(PageWebPart);
}

return null;
}
Expand All @@ -356,7 +360,7 @@ internal virtual void FromHtml(IElement element, bool isHeader)
canvasDataVersion = element.GetAttribute(CanvasDataVersionAttribute);
canvasControlData = element.GetAttribute(CanvasControlAttribute);
controlType = controlData.ControlType;
instanceId = new Guid(controlData.Id);
instanceId = new Guid(controlData.Id ?? Guid.NewGuid().ToString());
}

internal void MoveTo(ICanvasSection newSection, ICanvasColumn newColumn)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;

namespace PnP.Core.Model.SharePoint
{
Expand Down Expand Up @@ -33,5 +33,11 @@ internal class CanvasPosition
/// </summary>
[JsonPropertyName("layoutIndex")]
public int? LayoutIndex { get; set; }

/// <summary>
/// Gets or sets JsonProperty "zoneId"
/// </summary>
[JsonPropertyName("zoneId")]
public string ZoneId { get; set; }
}
}
9 changes: 6 additions & 3 deletions src/sdk/PnP.Core/Model/SharePoint/Pages/Internal/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ private void LoadFromHtml(string html, string pageHeaderHtml)
}
else
{
currentSection.AddColumn(new CanvasColumn(currentSection, (int)sectionData.Position.SectionIndex, sectionData.Position.SectionFactor));
currentSection.AddColumn(new CanvasColumn(currentSection, (int)sectionData.Position.SectionIndex, sectionData.Position.SectionFactor, sectionData.Position.ZoneId));
currentColumn = currentSection.Columns.Where(p => p.Order == sectionData.Position.SectionIndex).First();
}
}
Expand Down Expand Up @@ -1560,7 +1560,7 @@ private void ApplySectionAndColumn(CanvasControl control, CanvasControlPosition
{
if (position.LayoutIndex.HasValue)
{
(currentSection as CanvasSection).AddColumn(new CanvasColumn(currentSection as CanvasSection, (int)position.SectionIndex, position.SectionFactor, position.LayoutIndex.Value));
(currentSection as CanvasSection).AddColumn(new CanvasColumn(currentSection as CanvasSection, (int)position.SectionIndex, position.SectionFactor, position.LayoutIndex.Value, position.ZoneId));
currentColumn = currentSection.Columns.Where(p => p.Order == position.SectionIndex && p.LayoutIndex == position.LayoutIndex.Value).First();

// ZoneEmphasis on a vertical section column needs to be retained as that "overrides" the zone emphasis set on the section
Expand All @@ -1571,7 +1571,7 @@ private void ApplySectionAndColumn(CanvasControl control, CanvasControlPosition
}
else
{
(currentSection as CanvasSection).AddColumn(new CanvasColumn(currentSection as CanvasSection, (int)position.SectionIndex, position.SectionFactor));
(currentSection as CanvasSection).AddColumn(new CanvasColumn(currentSection as CanvasSection, (int)position.SectionIndex, position.SectionFactor, position.ZoneId));
currentColumn = currentSection.Columns.Where(p => p.Order == position.SectionIndex).First();
}
}
Expand Down Expand Up @@ -2628,6 +2628,9 @@ public DefaultWebPart WebPartIdToDefaultWebPart(string id)

internal static DefaultWebPart IdToDefaultWebPart(string id)
{
if (string.IsNullOrEmpty(id))
return DefaultWebPart.ThirdParty;

return (id.ToLower()) switch
{
"daf0b71c-6de8-4ef7-b511-faae7c388708" => DefaultWebPart.ContentRollup,
Expand Down
3 changes: 2 additions & 1 deletion src/sdk/PnP.Core/Model/SharePoint/Pages/Internal/PageText.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AngleSharp.Dom;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -93,6 +93,7 @@ public override string ToHtml(float controlIndex)
SectionFactor = Column.ColumnFactor,
LayoutIndex = Column.LayoutIndex,
ControlIndex = controlIndex,
ZoneId = column.ZoneId
},
Emphasis = new SectionEmphasis()
{
Expand Down
41 changes: 26 additions & 15 deletions src/sdk/PnP.Core/Model/SharePoint/Pages/Internal/PageWebPart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AngleSharp.Dom;
using AngleSharp.Dom;
using System;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -254,6 +254,7 @@ public override string ToHtml(float controlIndex)
SectionFactor = Column.ColumnFactor,
LayoutIndex = Column.LayoutIndex,
ControlIndex = controlIndex,
ZoneId = column.ZoneId
};

if (SpControlData != null)
Expand Down Expand Up @@ -522,7 +523,7 @@ internal void RenderHtmlProperties(ref StringBuilder htmlWriter)
internal override void FromHtml(IElement element, bool isHeader)
{
base.FromHtml(element, isHeader);

// Set/update dataVersion if it was provided as html attribute
var webPartDataVersion = element.GetAttribute(WebPartDataVersionAttribute);
if (!string.IsNullOrEmpty(webPartDataVersion))
Expand Down Expand Up @@ -602,28 +603,38 @@ internal override void FromHtml(IElement element, bool isHeader)
// Set property to trigger correct loading of properties
PropertiesJson = wpJObject.GetProperty("properties").ToString();

WebPartId = wpJObject.GetProperty("id").GetString();
if (wpJObject.TryGetProperty("id", out JsonElement webPartId))
{
WebPartId = webPartId.GetString();
}

// Set/update dataVersion if it was set in the json data
if (wpJObject.TryGetProperty("dataVersion", out JsonElement dataVersionValue))
{
dataVersion = dataVersionValue.GetString();
}

// Check for fullbleed supporting web parts
if (wpJObject.TryGetProperty("properties", out JsonElement properties))
if (controlType == 14) // Special control type for background image
{
if (properties.TryGetProperty("isFullWidth", out JsonElement isFullWidth))
{
SupportsFullBleed = isFullWidth.GetBoolean();
}
// Ensure that for first party web parts that support full bleed we set the SupportsFullBleed flag
else if (Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.PageTitle || //Message ID: MC791596 / Roadmap ID: 386904
Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.Image ||
Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.Hero ||
Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.CountDown)
SupportsFullBleed = true;
}
else
{
// Check for fullbleed supporting web parts
if (wpJObject.TryGetProperty("properties", out JsonElement properties))
{
SupportsFullBleed = true;
if (properties.TryGetProperty("isFullWidth", out JsonElement isFullWidth))
{
SupportsFullBleed = isFullWidth.GetBoolean();
}
// Ensure that for first party web parts that support full bleed we set the SupportsFullBleed flag
else if (Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.PageTitle || //Message ID: MC791596 / Roadmap ID: 386904
Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.Image ||
Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.Hero ||
Page.IdToDefaultWebPart(WebPartId) == DefaultWebPart.CountDown)
{
SupportsFullBleed = true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace PnP.Core.Model.SharePoint
{
Expand Down Expand Up @@ -42,6 +42,11 @@ public interface ICanvasColumn
/// </summary>
int? VerticalSectionEmphasis { get; }

/// <summary>
/// The Zone Identifier
/// </summary>
string ZoneId { get; }

/// <summary>
/// Resets the column, used in scenarios where a section is changed from type (e.g. from 3 column to 2 column)
/// </summary>
Expand Down

0 comments on commit d8af571

Please sign in to comment.