Preserve QPowerPoint slide transitions in PPTX
This commit is contained in:
@@ -38,6 +38,7 @@ const slideRelationshipType = "http://schemas.openxmlformats.org/officeDocument/
|
||||
const notesRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide";
|
||||
const imageRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
const emuPerInch = 914400;
|
||||
const slideTransitions = ["fade", "push", "wipe"] as const;
|
||||
const themeColors: Record<Slide["theme"], { background: string; title: string; subtitle: string }> = {
|
||||
classic: { background: "FFFFFF", title: "202124", subtitle: "3C4043" },
|
||||
ocean: { background: "EAF6FF", title: "075985", subtitle: "0F766E" },
|
||||
@@ -75,6 +76,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
: "";
|
||||
const backgroundColor = slideBackgroundColor(parsedSlide);
|
||||
const hidden = slideHidden(parsedSlide);
|
||||
const transition = slideTransition(parsedSlide);
|
||||
|
||||
return {
|
||||
id: `pptx-slide-${index}`,
|
||||
@@ -83,6 +85,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
notes,
|
||||
theme: "classic" as const,
|
||||
...(hidden ? { hidden } : {}),
|
||||
...(transition ? { transition } : {}),
|
||||
...(backgroundColor ? { backgroundColor } : {}),
|
||||
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
|
||||
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
|
||||
@@ -180,7 +183,7 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
|
||||
const result = await pptx.write({ outputType: "blob" });
|
||||
const blob = result instanceof Blob ? result : new Blob([result], { type: PPTX_MIME_TYPE });
|
||||
return deck.slides.some((slide) => slide.hidden) ? applyHiddenSlidesToPptxBlob(blob, deck.slides) : blob;
|
||||
return deck.slides.some((slide) => slide.hidden || slide.transition) ? applySlideXmlOverridesToPptxBlob(blob, deck.slides) : blob;
|
||||
}
|
||||
|
||||
export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
@@ -464,19 +467,29 @@ function slideHidden(slideXml: unknown) {
|
||||
return isFalseXmlBoolean(slide.show ?? slide["@_show"]);
|
||||
}
|
||||
|
||||
async function applyHiddenSlidesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
function slideTransition(slideXml: unknown): Slide["transition"] | undefined {
|
||||
const transition = childRecord(childRecord(slideXml, "sld"), "transition");
|
||||
const type = Object.keys(transition).find((key) => isSlideTransition(key));
|
||||
return isSlideTransition(type) ? type : undefined;
|
||||
}
|
||||
|
||||
function isSlideTransition(value: unknown): value is NonNullable<Slide["transition"]> {
|
||||
return typeof value === "string" && (slideTransitions as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
async function applySlideXmlOverridesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
const JSZip = await loadJsZip();
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
await Promise.all(
|
||||
slides.map(async (slide, index) => {
|
||||
if (!slide.hidden) {
|
||||
if (!slide.hidden && !slide.transition) {
|
||||
return;
|
||||
}
|
||||
|
||||
const path = `ppt/slides/slide${index + 1}.xml`;
|
||||
const xml = await zip.file(path)?.async("string");
|
||||
if (xml) {
|
||||
zip.file(path, pptxSlideXmlWithHiddenState(xml));
|
||||
zip.file(path, pptxSlideXmlWithOverrides(xml, slide));
|
||||
}
|
||||
})
|
||||
);
|
||||
@@ -484,6 +497,11 @@ async function applyHiddenSlidesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
return zip.generateAsync({ type: "blob", mimeType: PPTX_MIME_TYPE });
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithOverrides(xml: string, slide: Slide) {
|
||||
const withHiddenState = slide.hidden ? pptxSlideXmlWithHiddenState(xml) : xml;
|
||||
return slide.transition ? pptxSlideXmlWithTransition(withHiddenState, slide.transition) : withHiddenState;
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithHiddenState(xml: string) {
|
||||
return xml.replace(/<p:sld\b([^>]*)>/, (_match, attributes: string) => {
|
||||
const withoutShow = attributes.replace(/\sshow="[^"]*"/, "");
|
||||
@@ -491,6 +509,21 @@ function pptxSlideXmlWithHiddenState(xml: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithTransition(xml: string, transition: NonNullable<Slide["transition"]>) {
|
||||
const transitionXml = `<p:transition><p:${transition}/></p:transition>`;
|
||||
if (/<p:transition\b[\s\S]*?<\/p:transition>/.test(xml)) {
|
||||
return xml.replace(/<p:transition\b[\s\S]*?<\/p:transition>/, transitionXml);
|
||||
}
|
||||
if (/<p:transition\b[^>]*\/>/.test(xml)) {
|
||||
return xml.replace(/<p:transition\b[^>]*\/>/, transitionXml);
|
||||
}
|
||||
if (/<\/p:cSld>/.test(xml)) {
|
||||
return xml.replace("</p:cSld>", `</p:cSld>${transitionXml}`);
|
||||
}
|
||||
|
||||
return xml.replace(/<p:sld\b[^>]*>/, (match) => `${match}${transitionXml}`);
|
||||
}
|
||||
|
||||
function normalizeHexColor(value?: string) {
|
||||
const raw = value?.trim().replace(/^#/, "").toUpperCase() ?? "";
|
||||
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
|
||||
|
||||
Reference in New Issue
Block a user