Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-cougars-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"penpot-exporter": minor
---

Add loader when generating really large files after the page processing loader
11 changes: 7 additions & 4 deletions plugin-src/transformers/transformDocumentNode.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';
import { imagesLibrary } from '@plugin/ImageLibrary';
import { sleep } from '@plugin/utils';

import { PenpotDocument } from '@ui/types';

import { transformPageNode } from '.';

export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => {
const children = [];
let currentPage = 0;
let currentPage = 1;

figma.ui.postMessage({
type: 'PROGRESS_TOTAL_PAGES',
data: node.children.length
});

for (const page of node.children) {
await page.loadAsync();

children.push(await transformPageNode(page));

figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_PAGES',
data: currentPage++
});

await page.loadAsync();

children.push(await transformPageNode(page));
await sleep(0);
}

return {
Expand Down
29 changes: 22 additions & 7 deletions ui-src/components/ExporterProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { useEffect, useState } from 'react';

import { Stack } from './Stack';

export const ExporterProgress = () => {
type ExporterProgressProps = {
downloading: boolean;
};

export const ExporterProgress = ({ downloading }: ExporterProgressProps) => {
const [currentNode, setCurrentNode] = useState<string | undefined>();
const [totalPages, setTotalPages] = useState<number | undefined>();
const [processedPages, setProcessedPages] = useState<number | undefined>();
Expand All @@ -13,6 +17,7 @@ export const ExporterProgress = () => {
setCurrentNode(event.data.pluginMessage.data as string);
} else if (event.data.pluginMessage?.type === 'PROGRESS_TOTAL_PAGES') {
setTotalPages(event.data.pluginMessage.data as number);
setProcessedPages(0);
} else if (event.data.pluginMessage?.type === 'PROGRESS_PROCESSED_PAGES') {
setProcessedPages(event.data.pluginMessage.data as number);
}
Expand All @@ -38,15 +43,25 @@ export const ExporterProgress = () => {
<Stack space="small" horizontalAlign="center">
<LoadingIndicator />
<span style={{ textAlign: 'center' }}>
{processedPages} of {totalPages} pages exported 💪
{currentNode ? (
{!downloading ? (
<>
{processedPages} of {totalPages} pages exported 💪
{currentNode ? (
<>
<br />
Currently exporting layer
<br />
{'“' + truncateText(currentNode, 35) + '”'}
</>
) : undefined}
</>
) : (
<>
Generating Penpot file 🚀
<br />
Currently exporting layer
<br />
{'“' + truncateText(currentNode, 40) + '”'}
Please wait, this process might take a while...
</>
) : undefined}
)}
</span>
</Stack>
);
Expand Down
7 changes: 4 additions & 3 deletions ui-src/components/PenpotExporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ export const PenpotExporter = () => {
const [needsReload, setNeedsReload] = useState(false);
const [loading, setLoading] = useState(true);
const [exporting, setExporting] = useState(false);
const [downloading, setDownloading] = useState(false);
const methods = useForm<FormValues>();

methods.getValues();

const onMessage = (event: MessageEvent<{ pluginMessage: { type: string; data: unknown } }>) => {
if (event.data.pluginMessage?.type == 'PENPOT_DOCUMENT') {
setDownloading(true);

const document = event.data.pluginMessage.data as PenpotDocument;
const file = parse(document);

file.export();

setExporting(false);
} else if (event.data.pluginMessage?.type == 'CUSTOM_FONTS') {
setMissingFonts(event.data.pluginMessage.data as string[]);
setLoading(false);
Expand Down Expand Up @@ -72,7 +73,7 @@ export const PenpotExporter = () => {

if (loading) return <LoadingIndicator />;

if (exporting) return <ExporterProgress />;
if (exporting) return <ExporterProgress downloading={downloading} />;

if (needsReload) {
return (
Expand Down