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';
|
|
|
|
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
|
|
|
|
|
|
|
import { PenpotDocument } from '@ui/types';
|
2024-04-12 09:52:36 -05:00
|
|
|
|
|
|
|
import { transformPageNode } from '.';
|
2024-04-12 06:55:42 -05:00
|
|
|
|
|
|
|
export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => {
|
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({
|
|
|
|
type: 'PROGRESS_TOTAL_PAGES',
|
|
|
|
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({
|
|
|
|
type: 'PROGRESS_PROCESSED_PAGES',
|
|
|
|
data: currentPage++
|
|
|
|
});
|
|
|
|
|
2024-06-04 05:44:02 -05:00
|
|
|
await sleep(0);
|
2024-05-31 04:25:32 -05:00
|
|
|
}
|
|
|
|
|
2024-06-06 02:37:35 -05:00
|
|
|
if (remoteComponentLibrary.remaining() > 0) {
|
|
|
|
children.push({
|
|
|
|
name: 'External Components',
|
|
|
|
children: await translateRemoteChildren()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-05 05:36:49 -05:00
|
|
|
const images: Record<string, Uint8Array> = {};
|
|
|
|
|
|
|
|
for (const [key, image] of Object.entries(imagesLibrary.all())) {
|
|
|
|
const bytes = await image?.getBytesAsync();
|
|
|
|
|
|
|
|
if (!bytes) continue;
|
|
|
|
|
|
|
|
images[key] = bytes;
|
|
|
|
}
|
|
|
|
|
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-05 05:36:49 -05:00
|
|
|
images
|
2024-04-12 06:55:42 -05:00
|
|
|
};
|
|
|
|
};
|