grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Text component problem

Open gabrigcl opened this issue 8 years ago • 26 comments

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

gabrigcl avatar Jan 15 '18 14:01 gabrigcl

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?

ryandeba avatar Jan 18 '18 01:01 ryandeba

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.

gabrigcl avatar Jan 18 '18 11:01 gabrigcl

@gabrigcl I agree and think that Ryan's suggestion could be a solution (apart the fact insertHTML is not supported by IE)

artf avatar Jan 18 '18 13:01 artf

Yeah, I agree. Will you put this into roadmap?

gabrigcl avatar Jan 18 '18 13:01 gabrigcl

@gabrigcl I leave this issue as an opened bug so no need to put it into roadmap

artf avatar Jan 18 '18 13:01 artf

when use SHIFT + "Enter" to break line in text it will create <br/> tag instead of creating a new component maybe is rich text editor behavior (using default rte not using CKEditor).

richieteh94 avatar Jan 23 '18 02:01 richieteh94

when use SHIFT + "Enter" to break line in text it will create
tag instead of creating a new component maybe is rich text editor behavior (using default rte not using CKEditor).

WebKit's behavior

artf avatar Jan 24 '18 12:01 artf

Hi,

There should be a solution by listening on component:add and then convert these <div> to <br/>.

I need help on how to convert these in the event callback, please could you have a look ?

editor.on('component:add', (child) => {
   // Check if new component is a 'text'
   if (child.attributes.tagName === 'div' && child.attributes.type === 'text') {
      // Check if parent of this new component is also a "text"
      const parent = child.parent()
      if (parent.attributes.tagName === 'div' && parent.attributes.type === 'text') {
         // Now here, replace '<div>content</div>' to '<br/>content'
         // FIXME this doesn't work
         const t = child.getEl().innerHTML
         child.destroy()
         parent.getEl().innerHTML += '<br/>' + t
      }
   }
})

drasill avatar Jul 06 '18 10:07 drasill

I still struggle a lot with this issue.

Here is an idea, which is still not perfect, as it requires to rename the text component, and it removes empty lines :

var originalText = comps.getType('text')
comps.addType('text', {
   model: originalText.model.extend({
      defaults: Object.assign({}, originalText.model.prototype.defaults, {
         tagName: 'div',
         name: 'MyText',
         draggable: '*',
         droppable: false,
         attributes: { 'data-mytext': 'MyText' },
      }),
   }, {
      isComponent: function (el) {
         if (el.tagName === 'DIV' && el.getAttribute('data-mytext') === 'MyText') {
            const contentTexts = []
            Array.from(el.childNodes).forEach((node) => {
               if (node.textContent !== '') {
                  contentTexts.push(node.textContent)
               }
            })
            const content = contentTexts.join('<br/>')
            return { type: 'text', name: 'MyText', content: content, components: [] }
         }
      },
   }),
   view: originalText.view,
})

drasill avatar Jul 09 '18 16:07 drasill

Ok, this is simpler and seems to work, but I don't even know how / why.

var originalText = comps.getType('text')
comps.addType('text', {
   model: originalText.model.extend({
      defaults: Object.assign({}, originalText.model.prototype.defaults, {
         tagName: 'div',
         name: 'MyText',
         draggable: '*',
         droppable: false,
         attributes: { 'data-mytext': 'MyText' },
      }),
   }, {
      isComponent: function (el) {
         if (el.tagName === 'DIV' && el.getAttribute('data-mytext') === 'MyText') {
            return { type: 'text', name: 'MyText', content: el.innerHTML, components: [] }
         }
      },
   }),
   view: originalText.view,
})

drasill avatar Jul 09 '18 16:07 drasill

Hi drasil. Congratulations on your finding a solution, I'll test It. I've found a solution too, the same way, extending the text component

gabrigcl avatar Jul 09 '18 17:07 gabrigcl

I'll share it soon

gabrigcl avatar Jul 09 '18 17:07 gabrigcl

Any update for this?

v8jupiter avatar Jul 25 '18 15:07 v8jupiter

The new release https://github.com/artf/grapesjs/releases/tag/v0.14.33 improved a bit the TextComponent. It still creates new paragraphs but the editor will hide them from the selection. @gabrigcl from your original problem, one big issue that I guess you're doing there is, when you change the page, relying on HTML/CSS instead of the JSON and this is why are not editable after the change

artf avatar Sep 20 '18 21:09 artf

one big issue that I guess you're doing there is, when you change the page, relying on HTML/CSS instead of the JSON and this is why are not editable after the change

Yes, I was doing like this. However I'm currently using a custom text component, made by me, that doesn't have this issue.

gabrigcl avatar Sep 24 '18 17:09 gabrigcl

Great, for now, I close this

artf avatar Sep 27 '18 00:09 artf

one big issue that I guess you're doing there is, when you change the page, relying on HTML/CSS instead of the JSON and this is why are not editable after the change

Yes, I was doing like this. However I'm currently using a custom text component, made by me, that doesn't have this issue.

Any news on this? We are still having issues when reloading.

frasza avatar Dec 07 '18 08:12 frasza

@frasza your issue is not related https://github.com/artf/grapesjs/issues/1635#issuecomment-445242563

artf avatar Dec 07 '18 14:12 artf

@artf Not exactly, but it could be. I would much rather prefer my Text block to insert <br> instead of creating new <div>.

frasza avatar Dec 10 '18 12:12 frasza

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

Moikapy avatar Apr 17 '19 19:04 Moikapy