Re-render Component on Canvas when tagName has changed
See original GitHub issueI’m trying to build a basic Header component that lets you select H1 to H6 with a trait. But when an option is selected, the canvas doesn’t update. The change is visible in the code view, and if I move the element in the canvas with the drag tool, the tag then changes. I have been reading the API docs as well as the source, but I can’t make the component automatically re-render. I suspect that Grapes is not listening for a change of the component tag name. What is the appropriate way to force a re-render in this case?
var comps = editor.DomComponents;
var blocks = editor.BlockManager;
var textType = comps.getType('text');
var textModel = textType.model;
var textView = textType.view;
comps.addType('header', {
model: textModel.extend({
defaults: Object.assign({}, textModel.prototype.defaults, {
'custom-name': 'Header',
tagName: 'h1',
traits: [
{
type: 'select',
options: [
{value: 'h1', name: 'One (largest)'},
{value: 'h2', name: 'Two '},
{value: 'h3', name: 'Three '},
{value: 'h4', name: 'Four '},
{value: 'h5', name: 'Five '},
{value: 'h6', name: 'Six (smallest)'},
],
label: 'Size',
name: 'header-size',
changeProp: 1
}
]
}),
init() {
this.listenTo(this, 'change:header-size', this.changeTagName);
},
changeTagName() {
// view.tagName is a fn that returns model.tagName
this.set('tagName', this.get('header-size'));
}
}, {
isComponent: function(el) {
if(el && ['H1','H2','H3','H4','H5','H6'].includes(el.tagName)) {
return {type: 'header'};
}
}
}),
view: textView
});
blocks.add('header', {
label: 'Header',
category: 'Basic',
attributes: {class:'fa fa-header'},
content: {
type:'header',
content:'Insert your header text here',
activeOnRender: 1
}
});
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Rerender component as mobx store is updated - Stack Overflow
I am using chart.js to show price changes from the backend in real time. Backend sends a new price when it is changed...
Read more >HTML5 elements vanish when rerendering Visualforce
I have noticed that HTML 5 tags like <section> and <header> disappear from the page when I reRender the page. On initial load...
Read more >React: Creating an Interactive Canvas Component - Medium
In this guide, we will take a look at how to set up a basic React.js application that incorporates an interactive HTML5 canvas...
Read more >When does React re-render components? - Felix Gerschau
React re-render explained. React is known for providing a fast user experience by only updating the parts of the UI that have changed....
Read more >Reusable react component with Canvas doesn't render ...
[Solved]-Reusable react component with Canvas doesn't render canvas with its props-Chart.js ... The issue was here, I will share here in case someone...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@z1lk I already added such a thing for the next release
I’m not sure why
el
would be a string, but see the commit that closed the issue: https://github.com/artf/grapesjs/commit/e450cb98855d16ad819f1214350825a50e45e910If you’re using the latest Grapes version, the Component listens for a change of
tagName
and does the node replacement itself. So you should be able to remove yourinit
andtagUpdated
functions. The trait will updatetagName
and the Component will do the rest.Edit:
Another thing I thought of is: sometimes the object passed to
isComponent
doesn’t have the method that I call on it, and there will be an error thrown in that case. You could do a safety check first: