StyleManager events fire multiple times per change
Question
To reproduce:
On a fresh install of GrapesJS, within the demo page (within index.html), add an event listener like so:
`editor.on('component:styleUpdate:width', (model) => {
console.log(model);
});`
### Issue:
When changing the 'width' value of any element, the 'model' variable will be printed multiple times.
The issue seems to be more severe when listening for 'list' properties such as Font Family, Font Weight or Display. E.g:
`editor.on('component:styleUpdate:font-family', (model) => {
console.log(model);
});`
For these kinds of properties, I have noticed that the events fire multiple times when the editor is first loaded (before any user interaction), and also multiple times when their value is changed.
### Cause:
After some investigation, i thought the issue may relate to the value of the properties being set within the GrapesJS framework multiple times to accommodate the Undo Manager [(Property.js:58)](https://github.com/artf/grapesjs/blob/dev/src/style_manager/model/Property.js#L58), although I am still unable to determine the underlying cause or provide a fix for the issue myself.
### Symptoms:
Due to this, it will be hard for developers to use the StyleManager events as intended in some cases. For example, if you would like to listen for what the value was changed from via the ._previousAttributes property you will have difficulty, as the event is fired multiple times: and each time the previous attributes specified are different and don't relate to what the user actually chose.Answers (3)
Hello,
I am having the same problem and since that it is not solved yet, it would be a solution to use the debounce technique.
check this example.
Hello,
I also found this issue. The event is firing even without changing the width I added the following code to the html page and the log is generated multiple times.
editor.on('component:styleUpdate:width', (model) => { console.log('Component width updated:', model); });
I have recorded a video proof and its the below link
https://drive.google.com/file/d/1zoTUN-e400YDVPXt2DESAemSL6STh0TU/view?usp=sharing
I think the fix is already available on the dev branch (so ready for the next release) https://github.com/artf/grapesjs/blob/0dba07fd21b8e2dd953100bec5b7531f0761c9c2/src/style_manager/view/PropertyView.js#L411-L413
I see multiple events on the current online demo but seems to be ok on my local dev copy. If you can confirm it by running locally the dev branch it would be great.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3137
BUG: When I select an particular element I'm not getting its styles. This issue happens only with some elements.
Not getting styles of the selected element Here is my code, const model = editor.StyleManager.getModelToStyle(component); let styleObject =...
Issue #1364
[Bug]: Error on multiple instances of grapesjs
Hello, I use Grapesjs in my reactJS application. I have a react component which is an instance of Grapesjs. So I can add this component in...
Issue #596
Color picker displays wrong position
Issue #2403
editor.load possibly causes memory leak
How to reproduce:Naviagate to https://grapesjs.com/demo.htmlOpen browser console (Chrome) and type the following: setInterval(() => editor....
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.