Skip to content

Instantly share code, notes, and snippets.

@dhaupin
Last active June 28, 2017 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaupin/86a5d27dea7f83c606036428a148b010 to your computer and use it in GitHub Desktop.
Save dhaupin/86a5d27dea7f83c606036428a148b010 to your computer and use it in GitHub Desktop.
CKEditor - Example Plugin Wrapper/Init for Config.js - Extending Autosave
// Make sure you include the plugin_wrapper as last item in config.extraPluins
CKEDITOR.editorConfig = function(config) {
config.extraPlugins = 'autosave,plugin_wrapper';
// Define empty autosave object
config.autosave = {};
}
// Extend instance plugins
CKEDITOR.plugins.add('plugin_wrapper', {
init: function (editor) { // makes editor instance available
var trimmed_url = window.location.href,
ignore_querystrings = [
'selected_section',
'switch_company_id'
]
$(ignore_querystrings).each(function() {
trimmed_url = fn_removeUrlParam(this, null, trimmed_url);
});
var autosaveConfig = {
saveDetectionSelectors: '.cm-submit',
SaveKey: 'autosave | ' + trimmed_url + ' | ' + $('#' + editor.name).attr('name')
}
CKEDITOR.tools.extend(editor.config.autosave || {}, autosaveConfig, true);
}
});
// Querystring mitigator - Quick and dirty paste
// From - https://stackoverflow.com/a/11654436/2418655
function fn_removeUrlParam(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
hash;
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null) {
return url.replace(re, '$1' + key + "=" + value + '$2$3');
} else {
hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
url += '#' + hash[1];
}
return url;
}
} else {
if (typeof value !== 'undefined' && value !== null) {
var separator = url.indexOf('?') !== -1 ? '&' : '?';
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
url += '#' + hash[1];
}
return url;
} else {
return url;
}
}
}
@dhaupin
Copy link
Author

dhaupin commented Jun 28, 2017

This example will extend CKEditor autosave plugin with features not available in uses a pseudo plugin_wrapper to init an editor object (so input field name can be extracted for SaveKey). It also scrubs off non crucial querystrings that should be ignored for a broader SaveKey. In this case, for CS-Cart store switch and tab history.

You can extend any plugin that listens for user config with this tactic, without creating an actual plugin folder or subsequent plugin.js. Just make sure you define an empty object in CKEDITOR.editorConfig so that CKEDITOR.tools.extend has something to push into the real plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment