Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste content as plain text in summernote editor

I need to copy paste some content in my summernote editor .But when I paste, it takes the style of the page from where I have copied it.I need to paste it as plain text. Is there any solution?

like image 698
Revathy kr Avatar asked Jun 23 '15 04:06

Revathy kr


People also ask

How do I get plain text from Summernote editor?

html(text). text(); . Because if you the content of summernote contain some plain text and you don't wrap the whole content inside an HTML tag(like div) then, jQuery will through an error saying it was not expected.

How do I customize my Summernote toolbar?

If you look on the main Summernote website as opposed to the API docs you will see a clear example, that explains how to customise the toolbar. It is on the Deep Dive page and scroll down to Custom Toolbar. It also lists the available toolbar buttons. In your toolbar you set ['style', ['bold', 'italic',...

Is Summernote editor free?

The Summernote Editor is a free & open-source super simple WYSIWYG editor based on Bootstrap & jQuery. It is a JavaScript library that can be useful to build the WYSIWYG editors online.


1 Answers

Use the onPaste Callback

Use the onPaste option to define a callback that will strip the tags and manually insert the text.

$editor.summernote({     onPaste: function (e) {         var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');         e.preventDefault();         document.execCommand('insertText', false, bufferText);     } }); 

Credit: https://github.com/summernote/summernote/issues/303

Solve Firefox Problems:

You may still have problems with Firefox. If so, wrap document.execCommand in a timer function to delay its execution a tiny bit:

setTimeout(function(){     document.execCommand( 'insertText', false, bufferText ); }, 10); 

Update for v0.7.0+

Position of callbacks in options is changed after v0.7.0
After v0.7.0, every callbacks should be wrapped by callbacks object. (source)

This means that the original code should be written as

$editor.summernote({     callbacks: {         onPaste: function (e) {             var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');             e.preventDefault();             document.execCommand('insertText', false, bufferText);         }     } }); 

Credit to Mathieu Castets for pointing this out (so if this bit helped, upvote his answer!)

TL;DR: Here's a functional demo

like image 104
jcuenod Avatar answered Sep 21 '22 16:09

jcuenod