2024-05-29 10:33:29 -05:00
|
|
|
import { componentsLibrary } from '@plugin/ComponentLibrary';
|
2024-06-19 08:58:13 -05:00
|
|
|
import { styleLibrary } from '@plugin/StyleLibrary';
|
2024-06-06 10:24:59 -05:00
|
|
|
// @TODO: Direct import on purpose, to avoid problems with the tsc linting
|
|
|
|
import { sleep } from '@plugin/utils/sleep';
|
2024-05-29 10:33:29 -05:00
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
import { sendMessage } from '@ui/context';
|
2024-06-18 02:50:38 -05:00
|
|
|
import { createFile } from '@ui/parser/creators';
|
|
|
|
import { uiImages } from '@ui/parser/libraries';
|
2024-05-29 10:33:29 -05:00
|
|
|
import { PenpotDocument } from '@ui/types';
|
2024-05-29 05:52:21 -05:00
|
|
|
|
2024-06-18 02:50:38 -05:00
|
|
|
import { parseImage } from '.';
|
2024-05-30 10:54:37 -05:00
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
const optimizeImages = async (images: Record<string, Uint8Array>) => {
|
|
|
|
const imagesToOptimize = Object.entries(images);
|
2024-06-18 02:50:38 -05:00
|
|
|
|
|
|
|
if (imagesToOptimize.length === 0) return;
|
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
let imagesOptimized = 1;
|
|
|
|
|
|
|
|
sendMessage({
|
2024-06-18 02:50:38 -05:00
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: imagesToOptimize.length
|
2024-06-06 10:24:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
sendMessage({
|
2024-06-18 02:50:38 -05:00
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'optimization'
|
2024-06-06 10:24:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const [key, bytes] of imagesToOptimize) {
|
|
|
|
if (bytes) {
|
|
|
|
uiImages.register(key, await parseImage(bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: imagesOptimized++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-19 08:58:13 -05:00
|
|
|
export const parse = async ({
|
|
|
|
name,
|
|
|
|
children = [],
|
|
|
|
components,
|
|
|
|
images,
|
|
|
|
styles
|
|
|
|
}: PenpotDocument) => {
|
2024-05-29 10:33:29 -05:00
|
|
|
componentsLibrary.init(components);
|
2024-06-19 08:58:13 -05:00
|
|
|
styleLibrary.init(styles);
|
2024-06-05 05:36:49 -05:00
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
await optimizeImages(images);
|
2024-06-05 05:36:49 -05:00
|
|
|
|
2024-06-18 02:50:38 -05:00
|
|
|
return createFile(name, children);
|
2024-05-29 05:52:21 -05:00
|
|
|
};
|