diff --git a/src/ui/PenpotExporter.tsx b/src/ui/PenpotExporter.tsx index 49ee19b..62c2aab 100644 --- a/src/ui/PenpotExporter.tsx +++ b/src/ui/PenpotExporter.tsx @@ -1,110 +1,97 @@ -import { Component } from 'react'; +import { useEffect, useState } from 'react'; import slugify from 'slugify'; import { createPenpotFile } from './converters'; import { validateFont } from './validators'; -type PenpotExporterState = { - missingFonts: Set; - exporting: boolean; -}; +export const PenpotExporter = () => { + const [missingFonts, setMissingFonts] = useState(new Set()); + const [exporting, setExporting] = useState(false); -export default class PenpotExporter extends 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' } }, '*'); + const addFontWarning = (font: string) => { + setMissingFonts(missingFonts => missingFonts.add(font)); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any - onMessage = (event: any) => { + const 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())); + addFontWarning(slugify(font.family.toLowerCase())); } }); + file.penpotFile.export(); - this.setState(() => ({ exporting: false })); + + setExporting(false); } }; - setDimensions = () => { - const isMissingFonts = this.state.missingFonts.size > 0; + 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 += this.state.missingFonts.size * 20; + height += missingFonts.size * 20; width = 400; parent.postMessage({ pluginMessage: { type: 'resize', width: width, height: height } }, '*'); } }; - renderFontWarnings = () => { - return ( -
    - {Array.from(this.state.missingFonts).map(font => ( -
  • {font}
  • - ))} -
- ); - }; + useEffect(() => { + window.addEventListener('message', onMessage); - 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()}
+ return () => { + window.removeEventListener('message', onMessage); + }; + }, []); + + useEffect(() => { + setDimensions(); + }, [missingFonts]); + + return ( +
+
+ +

Penpot Exporter

+
+
+
0 ? 'inline' : 'none' }}> +
+ {missingFonts.size} non-default font + {missingFonts.size > 1 ? 's' : ''}:{' '}
-
-
- - -
-
- ); - } -} + Ensure fonts are installed in Penpot before importing. +
+
    + {Array.from(missingFonts).map(font => ( +
  • {font}
  • + ))} +
+
+
+
+ +
+ ); +}; diff --git a/src/ui/ui.tsx b/src/ui/ui.tsx index 7fae224..7497686 100644 --- a/src/ui/ui.tsx +++ b/src/ui/ui.tsx @@ -1,7 +1,7 @@ import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; -import PenpotExporter from './PenpotExporter'; +import { PenpotExporter } from './PenpotExporter'; import './ui.css'; createRoot(document.getElementById('penpot-export-page') as HTMLElement).render(