Browse Source

Refactor PluginManager

pull/4830/head
Artur Arseniev 4 years ago
parent
commit
ea95e98c8f
  1. 3
      src/plugin_manager/config/config.js
  2. 57
      src/plugin_manager/index.js
  3. 56
      src/plugin_manager/index.ts

3
src/plugin_manager/config/config.js

@ -1,3 +0,0 @@
export default {
plugins: [],
};

57
src/plugin_manager/index.js

@ -1,57 +0,0 @@
import defaults from './config/config';
export default config => {
var c = config || {};
// Set default options
for (var name in defaults) {
if (!(name in c)) c[name] = defaults[name];
}
var plugins = {};
return {
/**
* Add new plugin. Plugins could not be overwritten
* @param {string} id Plugin ID
* @param {Function} plugin Function which contains all plugin logic
* @return {Function} The plugin function
* @example
* PluginManager.add('some-plugin', function(editor){
* editor.Commands.add('new-command', {
* run: function(editor, senderBtn){
* console.log('Executed new-command');
* }
* })
* });
*/
add(id, plugin) {
if (plugins[id]) {
return plugins[id];
}
plugins[id] = plugin;
return plugin;
},
/**
* Returns plugin by ID
* @param {string} id Plugin ID
* @return {Function|undefined} Plugin
* @example
* var plugin = PluginManager.get('some-plugin');
* plugin(editor);
*/
get(id) {
return plugins[id];
},
/**
* Returns object with all plugins
* @return {Object}
*/
getAll() {
return plugins;
},
};
};

56
src/plugin_manager/index.ts

@ -0,0 +1,56 @@
import Editor from '../editor';
type PluginOptions = Record<string, any>;
export type Plugin<T extends PluginOptions = {}> = (editor: Editor, config: T) => void;
export default class PluginManager {
plugins: Record<string, Plugin> = {};
/**
* Add new plugin. Plugins could not be overwritten
* @param {string} id Plugin ID
* @param {Function} plugin Function which contains all plugin logic
* @return {Function} The plugin function
* @deprecated Don't use named plugins, create plugins as simple functions. More about [Plugins](https://grapesjs.com/docs/modules/Plugins.html)
* @example
* PluginManager.add('some-plugin', function(editor) {
* editor.Commands.add('new-command', {
* run: function(editor, senderBtn){
* console.log('Executed new-command');
* }
* })
* });
*/
add<T extends PluginOptions>(id: string, plugin: Plugin<T>) {
const plg = this.get(id);
if (plg) {
return plg;
}
// @ts-ignore
this.plugins[id] = plugin;
return plugin;
}
/**
* Returns plugin by ID
* @param {string} id Plugin ID
* @return {Function|undefined} Plugin
* @example
* var plugin = PluginManager.get('some-plugin');
* plugin(editor);
*/
get<T extends PluginOptions>(id: string): Plugin<T> | undefined {
return this.plugins[id];
}
/**
* Returns object with all plugins
*/
getAll() {
return this.plugins;
}
}
Loading…
Cancel
Save