How to refresh Classes view for active Component in the Style Manager
Question
I'm using a trait (type=select) to change the class of a component (a "column" component, so classes representing width 1-12). After I have removed previous col-* classes and added the newly selected one (via a function triggered by the select change), the Classes section of the Style Manager still has the old class. If I select a different component, then select the column again, it has the new, correct class. How do I force a refresh on the style manager without de/reselecting the component?
Perhaps I am adding/removing classes incorrectly, if the Style Manager view should update automatically. Is this standard Backbone stuff? If so, I'd love to be pointed in the direction of a concise Backbone primer.
Relevant component creation:
var comps = editor.DomComponents;
var defaultType = comps.getType('default');
var defaultModel = defaultType.model;
var defaultView = defaultType.view;
comps.addType('col', {
model: defaultModel.extend({
defaults: Object.assign({}, defaultModel.prototype.defaults, {
'custom-name': 'Column',
traits: [{
type: 'select',
options: /* map of classes */,
label: 'Width',
name: 'col-width',
changeProp: 1
}]
}),
init() {
this.listenTo(this, 'change:col-width', this.changeColClass);
},
changeColClass() {
var width = this.get('col-width');
var classes = this.get('classes');
// remove existing col class
var col_classes = /* list of classes */
classes.forEach(element => {
if (col_classes.includes(element.id)) {
classes.remove(element);
}
});
// add new class to selector manager and component class list
var class_name = 'col-' + width;
var sm = this.sm.get('SelectorManager');
var class_obj = sm.add(class_name)
classes.add(class_obj);
}
}, {
isComponent: function(el) { /* check if contains a col class */ }
}),
view: defaultView
});
Answers (2)
Ah. The ClassTagsView (classes section of the style manager) updates the selector list on the event "change:selectedComponent" on the editor model.
Fixed my problem by adding the following to the end of my changeColClass method:
this.em.trigger('change:selectedComponent');
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 #661
Class attribute not removed
Hello ! I noticed a problem with style manager when we add/remove classes. When we remove all classes it doesn't remove then from the gener...
Issue #1821
[QUESTIONS] How to apply the style changes to the ID instead of the class?
Hi, I have two elements with the same classes. I want to apply style changes with style manager to one of these two elements. I add ID but...
Issue #413
Use trait to modify component class
Hi! I'm trying to change the value of the classes with very little succes. Here is my custom trait's onValueChange function: Which results...
Issue #639
Re-render Component on Canvas when tagName has changed
I'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...
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.