Issue #1580Opened November 14, 2018by maxeckel0 reactions

[Question] Add Custom Button to RTE

Question

Hello all,

i'm currently trying to move away from ckeditor (used in an legacy app to create emails and microsites). This works very well actually, but i have the need to insert Placeholders/Tags/Merge Fields however you want to call them, into the text (like [[salutation]] [[firstname]] etc.).

So at first i looked at grapesjs source on how to add an custom element to the rte and found an example in a code comment. The same example as in the api docs:

    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;
          }
         }
      })

But even the example doesn't work. All it shows is an empty button in the RTE Toolbar:

rte empty select

Having looked around in the issues, i found something like this:

   rte.add('placeholder', {
        name: 'Placeholder',
        icon: 'P',
        options: [
          {name: 'Salutation', value: '[[salutation]]'},
          {name: 'Firstname', value: '[[firstname]]'},
          {name: 'Lastname', value: '[[lastname]]'},
        ],
        event: 'change',
        result: (rte, action) => console.log(action),
        update: (rte, action) => console.log(action)
    });

This piece of code gives me atleast an select dropdown, but the change event won't trigger when i select an option. I simply can't get it to work...

I hope someone of you might point me in the right direction.

Best regards,

ExCluSiv3

Answers (2)

maxeckelNovember 15, 20180 reactions

Nevermind 🤦‍♂️ I found the issue, i thought my local dev env would be using the grapesjs version (14.40) installed via npm, but it was loading an older (10.7) version from unpkg... that's what caused the problem.

I hope this will atleast help somebody else who runs into this...

Here is the final code i use:

rte.add('placeholder', {
        name: 'Placeholder',
        icon: `
            <select>
                <option selected="true" disabled="disabled" id="defaultPlaceholder">Placeholder</option>
                <option value="[[salutation]]">Salutation</option>
                <option value="[[firstname]]">Firstname</option>
                <option value="[[lastname]]">Lastname</option>
            </select>
        `,
        event: 'change',
        result: (rte, action) => {
            rte.insertHTML(action.btn.children[0].selectedOptions[0].value)
            document.getElementById('defaultPlaceholder').selected = true;
        },
    });

This will insert the value of the selected option at the cursors position and also displays a non selectable option as "Title", also after selecting a placeholder it resets the selected value to the default one, so that the before selected option won't be shown (because i only want to insert the placeholder and don't want it to be the "Title" of the dropdown)

lock[bot]November 15, 20190 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.