Issue #761Opened January 15, 2018by gabrigcl7 reactions

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! gif-screen-grapes

Answers (3)

MoikapyApril 17, 20194 reactions

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

grapesExample

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;
		}
	});
ryandebaJanuary 18, 20182 reactions

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?

gabrigclJanuary 18, 20181 reactions

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.

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.