Custom Component not editable and also after traits update canvas does not update
Question
below is code i used for traits and custom section but after adding component via block, cannot edit text or trait update does not update canvas. I have extended 'text', Also I do not get html back when I use editor.html(), Also when I click on image it does not open asset manager. I am sure I must be missing something here.
var defaultType = comps.getType('text');
var defaultModel = defaultType.model;
var defaultView = defaultType.view;
createSectionComp(comps, defaultModel, defaultView)
function createSectionComp(comps, defaultModel, defaultView) {
comps.addType('section', {
// Define the Model
model: defaultModel.extend({
// Extend default properties
defaults: Object.assign({}, defaultModel.prototype.defaults, {
type: 'section',
tagName: 'section',
title: 'Sample Title',
attributes: {
'data-gjs-type': 'cust-section',
},
traits: [{
type: 'text',
label: 'Title',
name: 'title',
placeholder: 'eg. Leadership Development',
changeProp: 1,
}],
}),
initialize(o, opt) {
defaultModel.prototype.initialize.apply(this, arguments);
this.listenTo(this, 'change:title', this.updateModelProps);
},
updateModelProps() {
var md = this;
this.set('title', md.get('title'));
},
}, {
isComponent(el) {
if ((el.getAttribute &&
el.getAttribute('data-gjs-type') == 'cust-section') || el.className === 'cust-section') {
return {
type: 'cust-section'
};
}
}
}),
// Define the View
view: defaultView.extend({
tagName: 'div',
events: {},
initialize(o) {
defaultView.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, 'change:title', this.updateView);
},
updateView() {
var rowDiv = document.createElement('section');
rowDiv.className = "row";
rowDiv.innerHTML = "Sample Text Please change here";
this.custTextRightHandImage = rowDiv;
return this.custTextRightHandImage;
},
render(...args) {
defaultView.prototype.render.apply(this, args);
this.el.appendChild(this.updateView());
return this;
}
})
});
};
Answers (2)
In your model, you don't need defining data-gjs-* it's only make sense for importing from HTML, so remove this:
attributes: { 'data-gjs-type': 'cust-section', },
and you don't need type: 'section', either.
From what I see you should define addType like this
comps.addType('cust-section', {...
You can't edit this
var rowDiv = document.createElement('section');
rowDiv.className = "row";
rowDiv.innerHTML = "Sample Text Please change here";
because it's just a plain HTML, the editor works with models attached to elements, so you probably want this this.model.append('<section class="row"> Sample ...</section>') in this way you update the model tree which then reflect changes on canvas
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 #460
When the component's view modifies the HTML, text components are no longer editable
I'm trying to build a custom component that contains some lorem ipsum text in the content, for editing once it's on the canvas. But I also...
Issue #1535
[Bug]: After custom component is deleted the Trait's view does not clear out
Hi, I'm using the grapesjs library version 0.14.33. I've created some custom components and I've noticed that on deleting the components th...
Issue #1100
unable to update the traits value in the HTML for custom component
I have created a custom component for social icons.When selected this component i have two traits in setting facebook URL and show facebook...
Issue #1198
QUESTIONS : Find element in Model
I have custom component and it render below html, I want to update value of h4 tilte and other element on trait changed, but not sure how c...
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.