Background image layers
Thanks @xamdam777 for the report. Actually, as you already indicated, this happens when you import the style (and browser's CSS parsers are pretty messy), and unfortunately, in few cases, conflicts happen. (eg. Chrome generates a style that even itself doesn't understand) the strange think happens here when you add a...
Read full answer below βQuestion
Bug: when your pre-defined block or saved templates have two or more background layers, if you add a new layer, a lot of new empty layers are created.
How to reproduce it:
- Go to https://grapesjs.com/demo.html
- Select the pink Header block
- Go to Decorations -> background and add a new layer
- Deselect the pink Header and select it again
- Go to Decorations -> background, you will see new empty layers
- If you repeat the previous cycle, you will get hundreds of empty layers.
30 seconds video showing the problem: https://screencast-o-matic.com/watch/cFQVb0q4Ex
It happens in Chrome, Safari and Firefox, last versions.
@artf can you give me any clue of where the new background layers addition is controlled in grapesjs? I would like to try to fix it myself. Thank you!
Answers (2)
Thanks @xamdam777 for the report. Actually, as you already indicated, this happens when you import the style (and browser's CSS parsers are pretty messy), and unfortunately, in few cases, conflicts happen. (eg. Chrome generates a style that even itself doesn't understand)

the strange think happens here when you add a new layer
as you see background-repeat and background-position contain more than 3 expected values (they have 5 for some reason).
BTW if you would like to fix it you can probably start from the function which adds a new layer https://github.com/artf/grapesjs/blob/dev/src/style_manager/view/PropertyStackView.js#L69
Thank you very much @artf, now I completely understand the problem, damned browser's parsers! :)
I have been doing some tests in addLayer() and refreshLayers().
This is my current approach, modifying refreshLayers() function, I try to detect the wrong layers and do not show them.
I placed the code below in line 131 of https://github.com/artf/grapesjs/blob/dev/src/style_manager/view/PropertyStackView.js#L69 (near the end of refreshLayers function)
It seems to be working fine with a lot of bugged samples (it never shows bugged layers), but since it does not really removes the wrong code (the wrong CSS is still there), I wonder if this is a bad solution.
var arrayWrongLayers = new Array;
var myLayersArray = layersObj;
var arrayLength = myLayersArray.length;
for (var i = 0; i < arrayLength; i++) {
//console.log(myLayersArray[i]);
var imagevalue= myLayersArray[i].properties[0].value;
if(( imagevalue.toLowerCase().indexOf('.png') >= 0)||( imagevalue.toLowerCase().indexOf('.jpg') >= 0)||( imagevalue.toLowerCase().indexOf('.gif') >= 0)||( imagevalue.toLowerCase().indexOf('.jpeg') >= 0)||( imagevalue.toLowerCase().indexOf('.svg') >= 0)){
console.log("It's image");
} else {
console.log("It's NOT image");
arrayWrongLayers.push(i);
}
}
console.log("the wrong layers are" + arrayWrongLayers);
var arrayWrongLayersLength = arrayWrongLayers.length;
for (var i = 0; i < arrayWrongLayersLength; i++) {
console.log(arrayWrongLayers[i]);
//Deletes the last layer on the stack before rendering them in the style manager
//Better to delete the layer by index, but I found problems doing that and pop() works too
layersObj.pop();
}
Maybe we could really delete the layer from the model using this approach?
Thanks again!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4249
image src won't change in exported HTML
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? chrome 100 Reproducible demo link https://...
Issue #5607
Cannot expand previously empty layers
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v120.0.6099.199 Reproducible demo l...
Issue #5378
Uncaught TypeError: Cannot read properties of undefined (reading 'Canvas') while dragging a component in Layer Manager Panel
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v116.0.5845.97 Reproducible demo li...
Issue #6669
Cross-level reordering of blocks in Layer Manager causes Sorter errors and breaks further move attempts
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Reproducible demo link https://grap...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins βPremium plugins ship with support, regular updates, and production-ready features β save days of integration work.
Browse premium plugins βRelated tutorials
In-depth guides on the same topic.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
GrapesJS Inline RTE Plugins Update: CKEditor 5 v0.1.4 and Froala Inline Text Editor
CKEditor 5 Inline for GrapesJS v0.1.4 fixes Studio SDK toolbar clipping, iframe injection and link balloon bugs. Compare with Froala Inline β both $69.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.