Issue #628Opened December 11, 2017by AH1N13 reactions

exclude components from final html

Question

Is there any ready solution to exclude some components from final html? Or any other mechanism that makes component exist only in view?

Answers (3)

ryandebaDecember 12, 20171 reactions

Hi @AH1N1,

By "final html", do you mean the value returned from editor.getHtml()? And are you referring to a custom component? If the answer to both of those questions is yes, then you should just be able to override the toHTML method of your custom component to return nothing. The code to do that would be something like this:

var defaultType = editor.DomComponents.getType("default");
editor.DomComponents.addType("someCustomComponent", {
	model: defaultType.model.extend({
		toHTML: function() {
			return ""; // return an empty string instead of the default toHTML behaviour
		}
	}, {
		isComponent: function(el){
			// add custom isComponent logic here
		}
	}),
	view: defaultType.view
});

If you had something else in mind, let me know some additional details and I can try to assist further.

BTW - @artf what do you think about making the HTML/Html capitalization consistent between editor.getHtml() and component.toHTML()? I don't have a preference on which style to go with, but I think it would make more sense to change them to either editor.getHtml() / component.toHtml() or editor.getHTML() / component.toHTML().

ryandebaDecember 12, 20171 reactions

@AH1N1 Here's a working code example: https://jsfiddle.net/tt32z2uz/2/. Open up you browser console and you should see this logged out: <div>here's some normal content</div><div>...more normal content</div>.

Does that give you what you need to debug your code? If not, can you provide a live code example of your implementation?

Some possible reasons I can think of why this might not be working for you:

  • The isComponent method is not correctly identifying your custom component
  • The editor instance has already parsed out the components before your custom component type is registered (like from using fromElement: true)
artfDecember 21, 20171 reactions

Hi @afotey

What about if I wanted to return just the ID of a div as entered using the Component Settings panel?

Id assigned from component settings goes directly inside attributes therefore you can use this

const comp = editor.getSelected();
const id = comp.getAttributes()['id'];

Is there a way to have the export be ONLY those IDs and NOT any of the accompanying HTML/CSS?

You get other ids when your component has some style. If you use classes you should not see any of those

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.