Search code examples
tinymceeditorwysiwygwysihtml5

Tinymce 4: how to allow converting pasted content to plain text AND to normal mode


The new TINYMCE (version 4) allows the use of:

paste_as_text: true

The problem is that this enabled all pastes to be converted to plain text. I would to be able to do this: when the user pastes content directly to the tinymce it will always clean the content and convert it to plain text BUT if the user cliks a button a dialog appears so user can paste content in this dialog and tinymce wouldnt "touch" it, it would paste it directly to the editor.

ACTUALLY this was the default behaviour on 3.X versions, you could have a specific PASTE FROM WORD button (which actually worked for anything you wanted to paste) so you cold paste content from word and it and it would not get converted to plain text.

I see there is an external "powerful paste plugin" from tinymce but it's not free, so how can I solve this problem without having to purchase a plugin?

EDIT:

Even attaching event to the paste process I cannot change the paste_as_text dynamically like this, for example:

 tinymce.settings.paste_as_text = false;

I can execute that line in my code with no errors but it produces no effect, I continue not being able to paste content without being simplified as plain text.


Solution

  • When you load the paste plugin by default it adds two items to the Edit menu:

    • Paste
    • Paste as Text

    ...so what you want is a standard part of the paste plugin. In TinyMCE 4 its exposed via the Edit Menu and not the Toolbar.

    EDIT #1: If you add the pastetext button to the toolbar that allows you to toggle the paste function between Paste as Text and regular Paste. The button toggles the behavior that happens on a paste event. It does not behave exactly as things did in TinyMCE 3 (the behavior you describe in your question).

    EDIT #2 - Programmatically changing this behavior

    You can use code to toggle this setting but you need a couple of different calls to get what you need.

    First, you need to determine what the current "mode" is for the paste plugin. For this you can use:

    tinymce.activeEditor.plugins.paste.clipboard.pasteFormat
    

    This will (based on the current TinyMCE release of 4.6.5) return one of three values: "html", "text", or undefined. I believe it always starts as undefined until you initiate the toggle once.

    If you want to switch the behavior to the other mode you can do this:

    tinymce.activeEditor.execCommand('mceTogglePlainTextPaste');
    

    Note that is is a toggle - so whatever it was set to it will switch to the opposite mode. There is no exposed method call that you can use to force it to one mode or the other.