import * as React from 'react'; import { createRoot } from 'react-dom/client'; import slugify from 'slugify'; import { createPenpotFile } from './converters'; import './ui.css'; import { validateFont } from './validators'; type PenpotExporterState = { missingFonts: Set; exporting: boolean; }; export default class PenpotExporter extends React.Component { state: PenpotExporterState = { missingFonts: new Set(), exporting: false }; componentDidMount = () => { window.addEventListener('message', this.onMessage); }; componentDidUpdate = () => { this.setDimensions(); }; componentWillUnmount = () => { window.removeEventListener('message', this.onMessage); }; addFontWarning(font: string) { const newMissingFonts = this.state.missingFonts; newMissingFonts.add(font); this.setState(() => ({ missingFonts: newMissingFonts })); } onCreatePenpot = () => { this.setState(() => ({ exporting: true })); parent.postMessage({ pluginMessage: { type: 'export' } }, '*'); }; onCancel = () => { parent.postMessage({ pluginMessage: { type: 'cancel' } }, '*'); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any onMessage = (event: any) => { if (event.data.pluginMessage.type == 'FIGMAFILE') { const file = createPenpotFile(event.data.pluginMessage.data); // validate file.fontNames file.fontNames.forEach(font => { if (!validateFont(font)) { this.addFontWarning(slugify(font.family.toLowerCase())); } }); file.penpotFile.export(); this.setState(() => ({ exporting: false })); } }; setDimensions = () => { const isMissingFonts = this.state.missingFonts.size > 0; let width = 300; let height = 280; if (isMissingFonts) { height += this.state.missingFonts.size * 20; width = 400; parent.postMessage({ pluginMessage: { type: 'resize', width: width, height: height } }, '*'); } }; renderFontWarnings = () => { return ( ); }; render() { // Update the dimensions of the plugin window based on available data and selections return (

Penpot Exporter

0 ? 'inline' : 'none' }}>
{this.state.missingFonts.size} non-default font {this.state.missingFonts.size > 1 ? 's' : ''}:{' '}
Ensure fonts are installed in Penpot before importing.
{this.renderFontWarnings()}
); } } createRoot(document.getElementById('penpot-export-page') as HTMLElement).render( );