grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Editable Cell

Open algodata44 opened this issue 8 years ago • 12 comments

Hello, I am using GrapesJS for newsletters (with the gjs-preset-newsletter plugin) and i have a problem when I import code. I can't edit the boxes of type 'cell' (tds, trs...). I read it is a parameter and try to change it, unsuccessfully. Is there a way to do it without changing the source code?

algodata44 avatar Jul 05 '18 13:07 algodata44

I have same issue, Waiting for the answer.... #1257

Shah-ali avatar Jul 06 '18 06:07 Shah-ali

You can add a custom tag in td

ionic666 avatar Jul 10 '18 03:07 ionic666

I guess a p tag should work but I don't know how to add it automatically.

algodata44 avatar Jul 10 '18 06:07 algodata44

You should wrap the text in some other tag. If you can't change the content you should probably have to extend the original component Eg. inside isComponent make some kind of change

isComponent(el) {
      let result = '';
      const tag = el.tagName;

      if (tag == 'TD' || tag == 'TH') {
        result = {
          type: 'cell',
          tagName: tag.toLowerCase()
        };

		if ( /* eg. el has one textnode element  */ ) {
			result.components = `<div>${el.innerText}</div>`
		}
      }

      return result;
    }

artf avatar Jul 10 '18 23:07 artf

@artf Thanks for that solution. It's working

Shah-ali avatar Jul 11 '18 06:07 Shah-ali

Thank you @artf ! It works well for me too

algodata44 avatar Jul 11 '18 08:07 algodata44

Thanks for your solution. I also have a cell which I'm not able to edit.

<tr data-gjs-type="row" data-highlightable="1"><td data-gjs-type="cell" data-highlightable="1" class="gjs-comp-selected">Text To Edit</td></tr>

I've tried extending the cell component, but isComponent is never executed. I've extended it as follows:

 this.comps = this.editor.DomComponents;

  var defaultType = this.comps.getType('default');    
  var defaultModel = defaultType.model;
  var defaultView = defaultType.view;
       
    this.comps.addType('cell', {
        model: defaultModel.extend({
            isComponent: function (el) {
            }
        }),
        // Define the View
        view: defaultView
    });

What can be the issue please?

markbartolo avatar Apr 05 '19 09:04 markbartolo

const components = editor.DomComponents;
const text = components.getType('text');
components.addType('cell', {
  model: text.model.extend({
    defaults: Object.assign({}, text.model.prototype.defaults, {
      type: 'cell',
      tagName: 'td',
      draggable: ['tr'],
    }),
  },
  {
    isComponent(el) {
      let result = '';
      const tag = el.tagName;
      if (tag == 'TD' || tag == 'TH') {
        result = {
          type: 'cell',
          tagName: tag.toLowerCase()
        };
      }
      return result;
    }
  }),
  view: text.view,
});

// Following code prevents cell cloning on enter pressed (jQuery)
const iframeBody = editor.Canvas.getBody();
$(iframeBody).on("keydown", "[contenteditable]", e => {
  if (e.keyCode === 13) {
      e.preventDefault();
      e.target.ownerDocument.execCommand("insertHTML", false, "<br/><br/>");
      return false;
  }
});

... Cell becomes not editable after page reload

radiocity avatar May 20 '19 19:05 radiocity

Found a dirty workaround if you have jQuery available, just put this code before editor.init, sadly just work if you use "fromElement: true" :

`
// Workaround making all td and tr editables $('td, th').each(function(k, item) {

      // Avoid loop elements and parent td/th elems
      if ($(item).find('td').length == 0 && $(item).find('div.gjs-text-container').length == 0) {
        var html = $(item).html();
        $(item).html('<div class="gjs-text-container">'+html+'</div>');
      }

    });`

mdrbx avatar Jun 02 '20 17:06 mdrbx

my solution:

editor.Components.addType('cell', {
    isComponent(el) {
      if (!(['td', 'th'].indexOf(el.tagName?.toLowerCase()) >= 0)) {
        return false;
      }

      const allChildNodes: Node[] = [];
      function collectChildNodes(arr: Node) {
        const { childNodes } = arr;
        if (childNodes.length > 0) {
          for (let index = 0; index < childNodes.length; index += 1) {
            const arrayChildNode = childNodes[index];
            if (arrayChildNode.childNodes?.length > 0) {
              collectChildNodes(arrayChildNode);
            } else {
              allChildNodes.push(arrayChildNode);
            }
          }
        } else {
          allChildNodes.push(arr);
        }
      }
      collectChildNodes(el);

      return !allChildNodes.every(
        (item) => item.nodeType === 3 || item.nodeName?.toLowerCase() === 'br',
      );
    },
  });

collect all childNodes, if all childNode is textNode or br element,I think it is not a td, back to a Text Wrapper and the user can edit the content

cody1991 avatar Apr 10 '23 02:04 cody1991

@cody1991

Can't figure out how to wrap the content in a text wrapper using your code. Can you explain, please?

ameerElsherif avatar May 15 '24 11:05 ameerElsherif

I created a plugin to manage the case of table cell containing only text

export function pluginTableCell(editor) {
    const comps = editor.DomComponents;

    const defaultType = comps.getType('cell');

    comps.addType('table-cell', {
        isComponent: el => {
            if (el.tagName === 'TD') {
                const isPlainText = !el.querySelector('*') && el.textContent.trim().length > 0;
                if (isPlainText) {
                    el.innerHTML = '<p>' + el.innerHTML + '</p>';
                }

                return { type: 'table-cell' };
            }
        },
        view: defaultType.view
    });
}

it only checks for <td> but can easily be extended if needed

i am not sure about the view property, is it needed?

allphat avatar Jul 10 '24 14:07 allphat