0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 21:53:27 -05:00
penpot-exporter-figma-plugin/ui-src/parser/parse.ts

146 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

import { init } from '@common/map';
2024-06-26 01:11:57 -05:00
import { sleep } from '@common/sleep';
import { sendMessage } from '@ui/context';
import { createFile } from '@ui/lib/penpot';
import { PenpotFile } from '@ui/lib/types/penpotFile';
import { TypographyStyle } from '@ui/lib/types/shapes/textShape';
import { FillStyle } from '@ui/lib/types/utils/fill';
import {
colors,
componentShapes,
images,
typographies,
componentProperties as uiComponentProperties
} from '@ui/parser';
import { buildFile } from '@ui/parser/creators';
import { PenpotDocument } from '@ui/types';
import { parseImage } from '.';
2024-06-26 01:11:57 -05:00
const optimizeImages = async (binaryImages: Record<string, Uint8Array>) => {
const imagesToOptimize = Object.entries(binaryImages);
if (imagesToOptimize.length === 0) return;
let imagesOptimized = 1;
sendMessage({
type: 'PROGRESS_TOTAL_ITEMS',
data: imagesToOptimize.length
});
sendMessage({
type: 'PROGRESS_STEP',
data: 'optimization'
});
for (const [key, bytes] of imagesToOptimize) {
if (bytes) {
2024-06-26 01:11:57 -05:00
images.set(key, await parseImage(bytes));
}
sendMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: imagesOptimized++
});
await sleep(0);
}
};
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 01:11:57 -05:00
typographies.set(key, style);
sendMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: stylesRegistered++
});
await sleep(0);
}
};
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 01:11:57 -05:00
colors.set(key, fillStyle);
sendMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: stylesRegistered++
});
await sleep(0);
}
};
export const parse = async ({
name,
children = [],
components,
images,
paintStyles,
textStyles,
componentProperties
}: PenpotDocument) => {
init(componentShapes, components);
init(uiComponentProperties, componentProperties);
const file = createFile(name);
await optimizeImages(images);
await prepareColorLibraries(file, paintStyles);
await prepareTypographyLibraries(file, textStyles);
return buildFile(file, children);
};