[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:
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)
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)
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 #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 #1669
[Question] Custom link modal doesn't update the current text but add another text with link
Hello Artur, I'm trying to have a custom modal when clicked link button in RTE. but it doesn't convert selected text to link but what it do...
Issue #1150
[Question] Get Uploads as Base64
Hello All, I need help integrating this into a web app that I have. Everything renders and works as expected but I am trying to get the upl...
Issue #1966
[QUESTION] Loop over all components
Hi all, I want to create a socket for every button that I add to the canvas. This is working just fine. However, when I reload (using stora...
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.