-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Onpaste event to clear formatting #303
Comments
@JackPoint I added |
Thanks! |
so any example/documentation on how to implement the onpaste method? |
@chongwang87 No sorry, I tried but no luck. Perhaps @hackerwins can help us out? |
I tried to implement real-time cleaning (without extra popup windows like in #446 'Paste from Word'), but no luck too. My problem is that I cannot restore cursor position after deleting/replacing tags. Does anybody know tricks to deal with cursor positioning? I was thinking about manipulating with real DOM on cleaning, but it seems to me that it will work very slow. |
I found this great answer on stackoverflow from CKEditor core developer: http://stackoverflow.com/a/11290082/1178806 |
Here's a function I wrote to parse pastes from Word into Summernote. My case is quite specific as I use Summernote as a WYSIWYG editor for Markdown, so some of the parsing isn't helpful for you, e.g. titles -> ## Text, but it could prove helpful as inspiration for some and the list parsing should work. function parseWordPaste(desc) {
var that = this;
if (/<!--StartFragment-->([^]*)<!--EndFragment-->/.test(desc)) {
// Pasted from word
// Strip all normal <p> tags, replace with divs
var result = desc.replace(/<p class="MsoNormal">([\s\S]*?)<\/p>/g, "<div>$1</div>\n");
// Fix titles
result = result.replace(/<p class="MsoTitle">([\s\S]*?)<\/p>/g, "## $1");
var $desc = $('<div>' + result + '</div>');
$desc.contents().each(function() {
if (this.nodeType == 8) { // nodeType 8 = comments
$(this).remove();
}
});
var firstItems = $desc.find('p').filter(function() {
return /MsoList.*?First/g.test(this.className);
});
var lists = [];
firstItems.each(function() {
lists.push($(this).nextUntil('.MsoListParagraphCxSpLast').addBack().next().addBack());
});
// Add lists with one item
lists.push($desc.find(".MsoListParagraph"));
// Change between ordered and un-ordered lists
if (lists.length != 0) {
lists.forEach(function(list) {
if (list.length > 0) {
if (/[\s\S]*?(Symbol|Wingdings)[\s\S]*?/.test(list.html()))
var unordered = true;
else if (/[^0-9]/.test(list.text()[0]))
var unordered = true;
list.each(function() {
if (/[\s\S]*?level[2-9][\s\S]*/.test(this.outerHTML))
var nested = true;
var $this = $(this);
if (unordered)
var newText = $this.text().replace(/[^0-9]([\s\S]*)/, "$1");
else {
var newText = $this.text().replace(/[0-9](\.|\))([\s\S]*)/, "$2");
}
$this.html(newText);
if (nested) {
if (unordered)
$this.wrapInner('<ul><li>');
else
$this.wrapInner('<ol><li>');
}
});
list.wrapInner('<li>');
if (unordered)
list.wrapAll('<ul>');
else
list.wrapAll('<ol>');
// Filter to make sure that we don't unwrap nested lists
list.find('li').filter(function() {
return this.parentNode.tagName == 'P'
}).unwrap();
}
});
}
return $desc.html();
} else return desc;
} |
@anton164 I'm trying to implement the code you provided in my summernote instance but I don't know how to do it. Can you show me where should I put this method to make it work? |
I managed to auto clean the text on paste now. The biggest pain point was figuring out how to use the This onpaste function will allow you to call your cleaning code after the text is pasted.
As for the function to clean it, I used the function here: http://patisserie.keensoftware.com/en/pages/remove-word-formatting-from-rich-text-editor-with-javascript
|
@cjdreiss solution works really good 👍. I modified it to remove |
This is what I was looking for! Many thanks. But I have a question. How do I do to remove every formatting except bold, italic and underline when I paste something? |
This is basically what we can do binding |
For clear all html tags i use this code: var editor=$(".summernote");
editor.summernote({
onpaste: function(content) {
setTimeout(function () {
editor.code(content.target.textContent);
}, 10);
}
}); |
My working solution
|
@septembermd The issue with your solution is that, at least on Firefox, it basically removes all images and text formats. :( The getData('Text') method seems to return plaintext alone. |
@davidmartingonzalez in my case this is what I expected |
@cjdreiss solution also worked well for me, thank you! |
+1 |
Summernote 0.61 Paste Text Plugin
|
@septembermd 's solution works gloriously in Chrome, but doenst in FF. Just try to paste in 1 word. Nothing'll show up. EDIT onpaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
setTimeout( function(){
document.execCommand( 'insertText', false, bufferText );
}, 10 );
} The setTimeout should do the trick. |
Is there any working example for IE8? |
omg Many thanks. |
At new version the plugin, consider to use onPaste instead of onpaste. |
why not add a feature |
Working version for me:
Combo of @AliPnhyn and @septembermd - many thanks. Works in ff and chrome EDIT: When pasting images, the ALT tag comes through as text, but the image doesn't load - works well in my use case. |
Seems to be able to still paste images in chrome using @stepembermd answer. Any help? i tried if (!bufferText) return true; PS: think e.preventDefault doesn't work for chrome on paste event |
I found this to be very helpful: https://github.com/StudioJunkyard/summernote-cleaner |
another solution that worked for me.
|
Please use external plugins for now. We have to discuss whether we have to implement own and independent document model. But we don't have a plan right now. So I'm closing this. Thanks. |
In the new version this code worked for me perfectly
|
Check here for a working plugin. |
These plugins and scripts are either stripping out all styling - which we want to keep - along with the microsoft styling - or they are simply leaving it in. The style tags are required to maintain HTML - so stripping them out is not what I'm after. Basically on paste I just look for any instance of "mso-" the common microsoft tag - then pass a confirmation to the tagStripper functions / plugins to strip styling since it is a microsoft style - and then alert the users. |
It is also stripping the normal break line from Ms-word and pasting all in one line. Any solution for that? |
I just used an onPaste even as part of the init - then called it after a slight delay. Formatting is not working on Github - sorry.
` function CleanPastedHTML(input,is_ms) {
` |
|
Your solution is working perfectly, <3 Worked by changing |
This is a very clever solution. Watch out with callbacks method and the event name, is onPaste (with camel case).
Reference: |
Watch out and be careful with the event name, is onPaste with camel case. And all the method must inside a callbacks method. |
By using
|
Is it possible to add an on paste event? I wanna remove all styles when someone copy pastes a text to the editor. If there is an event like this, I can try to build that.
Or is there an other (easyer) way to clear formatting when someone paste text from an other editor?
The text was updated successfully, but these errors were encountered: