Preserve QPowerPoint hidden slides in PPTX
This commit is contained in:
@@ -24,6 +24,14 @@ type ZipEntry = {
|
||||
async(type: "base64"): Promise<string>;
|
||||
};
|
||||
type ZipArchive = { files?: Record<string, unknown>; file: (path: string) => ZipEntry | null };
|
||||
type WritableZipArchive = {
|
||||
file: {
|
||||
(path: string): ZipEntry | null;
|
||||
(path: string, data: string): WritableZipArchive;
|
||||
};
|
||||
generateAsync: (options: { type: "blob"; mimeType: string }) => Promise<Blob>;
|
||||
};
|
||||
type ZipConstructor = { loadAsync: (data: ArrayBuffer) => Promise<WritableZipArchive> };
|
||||
|
||||
const slidePathPattern = /^ppt\/slides\/slide(\d+)\.xml$/;
|
||||
const slideRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide";
|
||||
@@ -66,6 +74,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
? extractOrderedTextBodyTexts(orderedParser.parse(notesXml)).join("\n") || extractTextBlocks(parser.parse(notesXml)).join("\n")
|
||||
: "";
|
||||
const backgroundColor = slideBackgroundColor(parsedSlide);
|
||||
const hidden = slideHidden(parsedSlide);
|
||||
|
||||
return {
|
||||
id: `pptx-slide-${index}`,
|
||||
@@ -73,6 +82,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
subtitle: slideTextBlocks.slice(1).map((block) => block.text).join("\n"),
|
||||
notes,
|
||||
theme: "classic" as const,
|
||||
...(hidden ? { hidden } : {}),
|
||||
...(backgroundColor ? { backgroundColor } : {}),
|
||||
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
|
||||
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
|
||||
@@ -169,7 +179,8 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
}
|
||||
|
||||
const result = await pptx.write({ outputType: "blob" });
|
||||
return result instanceof Blob ? result : new Blob([result], { type: PPTX_MIME_TYPE });
|
||||
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;
|
||||
}
|
||||
|
||||
export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
@@ -448,6 +459,38 @@ function slideBackgroundColor(slideXml: unknown) {
|
||||
return normalizeHexColor(stringValue(srgbColor.val ?? srgbColor["@_val"]));
|
||||
}
|
||||
|
||||
function slideHidden(slideXml: unknown) {
|
||||
const slide = childRecord(slideXml, "sld");
|
||||
return isFalseXmlBoolean(slide.show ?? slide["@_show"]);
|
||||
}
|
||||
|
||||
async function applyHiddenSlidesToPptxBlob(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) {
|
||||
return;
|
||||
}
|
||||
|
||||
const path = `ppt/slides/slide${index + 1}.xml`;
|
||||
const xml = await zip.file(path)?.async("string");
|
||||
if (xml) {
|
||||
zip.file(path, pptxSlideXmlWithHiddenState(xml));
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return zip.generateAsync({ type: "blob", mimeType: PPTX_MIME_TYPE });
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithHiddenState(xml: string) {
|
||||
return xml.replace(/<p:sld\b([^>]*)>/, (_match, attributes: string) => {
|
||||
const withoutShow = attributes.replace(/\sshow="[^"]*"/, "");
|
||||
return `<p:sld${withoutShow} show="0">`;
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeHexColor(value?: string) {
|
||||
const raw = value?.trim().replace(/^#/, "").toUpperCase() ?? "";
|
||||
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
|
||||
@@ -490,17 +533,26 @@ function numberAttribute(record: Record<string, unknown>, name: string) {
|
||||
return Number.isFinite(value) ? value : undefined;
|
||||
}
|
||||
|
||||
function isFalseXmlBoolean(value: unknown) {
|
||||
const normalized = String(value ?? "").trim().toLowerCase();
|
||||
return normalized === "0" || normalized === "false";
|
||||
}
|
||||
|
||||
function emuToInches(value: number) {
|
||||
return Math.max(0, Math.round((value / emuPerInch) * 1000) / 1000);
|
||||
}
|
||||
|
||||
async function loadZip(file: File): Promise<ZipArchive> {
|
||||
const moduleName = "jszip";
|
||||
const jsZipModule = (await import(moduleName)) as { default?: { loadAsync: (data: ArrayBuffer) => Promise<ZipArchive> } };
|
||||
const jsZip = jsZipModule.default ?? (jsZipModule as unknown as { loadAsync: (data: ArrayBuffer) => Promise<ZipArchive> });
|
||||
const jsZip = await loadJsZip();
|
||||
return jsZip.loadAsync(await readOfficeFileAsArrayBuffer(file));
|
||||
}
|
||||
|
||||
async function loadJsZip(): Promise<ZipConstructor> {
|
||||
const moduleName = "jszip";
|
||||
const jsZipModule = (await import(moduleName)) as { default?: ZipConstructor } & ZipConstructor;
|
||||
return jsZipModule.default ?? jsZipModule;
|
||||
}
|
||||
|
||||
async function createXmlParser(): Promise<{ parse: (xml: string) => unknown }> {
|
||||
const moduleName = "fast-xml-parser";
|
||||
const parserModule = (await import(moduleName)) as { XMLParser: XmlParserConstructor };
|
||||
|
||||
Reference in New Issue
Block a user