2024-04-08 04:43:30 -05:00
|
|
|
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';
|
2024-04-08 04:43:30 -05:00
|
|
|
import './ui.css';
|
2022-10-11 08:55:08 -05:00
|
|
|
|
|
|
|
type PenpotExporterState = {
|
2024-04-08 04:43:30 -05:00
|
|
|
missingFonts: Set<string>;
|
2024-04-08 10:03:34 -05:00
|
|
|
exporting: boolean;
|
2024-04-08 04:43:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class PenpotExporter extends React.Component<unknown, PenpotExporterState> {
|
2022-10-11 08:55:08 -05:00
|
|
|
state: PenpotExporterState = {
|
2023-01-30 03:11:55 -05:00
|
|
|
missingFonts: new Set(),
|
2024-04-08 10:03:34 -05:00
|
|
|
exporting: false
|
2022-10-11 08:55:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount = () => {
|
2024-04-08 04:43:30 -05:00
|
|
|
window.addEventListener('message', this.onMessage);
|
|
|
|
};
|
2023-01-30 03:11:55 -05:00
|
|
|
|
|
|
|
componentDidUpdate = () => {
|
|
|
|
this.setDimensions();
|
2024-04-08 04:43:30 -05:00
|
|
|
};
|
2023-01-30 03:11:55 -05:00
|
|
|
|
2024-04-08 04:43:30 -05:00
|
|
|
componentWillUnmount = () => {
|
2022-10-11 08:55:08 -05:00
|
|
|
window.removeEventListener('message', this.onMessage);
|
2024-04-08 04:43:30 -05:00
|
|
|
};
|
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 = () => {
|
2024-04-08 04:43:30 -05:00
|
|
|
parent.postMessage({ pluginMessage: { type: 'cancel' } }, '*');
|
2022-10-11 08:55:08 -05:00
|
|
|
};
|
|
|
|
|
2024-04-08 04:43:30 -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
|
|
|
}
|
2024-04-08 04:43:30 -05:00
|
|
|
};
|
2022-10-11 08:55:08 -05:00
|
|
|
|
2023-01-30 03:11:55 -05:00
|
|
|
setDimensions = () => {
|
|
|
|
const isMissingFonts = this.state.missingFonts.size > 0;
|
|
|
|
|
|
|
|
let width = 300;
|
|
|
|
let height = 280;
|
|
|
|
|
|
|
|
if (isMissingFonts) {
|
2024-04-08 04:43:30 -05:00
|
|
|
height += this.state.missingFonts.size * 20;
|
2023-01-30 03:11:55 -05:00
|
|
|
width = 400;
|
2024-04-08 10:03:34 -05:00
|
|
|
parent.postMessage({ pluginMessage: { type: 'resize', width: width, height: height } }, '*');
|
2023-01-30 03:11:55 -05:00
|
|
|
}
|
2024-04-08 04:43:30 -05:00
|
|
|
};
|
2023-01-30 03:11:55 -05:00
|
|
|
|
|
|
|
renderFontWarnings = () => {
|
|
|
|
return (
|
2024-04-08 04:43:30 -05:00
|
|
|
<ul>
|
|
|
|
{Array.from(this.state.missingFonts).map(font => (
|
2023-01-30 03:11:55 -05:00
|
|
|
<li key={font}>{font}</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
2024-04-08 04:43:30 -05:00
|
|
|
};
|
2022-10-11 08:55:08 -05:00
|
|
|
|
|
|
|
render() {
|
2023-01-30 03:11:55 -05:00
|
|
|
// Update the dimensions of the plugin window based on available data and selections
|
2022-10-11 08:55:08 -05:00
|
|
|
return (
|
|
|
|
<main>
|
|
|
|
<header>
|
2024-04-08 04:43:30 -05:00
|
|
|
<img src={require('./logo.svg')} />
|
2022-10-11 08:55:08 -05:00
|
|
|
<h2>Penpot Exporter</h2>
|
|
|
|
</header>
|
|
|
|
<section>
|
2024-04-08 04:43:30 -05:00
|
|
|
<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' : ''}:{' '}
|
2023-01-30 03:11:55 -05:00
|
|
|
</div>
|
2024-04-08 04:43:30 -05:00
|
|
|
<small>Ensure fonts are installed in Penpot before importing.</small>
|
|
|
|
<div id="missing-fonts-list">{this.renderFontWarnings()}</div>
|
2023-01-30 03:11:55 -05:00
|
|
|
</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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 04:43:30 -05:00
|
|
|
createRoot(document.getElementById('penpot-export-page') as HTMLElement).render(
|
2022-10-11 08:55:08 -05:00
|
|
|
<React.StrictMode>
|
2024-04-08 04:43:30 -05:00
|
|
|
<PenpotExporter />
|
|
|
|
</React.StrictMode>
|
2022-10-11 08:55:08 -05:00
|
|
|
);
|