Issue #1763💬 AnsweredOpened January 29, 2019by poeeain0 reactions

Add <Style> tag in block content objects (not a string)

快速解答by artf

BTW, I think, for your case, a custom component is more appropriate

Read full answer below ↓

Question

  • Browser = Chrome
  • GrapesJS = Latest version ( 0.14.52 )

I am trying to create a block includes nesting content object with script, style, type properties. It works fine except adding <style> tag in the content.

Issue

If I create block content as a String like option (A) as below, CSS goes to CSS editor. But if I create block content as an object like option (B), CSS remains in HTML editor. ( I am not sure this is a bug or not )

Here, What I want to achieve is I want to create block like option (B), but expect CSS to be in CSS editor.

option (A)

codepen demo (A) a

blocks: [
          {
                id: 'link',
                label: 'Link',
                category: 'Basic',
                attributes: {
                    class: 'fa fa-link'
                },
                content: `<a class="mylink-one" href="#">This is a link</a>
<a class="mylink-two" href="#">This is a link</a>
<style>.mylink-one{color:red;}.mylink-two{color:skyblue;}</style>`,     
            }
          ]

option (B)

codepen demo (B) b

blocks: [
          {
                id: 'link',
                label: 'Link',
                category: 'Basic',
                attributes: {
                    class: 'fa fa-link'
                },
                content: {
                    script: function(){
                        console.log("I am dragged!")
                    },
                    type: 'link',
                    content: `<a class="mylink-one" href="#">This is a link</a>
<a class="mylink-two" href="#">This is a link</a>
<style>.mylink-one{color:skyblue};.mylink-two{color:teal}</style>`,
                }
            }
          ]

Why do I want this? Because I have an inline CSS and custom javascript codes to append with the block (when user drag into the canvas).


English is not my mother tongue; please excuse my bad explanation.

Answers (2)

artfFebruary 6, 2019
content: {
                    script: function(){
                        console.log("I am dragged!")
                    },
                    components: `...`, // use components instead of content
                }

BTW, I think, for your case, a custom component is more appropriate

lock[bot]February 6, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

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...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.