I've got django-tinymce installed and various models in my app with an HTMLField
.
I'm trying to produce some sort of 'paste as plain text' functionality in the django admin. I want formatting from MS Word, HTML tags, and pretty much anything else stripped out when users do ctrl + v / cmd + v / right click > paste etc.
I can see lots of people talking about it but I can't seem to get anything to work. Being django-tinymce, I'm doing all of this in TINYMCE_DEFAULT_CONFIG
in settings.py
.
I thought I had it working - I'd overridden the base admin template to include a js file with the following function (I can't remember who to credit for the function - probably someone on SO)-
function tinymcePastePlainText() {
var ed = tinyMCE.get(0);
ed.pasteAsPlainText = true;
//adding handlers crossbrowser
if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {
ed.onKeyDown.add(function (ed, e) {
if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))
ed.pasteAsPlainText = true;
});
} else {
ed.onPaste.addToTop(function (ed, e) {
ed.pasteAsPlainText = true;
});
}
}
Then set up my default config -
TINYMCE_DEFAULT_CONFIG = {
'plugins': "'paste'",
'paste_text_sticky': "true",
'paste_retain_style_properties': "",
'oninit': "tinymcePastePlainText",
#...Further config
}
This seemed to be doing what I wanted last time I looked, but it's stopped working now - debugging in the browser seems to show that the tinymcePastePlainText()
function is never run (though it is loaded).
I've tried various other methods such as simply setting my config to -
TINYMCE_DEFAULT_CONFIG = {
'plugins': "'paste'",
'paste_auto_cleanup_on_paste': 'true',
'paste_remove_styles': 'true',
'paste_remove_styles_if_webkit': 'true',
'paste_strip_class_attributes': 'true',
#....further config
}
(that didn't seem to change anything). Or
TINYMCE_DEFAULT_CONFIG = {
'plugins': "'paste'",
'setup': "function(ed) { ed.onInit.add(function(ed) {ed.pasteAsPlainText = true;});",
#....further config
}
I've tried loading the above function from a seperate js file then including the function name in my config - 'setup': 'pasteAsPlainTextFuncion()'
but that didn't work either.
I seem to have spent ages on this, and I'm not really making any progress - I've no idea why my function was being called, and now it's not - the browser's debugging tool isn't picking up any errors.
Infuriating! - For some reason I'd applied both single and double quotes on the the line - 'plugins': "'paste'",
. The paste plugin wasn't being loaded. I don't know how, why or when I did that.
For anyone else trying to do the same thing (I'm using django 1.4.3 and django-tinymce 1.5.1b2). I managed to get some basic 'paste as plain text' functionality (as default) without the use of a javascript callback anywhere. I simply used the following setup -
TINYMCE_DEFAULT_CONFIG = {
'plugins': "paste",
'paste_remove_styles': 'true',
'paste_remove_styles_if_webkit': 'true',
'paste_strip_class_attributes': 'all',
#... further config
}
I found the paste plugin documentation and Thariama's answer on SO both very informative.