Issue #2742Opened April 28, 2020by ghost1 reactions

[QUESTION] How to prevent unique ids generation?

Question

Hi! Thanks for your work @artf. This is a simplified code of how I use grapesjs, I want to save the styles and then want to apply them again. The problem is that ids already changed in DOM after first call of container.components() so styles will not be applied. How to prevent this behavior? What should I clear/reset?

wrapper = [...]
style = [...]
components = [{mainBody: [...]}]

this.editor.setComponents(
  Array.isArray(wrapper) ? this.pruneWrapper(wrapper) : wrapper,
);
const grapesWrapper = this.editor.getWrapper();

const [container] = grapesWrapper.find(
  '[data-main-container]',
);

if (container) {
  container.components(components.mainBody);
}
if (style) {
  this.editor.setStyle(style);
  const grapesJsWrapper = this.editor.getWrapper();
  grapesJsWrapper.rerender();

}

Answers (3)

pouyamiralayiApril 30, 20201 reactions

Hi @nikita-balakirev see if #2706 comment can help you. Cheers!

ghostApril 28, 20200 reactions

I fixed it now using editor.DomComponents.componentsById = {}; but I think it can lead to collisions between blocks at some point what do you think?

artfMay 4, 20200 reactions

Sorry @nikita-balakirev can't help you here without a complete reproducible example. When you export components as JSON all IDs are preserved and reused if load correctly, so for sure you're doing something wrong with storing/loading data. Obviously you shouldn't prevent unique ids generation (or doing what you did) as it'll break a lot of stuff

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.