Background Image Starts duplicating in all the pages if we add only for one page
Please check this option first https://grapesjs.com/docs/modules/Selectors.html#component-first-selectors In case you still have problems, open a proper bug issue with a reproducible demo link.
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome v9
Reproducible demo link
Nothing
Describe the bug
How to reproduce the bug?
- Implement the multipage concept
- Create the 3 pages
- Click on the background image in decorations ( Style Manager ) and add it to first page
- Now add the new page or click on the second page or third page
What is the expected behavior? The background image should not be duplicated to all pages , it should add it to only that selected page
What is the current behavior? The background image is added to all pages when added to only page
If is necessary to execute some code in order to reproduce the bug, paste it here below:
const editor = grapesjs.init({
container: "#block",
plugins: ['gjs-preset-webpage'], // @AS Preset need to add for predefined components
// fromElement: 1,
avoidInlineStyle: 1,
blockManager: {
// appendTo: '.text-block-manager',
// ignoreCategories: true,
},
cssIcons: null,
canvas: {
styles: [
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css",
"http://localhost:3009/api/v1/html/1/styles/canvas.css",
],
scripts: [
"https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js",
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js",
],
},
protectedCss: "",
canvasCss: `
.gjs-selected {
outline: 2px solid #FF1430 !important;
outline-offset: -2px !important;
}
.gjs-hovered{
}`,
commands: {
defaults: [
{
id: "my-command-id",
run() {
alert("This is my command");
},
},
],
},
storageManager: {
id: "gjs_",
type: "remote",
autosave: true,
autoload: false,
urlStore: this.API_URL + "update_builder/" + this.state.id,
urlLoad: this.API_URL + this.state.id,
//method: 'patch',
params: {
draft: "1",
},
headers: { Authorization: localStorage.getItem("token") },
},
assetManager: {
storageType: "",
storeOnChange: true,
storeAfterUpload: true,
upload: process.env.REACT_APP_ASSET_PATH, //for temporary storage
assets: [],
uploadName: "file",
uploadFile: function (e) {
var files = e.dataTransfer
? e.dataTransfer.files
: e.target.files;
var formData = new FormData();
for (var i in files) {
formData.append("upload", files[i]);
}
PageService.contentUpload(
formData,
localStorage.getItem("token")
)
.then((response) => {
var imageUpload = response.data.path.replace(/[//:-]/g, "");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpe?g|png|gif|bmp)$/;
if (regex.test(imageUpload.toLowerCase())) {
editor.AssetManager.add(response.data.path);
} else {
alert("Please upload valid images.");
}
})
.catch((error) => {
alert("Please upload valid images.");
// CommonService.logout(error);
});
},
},
pageManager: {
pages: this.pagesList,
},
layerManager: {
appendTo: ".layers-container",
},
traitManager: {
appendTo: ".traits-container",
},
selectorManager: {
appendTo: ".selectors-container",
},
styleManager: {
appendTo: ".styles-container",
},
deviceManager: {
devices: [],
},
});
const panel = editor.Panels;
const pageManager = editor.Pages;
/*AS to set canvas freely dragging absolute or translate*/
editor.getModel().set("dmode", "translate");
// editor storage error redirect user to login screen.
editor.on("storage:error", (err) => {
alert(`Error: Token Expired, Please Login Again`);
localStorage.clear();
window.location.href = process.env.REACT_APP_SSO_LOGIN_URL;
});
editor.on("storage:start:store", (objectToStore, abcd) => {
this.getPageById();
});
// Remove top default buttons
panel.removeButton("views", "open-layers");
panel.removeButton("options", "export-template");
panel.removeButton("options", "fullscreen");
// panel.removeButton("options", "sw-visibility");
panel.removeButton("views", "open-sm");
panel.removeButton("views", "open-blocks");
panel.removeButton("views", "open-tm");
panel.removeButton("options", "preview");
editor.Commands.add('core:canvas-clear', {
run(e) {
e.DomComponents.clear();
e.CssComposer.clear();
e.setComponents(`<div class="container-fluid vh-100"></div>`)
}
});
const pagesList = pageManager.getAll();
this.pages = pagesList;
this.pageName = this.pages[0].attributes.name;
this.editorData = editor;
editor.Commands.add("preview-ext", {
run(e) {
e.runCommand("preview");
},
});
panel.addButton("options", {
id: "preview",
className: "fa fa-eye",
command: (e) => e.runCommand("preview-ext"),
attributes: { title: "Preview", 'data-tip':"Preview", 'data-for':"builderTT" },
});
editor.on("run:preview", () => {
self.previewPerform("none");
});
editor.on("stop:preview", () => {
self.previewPerform("block");
});
panel.addButton("options", {
id: "link-button",
command: {
run: function (editor) {
GenerateLink(self.state);
},
},
attributes: { id: "generate-link", class: "fa-link", 'data-tip':"Publish Page", 'data-for':"builderTT" },
)}
}
});
newpage(){
const pagesLength = this.editorData.Pages.getAll().length;
this.editorData.Pages.add({
id: `page-${pagesLength + 1}`,
name: `Page ${pagesLength + 1}`,
styles: ``,
component: ``,
});
![Uploading Background_image_issue.pngβ¦]()
}
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (2)
Please check this option first https://grapesjs.com/docs/modules/Selectors.html#component-first-selectors In case you still have problems, open a proper bug issue with a reproducible demo link.
Thanks for reporting this, @Sudhin35.
Great question about Background Image Starts duplicating in all the pages if we add only for one page. The recommended approach with StyleManager is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4435
pages Manager
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? chrome latest versionReproducible demo link...
Issue #6706
TypeError: e.getRoot is not a function (Race condition in Style Manager)
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? All browsers (Chrome, Safari, Firefox, Edg...
Issue #4350
Style Manager don't update background when component is selected
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Brave Browser V1.39.111 Reproducible demo...
Issue #6685
Custom component styles are not applied after deletion and re-addition
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v143.0.7499.193 Reproducible demo l...
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 βBrowse Plugin Categories
Jump directly to plugin category pages on the marketplace.