grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

typography sector not found

Open tasham16 opened this issue 6 years ago • 3 comments

When using trying to add a font to the style manager, I use the following code:

const prop = editor.StyleManager.getProperty('typography', 'font-family'); prop.set('options', [ {value: "'Oswald', sans-serif", name: 'Oswald'}, ... ]);

However, when I use this inside a plugin (in the index.js) I am creating, I call it with editor.on('load',....) I keep getting 'typography' sector not found. However if I call it outside of the plugin (in the index.html) in the same way, where I am initializing the editor with all the configs, it works. So I was wondering, can we not add new fronts in a plugin so that they are automatically packaged with that plugin? and if not, why? If it is possible, please let me know how I can achieve this.

P.S. Thank you so much for all the time/work you've put in to building this project!

tasham16 avatar Aug 10 '20 21:08 tasham16

Hi @tasham16 !

I think this is expected behaviour because modules are loaded after plugins in the editor's initialization sequence, & sectors are only added to the StyleManager module right after it has been loaded.

I don't know if this is a very clean way of achieving it, but without a better solution you could try overriding the StyleManager's onLoad method from your plugin to execute its logic at right moment like this following:

const onLoad = editor.StyleManager.onLoad;

editor.StyleManager.onLoad = () => {
    onLoad();
    this.getProperty('typography', 'font-family').set('options', [ /* ... */ ]); 
}

I'm sorry I didn't have the time to test this, hope it can help though.

Cheers !

mcottret avatar Aug 21 '20 14:08 mcottret

Thank you so much! I will try that out!

tasham16 avatar Aug 25 '20 19:08 tasham16

The right way is to wait for the load of the editor

editor.on('load' => {
  const prop = editor.StyleManager...
})

artf avatar Aug 28 '20 00:08 artf