grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

[Question] Using Style tag instead of css

Open richardabear opened this issue 8 years ago • 3 comments

Hi,

I've been taking a look inside of the dom_components, but cant seem to figure out how to manually override the styling aspect.

I need a way for the component to be able to save all new styles into the style tag instead of the actual css class. (Try moving a component that was styled with force css) and it removes all of the style.

Hope you can help, Best Regards, Richard

richardabear avatar Feb 28 '18 13:02 richardabear

Hi @Owchzzz,

It seems to be baked into the core of GrapesJS that components will not have a style tag - see this example where it is explicitly removed. I'm not sure if modifying this behavior would completely solve your use case, but you could start by overriding it with the following code:

var defaultType = editor.DomComponents.getType("default");
defaultType.model.prototype.getAttrToHTML = function() {
  return this.getAttributes();
};

You probably want this code to run before the content has been set into the editor, so it would be best to put it in a plugin. If you do that and still have issues to solve, let me know what the problems are and I can try to look into some more.

ryandeba avatar Mar 08 '18 03:03 ryandeba

So did you mange to find any complete solution that leaves the style tags intact with no changes?

Amir2828 avatar Aug 28 '19 12:08 Amir2828

The solution by @ryandeba seems to influence only the use of inline style attributes like <div style="...">. (but for me those were also not filtered out without overwriting that prototype. We are having the problem that

<style>
...
</style>

is filtered out, not sure if the OP has the same problem

amenk avatar Apr 17 '21 12:04 amenk