[Questions] Ordering of selector with id and class selectors, space after id-selector
Question
I am currently working on a web application for which we integrate this project as GUI editor to customize the environments.
Our GUI components are loaded and parsed from the database. We can identify e.g. a button, data grid, etc. by an ID (e.g. _17190fbd-13d6-430f-bb32-1a09d2151a77). These components are then built from several nested HTML elements by a framework, which adds classes for styling.
My goal is now to access these inner elements from GrapesJS with something like #ancestor-id .framework-class1.framework-class2 (e.g. #_17190fbd-13d6-430f-bb32-1a09d2151a77 .my-text-label)
I add the id-selectors to each nested element after the canvas is loaded:
allComponents.forEach((component: any) => {
const ancestorId = this.getAncestorId(component);
const idSelector = selectorManager.add('#${ ancestorId }');
const selectors = component.getSelectors();
selectors.add(idSelector);
});
My problem is now that the class selectors are listed before my Ancestor-id selector.
This results in a full selector like .my-text-label#_17190fbd-13d6-430f-bb32-1a09d2151a77 instead of #_17190fbd-13d6-430f-bb32-1a09d2151a77 .my-text-label with the space between the id and classes.
Does anyone have an idea where or how I could change this behavior?
Thank you very much for your support.
Answers (3)
a short update:
I managed to get the the selectors in the right order when using selectors.unshift(idSelector) instead of selectors.add(idSelector) in above sample.
Now i still have the issue with the missing space between the id- and the class-selectors.
Until now I could not find a way to get into Selectors.getFullString(..) to change the behavior
Hi @dintifla SelectorManager is not meant to work like that (you can only have a mix of classes .class1.class2 but not with IDs), but honestly I don't understand what is your goal of using the SelectorManager at all.
If you need to query a component in the editor you can just do this anyParentComponent.find('#my-id .my-class')[0]
Hi @artf Thanks for your reply. In the end it is not just about querying a component, but generate the CSS for it. I don't need the HTML since my application is designed in a rich client application, stored in an abstract representation into the database and then parsed into a DOM. As a result, I can't use the IDs generated by GrapesJS, since I have no chance to store back HTML (or any other representation of the DOM).
In the end, i expect all my rules to look like this: #top-level-component-id .my-class { ...}
maybe as a simple example, the generated HTML of a button:
<div id="_4b9c8f66-85d3-4488-8e23-5932bc3e8420"
class="dx-button dx-button-normal dx-widget dx-button-has-text dx-button-mode-contained" role="button"
aria-label="x" tabindex="0">
<div class="dx-button-content"><span class="dx-button-text">Close</span></div>
</div>
to change the label of that specific button my CSS would look like this:
#_4b9c8f66-85d3-4488-8e23-5932bc3e8420 .dx-button-text {...}
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1902
[QUESTIONS] Disable/change build-in components behavior
Hey, Trying to get GrapesJs work for a project I'm currently working on. But is there a way to disable the built-in components that convert...
Issue #2814
[QUESTION] Custom panel button that dynamically Enable/Disable user scripts
Hello and thanks for sharing such a good project. I'm trying to customize some things in the editor but I have some questions that I would...
Issue #2674
[QUESTIONS] i18n content integration
Hello, I'm working on an integration of my localization system (i18n) with grapes.js. The idea is to have a data field, which will hold the...
Issue #1519
[QUESTIONS]: Init components in grapesjs.init
Is it possible to initialise the components at grapesjs.init. We are not using StorageManager.load. We already have the project data at ini...
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.