question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Appending HTML content using custom plugin

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JiHong88commented, Mar 4, 2020

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.

0reactions
ghilaslinzcommented, Jun 11, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

WP Coder – add custom html, css and js code
WP Coder – plugin for adding custom code to the site. You can easily add HTML CSS JS code to the page of...
Read more >
Working with custom HTML - BEE Plugin
The Custom HTML content block allows you to easily add your own HTML code to an email message that you are designing with...
Read more >
how to make the content script append html to the original ...
I am using google chrome extension v3 to write a plugin. Now I want to append the html content that contains a popup...
Read more >
How to insert formatted HTML into editor from custom plugin?
I have created a custom plugin which with the onOk function includes the following: var dialog = this; var srcURL = dialog.
Read more >
Use action, filter, or hook to append HTML to WordPress ...
The paragraph with the class of "appended-text" is what should be appended to the HTML using an action, filter, or hook. Plugin's function:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found