Issue #4432Opened July 9, 2022by booellean0 reactions

BUG: Undo Manager creates Duplicate CSS during custom command

Question

GrapesJS version

  • I confirm to use the latest version of GrapesJS

What browser are you using?

103.0.5060.66 (Official Build) (64-bit) (cohort: Stable)

Reproducible demo link

https://codesandbox.io/s/cranky-morning-fh5diz?file=/index.js

Describe the bug

How to reproduce the bug?

  1. Drop the custom block "picture" into the frame and resize.
  2. Look at markup preview, and you will see two ids in the css with duplicate markup (picture id and img id)
  3. "Undo" until resize changes again
  4. Look at markup preview, and now css markup has duplicate id of picture element, preventing further styling

What is the expected behavior? The picture element relies on css styling of the img tag for dimensions to display properly. While exploring this, I created a command sync-styles that will duplicate any styles defined on the parent to the child (pending if a property is present). Expected behavior is that the styles will be completely synced, even when Undo Manager is called

What is the current behavior? The command sync-styles works as expected until the Undo Manager is activated. I'm not sure exactly which piece is causing the break, but currently the styles for the img id are gone, and there are two definitions of style for the picture id.

I actually created the command in hopes that that would fix the error, since originally I just executed the code in the watched event handler for style:property:update. Regardless of where I execute the style updates, the UndoManager breaks the same way.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Answers (1)

artfJuly 15, 20220 reactions

I'm not sure exactly what is happening but I'd suggest switching to styleable:change event and pass its options to setStyle, eg.

editor.on("styleable:change", (model, prop, opts) => {
  let component = editor.getSelected();
  if (component?.attributes.styleShared) {
    let child = component.attributes.components.models.filter(
      (m) => m.attributes.tagName === component.attributes.styleShared
    )[0];
    let styles = component.getStyle();
    child.setStyle(styles, opts);
  }
});

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.