Merge pull request #108 from presenton/feat/presentation_export

feat/presentation export
This commit is contained in:
Saurav Niraula 2025-07-20 23:07:31 +05:45 committed by GitHub
commit b25cc3db92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -72,12 +72,6 @@ async function getBrowserAndPage(id: string): Promise<[Browser, Page]> {
const page = await browser.newPage();
page.on('console', (msg) => {
const type = msg.type();
const text = msg.text();
console.log(`${type}: ${text}`);
});
await page.setViewport({ width: 1920, height: 1080, deviceScaleFactor: 1 });
await page.goto(`http://localhost/pdf-maker?id=${id}`, {
waitUntil: "networkidle0",
@ -122,27 +116,29 @@ async function screenshotElement(element: ElementAttributes, screenshotsDir: str
// Hide all elements except the target element and its ancestors
await element.element?.evaluate((el) => {
const originalDisplays = new Map();
const originalOpacities = new Map();
const hideAllExcept = (targetElement: Element) => {
const allElements = document.querySelectorAll('*');
allElements.forEach((elem) => {
const computedStyle = window.getComputedStyle(elem);
originalOpacities.set(elem, computedStyle.opacity);
if (targetElement === elem || targetElement.contains(elem) || elem.contains(targetElement)) {
(elem as HTMLElement).style.opacity = computedStyle.opacity || "1";
return;
}
const computedStyle = window.getComputedStyle(elem);
originalDisplays.set(elem, computedStyle.display);
(elem as HTMLElement).style.display = 'none';
(elem as HTMLElement).style.opacity = '0';
});
};
hideAllExcept(el);
(el as any).__restoreDisplays = () => {
originalDisplays.forEach((display, elem) => {
(elem as HTMLElement).style.display = display;
(el as any).__restoreOpacities = () => {
originalOpacities.forEach((opacity, elem) => {
(elem as HTMLElement).style.opacity = opacity;
});
};
});
@ -153,8 +149,8 @@ async function screenshotElement(element: ElementAttributes, screenshotsDir: str
}
await element.element?.evaluate((el) => {
if ((el as any).__restoreDisplays) {
(el as any).__restoreDisplays();
if ((el as any).__restoreOpacities) {
(el as any).__restoreOpacities();
}
});
@ -163,9 +159,11 @@ async function screenshotElement(element: ElementAttributes, screenshotsDir: str
async function getSlidesAttributes(slides: ElementHandle<Element>[], screenshotsDir: string): Promise<SlideAttributesResult[]> {
const slideAttributes = await Promise.all(slides.map(async (slide) => {
return await getAllChildElementsAttributes({ element: slide, screenshotsDir });
}));
const slideAttributes = [];
for (const slide of slides) {
const attributes = await getAllChildElementsAttributes({ element: slide, screenshotsDir });
slideAttributes.push(attributes);
}
return slideAttributes;
}
@ -185,9 +183,6 @@ async function getSlidesWrapper(page: Page): Promise<ElementHandle<Element>> {
return slides_wrapper;
}
async function getAllChildElementsAttributes({ element, rootRect = null, depth = 0, inheritedFont, inheritedBackground, inheritedBorderRadius, screenshotsDir }: GetAllChildElementsAttributesArgs): Promise<SlideAttributesResult> {
const currentRootRect = rootRect || await element.evaluate((el) => {
const rect = el.getBoundingClientRect();
@ -237,6 +232,7 @@ async function getAllChildElementsAttributes({ element, rootRect = null, depth =
break;
}
const childResults = await getAllChildElementsAttributes({
element: childElementHandle,
rootRect: currentRootRect,