Issue #1888Opened March 13, 2019by RobertoCorreale0 reactions

[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)

artfMarch 22, 20190 reactions

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?

no-response[bot]April 1, 20190 reactions

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.

lock[bot]April 2, 20200 reactions

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...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.