From f2ee2776e74123ee491b9be70327afc3a6cbd42c Mon Sep 17 00:00:00 2001 From: talves Date: Fri, 16 Mar 2018 16:37:09 -0700 Subject: [PATCH] allow for single bundle init --- src/bootstrap.js | 19 +++++++------------ src/index.js | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index 01b22ff0d33a..1163c16f0361 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -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; /** @@ -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. diff --git a/src/index.js b/src/index.js index 2647cbfa5514..0931ab6fbcc6 100644 --- a/src/index.js +++ b/src/index.js @@ -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
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; @@ -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 };