0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-01-03 05:10:13 -05:00
penpot-exporter-figma-plugin/src/ui/ui.tsx

113 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import { createRoot } from 'react-dom/client';
2022-10-11 08:55:08 -05:00
2024-04-08 10:50:01 -05:00
import { createPenpotFile } from './converters';
import './ui.css';
2022-10-11 08:55:08 -05:00
type PenpotExporterState = {
missingFonts: Set<string>;
2024-04-08 10:03:34 -05:00
exporting: boolean;
};
export default class PenpotExporter extends React.Component<unknown, PenpotExporterState> {
2022-10-11 08:55:08 -05:00
state: PenpotExporterState = {
missingFonts: new Set(),
2024-04-08 10:03:34 -05:00
exporting: false
2022-10-11 08:55:08 -05:00
};
componentDidMount = () => {
window.addEventListener('message', this.onMessage);
};
componentDidUpdate = () => {
this.setDimensions();
};
componentWillUnmount = () => {
2022-10-11 08:55:08 -05:00
window.removeEventListener('message', this.onMessage);
};
2022-10-11 08:55:08 -05:00
2024-04-08 10:50:01 -05:00
// TODO: FIX THIS CODE
// addFontWarning(font: string) {
// const newMissingFonts = this.state.missingFonts;
// newMissingFonts.add(font);
//
// this.setState(() => ({ missingFonts: newMissingFonts }));
// }
2022-10-11 08:55:08 -05:00
onCreatePenpot = () => {
2024-04-08 10:03:34 -05:00
this.setState(() => ({ exporting: true }));
parent.postMessage({ pluginMessage: { type: 'export' } }, '*');
2022-10-11 08:55:08 -05:00
};
onCancel = () => {
parent.postMessage({ pluginMessage: { type: 'cancel' } }, '*');
2022-10-11 08:55:08 -05:00
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onMessage = (event: any) => {
if (event.data.pluginMessage.type == 'FIGMAFILE') {
2024-04-08 10:50:01 -05:00
const file = createPenpotFile(event.data.pluginMessage.data);
2024-04-08 10:03:34 -05:00
file.export();
this.setState(() => ({ exporting: false }));
2022-10-11 08:55:08 -05:00
}
};
2022-10-11 08:55:08 -05:00
setDimensions = () => {
const isMissingFonts = this.state.missingFonts.size > 0;
let width = 300;
let height = 280;
if (isMissingFonts) {
height += this.state.missingFonts.size * 20;
width = 400;
2024-04-08 10:03:34 -05:00
parent.postMessage({ pluginMessage: { type: 'resize', width: width, height: height } }, '*');
}
};
renderFontWarnings = () => {
return (
<ul>
{Array.from(this.state.missingFonts).map(font => (
<li key={font}>{font}</li>
))}
</ul>
);
};
2022-10-11 08:55:08 -05:00
render() {
// Update the dimensions of the plugin window based on available data and selections
2022-10-11 08:55:08 -05:00
return (
<main>
<header>
<img src={require('./logo.svg')} />
2022-10-11 08:55:08 -05:00
<h2>Penpot Exporter</h2>
</header>
<section>
<div style={{ display: this.state.missingFonts.size > 0 ? 'inline' : 'none' }}>
<div id="missing-fonts">
{this.state.missingFonts.size} non-default font
{this.state.missingFonts.size > 1 ? 's' : ''}:{' '}
</div>
<small>Ensure fonts are installed in Penpot before importing.</small>
<div id="missing-fonts-list">{this.renderFontWarnings()}</div>
</div>
2022-10-11 08:55:08 -05:00
</section>
<footer>
2024-04-08 10:03:34 -05:00
<button className="brand" disabled={this.state.exporting} onClick={this.onCreatePenpot}>
{this.state.exporting ? 'Exporting...' : 'Export to Penpot'}
2022-10-11 08:55:08 -05:00
</button>
<button onClick={this.onCancel}>Cancel</button>
</footer>
</main>
);
}
}
createRoot(document.getElementById('penpot-export-page') as HTMLElement).render(
2022-10-11 08:55:08 -05:00
<React.StrictMode>
<PenpotExporter />
</React.StrictMode>
2022-10-11 08:55:08 -05:00
);