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)
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().
@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
isComponentmethod 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)
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.
Issue #460
When the component's view modifies the HTML, text components are no longer editable
I'm trying to build a custom component that contains some lorem ipsum text in the content, for editing once it's on the canvas. But I also...
Issue #604
trigger after removing component
Guys I need to trigger some action after removing editor's components. Is there any way to do so? Or have I to write my own trigger?
Issue #1792
[Question] Exclude some components from final html (Multi-level)
Hi, I have a question about how to exclude HTML from editor->getHtml(). My structure is something like that. ParentChild -- Child --- Child...
Issue #1535
[Bug]: After custom component is deleted the Trait's view does not clear out
Hi, I'm using the grapesjs library version 0.14.33. I've created some custom components and I've noticed that on deleting the components th...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.