2024-05-29 10:33:29 -05:00
|
|
|
import { componentsLibrary } from '@plugin/ComponentLibrary';
|
2024-06-03 10:29:33 -05:00
|
|
|
import { imagesLibrary } from '@plugin/ImageLibrary';
|
2024-06-06 02:37:35 -05:00
|
|
|
import { remoteComponentLibrary } from '@plugin/RemoteComponentLibrary';
|
2024-06-19 08:58:13 -05:00
|
|
|
import { styleLibrary } from '@plugin/StyleLibrary';
|
2024-06-06 02:37:35 -05:00
|
|
|
import { translateRemoteChildren } from '@plugin/translators';
|
2024-06-04 05:44:02 -05:00
|
|
|
import { sleep } from '@plugin/utils';
|
2024-05-29 10:33:29 -05:00
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
import { PenpotPage } from '@ui/lib/types/penpotPage';
|
2024-05-29 10:33:29 -05:00
|
|
|
import { PenpotDocument } from '@ui/types';
|
2024-04-12 09:52:36 -05:00
|
|
|
|
|
|
|
import { transformPageNode } from '.';
|
2024-04-12 06:55:42 -05:00
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
const downloadImages = async (): Promise<Record<string, Uint8Array>> => {
|
|
|
|
const imageToDownload = Object.entries(imagesLibrary.all());
|
|
|
|
const images: Record<string, Uint8Array> = {};
|
2024-06-18 02:50:38 -05:00
|
|
|
|
|
|
|
if (imageToDownload.length === 0) return images;
|
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
let currentImage = 1;
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
2024-06-18 02:50:38 -05:00
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: imageToDownload.length
|
2024-06-06 10:24:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
2024-06-18 02:50:38 -05:00
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'images'
|
2024-06-06 10:24:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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[]> => {
|
2024-05-31 04:25:32 -05:00
|
|
|
const children = [];
|
2024-06-04 05:44:02 -05:00
|
|
|
let currentPage = 1;
|
2024-05-31 04:25:32 -05:00
|
|
|
|
|
|
|
figma.ui.postMessage({
|
2024-06-06 10:24:59 -05:00
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
2024-05-31 04:25:32 -05:00
|
|
|
data: node.children.length
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const page of node.children) {
|
2024-06-04 05:44:02 -05:00
|
|
|
await page.loadAsync();
|
|
|
|
|
|
|
|
children.push(await transformPageNode(page));
|
|
|
|
|
2024-05-31 04:25:32 -05:00
|
|
|
figma.ui.postMessage({
|
2024-06-06 10:24:59 -05:00
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
2024-05-31 04:25:32 -05:00
|
|
|
data: currentPage++
|
|
|
|
});
|
|
|
|
|
2024-06-04 05:44:02 -05:00
|
|
|
await sleep(0);
|
2024-05-31 04:25:32 -05:00
|
|
|
}
|
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
return children;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => {
|
|
|
|
const children = await processPages(node);
|
|
|
|
|
2024-06-06 02:37:35 -05:00
|
|
|
if (remoteComponentLibrary.remaining() > 0) {
|
|
|
|
children.push({
|
|
|
|
name: 'External Components',
|
|
|
|
children: await translateRemoteChildren()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-12 06:55:42 -05:00
|
|
|
return {
|
|
|
|
name: node.name,
|
2024-05-31 04:25:32 -05:00
|
|
|
children,
|
2024-06-03 10:29:33 -05:00
|
|
|
components: componentsLibrary.all(),
|
2024-06-19 08:58:13 -05:00
|
|
|
images: await downloadImages(),
|
|
|
|
styles: styleLibrary.all()
|
2024-04-12 06:55:42 -05:00
|
|
|
};
|
|
|
|
};
|