How add new traits to custom block
Question
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)
Could you please provide more information? And could you please indent your code correctly that it's getting rendered by github?
Please help me
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.
Issue #1568
List/Array Trait
Hello, I am trying to write up a trait that allows creating a list of forms and generates the respective in the model. So far I have this:...
Issue #1817
Change HTML content while block is render in editor
Hello @artf I have one block into block manager like As per above script inside the block, i want to add dynamic HTML content while this bl...
Issue #580
[Question] Any/Trigger event when any block is dropped inside canvas?
Hey, How can I trigger some event when any new block or content inside canvas is updated? Something like editor.onCanvasUpdate() Thanks!
Issue #862
Jquery Image Viewer plugin in GrapesJs custom Block
I want to integrate Image viewer jquery plugin in grapesjs new custom block but jquery code doesn't recognize. How can write jquery code in...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.