openflexure-microscope-server/webapp/public/OpenFlexureScriptEditor.imjoy.html
2021-09-16 15:13:19 +01:00

131 lines
No EOL
3.6 KiB
HTML

<config lang="json">
{
"name": "Script Editor",
"type": "web-worker",
"tags": [],
"ui": "",
"version": "0.1.0",
"cover": "",
"description": "A Script Editor for Controlling OpenFlexure Microscope with ImJoy",
"icon": "extension",
"inputs": null,
"outputs": null,
"api_version": "0.1.8",
"env": "",
"permissions": [],
"requirements": [],
"dependencies": []
}
</config>
<script lang="javascript">
class ImJoyPlugin {
async setup() {
api.log('initialized')
}
async run(ctx) {
let pluginInEditor, stopped, editorWindow;
const config = {lang: 'javascript'}
config.templates = [
{
name: "New",
url: null,
lang: 'javascript',
},
{
name: "Snap Image Template",
url: "./OpenFlexureSnapImageTemplate.imjoy.html",
lang: "html",
}
]
config.ui_elements = {
save: {
_rintf: true,
type: 'button',
label: "Save",
visible: false,
icon: "content-save",
callback(content) {
console.log(content)
}
},
run: {
_rintf: true,
type: 'button',
label: "Run",
icon: "play",
visible: true,
shortcut: 'Shift-Enter',
async callback(content) {
try {
editorWindow.setLoader(true);
editorWindow.updateUIElement('stop', {
visible: true
})
api.showProgress(0);
pluginInEditor = await api.getPlugin({src: content, hot_reloading: true})
if (stopped) {
pluginInEditor = null;
return;
}
if (pluginInEditor && pluginInEditor.run) {
return await pluginInEditor.run({
config: {},
data: {}
});
}
if (stopped) {
pluginInEditor = null;
return;
}
} catch (e) {
api.showMessage("Failed to load plugin, error: " + e.toString());
} finally {
editorWindow.updateUIElement('stop', {
visible: false
})
editorWindow.setLoader(false);
api.showProgress(100);
}
}
},
stop: {
_rintf: true,
type: 'button',
label: "Stop",
style: "color: #ff0080cf;",
icon: "stop",
visible: false,
async callback() {
stopped = true;
await editorWindow.setLoader(false);
await editorWindow.updateUIElement('stop', {
visible: false
})
}
},
export: {
_rintf: true,
type: 'button',
label: "Export",
icon: "file-download-outline",
visible: true,
async callback(content) {
const fileName = (pluginInEditor && pluginInEditor.config.name && pluginInEditor.config.name + '.imjoy.html') || config.name + '.imjoy.html' || "myPlugin.imjoy.html";
await api.exportFile(content, fileName);
}
}
}
editorWindow = await api.createWindow({
src: 'https://if.imjoy.io',
name: (ctx && ctx.data && ctx.data.name) ||'OpenFlexure Script Editor',
config,
data: {code: ctx && ctx.data && ctx.data.code}
})
}
}
api.export(new ImJoyPlugin())
</script>