Skip to content

Instantly share code, notes, and snippets.

@oeway
Created August 16, 2023 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oeway/b734c35f69a0ec0dcebe00b078676edb to your computer and use it in GitHub Desktop.
Save oeway/b734c35f69a0ec0dcebe00b078676edb to your computer and use it in GitHub Desktop.
<docs lang="markdown">
[TODO: write documentation for this plugin.]
</docs>
<config lang="json">
{
"name": "React UI Plugin",
"type": "window",
"tags": [],
"ui": "",
"version": "0.1.0",
"cover": "",
"description": "Generate React UI from jsx script",
"icon": "extension",
"inputs": null,
"outputs": null,
"api_version": "0.1.8",
"env": "",
"permissions": [],
"requirements": ["https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js", "https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js", "https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js", "https://cdn.tailwindcss.com"],
"dependencies": [],
"defaults": {"w": 20, "h": 10}
}
</config>
<attachment name="react-src">
const WelcomePage = () => {
return (
<div className="flex flex-col justify-center items-center min-h-screen bg-gray-100 text-gray-800 p-8">
<div className="p-8 bg-white shadow-md rounded-lg">
<h1 className="text-2xl font-bold mb-4">Welcome to ImJoy React UI Plugin!</h1>
<p className="text-base">Here you can create, customize and enjoy your ImJoy plugins with React and Tailwind CSS.</p>
</div>
</div>
);
};
ReactDOM.render(<WelcomePage />, document.getElementById('root'));
</attachment>
<script lang="javascript">
async function loadBabelScript(content) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = 'text/babel';
script.setAttribute('data-presets', 'react');
script.textContent = content;
script.onerror = function() {
reject(new Error('Failed to load the Babel script.'));
};
document.head.appendChild(script);
setTimeout(()=>{
try{
Babel.transformScriptTags();
resolve('Babel script has been loaded!');
} catch (error) {
reject(error);
}
}, 0);
});
}
class ImJoyPlugin {
async setup() {
await api.log('initialized')
}
async loadJsxScript(script){
await loadBabelScript(script);
}
async run(ctx) {
if(ctx.data && ctx.data.jsx_script)
await loadBabelScript(ctx.data.jsx_script);
else
await loadBabelScript(await api.getAttachment('react-src'));
}
}
api.export(new ImJoyPlugin())
</script>
<window lang="html">
<div id="root"></div>
</window>
<style lang="css">
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment