diff --git a/Clippit.Tests/PowerPoint/PresentationBuilderTests.cs b/Clippit.Tests/PowerPoint/PresentationBuilderTests.cs index 48ea0e0c..abb2f6ae 100644 --- a/Clippit.Tests/PowerPoint/PresentationBuilderTests.cs +++ b/Clippit.Tests/PowerPoint/PresentationBuilderTests.cs @@ -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 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 { 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); diff --git a/Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs b/Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs index ec3d0fcb..beff48e9 100644 --- a/Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs +++ b/Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs @@ -498,6 +498,10 @@ IEnumerable 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); @@ -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(); + 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(); + 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, diff --git a/Clippit/PtOpenXmlUtil.cs b/Clippit/PtOpenXmlUtil.cs index 646e36b2..7764aeee 100644 --- a/Clippit/PtOpenXmlUtil.cs +++ b/Clippit/PtOpenXmlUtil.cs @@ -5472,6 +5472,16 @@ public static class W16SE 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"; diff --git a/TestFiles/PB013-ModernComment.pptx b/TestFiles/PB013-ModernComment.pptx new file mode 100644 index 00000000..0a8566e3 Binary files /dev/null and b/TestFiles/PB013-ModernComment.pptx differ