Merge Fields
Generally, to prevent losing selection on element click you use event.preventDefault() so it might your case, but I'm not sure. Try at least to create a reproducible demo, BTW I close the issue as it has nothing to do with the core itself
Read full answer below ↓Question
I have a question regarding merge fields, similar to https://github.com/artf/grapesjs/issues/481. I'm trying to have my merge field icon button toggle a modal with an inputdropdown, then after selecting an option from the dropdown, have the merge field insert itself.
rte.add('newMergeField', {
icon: `<button onclick="searchMergField(true)" id="newMergeFieldButton">Merge Field Search</button>`,
event: 'click',
result: rte => {
const { focusOffset, anchorOffset } = rte.doc.getSelection();
window.rteInsertHTML = res => {
const currentHtml = cloneDeep(rte.el.innerText);
const [start, end] = focusOffset < anchorOffset ? [focusOffset, anchorOffset] : [anchorOffset, focusOffset];
const [first, last] = [currentHtml.slice(0, start), currentHtml.slice(end, currentHtml.length - 1)];
rte.el.innerText = first + res + last;
}
},
});
^ I have something along these lines. Clicking the icon button simply stores a function in the window object to be called after I select an option from the inputdropdown. Now this actually works perfectly except for one problem. If I decide to hit "enter" multiple times at the very end of the text area and try to add a merge field at the very bottom, it'll instead add it to the beginning of the text field.
I believe this is because the Range object loses all of its offsets when I hit "enter". But if this is the case, how does simply calling rte.insertHTML() work inside of the built in event handler for the icon?
Is there anyway I can replica the behavior of rte.insertHTML() and have it insert at the very bottom of my text area after creating multiple new lines by hitting "enter"?
I've tried this with both the Selection object and the Range object (calling rte.doc.getSelection().getRangeAt(0)
Answers (1)
Generally, to prevent losing selection on element click you use event.preventDefault() so it might your case, but I'm not sure. Try at least to create a reproducible demo, BTW I close the issue as it has nothing to do with the core itself
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3245
grapesjs-custom-code doesn't execute <script> from GrapesJS 0.16.30 and up
grapesjs-custom-code work fine with script tag until GrapesJS 0.16.27, after this version, no script content is executed. ✔️ Editor init()...
Issue #3313
There are lots of click required to make text editable, can we make text field editable on a single Click.
WARNING READ and FOLLOW next 3 steps, then REMOVE them before posting the issueFollow CONTRIBUTING Guidelines https://github.com/artf/grape...
Issue #3668
Cant Reset Canvas after Moving/scaling it
WARNING READ and FOLLOW next 3 steps, then REMOVE them before posting the issueFollow CONTRIBUTING Guidelines https://github.com/artf/grape...
Issue #3564
Unable to refresh the iframe
0.17.19 Are you able to reproduce the bug from the demo? YES What is the expected behavior? I had asked a question regarding iframe refresh...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.