[QUESTION]: New toolbar button works only for new added components and not for existing ones
Question
Hi everybody, can somebody explain me please why when I create a new custom toolbar button this button shows only when I add a new component and not an existing one even this components are the same. I was thinking that might be something about the editor initiation that I'm not including. @artf What am I missing to make it work on init? Here is an example of how I'm adding the button.
const scriptTypesSupport = ['default','wrapper','text','textnode','image','video','svg','link'];
scriptTypesSupport && scriptTypesSupport.forEach(type => {
const typeOpt = dc.getType(type).model;
dc.addType(type, {
model: {
initToolbar() {
typeOpt.prototype.initToolbar.apply(this, arguments);
const tb = this.get('toolbar');
const tbExists = tb.some(item => item.command === 'edit-component');
if (!tbExists) {
tb.unshift({ // unshift to be the first and push to be the last
command: 'edit-component',
label: '<i class="fa fa-code"></i>',
});
this.set('toolbar', tb);
}
}
}
});
})
It works perfectly but like I said only for new components added and not for existing ones (example, if I init the editor with some code inside the gjs container the components won't have this new option in the toolbar)
Above you can see when I add a second link the toolbar have the new button (pencil) but the existing one doens't.

Answers (3)
Probably the editor doesn't change a component during runtime when it's model is changed, so the component on the canvas was created before the model was modified therefore it wont show the changes to the toolbar.
I've gotten around this by loading the canvas after loading all components and component modifications, but I guess depending on storage setup running editor.load() might fix that.
Thanks for your response @Ju99ernaut, any ideas of how can I change them during runtime? I think It can work if I make those changes in the core but I was thinking in make some plugins with this type of functions and this way we can't make it work in external plugins. So it has to be after init (I think...)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #868
[BUG]: "component:add" event triggers when components are moved
Taken from https://github.com/artf/grapesjs/wiki/API-Editorcomponent:add - Triggered when a new component is added to the editor, the model...
Issue #3160
[Question] Add toggle-move label in custom setting panel in Vue
Version: ^0.16.18 This question is intended to extract layer moving function to custom one. I only want to given components could be seen i...
Issue #1822
[QUESTION] - Add style sector for specific Css rule
Hey @artf , COuld you give your help on this one? I want to disable the user to able to select the wrapper block (body) but I still want to...
Issue #2007
[QUESTION] Adding new traits to component and update current ones with these
I have some custom components created before modifying their traits (for example, I created a new trait for it), these components wont cont...
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.