Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamically load all scripts #961

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Include the SCEditor JavaScript:
```html
<link rel="stylesheet" href="minified/themes/default.min.css" />
<script src="minified/sceditor.min.js"></script>
<script src="minified/formats/bbcode.js"></script>
<script src="minified/formats/xhtml.js"></script>
```

Then to convert a textarea into SCEditor, simply do:
Expand Down
52 changes: 49 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,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
Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand Down Expand Up @@ -3513,7 +3559,7 @@ export default function SCEditor(original, userOptions) {
};

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


Expand Down
15 changes: 14 additions & 1 deletion src/lib/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
Loading