0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 13:43:03 -05:00

Improve UX for larger files, when the download also takes time to process (#136)

* Improve UX for larger files, when the download also takes time to process

* add changelog
This commit is contained in:
Jordi Sala Morales 2024-06-04 12:44:02 +02:00 committed by GitHub
parent 8a86304fea
commit 7895daaea8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 15 deletions

View file

@ -0,0 +1,5 @@
---
"penpot-exporter": minor
---
Add loader when generating really large files after the page processing loader

View file

@ -1,5 +1,6 @@
import { componentsLibrary } from '@plugin/ComponentLibrary'; import { componentsLibrary } from '@plugin/ComponentLibrary';
import { imagesLibrary } from '@plugin/ImageLibrary'; import { imagesLibrary } from '@plugin/ImageLibrary';
import { sleep } from '@plugin/utils';
import { PenpotDocument } from '@ui/types'; import { PenpotDocument } from '@ui/types';
@ -7,7 +8,7 @@ import { transformPageNode } from '.';
export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => { export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => {
const children = []; const children = [];
let currentPage = 0; let currentPage = 1;
figma.ui.postMessage({ figma.ui.postMessage({
type: 'PROGRESS_TOTAL_PAGES', type: 'PROGRESS_TOTAL_PAGES',
@ -15,14 +16,16 @@ export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotD
}); });
for (const page of node.children) { for (const page of node.children) {
await page.loadAsync();
children.push(await transformPageNode(page));
figma.ui.postMessage({ figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_PAGES', type: 'PROGRESS_PROCESSED_PAGES',
data: currentPage++ data: currentPage++
}); });
await page.loadAsync(); await sleep(0);
children.push(await transformPageNode(page));
} }
return { return {

View file

@ -3,7 +3,11 @@ import { useEffect, useState } from 'react';
import { Stack } from './Stack'; import { Stack } from './Stack';
export const ExporterProgress = () => { type ExporterProgressProps = {
downloading: boolean;
};
export const ExporterProgress = ({ downloading }: ExporterProgressProps) => {
const [currentNode, setCurrentNode] = useState<string | undefined>(); const [currentNode, setCurrentNode] = useState<string | undefined>();
const [totalPages, setTotalPages] = useState<number | undefined>(); const [totalPages, setTotalPages] = useState<number | undefined>();
const [processedPages, setProcessedPages] = useState<number | undefined>(); const [processedPages, setProcessedPages] = useState<number | undefined>();
@ -13,6 +17,7 @@ export const ExporterProgress = () => {
setCurrentNode(event.data.pluginMessage.data as string); setCurrentNode(event.data.pluginMessage.data as string);
} else if (event.data.pluginMessage?.type === 'PROGRESS_TOTAL_PAGES') { } else if (event.data.pluginMessage?.type === 'PROGRESS_TOTAL_PAGES') {
setTotalPages(event.data.pluginMessage.data as number); setTotalPages(event.data.pluginMessage.data as number);
setProcessedPages(0);
} else if (event.data.pluginMessage?.type === 'PROGRESS_PROCESSED_PAGES') { } else if (event.data.pluginMessage?.type === 'PROGRESS_PROCESSED_PAGES') {
setProcessedPages(event.data.pluginMessage.data as number); setProcessedPages(event.data.pluginMessage.data as number);
} }
@ -38,15 +43,25 @@ export const ExporterProgress = () => {
<Stack space="small" horizontalAlign="center"> <Stack space="small" horizontalAlign="center">
<LoadingIndicator /> <LoadingIndicator />
<span style={{ textAlign: 'center' }}> <span style={{ textAlign: 'center' }}>
{!downloading ? (
<>
{processedPages} of {totalPages} pages exported 💪 {processedPages} of {totalPages} pages exported 💪
{currentNode ? ( {currentNode ? (
<> <>
<br /> <br />
Currently exporting layer Currently exporting layer
<br /> <br />
{'“' + truncateText(currentNode, 40) + '”'} {'“' + truncateText(currentNode, 35) + '”'}
</> </>
) : undefined} ) : undefined}
</>
) : (
<>
Generating Penpot file 🚀
<br />
Please wait, this process might take a while...
</>
)}
</span> </span>
</Stack> </Stack>
); );

View file

@ -16,18 +16,19 @@ export const PenpotExporter = () => {
const [needsReload, setNeedsReload] = useState(false); const [needsReload, setNeedsReload] = useState(false);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [exporting, setExporting] = useState(false); const [exporting, setExporting] = useState(false);
const [downloading, setDownloading] = useState(false);
const methods = useForm<FormValues>(); const methods = useForm<FormValues>();
methods.getValues(); methods.getValues();
const onMessage = (event: MessageEvent<{ pluginMessage: { type: string; data: unknown } }>) => { const onMessage = (event: MessageEvent<{ pluginMessage: { type: string; data: unknown } }>) => {
if (event.data.pluginMessage?.type == 'PENPOT_DOCUMENT') { if (event.data.pluginMessage?.type == 'PENPOT_DOCUMENT') {
setDownloading(true);
const document = event.data.pluginMessage.data as PenpotDocument; const document = event.data.pluginMessage.data as PenpotDocument;
const file = parse(document); const file = parse(document);
file.export(); file.export();
setExporting(false);
} else if (event.data.pluginMessage?.type == 'CUSTOM_FONTS') { } else if (event.data.pluginMessage?.type == 'CUSTOM_FONTS') {
setMissingFonts(event.data.pluginMessage.data as string[]); setMissingFonts(event.data.pluginMessage.data as string[]);
setLoading(false); setLoading(false);
@ -72,7 +73,7 @@ export const PenpotExporter = () => {
if (loading) return <LoadingIndicator />; if (loading) return <LoadingIndicator />;
if (exporting) return <ExporterProgress />; if (exporting) return <ExporterProgress downloading={downloading} />;
if (needsReload) { if (needsReload) {
return ( return (