BUG: TextNodes interfere when changing content on element
Question
Version: 0.16.44
Hi @artf
I have a custom block with a custom button component, which its content is similar to this one.
<a data-gjs-type="custom_button" class="btn btn-1" href="">Click here</a>
Additionally, I've added a text trait to change the button content, in this particular case I do not want to use the editor RTE. <img width="286" alt="image" src="https://user-images.githubusercontent.com/1976679/109966677-d3a91180-7cf0-11eb-8463-e28b8de806c2.png">
When i change the value, the first time it gets reflected correctly <img width="975" alt="image" src="https://user-images.githubusercontent.com/1976679/109967201-6c3f9180-7cf1-11eb-943e-0c96e8693845.png">
But when i save and reload the page again, it gets back to its original value <img width="967" alt="image" src="https://user-images.githubusercontent.com/1976679/109967439-b6287780-7cf1-11eb-8c81-053458c1dc38.png">
This is happening in the updateContent function of the component view, the component is checking if it has children, in that case, the component does not render the content, the element has children, but they are text nodes.
Also if you set the content beforehand as a component configuration attribute, it does this.
<img width="972" alt="image" src="https://user-images.githubusercontent.com/1976679/109968113-9180cf80-7cf2-11eb-9d5a-961d0fe78237.png">The toHtml function of the Component model first prints the content and then its children (the text nodes).
In my case i have solved this by overriding the renderChildren and toHtml functions in view and model of my custom component, also removing the default text of the tag directly and adding it to the component configuration only. I'm writing this because maybe the case goes beyond my particular situation and can cause other problems.
For what I've seen the text nodes are a little tricky to handle. When i have some time i can go back to this again and provide a
Answers (3)
Hi Juan, thanks for the report, are you able to show me how your text trait works?
I'd expect it to work correctly if you update your component in this way component.components(e.target.value)
This was my trait
tm.addType('content', {
events: {
'change': 'onValueChange'
},
getInputEl: function() {
this.inputEl = document.createElement('input');
this.inputEl.value = this.getModelValue();
return this.inputEl;
},
getModelValue: function() {
const { model, target } = this;
var value = target.get('content') || target.getEl().textContent;
return value;
},
onValueChange: function() {
const { model, target } = this;
var value = this.inputEl.value;
target.set('content', value);
target.getEl().textContent = value;
},
onUpdate: function() {
var value = this.getModelValue();
this.target.getEl().textContent = value;
}
});
I have changed it to
tm.addType('content', {
events: {
'change': 'onValueChange'
},
getInputEl: function() {
this.inputEl = document.createElement('input');
this.inputEl.value = this.getModelValue();
return this.inputEl;
},
getModelValue: function() {
const { model, target } = this;
var value = target.get('content') || target.getEl().textContent;
return value;
},
onValueChange: function() {
const { model, target } = this;
var value = this.inputEl.value;
target.set('content', value);
target.components(value);
},
onUpdate: function() {
var value = this.getModelValue();
this.target.getEl().textContent = value;
}
});
And now it works without overriding update content or render children, thanks! I would have never arrived to the conclusion of using the components method. Now i don't like text nodes 😅
I think we can close this issue unless there's some other point you want to add to improve.
Thanks!
I don't think you need this one target.set('content', value);. For the rest all good 👍
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1121
[BUG] - <Tbody> inside <Table>
Hi @artf , I created a new component with tagName = 'table'. When selected and one option of its toolbar I also created is clicked, a new m...
Issue #1135
[Question] Two html tags as one (link/button)
Is it possible to have two elements (div and lnik) and consider as a single? For example: there is the my following link block: <div class=...
Issue #2193
[QUESTION] Reset trait on edit the content of a custom component
Hi, I have a custom component which content can be changed by switching trait select options, but also can be edited similar to the text co...
Issue #1767
Ensure that getHtml() / runCommand("gjs-get-inlined-html") returns the very latest contents of the canvas
Hi @artf , I ran into a similar, if not the same issue as this one: #1327 While editing a text block, I click on an external button to get...
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.