Browse Source

Update root index

up-ts-2
Artur Arseniev 4 years ago
parent
commit
ec738b7936
  1. 44
      src/index.ts

44
src/index.js → src/index.ts

@ -4,30 +4,25 @@ import Editor from './editor';
import polyfills from './utils/polyfills'; import polyfills from './utils/polyfills';
import { getGlobal } from './utils/mixins'; import { getGlobal } from './utils/mixins';
import PluginManager from './plugin_manager'; import PluginManager from './plugin_manager';
import { EditorConfig } from './editor/config/config';
interface InitEditorConfig extends EditorConfig {
grapesjs?: typeof GrapesJS;
}
polyfills(); polyfills();
const plugins = new PluginManager(); const plugins = new PluginManager();
const editors = []; const editors: Editor[] = [];
const defaultConfig = {
// If true renders editor on init
autorender: 1,
// Array of plugins to init
plugins: [],
// Custom options for plugins const GrapesJS = {
pluginsOpts: {},
};
export default {
$, $,
editors, editors,
plugins, plugins,
// Will be replaced on build // @ts-ignore Will be replaced on build
version: __GJS_VERSION__, version: __GJS_VERSION__,
/** /**
@ -46,22 +41,29 @@ export default {
* style: '.hello{color: red}', * style: '.hello{color: red}',
* }) * })
*/ */
init(config = {}) { init(config: EditorConfig = {}) {
const { headless } = config; const { headless } = config;
const els = config.container; const els = config.container;
if (!els && !headless) throw new Error("'container' is required"); if (!els && !headless) throw new Error("'container' is required");
config = { ...defaultConfig, ...config, grapesjs: this }; const initConfig: InitEditorConfig = {
config.el = !headless && (isElement(els) ? els : document.querySelector(els)); autorender: true,
const editor = new Editor(config, { $ }); plugins: [],
pluginsOpts: {},
...config,
grapesjs: this,
el: headless ? undefined : isElement(els) ? els : (document.querySelector(els!) as HTMLElement),
};
const editor = new Editor(initConfig, { $ });
const em = editor.getModel(); const em = editor.getModel();
// Load plugins // Load plugins
config.plugins.forEach(pluginId => { initConfig.plugins!.forEach(pluginId => {
let plugin = isFunction(pluginId) ? pluginId : plugins.get(pluginId); let plugin = isFunction(pluginId) ? pluginId : plugins.get(pluginId);
const plgOptions = config.pluginsOpts[pluginId] || {}; const plgOptions = initConfig.pluginsOpts![pluginId as string] || {};
// Try to search in global context // Try to search in global context
if (!plugin) { if (!plugin) {
// @ts-ignore
const wplg = getGlobal()[pluginId]; const wplg = getGlobal()[pluginId];
plugin = wplg?.default || wplg; plugin = wplg?.default || wplg;
} }
@ -82,9 +84,11 @@ export default {
// A plugin might have extended/added some custom type so this // A plugin might have extended/added some custom type so this
// is a good point to load stuff like components, css rules, etc. // is a good point to load stuff like components, css rules, etc.
em.loadOnStart(); em.loadOnStart();
config.autorender && !headless && editor.render(); initConfig.autorender && !headless && editor.render();
editors.push(editor); editors.push(editor);
return editor; return editor;
}, },
}; };
export default GrapesJS;
Loading…
Cancel
Save