add import to api

This commit is contained in:
Jiiks 2018-02-13 18:18:01 +02:00
parent 1f1d748687
commit 9a652e3748
3 changed files with 36 additions and 10 deletions

View File

@ -99,4 +99,10 @@ export default class PluginApi {
return plugin.exports;
}
import(m) {
const module = PluginManager.findPlugin(m);
if (module && module.__require) return module.__require;
return null;
}
}

View File

@ -15,6 +15,32 @@ import Vendor from './vendor';
import { ClientLogger as Logger } from 'common';
import { Events } from 'modules';
class Module {
constructor(pluginInternals) {
this.__pluginInternals = pluginInternals;
this.__require = window.require(this.paths.mainPath);
this.hasSettings = false;
}
get type() { return 'module' }
get configs() { return this.__pluginInternals.configs }
get info() { return this.__pluginInternals.info }
get icon() { return this.info.icon }
get paths() { return this.__pluginInternals.paths }
get main() { return this.__pluginInternals.main }
get defaultConfig() { return this.configs.defaultConfig }
get userConfig() { return this.configs.userConfig }
get id() { return this.info.id || this.info.name.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-') }
get name() { return this.info.name }
get authors() { return this.info.authors }
get version() { return this.info.version }
get pluginPath() { return this.paths.contentPath }
get dirName() { return this.paths.dirName }
get enabled() { return true }
get pluginConfig() { return this.userConfig.config || [] }
}
export default class extends ContentManager {
static get localPlugins() {
@ -38,17 +64,11 @@ export default class extends ContentManager {
static get loadContent() { return this.loadPlugin }
static async loadPlugin(paths, configs, info, main, type) {
if (type === 'module') return {
paths,
configs,
info,
main,
type,
module: window.require(paths.mainPath)
}
if (type === 'module') return new Module({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName, mainPath: paths.mainPath } });
const plugin = window.require(paths.mainPath)(Plugin, new PluginApi(info), Vendor);
const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName } });
const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName, mainPath: paths.mainPath } });
if (instance.enabled) instance.start();
return instance;

View File

@ -10,7 +10,7 @@
<template>
<Card :item="plugin">
<SettingSwitch slot="toggle" :checked="plugin.enabled" :change="() => plugin.enabled ? plugin.stop() : plugin.start()" />
<SettingSwitch v-if="plugin.type === 'plugin'" slot="toggle" :checked="plugin.enabled" :change="() => plugin.enabled ? plugin.stop() : plugin.start()" />
<ButtonGroup slot="controls">
<Button v-tooltip="'Settings'" v-if="plugin.hasSettings" :onClick="() => showSettings(plugin)">
<MiSettings size="18" />