question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to update styles in Style Manager

See original GitHub issue

Hello. I created custom component where I set backgraound-image via custom asset manager Custom block code:

  domc.addType('image-block', {
    extend: 'default',
    model: {
      defaults() {
        return {
          name: 'Background image',
          type: 'image-block',
          tagName: 'div',
          void: false,
          droppable: true,
        };
      },
    },
    view: {
      init() {
        this.listenTo(this.model, 'change:src', this.updateImage);
      },
      events: {
        dblclick: 'onActive',
      },
      onActive() {
        editor.runCommand('open-assets');
      },
      updateImage(model, url) {
        if (url) {
          const style = model.getStyle();

          model.setStyle({
            'background-image': style['background-color'] || `url("${url}")`,
            'background-size': style['background-size'] || 'cover',
            'background-position':
              style['background-position'] || 'center center',
            'background-repeat': style['background-repeat'] || 'no-repeat',
            'min-height': style['min-height'] || '200px',
          });
        }
      },
    },
    isComponent(el) {
      if (el.dataset && el.dataset.type === 'image-block') {
        return { type: 'image-block' };
      }
    },
  });

Command which I run to set new image:

  cmd.add('set-image-url', {
    run(editor, sender, { url }) {
      const target = editor.getSelected();
      target.set('src', url);
    },
  });

It is working fine, image sets. But background property in Styles Manager doesn’t update after image set.

How can I update background property in style manager?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Ju99ernautcommented, May 18, 2020

Noticed that in general when you update styles for a component through code they do not reflect in the Styles Manager until you deselect and reselect the component and I can’t find anything in the docs about refreshing the Styles Manager.

As a workaround you can use:

editor.selectToggle(model);
editor.select(model);

In your case

domc.addType('image-block', {
   //...
   view: {
       //...
       updateImage(model, url) {
        if (url) {
          const style = model.getStyle();

          model.setStyle({
            'background-image': style['background-color'] || `url("${url}")`,
            'background-size': style['background-size'] || 'cover',
            'background-position':
              style['background-position'] || 'center center',
            'background-repeat': style['background-repeat'] || 'no-repeat',
            'min-height': style['min-height'] || '200px',
          });
          editor.selectToggle(model);
          editor.select(model);
        }
      },
    },
    //...
  });

@Ju99ernaut no

Check here on why you would want to use a custom css parser if you’re gonna be setting some styles outside of the Styles Manager

0reactions
kuhelbehercommented, May 19, 2020

Thanks for suggestion with this workaround. It’s not ideal but it’s working. If someone knows better solution it would be nice.

And thanks for postcss-parser advice. I’ll try it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 ways to modify and customize styles in Microsoft Word
Click the Home tab, right-click the style you want to modify in the Style Gallery, and then choose Modify. In the Modify Style...
Read more >
Style manager in MS Word 2016 for mac? - Microsoft Community
Open the Word application> Click on the Format in the menu bar> Select the Styles> Choose the Style you want to modify> Click...
Read more >
How to modify styles in Microsoft Word - ShaunaKelly.com
Word 2002 and Word 2003: Format > Styles and Formatting. Right-click your style. Choose Modify. Other versions of Word: Format > Style. Click...
Read more >
Organizing Styles In WORD 🔥🔥 - YouTube
Organizing Styles In WORD** Enroll in complete course**https://www.udemy.com/user/syedraza2/📒 Show Description and Resources 📒1.
Read more >
Word 2016 - How to Modify a Style in Microsoft MS - YouTube
This Microsoft Word 2016 tutorial shows you how to create a styles. Creating a style in MS Office 365 can be done using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found