Skip to content

Commit

Permalink
fix and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Jun 3, 2024
1 parent ae51d38 commit 4794e47
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformDocumentNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';
import { imagesLibrary } from '@plugin/ImageLibrary';

import { PenpotDocument } from '@ui/types';

Expand Down Expand Up @@ -27,6 +28,7 @@ export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotD
return {
name: node.name,
children,
components: componentsLibrary.all()
components: componentsLibrary.all(),
images: imagesLibrary.all()
};
};
18 changes: 14 additions & 4 deletions plugin-src/translators/fills/translateImageFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ export const translateImageFill = async (fill: ImagePaint): Promise<Fill | undef
const translateImage = async (imageHash: string | null): Promise<ImageColor | undefined> => {
if (!imageHash) return;

const libraryImage = imagesLibrary.get(imageHash);
const imageColor = imagesLibrary.get(imageHash) ?? (await calculateAndRegister(imageHash));

if (libraryImage) return libraryImage;
if (!imageColor) return;

const { dataUri, ...rest } = imageColor;

return {
...rest,
imageHash
};
};

const calculateAndRegister = async (imageHash: string) => {
const image = figma.getImageByHash(imageHash);

if (!image) return;

const bytes = await image.getBytesAsync();
const { width, height } = await image.getSizeAsync();
const b64 = figma.base64Encode(await image.getBytesAsync());
const b64 = figma.base64Encode(bytes);
const mtype = detectMimeType(b64);
const dataUri = `data:${mtype};base64,${b64}`;

const imageColor = {
const imageColor: ImageColor = {
width,
height,
mtype,
Expand Down
1 change: 1 addition & 0 deletions ui-src/lib/types/utils/imageColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type ImageColor = {
id?: Uuid;
keepAspectRatio?: boolean;
dataUri?: string;
imageHash?: string;
};
12 changes: 12 additions & 0 deletions ui-src/parser/creators/symbols/symbolFillGradients.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { imagesLibrary } from '@plugin/ImageLibrary';

import { Fill } from '@ui/lib/types/utils/fill';
import { Gradient, LINEAR_TYPE, RADIAL_TYPE } from '@ui/lib/types/utils/gradient';

Expand All @@ -9,6 +11,16 @@ export const symbolFillGradients = (fills?: Fill[]): Fill[] | undefined => {
fill.fillColorGradient = symbolFillGradient(fill.fillColorGradient);
}

if (fill.fillImage?.imageHash) {
const imageColor = imagesLibrary.get(fill.fillImage?.imageHash);
const { imageHash, ...rest } = fill.fillImage;

fill.fillImage = {
...rest,
dataUri: imageColor?.dataUri
};
}

return fill;
});
};
Expand Down
5 changes: 4 additions & 1 deletion ui-src/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';
import { imagesLibrary } from '@plugin/ImageLibrary';

import { createFile } from '@ui/lib/penpot';
import { createComponentLibrary, createPage } from '@ui/parser/creators';
Expand All @@ -7,8 +8,10 @@ import { PenpotDocument } from '@ui/types';

import { idLibrary } from '.';

export const parse = ({ name, children = [], components }: PenpotDocument) => {
export const parse = ({ name, children = [], components, images }: PenpotDocument) => {
componentsLibrary.init(components);
imagesLibrary.init(images);

uiComponents.init();
idLibrary.init();

Expand Down
2 changes: 2 additions & 0 deletions ui-src/types/penpotDocument.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { PenpotPage } from '@ui/lib/types/penpotPage';
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';
import { ImageColor } from '@ui/lib/types/utils/imageColor';

export type PenpotDocument = {
name: string;
children?: PenpotPage[];
components: Record<string, ComponentShape>;
images: Record<string, ImageColor>;
};

0 comments on commit 4794e47

Please sign in to comment.