Issue #1584Opened November 15, 2018by coder-strange1 reactions

Not getting the Script of Block from editor.getComponents()

Question

Below is the Component that I'm adding.

Code SnippetTEXT
>  this.editor.BlockManager.add('dob-mmyy-pii',  {
        id: 'dob-mmyy',
        label: `DOB (MM, YYYY) (PII)`,
        category: 'Forms',
        attributes: {class:'fa fa-birthday-cake'},
        content : {
          content :  `<div>
            <label>DOB (Month and Year)</label>
            <input  name='dob' class="form-control">
          </div>`,
          script : function(){
           
            $( $(this).find('input[name="dob"]') ).datepicker({
              changeMonth: true,
              changeYear: true,
              showButtonPanel: true,
              dateFormat: 'MM yy',
              onClose: function(dateText, inst) { 
                  console.log("inst",inst)
                  $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
              },
              beforeShow:function(){
                setTimeout(()=>{$('.ui-datepicker-calendar').css("display", "none").hide();
                }, 1);
              },
              onChangeMonthYear:function(){
                setTimeout(()=>{$('.ui-datepicker-calendar').css("display", "none").hide();
                }, 1);
              
              }
            })
          }
        }
    });

and doing editor.getComponents() this is what I'm getting.

`[{"tagName":"div","type":"","name":"","removable":true,"draggable":true,"droppable":true,"badgable":true,"stylable":true,"stylable-require":"","style-signature":"","unstylable":"","highlightable":true,"copyable":true,"resizable":false,"editable":false,"layerable":true,"selectable":true,"hoverable":true,"void":false,"state":"","status":"selected","content":"<div>\n            <label>DOB (Month and Year)</label>\n            <input  name='dob' class=\"form-control\">\n          </div>","icon":"","style":"","classes":[],"attributes":{"id":"ixm7"},"traits":[{"type":"text","label":"","name":"id","min":"","max":"","unit":"","step":1,"value":"ixm7","default":"","placeholder":"eg. Text here","changeProp":0,"options":[]},{"type":"text","label":"","name":"title","min":"","max":"","unit":"","step":1,"value":"","default":"","placeholder":"eg. Text here","changeProp":0,"options":[]}],"propagate":"","components":[]}]`

I don't find the script I've defined in the block, please let me know if I'm doing something wrong.

Edit:
Found a bug similar to mine: [#1569](https://github.com/artf/grapesjs/issues/1569) but the link given there doesn't cover my issue.

Answers (3)

artfNovember 19, 20181 reactions

@taufeek6 #1576

artfNovember 17, 20180 reactions

If the script is a function and you can't serialize it in a JSON. I think, in your case, you need a new custom component type (you're just adding a block) then you'll export only changed properties (the script will be part of the component definition, so you won't need it in the JSON)

coder-strangeNovember 19, 20180 reactions

Bingo! that worked perfectly fine but it's letting all of the element to be selected in the block (By directly giving the block html in 'content', content : { content : 'html' } was working fine) so how do I forbid the elements to be selected in a block? image

For example, in the above image, I don't want that label, button or headings to be selected, whenever user selects, the whole block will be selected.

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.