0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-31 12:03:58 -05:00
penpot-exporter-figma-plugin/ui-src/PenpotExporter.tsx
Alex Sánchez 8021da2623
Google Fonts (#80)
* moved validate font logic

* google fonts working

* fixes

* minor improvements

* fix linter

* fixes

* Changeset

* refactor

* refactor

* move files around

---------

Co-authored-by: Jordi Sala Morales <jordism91@gmail.com>
2024-04-29 15:44:49 +02:00

94 lines
2.6 KiB
TypeScript

import { useEffect, useState } from 'react';
import { createPenpotFile } from '@ui/converters';
import { PenpotDocument } from '@ui/lib/types/penpotDocument';
import Logo from './logo.svg?react';
export const PenpotExporter = () => {
const [missingFonts, setMissingFonts] = useState(new Set<string>());
const [exporting, setExporting] = useState(false);
const addFontWarning = (font: string) => {
setMissingFonts(missingFonts => missingFonts.add(font));
};
const onMessage = (event: MessageEvent<{ pluginMessage: { type: string; data: unknown } }>) => {
if (event.data.pluginMessage?.type == 'FIGMAFILE') {
const document = event.data.pluginMessage.data as PenpotDocument;
const file = createPenpotFile(document);
file.export();
setExporting(false);
} else if (event.data.pluginMessage?.type == 'FONT_NAME') {
addFontWarning(event.data.pluginMessage.data as string);
}
};
const onCreatePenpot = () => {
setExporting(true);
parent.postMessage({ pluginMessage: { type: 'export' } }, '*');
};
const onCancel = () => {
parent.postMessage({ pluginMessage: { type: 'cancel' } }, '*');
};
const setDimensions = () => {
const isMissingFonts = missingFonts.size > 0;
let width = 300;
let height = 280;
if (isMissingFonts) {
height += missingFonts.size * 20;
width = 400;
parent.postMessage({ pluginMessage: { type: 'resize', width: width, height: height } }, '*');
}
};
useEffect(() => {
window.addEventListener('message', onMessage);
return () => {
window.removeEventListener('message', onMessage);
};
}, []);
useEffect(() => {
setDimensions();
}, [missingFonts]);
return (
<main>
<header>
<Logo />
<h2>Penpot Exporter</h2>
</header>
<section>
<div style={{ display: missingFonts.size > 0 ? 'inline' : 'none' }}>
<div id="missing-fonts">
{missingFonts.size} non-default font
{missingFonts.size > 1 ? 's' : ''}:{' '}
</div>
<small>Ensure fonts are installed in Penpot before importing.</small>
<div id="missing-fonts-list">
<ul>
{Array.from(missingFonts).map(font => (
<li key={font}>{font}</li>
))}
</ul>
</div>
</div>
</section>
<footer>
<button className="brand" disabled={exporting} onClick={onCreatePenpot}>
{exporting ? 'Exporting...' : 'Export to Penpot'}
</button>
<button onClick={onCancel}>Cancel</button>
</footer>
</main>
);
};