2024-06-26 08:11:57 +02:00
|
|
|
import { sleep } from '@common/sleep';
|
2024-05-29 17:33:29 +02:00
|
|
|
|
2024-06-06 17:24:59 +02:00
|
|
|
import { sendMessage } from '@ui/context';
|
2024-06-25 14:12:37 +02:00
|
|
|
import { createFile } from '@ui/lib/penpot';
|
|
|
|
import { PenpotFile } from '@ui/lib/types/penpotFile';
|
2024-06-25 16:08:59 +02:00
|
|
|
import { TypographyStyle } from '@ui/lib/types/shapes/textShape';
|
2024-06-25 14:12:37 +02:00
|
|
|
import { FillStyle } from '@ui/lib/types/utils/fill';
|
2024-06-26 08:11:57 +02:00
|
|
|
import { colors, componentShapes, images, init, typographies } from '@ui/parser';
|
2024-06-25 14:12:37 +02:00
|
|
|
import { buildFile } from '@ui/parser/creators';
|
2024-05-29 17:33:29 +02:00
|
|
|
import { PenpotDocument } from '@ui/types';
|
2024-05-29 12:52:21 +02:00
|
|
|
|
2024-06-18 09:50:38 +02:00
|
|
|
import { parseImage } from '.';
|
2024-05-30 17:54:37 +02:00
|
|
|
|
2024-06-26 08:11:57 +02:00
|
|
|
const optimizeImages = async (binaryImages: Record<string, Uint8Array>) => {
|
|
|
|
const imagesToOptimize = Object.entries(binaryImages);
|
2024-06-18 09:50:38 +02:00
|
|
|
|
|
|
|
if (imagesToOptimize.length === 0) return;
|
|
|
|
|
2024-06-06 17:24:59 +02:00
|
|
|
let imagesOptimized = 1;
|
|
|
|
|
|
|
|
sendMessage({
|
2024-06-18 09:50:38 +02:00
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: imagesToOptimize.length
|
2024-06-06 17:24:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
sendMessage({
|
2024-06-18 09:50:38 +02:00
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'optimization'
|
2024-06-06 17:24:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const [key, bytes] of imagesToOptimize) {
|
|
|
|
if (bytes) {
|
2024-06-26 08:11:57 +02:00
|
|
|
images.set(key, await parseImage(bytes));
|
2024-06-06 17:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: imagesOptimized++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-25 16:08:59 +02:00
|
|
|
const prepareTypographyLibraries = async (
|
|
|
|
file: PenpotFile,
|
|
|
|
styles: Record<string, TypographyStyle>
|
|
|
|
) => {
|
|
|
|
const stylesToRegister = Object.entries(styles);
|
|
|
|
|
|
|
|
if (stylesToRegister.length === 0) return;
|
|
|
|
|
|
|
|
let stylesRegistered = 1;
|
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: stylesToRegister.length
|
|
|
|
});
|
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'typoFormat'
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [key, style] of stylesToRegister) {
|
|
|
|
const typographyId = file.newId();
|
|
|
|
style.textStyle.typographyRefId = typographyId;
|
|
|
|
style.textStyle.typographyRefFile = file.getId();
|
|
|
|
style.typography.id = typographyId;
|
|
|
|
|
2024-06-26 08:11:57 +02:00
|
|
|
typographies.set(key, style);
|
2024-06-25 16:08:59 +02:00
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: stylesRegistered++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-25 14:12:37 +02:00
|
|
|
const prepareColorLibraries = async (file: PenpotFile, styles: Record<string, FillStyle>) => {
|
|
|
|
const stylesToRegister = Object.entries(styles);
|
|
|
|
|
|
|
|
if (stylesToRegister.length === 0) return;
|
|
|
|
|
|
|
|
let stylesRegistered = 1;
|
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: stylesToRegister.length
|
|
|
|
});
|
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'format'
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [key, fillStyle] of stylesToRegister) {
|
|
|
|
for (let index = 0; index < fillStyle.fills.length; index++) {
|
|
|
|
const colorId = file.newId();
|
|
|
|
fillStyle.fills[index].fillColorRefId = colorId;
|
|
|
|
fillStyle.fills[index].fillColorRefFile = file.getId();
|
|
|
|
fillStyle.colors[index].id = colorId;
|
|
|
|
fillStyle.colors[index].refFile = file.getId();
|
|
|
|
}
|
|
|
|
|
2024-06-26 08:11:57 +02:00
|
|
|
colors.set(key, fillStyle);
|
2024-06-25 14:12:37 +02:00
|
|
|
|
|
|
|
sendMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: stylesRegistered++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-19 15:58:13 +02:00
|
|
|
export const parse = async ({
|
|
|
|
name,
|
|
|
|
children = [],
|
|
|
|
components,
|
|
|
|
images,
|
2024-06-25 17:12:20 +02:00
|
|
|
paintStyles,
|
|
|
|
textStyles
|
2024-06-19 15:58:13 +02:00
|
|
|
}: PenpotDocument) => {
|
2024-06-26 08:11:57 +02:00
|
|
|
init(components, componentShapes);
|
2024-06-25 14:12:37 +02:00
|
|
|
|
|
|
|
const file = createFile(name);
|
2024-06-05 12:36:49 +02:00
|
|
|
|
2024-06-06 17:24:59 +02:00
|
|
|
await optimizeImages(images);
|
2024-06-25 17:12:20 +02:00
|
|
|
await prepareColorLibraries(file, paintStyles);
|
|
|
|
await prepareTypographyLibraries(file, textStyles);
|
2024-06-05 12:36:49 +02:00
|
|
|
|
2024-06-25 14:12:37 +02:00
|
|
|
return buildFile(file, children);
|
2024-05-29 12:52:21 +02:00
|
|
|
};
|