Add new class programatically
Question
Hello Artf,
I want to edit the styles of components (mostly imported html) without modifying all components with the same class. I tried to add a random named class programmatically like this:
`
this.editor.on('component:selected', function(model) {
if (model && model.length > 2) {
const uuid = this.s4(); // generate new random class name
const selector = {
name: uuid,
type: 'class',
label: uuid,
};
model[1].attributes.classes.push(selectorManager.get(uuid));
let mClasees = model[1].attributes.classes;
const uuid = this.s4();
const selector = {
name: uuid,
type: 'class',
label: uuid,
};
mClasees.push(selector);
}.bind(this));
`
This add a class programmatically to the component but it seem to no let me edit it's style.
I just want be able to change the style of a single component without having to care which class it belongs to. Event if I copy past a component, I want be able to edit each components individually.
I hope that clear. If you need any further information, don't hesitate to ask me :)
Thanks your help!Answers (3)
@wemod123 editor.getWrapper().addClass('my-class')
@IrinaCodixis from the https://github.com/artf/grapesjs/releases/tag/v0.14.52 we've added setRule/getRule methods, so to add some CSS rule programatically you should do this:
editor.CssComposer.setRule('.some-class', { color: 'red' })
// Remove
const rule = editor.CssComposer.getRule('.some-class');
editor.CssComposer.getAll().remove(rule);
You mean something like model.set('style', {...})? This adds an inline style on the component
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3211
how to move components
Hello dear: I want move a component to first/end in the wrapper programmatically, i think something like editor.getWrapper().add(editor.sel...
Issue #1817
Change HTML content while block is render in editor
Hello @artf I have one block into block manager like As per above script inside the block, i want to add dynamic HTML content while this bl...
Issue #791
Adding new button in toolbar
Hello, I am trying to add a new button to the toolbar of all the components. I been able to add for each component separatly, but not for a...
Issue #2378
[QUESTION] Modifying global CSS in style manager
We want to achieve the following:Mark our block HTML elements with some classes like:Add some styles that define CSS classes like:Using Gra...
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.