GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

Custom Select list to text component? #2826

Closed tonypartridge closed 4 years ago

tonypartridge commented 4 years ago

Hey Guys.

I've seen this before but now I cannot find it for the life of me. Basically on the text component we have the top bar: https://gyazo.com/754513edcebbb0566f18e384d3eba453

How can we insert a select list here for example:

Name Email Phone

which inserts: {{NAME}} {{EMAIL}} {{PHONE}}

It's a short code selector basically?

RJCAM commented 4 years ago

Hi @tonypartridge here is an example code to achieve that

// An example with fontSize
rte.add('fontSize', {
  icon: `<select class="gjs-field">
        <option>1</option>
        <option>4</option>
        <option>7</option>
      </select>`,
    // Bind the 'result' on 'change' listener
  event: 'change',
  result: (rte, action) => rte.exec('fontSize', action.btn.firstChild.value),
  // Callback on any input change (mousedown, keydown, etc..)
  update: (rte, action) => {
    const value = rte.doc.queryCommandValue(action.name);
    if (value != 'false') { // value is a string
      action.btn.firstChild.value = value;
    }
   }
  })

You can see in the documentation for more details: https://grapesjs.com/docs/api/rich_text_editor.html#add

artf commented 4 years ago

@tonypartridge probably you're referring to this https://github.com/artf/grapesjs/issues/481#issuecomment-342282125