When the component's view modifies the HTML, text components are no longer editable
Question
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 want to modify the HTML in the view (to add some content that I want shown in the editor but not in the saved HTML). I tried something like this:
comps.addType('section-test', {
// Define the Model
model: defaultModel.extend({
// Extend default properties
defaults: Object.assign({}, defaultModel.prototype.defaults, {
draggable: 'body, body div'
}),
},
{
isComponent: function(el) {
if(el.tagName == "SECTION"){
return {type: 'section-test'};
}
},
}),
// Define the View
view: defaultType.view.extend({
// The render() should return 'this'
render: function () {
// Extend the original render method
defaultType.view.prototype.render.apply(this, arguments);
//this.el.placeholder = 'Text here'; // <- Doesn't affect the final HTML code
var html = this.el.innerHTML;
html = "<h1>ABC</h1>" + html;
this.el.innerHTML = html;
return this;
},
})
});
blockManager.add('section-typography', {
label: 'Text section',
category: 'Sections',
content: `<section class="bdg-sect">
<h1 class="heading">Insert title here</h1>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>
</section>`,
attributes: {class:'gjs-fonts gjs-f-h1p'}
});
The issue is if I modify the render() function as above, the text components in the Text section (the h1 and the p) can no longer have their text edited by double-clicking on the text.
Is there a better way to do what I want to do?
Thanks!
Answers (3)
I tried the above, as follows:
var textType = comps.getType('text');
comps.addType('section-test', {
// Define the Model
model: defaultModel.extend({
// Extend default properties
defaults: Object.assign({}, defaultModel.prototype.defaults, {
draggable: 'body, body div'
}),
},
{
isComponent: function(el) {
if(el.tagName == "SECTION"){
return {type: 'section-test'};
}
},
}),
// Define the View
view: textType.view.extend({
// The render() should return 'this'
render: function () {
// Extend the original render method
defaultType.view.prototype.render.apply(this, arguments);
//this.el.placeholder = 'Text here'; // <- Doesn't affect the final HTML code
var html = this.el.innerHTML;
html = "<h1>ABC</h1>" + html;
this.el.innerHTML = html;
return this;
},
})
});
blockManager.add('section-typography', {
label: 'Text section',
category: 'Sections',
content: `<section class="bdg-sect">
<h1 class="heading">Click here to edit title</h1>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>
</section>`,
attributes: {class:'gjs-fonts gjs-f-h1p'}
});
But I'm still unable to edit the text in the h1 and p elements after dropping the component on the canvas. :(
Sorry I didn't notice this
var html = this.el.innerHTML;
html = "<h1>ABC</h1>" + html;
this.el.innerHTML = html;
you basically remove elements with models and attach a new string as HTML (the editor can't access their models anymore). If you want to add a new component you should do it via models (eg. view.model.append('<div>stuff</div>'))
You can extend the Text Component instead of the base one
const defaultType = comps.getType('default');
const textType = comps.getType('text');
...
view: textType.view.extend({ ...
Related Questions and Answers
Continue research with similar issue discussions.
Issue #701
Modifying a component in the editor clears the model content
What I'm trying to achieve: I have special "data-" attributes on some of the components I pull into the editor. When one of these is modifi...
Issue #1030
[Question] Firing events with custom components
I'm trying to build a plugin for Grapesjs with some custom components, and I'm having trouble with triggering events and interacting with t...
Issue #1912
[QUESTION] Custom component default children
Hi, I am trying to understand how I can achieve the following.Create a component with a default content as children.Create a block to add t...
Issue #486
Custom Component not editable and also after traits update canvas does not update
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 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.