Preserve QPowerPoint image hyperlinks in PPTX
This commit is contained in:
@@ -290,6 +290,35 @@ describe("powerPointOffice helpers", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("импортирует внешние ссылки изображений PPTX через hlinkClick relationships", async () => {
|
||||
const zip = new JSZip();
|
||||
zip.file(
|
||||
"ppt/presentation.xml",
|
||||
`<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>`
|
||||
);
|
||||
zip.file(
|
||||
"ppt/_rels/presentation.xml.rels",
|
||||
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/></Relationships>`
|
||||
);
|
||||
zip.file("ppt/slides/slide1.xml", slideXmlWithLinkedPicture("Слайд с кликабельным изображением"));
|
||||
zip.file(
|
||||
"ppt/slides/_rels/slide1.xml.rels",
|
||||
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rIdImage" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/logo.png"/><Relationship Id="rIdImageLink" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com/logo" TargetMode="External"/></Relationships>`
|
||||
);
|
||||
zip.file("ppt/media/logo.png", tinyPngBase64, { base64: true });
|
||||
|
||||
const blob = await zip.generateAsync({
|
||||
type: "blob",
|
||||
mimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
||||
});
|
||||
const imported = await importPptxFile(new File([blob], "Кликабельное изображение.pptx", { type: blob.type }));
|
||||
|
||||
expect(imported.slides[0].images?.[0]).toMatchObject({
|
||||
alt: "Логотип",
|
||||
hyperlink: "https://example.com/logo"
|
||||
});
|
||||
});
|
||||
|
||||
it("экспортирует изображения слайда в PPTX media relationships", async () => {
|
||||
const blob = await exportPptxBlob({
|
||||
title: "Слайды с изображением.pptx",
|
||||
@@ -325,6 +354,42 @@ describe("powerPointOffice helpers", () => {
|
||||
expect(mediaFiles.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("экспортирует внешние ссылки изображений PPTX", async () => {
|
||||
const blob = await exportPptxBlob({
|
||||
title: "Кликабельное изображение.pptx",
|
||||
slides: [
|
||||
{
|
||||
id: "slide-1",
|
||||
title: "Слайд с кликабельным изображением",
|
||||
subtitle: "Проверка ссылки",
|
||||
notes: "",
|
||||
theme: "classic",
|
||||
images: [
|
||||
{
|
||||
id: "image-1",
|
||||
src: `data:image/png;base64,${tinyPngBase64}`,
|
||||
alt: "Логотип",
|
||||
hyperlink: "https://example.com/logo",
|
||||
x: 1,
|
||||
y: 2,
|
||||
width: 3,
|
||||
height: 1.5
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
const slideXml = (await zip.file("ppt/slides/slide1.xml")?.async("string")) ?? "";
|
||||
const relationshipsXml = (await zip.file("ppt/slides/_rels/slide1.xml.rels")?.async("string")) ?? "";
|
||||
const reimported = await importPptxFile(new File([blob], "Кликабельное изображение.pptx", { type: blob.type }));
|
||||
|
||||
expect(slideXml).toContain("hlinkClick");
|
||||
expect(relationshipsXml).toContain('Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"');
|
||||
expect(relationshipsXml).toContain('Target="https://example.com/logo"');
|
||||
expect(reimported.slides[0].images?.[0]).toMatchObject({ hyperlink: "https://example.com/logo" });
|
||||
});
|
||||
|
||||
it("экспортирует заметки докладчика в PPTX notesSlide relationships", async () => {
|
||||
const blob = await exportPptxBlob({
|
||||
title: "Заметки.pptx",
|
||||
@@ -641,3 +706,7 @@ function slideXmlWithListSubtitle(title: string, subtitleLines: string[], listSt
|
||||
function slideXmlWithPicture(title: string) {
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:pic><p:nvPicPr><p:cNvPr id="4" name="Picture 1" descr="Логотип"/></p:nvPicPr><p:blipFill><a:blip r:embed="rIdImage"/></p:blipFill><p:spPr><a:xfrm><a:off x="914400" y="1828800"/><a:ext cx="2743200" cy="1371600"/></a:xfrm></p:spPr></p:pic></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithLinkedPicture(title: string) {
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:pic><p:nvPicPr><p:cNvPr id="4" name="Picture 1" descr="Логотип"><a:hlinkClick r:id="rIdImageLink"/></p:cNvPr></p:nvPicPr><p:blipFill><a:blip r:embed="rIdImage"/></p:blipFill><p:spPr><a:xfrm><a:off x="914400" y="1828800"/><a:ext cx="2743200" cy="1371600"/></a:xfrm></p:spPr></p:pic></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
const slideRelationshipsXml = await readOptionalParsedXml(zip, parser, relationshipsPath);
|
||||
const hyperlinkTargets = relationshipTargetsById(slideRelationshipsXml, hyperlinkRelationshipType, path);
|
||||
const slideTextBlocks = mergeOrderedSlideTextBlocks(extractSlideTextBlocks(parsedSlide, hyperlinkTargets), extractOrderedTextBodyTexts(orderedSlide));
|
||||
const images = await extractSlideImages(zip, parser, parsedSlide, path, index);
|
||||
const images = await extractSlideImages(zip, parser, parsedSlide, path, index, slideRelationshipsXml);
|
||||
const notesPath = await slideNotesPath(zip, parser, path);
|
||||
const notesXml = notesPath ? await readOptionalZipText(zip, notesPath) : await readOptionalZipText(zip, `ppt/notesSlides/notesSlide${index}.xml`);
|
||||
const notes = notesXml
|
||||
@@ -157,13 +157,15 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
return;
|
||||
}
|
||||
|
||||
const imageHyperlink = normalizedExternalHyperlinkTarget(image.hyperlink);
|
||||
slide.addImage({
|
||||
data,
|
||||
x: image.x,
|
||||
y: image.y,
|
||||
w: image.width,
|
||||
h: image.height,
|
||||
altText: image.alt || `Изображение ${index + 1}`
|
||||
altText: image.alt || `Изображение ${index + 1}`,
|
||||
...(imageHyperlink ? { hyperlink: { url: imageHyperlink } } : {})
|
||||
});
|
||||
});
|
||||
slide.addText(sourceSlide.title || "Без заголовка", {
|
||||
@@ -426,14 +428,16 @@ async function extractSlideImages(
|
||||
parser: { parse: (xml: string) => unknown },
|
||||
slideXml: unknown,
|
||||
slidePath: string,
|
||||
slideIndex: number
|
||||
slideIndex: number,
|
||||
relationshipsXml?: unknown
|
||||
): Promise<SlideImage[]> {
|
||||
const relationshipsPath = `${partDirectory(slidePath)}/_rels/${partFileName(slidePath)}.rels`;
|
||||
const relationshipsXml = await readOptionalParsedXml(zip, parser, relationshipsPath);
|
||||
const imageTargets = relationshipTargetsById(relationshipsXml, imageRelationshipType, slidePath);
|
||||
const parsedRelationshipsXml = relationshipsXml ?? (await readOptionalParsedXml(zip, parser, relationshipsPath));
|
||||
const imageTargets = relationshipTargetsById(parsedRelationshipsXml, imageRelationshipType, slidePath);
|
||||
const hyperlinkTargets = relationshipTargetsById(parsedRelationshipsXml, hyperlinkRelationshipType, slidePath);
|
||||
const pictures = recordsByKey(slideXml, "pic");
|
||||
const images = await Promise.all(
|
||||
pictures.map(async (picture, imageIndex) => slideImageFromPicture(zip, picture, imageTargets, slideIndex, imageIndex))
|
||||
pictures.map(async (picture, imageIndex) => slideImageFromPicture(zip, picture, imageTargets, hyperlinkTargets, slideIndex, imageIndex))
|
||||
);
|
||||
|
||||
return images.filter((image): image is SlideImage => Boolean(image));
|
||||
@@ -443,6 +447,7 @@ async function slideImageFromPicture(
|
||||
zip: ZipArchive,
|
||||
picture: Record<string, unknown>,
|
||||
imageTargets: Record<string, string>,
|
||||
hyperlinkTargets: Record<string, string>,
|
||||
slideIndex: number,
|
||||
imageIndex: number
|
||||
): Promise<SlideImage | null> {
|
||||
@@ -453,14 +458,27 @@ async function slideImageFromPicture(
|
||||
return null;
|
||||
}
|
||||
|
||||
const hyperlink = pictureHyperlink(picture, hyperlinkTargets);
|
||||
return {
|
||||
id: `pptx-slide-${slideIndex}-image-${imageIndex + 1}`,
|
||||
src,
|
||||
alt: pictureAltText(picture) || `Изображение ${imageIndex + 1}`,
|
||||
...(hyperlink ? { hyperlink } : {}),
|
||||
...pictureBounds(picture)
|
||||
};
|
||||
}
|
||||
|
||||
function pictureHyperlink(picture: Record<string, unknown>, hyperlinkTargets: Record<string, string>) {
|
||||
return (
|
||||
recordsByKey(picture, "hlinkClick")
|
||||
.map((record) => {
|
||||
const id = relationshipId(record);
|
||||
return id ? normalizedExternalHyperlinkTarget(hyperlinkTargets[id]) : "";
|
||||
})
|
||||
.find(Boolean) ?? ""
|
||||
);
|
||||
}
|
||||
|
||||
function pictureRelationshipId(picture: Record<string, unknown>) {
|
||||
const blip = firstRecordByKey(picture, "blip");
|
||||
return stringValue(
|
||||
|
||||
Reference in New Issue
Block a user