[QUESTION] Custom component default children
Question
Hi,
I am trying to understand how I can achieve the following.
- Create a component with a default content as children.
- Create a block to add that component to the canvas.
- Edit the component children to anything else and save the HTML result.
- Reload the component in the editor later on and continue editing.
My issue is the following:
- I have tried using
componentsandcontentin defaults. - I have tried defining some custom logic in
initfunction of the view of my component, to only append the default value is no components exists.
Both these methods, do not work, they work at first, but if I edit the text, save the values, grab the HTML and re-import it or reload it in the canvas using the components option of the editor, the default value gets inserted with the edited content (as children).
I don't understand why. I also noticed that this logic, only works for actual components and not text nodes, as comps.length does not detect text nodes as children.
view: {
init() {
const comps = this.model.components();
if (!comps.length) {
comps.add('default value');
}
}
}
I was only able to fix my issue by doing the following:
view: {
onRender() {
if (!this.el.childNodes.length) this.el.innerHTML = 'Article Title';
}
}
The component looks like this:
<div my-attribute="" data-gjs-type="article-title">Article Title</div>
But I do not think it might be the best way to do this. I was wondering if anyone had suggestions or can let me know how they did this?
Thanks!
Answers (2)
@simplecommerce a working example of a custom component with default children (it uses local storage) https://jsfiddle.net/t5yw48cs/
Try to review the way you store/load templates, maybe the issue is there https://grapesjs.com/docs/modules/Storage.html#store-and-load-templates
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
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 #1574
Question, Block content from file
Hi, this is not really an issue but more a question. I'm currently using custom blocks in my GrapesJS editor. This is how I create them : I...
Issue #906
[Question]How to change custom component content (html) from ajax?
Hi @artf, First of all thanks for this awesome plugin. Can you please let me know how i can update/change block content? I created custom b...
Issue #1313
[Question] Set traits value from html on component load
I have custom component and want to set trait default values from html. below is html rendered by component. traits I am trying to set for...
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.