Last active
June 28, 2017 18:51
-
-
Save dhaupin/86a5d27dea7f83c606036428a148b010 to your computer and use it in GitHub Desktop.
CKEditor - Example Plugin Wrapper/Init for Config.js - Extending Autosave
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 forSaveKey
). It also scrubs off non crucial querystrings that should be ignored for a broaderSaveKey
. 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 thatCKEDITOR.tools.extend
has something to push into the real plugin.