Text component problem
Question
Hi. I wish a text component that do not create new components every time I press "Enter" key to create new paragraphs (this is a bad usability). In addition, the actual text component from the core of grapes has a problem (described in the screencast below). Thanks in advance for your attention!

Answers (3)
Not sure if I found this code in an Issue on here or on Stack Overflow, but this code here replaces the Divs with a <br/> when you press the enter key. I placed this code in the same file as my grapes-config
var iframeBody = editor.Canvas.getBody();
$(iframeBody).on("keydown", "[contenteditable]", e => {
// trap the return key being pressed
if (e.keyCode === 13) {
e.preventDefault();
// insert 2 br tags (if only one br tag is inserted the cursor won't go to the next line)
e.target.ownerDocument.execCommand("insertHTML", false, "<br><br>");
// prevent the default behaviour of return key pressed
return false;
}
});
I looked into this myself a few days ago...apparently there's an insertBrOnReturn option for execCommand, but the browser support is terrible. A solution like this is probably better.
@artf How would you want this to change? Should hitting enter insert <br> tags as shown in the top answer on the that SO discussion? Would you want to expose this as a configuration option?
I've found a solution to my needs using CKEditor and overriding the native "text" component creating my own. My text component now is a div with a css class that identifies the component:
comps.addType('text', {
model: textModel.extend({
defaults: Object.assign({}, textModel.prototype.defaults, {
tagName: 'div',
name: 'Texto',
draggable: '*',
droppable: false
}),
},
{
isComponent: function(el) {
if(el.tagName == 'DIV' && el.classList.contains('txt')){
return {type: 'text'};
}
},
})
/*...*/
Now, typing ENTER does not create a new component. Nevertheless, this solution do not solve this problem, for those who don't want to use third party text editors.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2656
[BUG] Wrong position of toolbar when duplicate children component
Hi, you can check this example: https://codepen.io/abozhinov/pen/XWbqjEJ Steps to reproduce the problem:If components existing delete them...
Issue #2771
[BUG] component with textable:1 not working since version 0.16.12
Hello, I've just updated to the lasted version 0.16.12 and have a bug when trying to move a component that has the property textable:1 into...
Issue #4847
BUG: Css edits aren't persisted to the DOM
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome Version 109.0.5414.87 Describe the bu...
Issue #2681
[BUG]: destroy() method didn't fully remove events
version: latest: v-0.16.3. important: update from v-0.15.9 to v-0.16.3 while keep everything same, the error will show in console: This err...
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.