3
votes

I am working on Episerver 9. I have a requirement where user can copy content (which includes HTML tags) into the TinyMCE editor.

I want only the text content to be pasted. HTML tags should be filtered out automatically by default.

Is there any way to achieve this using TinyMCE?

2

2 Answers

2
votes

You can register a custom TinyMCE plugin in Episerver using the TinyMCEPluginNonVisual attribute. By setting AlwaysEnabled to false, you can use property settings to determine whether the plugin should be enabled or not for a specific editor/XHTML property.

[TinyMCEPluginNonVisual(AlwaysEnabled = false, PlugInName = "customplugin")]
public class MyCustomPlugin
{

}

Your actual TinyMCE plugin (i.e. JavaScript code) could be something like the following:

(function (tinymce, $) {

    tinymce.create('tinymce.plugins.customplugin', {

        init: function (editor, url) {

            editor.onPaste.add(function (editor, event) {

                if (!event.clipboardData || !event.clipboardData.items) {
                    return;
                }

                // TODO Modify event.clipboardData, for example to strip out HTML tags

            });
        }
    });

    // Register plugin
    tinymce.PluginManager.add('customplugin', tinymce.plugins.customplugin);

}(tinymce, epiJQuery));

While this isn't a complete example, it should get you started in the right direction.

You should also have a look at the official documentation.

Edit: If you just want to alter the paste_as_text setting, you could register a plugin and set the configuration through the TinyMCEPluginNonVisual attribute:

[TinyMCEPluginNonVisual(EditorInitConfigurationOptions = "{ paste_as_text: true }")]
public class PasteAsTextPlugin
{

}
1
votes

Assuming that you are loading the paste plugin you can force TinyMCE to always paste as plain text with the following:

tinymce.init({
    ...
    plugins: "paste",
    paste_as_text: true
    ...
});

https://www.tinymce.com/docs/plugins/paste/#paste_as_text

I would assume that Episerver provides you some way to manipulate the configuration of TinyMCE. Adding the paste_as_text option to that configuration should do what you need.