Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Clippit.Tests/PowerPoint/PresentationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,50 @@ await Assert
await Assert.That(copiedSnapshotLink.Uri).IsEqualTo(snapshotUrl);
}

[Test]
public async Task PB013_SlideWithModernComment()
{
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
var sourcePath = Path.Combine(sourceDir.FullName, "PB013-ModernComment.pptx");

XElement sourceComment;
List<string> sourceAuthorIds;
using (var source = PresentationDocument.Open(sourcePath, false))
{
var commentedSlide = source.PresentationPart!.SlideParts.Single(s => s.commentParts.Any());
sourceComment = commentedSlide.commentParts.Single().GetXDocument().Root!.Elements(P188.cm).Single();
sourceAuthorIds = source
.PresentationPart.authorsPart!.GetXDocument()
.Root!.Elements(P188.author)
.Select(a => (string)a.Attribute("id"))
.ToList();
}

var sources = new List<SlideSource> { new(new PmlDocument(sourcePath), 1, 1, true) };
var outputPptx = new FileInfo(Path.Combine(TempDir, "PB013-Output.pptx"));
PresentationBuilder.BuildPresentation(sources).SaveAs(outputPptx.FullName);

using var pptx = PresentationDocument.Open(outputPptx.FullName, false);
await Validate(pptx);

var slidePart = pptx.PresentationPart!.SlideParts.Single();
var relId = (string)slidePart.GetXDocument().Descendants(P188.commentRel).Single().Attribute(R.id);
var copiedPart = slidePart.commentParts.Single();
await Assert.That(slidePart.GetIdOfPart(copiedPart)).IsEqualTo(relId);

var copiedComment = copiedPart.GetXDocument().Root!.Elements(P188.cm).Single();
await Assert.That((string)copiedComment.Attribute("id")).IsEqualTo((string)sourceComment.Attribute("id"));
await Assert
.That(copiedComment.Descendants(A.t).Select(t => t.Value))
.IsEquivalentTo(["Review this slide", "Done"]);

var copiedAuthorIds = pptx
.PresentationPart.authorsPart!.GetXDocument()
.Root!.Elements(P188.author)
.Select(a => (string)a.Attribute("id"));
await Assert.That(copiedAuthorIds).IsEquivalentTo(sourceAuthorIds);
}

private static byte[] ReadAllBytes(OpenXmlPart part)
{
using var stream = part.GetStream(FileMode.Open, FileAccess.Read);
Expand Down
69 changes: 69 additions & 0 deletions Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ IEnumerable<XElement> newContent
{
CopyWebExtensionPart(oldContentPart, newContentPart, element, R.id);
}
else if (name == P188.commentRel)
{
CopyModernCommentPart(oldContentPart, newContentPart, element, R.id);
}
else if (name == P.contentPart)
{
PBT.CopyInkPart(oldContentPart, newContentPart, element, R.id);
Expand Down Expand Up @@ -852,6 +856,71 @@ is not WebExtensionPart oldPart
CopyRelatedPartsForContentParts(oldPart, newPart, [newPart.GetXDocument().Root]);
}

private void CopyModernCommentPart(
OpenXmlPart oldContentPart,
OpenXmlPart newContentPart,
XElement commentReference,
XName attributeName
)
{
var relId = (string)commentReference.Attribute(attributeName);
if (newContentPart.HasRelationship(relId) || newContentPart is not SlidePart newSlidePart)
return;

if (
oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId).OpenXmlPart
is not PowerPointCommentPart oldPart
)
return;

var newPart = newSlidePart.AddNewPart<PowerPointCommentPart>();
newPart.GetXDocument().Add(oldPart.GetXDocument().Root);
commentReference.Attribute(attributeName).Set(newSlidePart.GetIdOfPart(newPart));
PBT.AddRelationships(oldPart, newPart, [newPart.GetXDocument().Root]);
CopyModernCommentAuthors((PresentationDocument)oldContentPart.OpenXmlPackage, newPart.GetXDocument().Root);
}

private void CopyModernCommentAuthors(PresentationDocument oldDocument, XElement comments)
{
if (oldDocument.PresentationPart.authorsPart is not { } oldAuthorsPart)
return;

var referencedIds = comments
.DescendantsAndSelf()
.Attributes()
.Where(a => !a.IsNamespaceDeclaration)
.Select(a => a.Value)
.ToHashSet();
var oldAuthors = oldAuthorsPart
.GetXDocument()
.Root.Elements(P188.author)
.Where(author => referencedIds.Contains((string)author.Attribute(NoNamespace.id)))
.ToList();
if (oldAuthors.Count == 0)
return;

var newAuthorsPart = _newDocument.PresentationPart.authorsPart
?? _newDocument.PresentationPart.AddNewPart<PowerPointAuthorsPart>();
var newAuthors = newAuthorsPart.GetXDocument();
if (newAuthors.Root is null)
{
newAuthors.Add(
new XElement(
P188.authorLst,
new XAttribute(XNamespace.Xmlns + "a", A.a),
new XAttribute(XNamespace.Xmlns + "r", R.r),
new XAttribute(XNamespace.Xmlns + "p188", P188.p188)
)
);
}

var existingIds = newAuthors
.Root.Elements(P188.author)
.Select(a => (string)a.Attribute(NoNamespace.id))
.ToHashSet();
newAuthors.Root.Add(oldAuthors.Where(a => existingIds.Add((string)a.Attribute(NoNamespace.id))));
}

private void CopyRelatedImage(
OpenXmlPart oldContentPart,
OpenXmlPart newContentPart,
Expand Down
10 changes: 10 additions & 0 deletions Clippit/PtOpenXmlUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@
public bool Equals(ContentDataKey other) =>
_contentType == other._contentType && _hash.AsSpan().SequenceEqual(other._hash);

public override bool Equals(object obj) => obj is ContentDataKey other && Equals(other);

Check warning on line 1681 in Clippit/PtOpenXmlUtil.cs

View workflow job for this annotation

GitHub Actions / NativeAOT tool install (osx-arm64)

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).

Check warning on line 1681 in Clippit/PtOpenXmlUtil.cs

View workflow job for this annotation

GitHub Actions / NativeAOT tool install (linux-x64)

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).

public override int GetHashCode()
{
Expand Down Expand Up @@ -5472,6 +5472,16 @@
public static XNamespace w16se = "http://schemas.microsoft.com/office/word/2015/wordml/symex";
}

public static class P188
{
public static readonly XNamespace p188 = "http://schemas.microsoft.com/office/powerpoint/2018/8/main";
public static readonly XName author = p188 + "author";
public static readonly XName authorLst = p188 + "authorLst";
public static readonly XName cm = p188 + "cm";
public static readonly XName cmLst = p188 + "cmLst";
public static readonly XName commentRel = p188 + "commentRel";
}

public static class WE
{
public static readonly XNamespace we = "http://schemas.microsoft.com/office/webextensions/webextension/2010/11";
Expand Down
Binary file added TestFiles/PB013-ModernComment.pptx
Binary file not shown.
Loading