suneditor icon indicating copy to clipboard operation
suneditor copied to clipboard

Appending HTML content using custom plugin

Open oneadera opened this issue 6 years ago • 5 comments

Hello. I have successfully created a custom plugin, everything works fine if I'm only typing text. I however have a scenario where I declare a title variable, then concatenate the contents of the input field in the custom editor.

This appends even HTML tags into the editor, not the clean, formatted text. Here is my onClick property of the plugin:

onClick: function () {
        this.history.push(true);

        const initialEditorContent = this.getContents();
    
        const title = initialEditorContent.search('Resolutions') === -1 ? '<h4><u>Resolutions</u></h4>' : '';

        if(this.context.custom.textElement.value.length > 0) {
            // Get Input value
            const value = this.util.createTextNode(title.concat('<br />', this.context.custom.textElement.value));
            
            // insert
            this.insertNode(value);

            // set range
            this.setRange(value, value.length, value, value.length);

           // clear content
            this.context.custom.textElement.value = null;
        }

        // submenu off
        this.submenuOff();

        // focus
        this.focus();
    } 

Here is the text I'm typing: Screenshot 2020-02-27 at 12 42 05

When I click Done, here is what is added to the editor: Screenshot 2020-02-27 at 12 43 36

@JiHong88 , how do I insert that text, well edited without the HTML tags?

oneadera avatar Feb 27 '20 09:02 oneadera

Hi, @aderaHenry Can you upload custom plugin source?

JiHong88 avatar Feb 27 '20 11:02 JiHong88

Hi, @aderaHenry Can you upload custom plugin source?

Sure @JiHong88 . Here it is:

// Plugin to append resolution contents to the WYSIWYG editor
export default {
    // plugin name
    name: 'Resolutions',

    // add function - It is called only once when the plugin is first run.
    // This function generates HTML to append and register the event.
    // arguments - (core : core object, targetElement : clicked button element)
    add: function (core, targetElement) {

        // Registering a namespace for caching as a plugin name in the context object
        const context = core.context;
        context.custom = {
            textElement: null
        };

        // Generate submenu HTML
        // Always bind "core" when calling a plugin function
        let listDiv = this.setSubmenu.call(core);

        // Input tag caching
        context.custom.textElement = listDiv.getElementsByTagName('TEXTAREA')[0];

        // You must bind "core" object when registering an event.
        /** add event listeners */
        listDiv.getElementsByTagName('BUTTON')[0].addEventListener('click', this.onClick.bind(core));

        /** append html */
        targetElement.parentNode.appendChild(listDiv);
    },

    setSubmenu: function () {
        const listDiv = this.util.createElement('DIV');

        listDiv.className = 'se-list-layer';
        listDiv.innerHTML = '' +
            '<div class="se-list-inner se-list-align" style="width: auto;">' + 
            '   <form class="se-submenu-form-group">' + 
            '   <ul class="se-list-basic">' +
            '       <li><div class="form-group form-material floating" data-plugin="formMaterial" style="margin: auto 2px;"><label class="form-control-label">Agenda item resolutions</label><textarea class="form-control" style="width: 500px; height: 90px;"></textarea></div></li>' +
            '       <li><button type="button" class="se-btn se-tooltip">' +
            '           <span>Done</span>' +
            '           <span class="se-tooltip-inner">' +
            '               <span class="se-tooltip-text">Append resolution(s)</span>' +
            '           </span>' +
            '       </button></li>' +
            '   </ul>' +
            '   </form>' + 
            '</div>';

        return listDiv;
    },

    // Called after the submenu has been rendered
    on: function () {
        this.context.custom.textElement.focus();
    },

    onClick: function () {
        this.history.push(true);

        const initialEditorContent = this.getContents();
    
        const title = initialEditorContent.search('Resolutions') === -1 ? '<h4><u>Resolutions</u></h4>' : '';


        if(this.context.custom.textElement.value.length > 0) {
            // Get Input value
            const value = this.util.createTextNode(title.concat('<br />', this.context.custom.textElement.value));
            
            // insert
            this.insertNode(value);

            // set range
            this.setRange(value, value.length, value, value.length);
            
            // clear content
            this.context.custom.textElement.value = null;
        }

        // submenu off
        this.submenuOff();

        // focus
        this.focus();
    }
};

Thank you in advance

oneadera avatar Feb 27 '20 11:02 oneadera

Hi sorry for the late reply. The util.createTextNode method only creates text nodes. You must create a tag with the util.createElement method.

const title = initialEditorContent.search('Resolutions') === -1 ? '<h4><u>Resolutions</u></h4>' : '';

if(this.context.custom.textElement.value.length > 0) {
	// Get Input value
	let value = title;
	const lines = this.context.custom.textElement.value.split('\n');
	for (let i = 0, len = lines.length; i < len; i++) {
		value += '<p>' + lines[i] + '</p>';
	}

	// rendering
	const template = this.util.createElement('DIV');
	template.innerHTML = value;
	
	// insert
	const children = template.children;
	let after, child;
	while (children[0]) {
		child = children[0];
		this.insertNode(child, after);
		after = child;
	}

	// set range **(It is not necessary this code in the next version)**
	this.setRange(after, 1, after, 1);
	
	// clear content
	this.context.custom.textElement.value = null;
}

In the current version, lines may overlap, but It will be fine in the next version.

JiHong88 avatar Mar 04 '20 09:03 JiHong88

The 2.26.0 version has been updated. If there is an issue, please register as a new issue. Thank you.

JiHong88 avatar Mar 11 '20 18:03 JiHong88

Hi @aderaHenry, Could you please share us your code about list hierarchy.

1.Where it does come
   1.1
        1.1.1
   1.2
2

Best regardss

ghilaslinz avatar Jun 11 '21 08:06 ghilaslinz