Skip to content

Commit

Permalink
enable manual initialization via global flag
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed Mar 19, 2018
1 parent f2ee277 commit 64c35a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 63 deletions.
30 changes: 18 additions & 12 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'EditorWidgets';
import 'MarkdownPlugins';
import './index.css';

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

function bootstrap(opts = {}) {
const { config } = opts;
Expand All @@ -24,13 +24,24 @@ function bootstrap(opts = {}) {
console.log(`Netlify CMS version ${NETLIFY_CMS_VERSION}`);

/**
* Create mount element dynamically.
* Get DOM element where app will mount.
*/
let el = document.getElementById(ROOT_ID);
if (!el) {
el = document.createElement('div');
el.id = ROOT_ID;
function getRoot() {
/**
* Return existing root if found.
*/
const existingRoot = document.getElementById(ROOT_ID);
if (existingRoot) {
return existingRoot;
}

/**
* If no existing root, create and return a new root.
*/
const newRoot = document.createElement('div');
newRoot.id = ROOT_ID;
document.body.appendChild(el);
return newRoot;
}

/**
Expand Down Expand Up @@ -68,12 +79,7 @@ function bootstrap(opts = {}) {
/**
* Render application root.
*/
render(<Root />, el);

/**
* Return true to indicate bootstrap success to caller.
*/
return true;
render(<Root />, getRoot());
}

export default bootstrap;
61 changes: 10 additions & 51 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,22 @@
/**
* This module provides a self-initializing CMS instance with API hooks added to
* the `window` object.
*/
import React from 'react';
import bootstrap, { ROOT_ID } from './bootstrap';
import bootstrap from './bootstrap';
import registry from 'Lib/registry';
import createReactClass from 'create-react-class';

let initialized = false;

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

/**
* Allow reset of the CMS to allow render after unmount
* Add extension hooks to global scope.
*/
function reset() {
initialized = false;
if (typeof window !== 'undefined') {
window.CMS = registry;
window.createClass = window.createClass || createReactClass;
window.h = window.h || React.createElement;
}

const CMS = (function startApp() {
/**
* 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 (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 = { init, reset, ...registry };
if (typeof window !== 'undefined') {
window.CMS = api;
window.createClass = window.createClass || createReactClass;
window.h = window.h || React.createElement;
}
return api;
})()

/**
* Export the registry for projects that import the CMS.
*/
export default CMS;

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

0 comments on commit 64c35a7

Please sign in to comment.