kevinchappell / formBuilder

A jQuery plugin for drag and drop form creation
https://formbuilder.online
MIT License
2.63k stars 1.4k forks source link

Rich text pasted into fields - workaround / solution attached #1207

Open richxrich opened 3 years ago

richxrich commented 3 years ago

Description:

Input boxes on config page are div's with contenteditable set, but this allows for rich text content to be pasted in and saved in the JSON config file incorrectly.

Environment Details:

Expected Behavior

Only a text version of the text to be paste not the rich text version.

Actual Behavior

Extra formating such as HTML etc will be copied into the field.

Steps to Reproduce

An example, copy an excel cell into the name of a new section. If you inspect the field it has inserted an HTML table and other formatting - This will also save into the JSON.

WORKAROUND

Using a promise I worked around it, but ultimately it would be better perhaps tightly integrated or at least label the fields that need this functionality so a better binding could be used (in case contenteditable is used elsewhere on the page for other things).

    // init
    var formBuilder = $('#fb-editor').formBuilder(options);

    formBuilder.promise.then(function(fb) {
        $("[contenteditable]").bind("paste", function(e){
            e.preventDefault()
            var text = e.originalEvent.clipboardData.getData('text').replace(/(\r\n|\n|\r)/gm, "");
            $(this).text(text);
        } );
    });

References:

Code was inspired by:

https://stackoverflow.com/questions/58980235/stop-pasting-html-style-in-a-contenteditable-div-only-paste-the-plain-text

akshay-bhanderi commented 3 years ago

Had the same issue with rich text. ended up writing code for similar work around.

Thank you for sharing. @richxrich

Jojoshua commented 2 years ago

@richxrich wouldn't you expect html to be allowed to be pasted into the paragraph?

richxrich commented 2 years ago

@richxrich wouldn't you expect html to be allowed to be pasted into the paragraph?

This is the config, the variable fields such as label, type, class etc.... this data needs to be plain text and not HTML... it saves the config files with all the HTML in it... a label turns into <p class="something">Name</p> or something like that rather than just "Name"