Issue #1025💬 AnsweredOpened April 10, 2018by vladbpython1 reactions

How add new traits to custom block

快速解答by chapterjason1

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

Read full answer below ↓

Question

代码片段TEXT
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, 2018

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

vladbpythonApril 10, 2018

Please help me

lock[bot]September 17, 2019

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.