diff --git a/README.md b/README.md index 7926a552..8756993a 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,6 @@ Include the SCEditor JavaScript: ```html - - ``` Then to convert a textarea into SCEditor, simply do: diff --git a/src/lib/SCEditor.js b/src/lib/SCEditor.js index 875ef185..206b6c66 100644 --- a/src/lib/SCEditor.js +++ b/src/lib/SCEditor.js @@ -334,6 +334,7 @@ export default function SCEditor(original, userOptions) { handleComposition, handleEvent, handleDocumentClick, + loadScripts, updateToolBar, updateActiveButtons, sourceEditorSelectedText, @@ -434,6 +435,51 @@ export default function SCEditor(original, userOptions) { }); }; + /** + * Loads a JavaScript file and returns a Promise for when it is loaded + */ + const loadScript = src => { + return new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.onload = resolve; + script.onerror = reject; + script.src = src; + document.head.append(script); + }); + }; + + /** + * Loads all scripts + * @private + */ + loadScripts = function () { + var promises = []; + + // Load format script + promises.push( + loadScript(`../${options.basePath}/formats/${options.format}.js`)); + + // load plugins scripts + if (options.plugins) { + (options.plugins || '').split(',').forEach(function (plugin) { + var src = `../${options.basePath}/plugins/${plugin.trim()}.js`; + promises.push( + loadScript(src)); + }); + } + + // load icons script + if (options.icons) { + promises.push( + loadScript(`../${options.basePath}/icons/${options.icons}.js`)); + } + + Promise.all(promises).then(() => { + init(); + }).catch(() => console.error('Something went wrong.')); + }; + /** * Creates the editor iframe and textarea * @private @@ -721,7 +767,7 @@ export default function SCEditor(original, userOptions) { var group, commands = base.commands, exclude = (options.toolbarExclude || '').split(','), - groups = options.toolbar.split('|'); + groups = options.toolbar.split('|'); toolbar = dom.createElement('div', { className: 'sceditor-toolbar', @@ -739,7 +785,7 @@ export default function SCEditor(original, userOptions) { utils.each(menuItems.split(','), function (_, commandName) { var button, shortcut, - command = commands[commandName]; + command = commands[commandName]; // The commandName must be a valid command and not excluded if (!command || exclude.indexOf(commandName) > -1) { @@ -3513,7 +3559,7 @@ export default function SCEditor(original, userOptions) { }; // run the initializer - init(); + loadScripts(); }; diff --git a/src/lib/defaultOptions.js b/src/lib/defaultOptions.js index 2e976f82..587cd71b 100644 --- a/src/lib/defaultOptions.js +++ b/src/lib/defaultOptions.js @@ -395,5 +395,18 @@ export default { * * @type {Array} */ - allowedAttributes: [] + allowedAttributes: [], + + /** + * Default path for the script files + * + * @type {string} + */ + basePath: 'minified', + /** + * The input format + * + * @type {string} + */ + format: 'bbcode' };