Implemented basic Plugin handling

This commit is contained in:
Pax1601
2023-09-15 17:05:26 +02:00
parent ad06117b78
commit 588228c050
75 changed files with 1920 additions and 1657 deletions

View File

@@ -1,36 +0,0 @@
import { OlympusApp } from "../olympusapp";
const templateParser = require( "ejs" );
export abstract class Plugin {
#olympusApp!:OlympusApp;
protected name = "";
#templateParser:any;
constructor( olympusApp:OlympusApp, pluginName:string ) {
const regex = "^[a-zA-Z][a-zA-Z\d]{4,}"
if ( new RegExp( regex ).test( pluginName ) === false ) {
throw new Error( `Plugin names must match regex: ${regex}` );
}
this.name = pluginName;
this.#olympusApp = olympusApp;
this.#templateParser = templateParser;
}
getName() {
return this.name;
}
getOlympusApp() {
return this.#olympusApp;
}
getTemplateParser() {
return this.#templateParser;
}
}

View File

@@ -1,13 +1,55 @@
import { OlympusApp } from "../olympusapp";
import path from "path";
import { Manager } from "../other/manager";
import { getApp } from "..";
export class PluginsManager extends Manager {
constructor() {
super();
export class PluginManager extends Manager {
constructor( olympusApp:OlympusApp ) {
super( olympusApp );
var xhr = new XMLHttpRequest();
xhr.open('GET', "/plugins/list", true);
xhr.responseType = 'json';
xhr.onload = () => {
var status = xhr.status;
if (status === 200) {
this.#loadPlugins(xhr.response);
} else {
console.error(`Error retrieving plugins`)
}
};
xhr.send();
}
#loadPlugins(pluginsFolders: string[]) {
pluginsFolders.forEach((pluginName: string) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', path.join("/plugins", pluginName, "index.js"), true);
xhr.responseType = 'text';
xhr.onload = () => {
var status = xhr.status;
if (status === 200) {
/* Inject the plugin style */
var link = document.createElement("link");
link.href = path.join("/plugins", pluginName, "style.css");
link.type = "text/css";
link.rel = "stylesheet";
document.getElementsByTagName("head")[0].appendChild(link);
/* Evaluate the plugin javascript */
eval(xhr.response);
const plugin = globalThis.getOlympusPlugin() as OlympusPlugin;
console.log(plugin.getName() + " loaded correctly");
if (plugin.initialize(getApp())) {
console.log(plugin.getName() + " initialized correctly");
this.add(pluginName, plugin);
}
} else {
console.error(`Error retrieving plugin from ${pluginName}`)
}
};
xhr.send();
})
}
}