mirror of https://github.com/artf/grapesjs.git
3 changed files with 56 additions and 60 deletions
@ -1,3 +0,0 @@ |
|||||
export default { |
|
||||
plugins: [], |
|
||||
}; |
|
||||
@ -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; |
|
||||
}, |
|
||||
}; |
|
||||
}; |
|
||||
@ -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…
Reference in new issue