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

94 lines
2.1 KiB
TypeScript
Raw Normal View History

import { componentsLibrary } from '@plugin/ComponentLibrary';
import { imagesLibrary } from '@plugin/ImageLibrary';
import { remoteComponentLibrary } from '@plugin/RemoteComponentLibrary';
import { styleLibrary } from '@plugin/StyleLibrary';
import { translateRemoteChildren } from '@plugin/translators';
import { sleep } from '@plugin/utils';
import { PenpotPage } from '@ui/lib/types/penpotPage';
import { PenpotDocument } from '@ui/types';
import { transformPageNode } from '.';
const downloadImages = async (): Promise<Record<string, Uint8Array>> => {
const imageToDownload = Object.entries(imagesLibrary.all());
const images: Record<string, Uint8Array> = {};
if (imageToDownload.length === 0) return images;
let currentImage = 1;
figma.ui.postMessage({
type: 'PROGRESS_TOTAL_ITEMS',
data: imageToDownload.length
});
figma.ui.postMessage({
type: 'PROGRESS_STEP',
data: 'images'
});
for (const [key, image] of imageToDownload) {
const bytes = await image?.getBytesAsync();
if (bytes) {
images[key] = bytes;
}
figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: currentImage++
});
await sleep(0);
}
await sleep(20);
return images;
};
const processPages = async (node: DocumentNode): Promise<PenpotPage[]> => {
const children = [];
let currentPage = 1;
figma.ui.postMessage({
type: 'PROGRESS_TOTAL_ITEMS',
data: node.children.length
});
for (const page of node.children) {
await page.loadAsync();
children.push(await transformPageNode(page));
figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: currentPage++
});
await sleep(0);
}
return children;
};
export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => {
const children = await processPages(node);
if (remoteComponentLibrary.remaining() > 0) {
children.push({
name: 'External Components',
children: await translateRemoteChildren()
});
}
return {
name: node.name,
children,
components: componentsLibrary.all(),
images: await downloadImages(),
styles: styleLibrary.all()
};
};