Skip to content

Commit

Permalink
dynamically load all scripts
Browse files Browse the repository at this point in the history
Only the sceditor.min.js needs to included in the html page format, plugins and icon js files are loaded dynamically.
  • Loading branch information
w8tcha committed Apr 1, 2024
1 parent 6b154af commit 8594cb9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
50 changes: 47 additions & 3 deletions src/lib/SCEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export default function SCEditor(original, userOptions) {
handleComposition,
handleEvent,
handleDocumentClick,
loadScripts,
updateToolBar,
updateActiveButtons,
sourceEditorSelectedText,
Expand Down Expand Up @@ -434,6 +435,48 @@ 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
(options.plugins || '').split(',').forEach(function (plugin) {
promises.push(
loadScript(`../${options.basePath}/plugins/${plugin.trim()}.js`));
});

// 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
Expand Down Expand Up @@ -721,7 +764,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',
Expand All @@ -739,10 +782,11 @@ 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) {
console.log(command);
return;
}

Expand Down Expand Up @@ -3513,7 +3557,7 @@ export default function SCEditor(original, userOptions) {
};

// run the initializer
init();
loadScripts();
};


Expand Down
9 changes: 8 additions & 1 deletion src/lib/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,12 @@ export default {
*
* @type {Array}
*/
allowedAttributes: []
allowedAttributes: [],

/**
* Default path for the script files
*
* @type {string}
*/
basePath: 'minified'
};

0 comments on commit 8594cb9

Please sign in to comment.