Issue #1025Opened April 10, 2018by vladbpython1 reactions

How add new traits to custom block

Question

Code SnippetTEXT
grapesjs.plugins.add('CustomBlocks', function(editor, options) {
  options = options || {};

  TraitsToComponent();
  addHTMLCodeComponent();
  addHTMLCodeBlock();



  function TraitsToComponent(){
            editor.TraitManager.addType('content', {
              events:{
                'keyup': 'onChange',  // trigger parent onChange method on keyup
              },

              /**
              * Returns the input element
              * @return {HTMLElement}
              */
              getInputEl: function() {
                if (!this.inputEl) {
                  var input = document.createElement('textarea');
                  input.value = this.target.get('content');
                  this.inputEl = input;
                }
                return this.inputEl;
              },

              /**
               * Triggered when the value of the model is changed
               */
              onValueChange: function () {
                this.target.set('content', this.model.get('value'));
              }
            });


  }

  function addHTMLCodeComponent() {
    var defaultType = editor.DomComponents.getType('text');

    var _initToolbar = defaultType.model.prototype.initToolbar;

    editor.DomComponents.addType('custom_block', {
      model: defaultType.model.extend({
           defaults: Object.assign({}, defaultType.model.prototype.defaults, {
            traits: [{
  label: 'My custom label',
  type: 'content',name:'content', // built-in types: text, number, select, checkbox, color
}],
          }),
      }, {
        isComponent: function(el) {
          if (typeof el.hasAttribute == "function" && el.hasAttribute("data-html-code")) {
            return {type: "text"};
          }
        }
      }),
      view: defaultType.view
    });

  };

  function addHTMLCodeBlock() {
    editor.BlockManager.add("custom_block", {
      attributes: {class: "fa fa-code"},
      label: "HTML Code",
      content: '<div data-html-code>Edit my HTML content</div>'
    });
  };

});

Answers (3)

chapterjasonApril 11, 20181 reactions

Could you please provide more information? And could you please indent your code correctly that it's getting rendered by github?

vladbpythonApril 10, 20180 reactions

Please help me

lock[bot]September 17, 20190 reactions

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

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.