Skip to content

Commit

Permalink
allow for single bundle init
Browse files Browse the repository at this point in the history
  • Loading branch information
talves authored and erquhart committed Mar 19, 2018
1 parent 9bb1a81 commit f2ee277
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
19 changes: 7 additions & 12 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ import 'EditorWidgets';
import 'MarkdownPlugins';
import './index.css';

const ROOT_ID = 'nc-root';
export const ROOT_ID = 'nc-root';

function bootstrap(opts = {}) {
/**
* Error and return if this function was already called.
*/
if (document.getElementById(ROOT_ID)) {
console.error('Bootstrap attempted, but Netlify CMS is already initialized!');
return;
}

const { config } = opts;

/**
Expand All @@ -34,9 +26,12 @@ function bootstrap(opts = {}) {
/**
* Create mount element dynamically.
*/
const el = document.createElement('div');
el.id = 'nc-root';
document.body.appendChild(el);
let el = document.getElementById(ROOT_ID);
if (!el) {
el = document.createElement('div');
el.id = ROOT_ID;
document.body.appendChild(el);
}

/**
* Configure Redux store.
Expand Down
41 changes: 35 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,47 @@
* the `window` object.
*/
import React from 'react';
import bootstrap from './bootstrap';
import bootstrap, { ROOT_ID } from './bootstrap';
import registry from 'Lib/registry';
import createReactClass from 'create-react-class';

let initialized = false;

/**
* Allow init of the CMS.
*/
function init(opts = {}) {
if (initialized) {
console.error('Bootstrap attempted, but Netlify CMS is already initialized!');
return;
}
initialized = bootstrap(opts);
return initialized;
}

/**
* Allow reset of the CMS to allow render after unmount
*/
function reset() {
initialized = false;
}

const CMS = (function startApp() {
/**
* Load the app, bail if bootstrapping fails. This will most likely occur
* if both automatic and manual initialization are attempted.
* Load the app, bail if root element exists or no-auto element.
* Use <div id="no-auto" /> in your app if you are not persisting the root element.
* (in example: as a React Route)
*/
if (!bootstrap()) {
return;
if (document.getElementById('no-auto') || document.getElementById(ROOT_ID)) {
console.log('CMS is running in manual mode. [using init() to initialize]');
} else {
init();
}

/**
* Add extension hooks to global scope.
*/
const api = { ...registry };
const api = { init, reset, ...registry };
if (typeof window !== 'undefined') {
window.CMS = api;
window.createClass = window.createClass || createReactClass;
Expand All @@ -32,3 +56,8 @@ const CMS = (function startApp() {
* Export the registry for projects that import the CMS.
*/
export default CMS;

/**
* Export the init, reset and registry standalone. (optional)
*/
export { init, reset, registry };

0 comments on commit f2ee277

Please sign in to comment.