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-25 09:08:59 -05:00
|
|
|
import { textLibrary } from '@plugin/TextLibrary';
|
2024-06-06 02:37:35 -05:00
|
|
|
import { translateRemoteChildren } from '@plugin/translators';
|
2024-06-25 09:08:59 -05:00
|
|
|
import { translatePaintStyle, translateTextStyle } from '@plugin/translators/styles';
|
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-06-25 09:08:59 -05:00
|
|
|
import { TypographyStyle } from '@ui/lib/types/shapes/textShape';
|
2024-06-25 07:12:37 -05:00
|
|
|
import { FillStyle } from '@ui/lib/types/utils/fill';
|
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-25 09:08:59 -05:00
|
|
|
const isPaintStyle = (style: BaseStyle): style is PaintStyle => {
|
|
|
|
return style.type === 'PAINT';
|
|
|
|
};
|
|
|
|
|
|
|
|
const isTextStyle = (style: BaseStyle): style is TextStyle => {
|
|
|
|
return style.type === 'TEXT';
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2024-06-25 07:12:37 -05:00
|
|
|
const getFillStyles = async (): Promise<Record<string, FillStyle>> => {
|
|
|
|
const stylesToFetch = Object.entries(styleLibrary.all());
|
|
|
|
const styles: Record<string, FillStyle> = {};
|
|
|
|
|
|
|
|
if (stylesToFetch.length === 0) return styles;
|
|
|
|
|
|
|
|
let currentStyle = 1;
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: stylesToFetch.length
|
|
|
|
});
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'fills'
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [styleId, paintStyle] of stylesToFetch) {
|
|
|
|
const figmaStyle = paintStyle ?? (await figma.getStyleByIdAsync(styleId));
|
|
|
|
if (figmaStyle && isPaintStyle(figmaStyle)) {
|
2024-06-25 09:08:59 -05:00
|
|
|
styles[styleId] = translatePaintStyle(figmaStyle);
|
2024-06-25 07:12:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: currentStyle++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
await sleep(20);
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
};
|
|
|
|
|
2024-06-25 09:08:59 -05:00
|
|
|
const getTextStyles = async (): Promise<Record<string, TypographyStyle>> => {
|
|
|
|
const stylesToFetch = Object.entries(textLibrary.all());
|
|
|
|
const styles: Record<string, TypographyStyle> = {};
|
|
|
|
|
|
|
|
if (stylesToFetch.length === 0) return styles;
|
|
|
|
|
|
|
|
let currentStyle = 1;
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: stylesToFetch.length
|
|
|
|
});
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'typographies'
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [styleId, style] of stylesToFetch) {
|
|
|
|
const figmaStyle = style ?? (await figma.getStyleByIdAsync(styleId));
|
|
|
|
if (figmaStyle && isTextStyle(figmaStyle)) {
|
|
|
|
styles[styleId] = translateTextStyle(figmaStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: currentStyle++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
await sleep(20);
|
|
|
|
|
|
|
|
return styles;
|
2024-06-25 07:12:37 -05:00
|
|
|
};
|
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
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> => {
|
2024-06-25 07:12:37 -05:00
|
|
|
const localPaintStyles = await figma.getLocalPaintStylesAsync();
|
|
|
|
localPaintStyles.forEach(style => {
|
|
|
|
styleLibrary.register(style.id, style);
|
|
|
|
});
|
|
|
|
|
2024-06-25 09:08:59 -05:00
|
|
|
const localTextStyles = await figma.getLocalTextStylesAsync();
|
|
|
|
localTextStyles.forEach(style => {
|
|
|
|
textLibrary.register(style.id, style);
|
|
|
|
});
|
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
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-06-25 07:12:37 -05:00
|
|
|
const styles = await getFillStyles();
|
|
|
|
|
|
|
|
const images = await downloadImages();
|
|
|
|
|
2024-06-25 09:08:59 -05:00
|
|
|
const typographies = await getTextStyles();
|
|
|
|
|
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-25 07:12:37 -05:00
|
|
|
images,
|
2024-06-25 09:08:59 -05:00
|
|
|
styles,
|
|
|
|
typographies
|
2024-04-12 06:55:42 -05:00
|
|
|
};
|
|
|
|
};
|