mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-22 21:53:27 -05:00
c013e80962
* wip * moved logic to validate custom fonts * missing fonts section * lint * refactor and improve * simplify resize * refactor * Implemente custom fonts * styling plugin * styling plugin * minor styling fix * changeset * fix * fix eslint --------- Co-authored-by: Jordi Sala Morales <jordism91@gmail.com>
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { FormProvider, useForm } from 'react-hook-form';
|
|
|
|
import { createPenpotFile } from '@ui/converters';
|
|
import { PenpotDocument } from '@ui/lib/types/penpotDocument';
|
|
|
|
import { Loader } from './Loader';
|
|
import { MissingFontsSection } from './MissingFontsSection';
|
|
|
|
type FormValues = Record<string, string>;
|
|
|
|
export const PenpotExporter = () => {
|
|
const [missingFonts, setMissingFonts] = useState<string[]>();
|
|
const [exporting, setExporting] = useState(false);
|
|
const methods = useForm<FormValues>();
|
|
|
|
methods.getValues();
|
|
|
|
const onMessage = (event: MessageEvent<{ pluginMessage: { type: string; data: unknown } }>) => {
|
|
if (event.data.pluginMessage?.type == 'PENPOT_DOCUMENT') {
|
|
const document = event.data.pluginMessage.data as PenpotDocument;
|
|
const file = createPenpotFile(document);
|
|
|
|
file.export();
|
|
|
|
setExporting(false);
|
|
} else if (event.data.pluginMessage?.type == 'CUSTOM_FONTS') {
|
|
setMissingFonts(event.data.pluginMessage.data as string[]);
|
|
}
|
|
};
|
|
|
|
const exportPenpot = (data: FormValues) => {
|
|
setExporting(true);
|
|
|
|
parent.postMessage(
|
|
{
|
|
pluginMessage: {
|
|
type: 'export',
|
|
data
|
|
}
|
|
},
|
|
'*'
|
|
);
|
|
};
|
|
|
|
const cancel = () => {
|
|
parent.postMessage({ pluginMessage: { type: 'cancel' } }, '*');
|
|
};
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('message', onMessage);
|
|
|
|
parent.postMessage({ pluginMessage: { type: 'ready' } }, '*');
|
|
|
|
return () => {
|
|
window.removeEventListener('message', onMessage);
|
|
};
|
|
}, []);
|
|
|
|
const pluginReady = missingFonts !== undefined;
|
|
|
|
return (
|
|
<FormProvider {...methods}>
|
|
<form className="centered-form" onSubmit={methods.handleSubmit(exportPenpot)}>
|
|
<Loader loading={!pluginReady} />
|
|
<div className="missing-fonts-form-container">
|
|
<MissingFontsSection fonts={missingFonts} />
|
|
</div>
|
|
<footer>
|
|
<button type="submit" className="brand" disabled={exporting || !pluginReady}>
|
|
{exporting ? 'Exporting...' : 'Export to Penpot'}
|
|
</button>
|
|
<button onClick={cancel}>Cancel</button>
|
|
</footer>
|
|
</form>
|
|
</FormProvider>
|
|
);
|
|
};
|