[Question] Add custom html inside canvas
Question
Hi there,
I'm using grapes to export a custom json with all the information i need, instead of HTML and CSS. For doing that I'm parsing the gpjs-components stored on localStorage.
Now iI need to customize how components are rendered inside canvas. For example I have an Input component with two traits (label and value), and i would like to render it inside the canvas with the label traits as <label> tag and value as placeholder inside <input> tag.
Of course I can use a custom view, but doing that inside the localStorage will be saved also the label tag inside the object, but i would like to avoid it, because when i export all the canvas code I only expect to have
{
"input": [
{"label": "lblValue"},
{"value": "traitValue"}
]
}
I have tried to modify how components are rendered inside the view, doing something like the following.
view: {
init() {
this.listenTo(this.model, 'change:attributes', this.updateComponentView);
},
onRender() {
this.updateComponentView();
},
updateComponentView() {
const { attributes } = this.model.attributes;
if (!this.el.parentNode.querySelector(`#${this.model.cid}`)) {
const newDOM = `
<label id="${this.model.cid}" class="inputLabel" data-customelement>
${attributes.label}
</label>`;
this.el.parentNode.append(replaceDOM(newDOM), this.el);
}
const label = this.el.parentNode.querySelector(`#${this.model.cid}`);
label.style.pointerEvents = 'none';
this.el.value = attributes.fieldLabel;
this.el.placeholder = attributes.fieldLabel || attributes.name;
label.innerText = `${attributes.name} ${(attributes.required) ? ' *' : ''}`;
},
It works, but when I drag other blocks inside the canvas and i reload the page, input components are not in the correct order anymore. I think is because adding new nodes inside the canvas, grapes cannot calculate the correct order.
Any suggestions on how to mange this case?
Thank you
Answers (3)
I think is because adding new nodes inside the canvas, grapes cannot calculate the correct order.
Correct, the order is based on components in the model, not the view. So, just to be clear, that <label> you're adding in updateComponentView it's just for the canvas and you don't need in your JSON structure, correct?
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.
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 #1255
[QUESTION]: How to set editor html contents from custom component?
I'm having trouble getting a custom block+component to display its template HTML inside the grapes editor. Within the exported HTML, the ma...
Issue #2008
[Question]The canvas turns dark and unmodifiable on adding a large piece of custom html.
I used to add custom html with editor.addComponent(). It works well at most of the time, but when I try to insert a large piece of HTML whi...
Issue #1162
[Question]: Editable components in Handlebars rendered template
Hi, I've been using grapes for about a couple of days now. Thanks for the awesome work on this project. I've read through the API docs (hop...
Issue #1693
[BUG|/QUESTION] Strange characters (Â) within CSS/HTML preview; Elements are not deleted completely
BUGAre you using the latest release (older versions are NOT supported)?0.14.50Are you facing the bug with your local copy of GrapesJS or wi...
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.