Editable Cell
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?
I have same issue, Waiting for the answer.... #1257
You can add a custom tag in td
I guess a p tag should work but I don't know how to add it automatically.
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 Thanks for that solution. It's working
Thank you @artf ! It works well for me too
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?
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
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>');
}
});`
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
Can't figure out how to wrap the content in a text wrapper using your code. Can you explain, please?
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?