diff --git a/src/DocxEmbed.tsx b/src/DocxEmbed.tsx index 7f3da7f..31419f5 100644 --- a/src/DocxEmbed.tsx +++ b/src/DocxEmbed.tsx @@ -29,17 +29,30 @@ function resolveDocxEmbed(app: App, linkPath: string, sourcePath: string) { return file && file.extension.toLowerCase() === 'docx' ? file : null; } -function DocxEmbedPreview({ - file, - buffer, - hostEl, - i18n, -}: { - file: TFile; - buffer: ArrayBuffer; - hostEl: HTMLElement; - i18n: Translations | undefined; -}) { +function copyRenderedPages(pagesContainer: HTMLElement, pagesEl: HTMLElement, hostEl: HTMLElement) { + const pages = Array.from(pagesContainer.querySelectorAll(DOCX_RENDERED_PAGE_SELECTOR)); + if (pages.length === 0) { + return; + } + + pagesEl.empty(); + for (const page of pages) { + pagesEl.appendChild(page.cloneNode(true)); + } + + const firstPage = pages[0]; + if (!firstPage) { + return; + } + + const pageRect = firstPage.getBoundingClientRect(); + hostEl.setCssProps({ + '--native-powerpoint-doc-editor-embed-page-height': `${Math.ceil(pageRect.height)}px`, + '--native-powerpoint-doc-editor-embed-page-width': `${Math.ceil(pageRect.width)}px`, + }); +} + +function useDocxEmbedSync(hostEl: HTMLElement) { const sourceRef = useRef(null); const pagesRef = useRef(null); const renderedDomContextRef = useRef(null); @@ -52,26 +65,7 @@ function DocxEmbedPreview({ return; } - const pages = Array.from(renderedDomContext.pagesContainer.querySelectorAll(DOCX_RENDERED_PAGE_SELECTOR)); - if (pages.length === 0) { - return; - } - - pagesEl.empty(); - for (const page of pages) { - pagesEl.appendChild(page.cloneNode(true)); - } - - const firstPage = pages[0]; - if (!firstPage) { - return; - } - - const pageRect = firstPage.getBoundingClientRect(); - hostEl.setCssProps({ - '--native-powerpoint-doc-editor-embed-page-height': `${Math.ceil(pageRect.height)}px`, - '--native-powerpoint-doc-editor-embed-page-width': `${Math.ceil(pageRect.width)}px`, - }); + copyRenderedPages(renderedDomContext.pagesContainer, pagesEl, hostEl); }, [hostEl]); const queueSyncPages = useCallback(() => { @@ -142,6 +136,27 @@ function DocxEmbedPreview({ }; }, [queueSyncPages]); + return { + pagesRef, + sourceRef, + queueSyncPages, + handleRenderedDomContextReady, + }; +} + +function DocxEmbedPreview({ + file, + buffer, + hostEl, + i18n, +}: { + file: TFile; + buffer: ArrayBuffer; + hostEl: HTMLElement; + i18n: Translations | undefined; +}) { + const { pagesRef, sourceRef, queueSyncPages, handleRenderedDomContextReady } = useDocxEmbedSync(hostEl); + return ( <>