Issue #460Opened October 28, 2017by roytang2 reactions

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)

roytangNovember 4, 20171 reactions

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

artfNovember 7, 20171 reactions

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

artfOctober 30, 20170 reactions

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.

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.