I have to modify the Ckeditor 5 table plugin to be able to apply styles to table and all cells at the same time. https://ckeditor.com/docs/ckeditor5/latest/api/table.html
Any easy way to do this? Currently, I iterate over the table children and apply same style, but this is not the proper way. Because the value is not updated in fields for cells. Here is some code that is triggered when table property is changed.
export function downcastTableAttribute(conversion, modelAttribute, styleName) {
conversion.for('downcast').add(dispatcher => dispatcher.on(`attribute:${modelAttribute}:table`, (evt, data, conversionApi) => {
const {item, attributeNewValue} = data;
const {mapper, writer} = conversionApi;
if (!conversionApi.consumable.consume(data.item, evt.name)) {
return;
}
const table = [...mapper.toViewElement(item).getChildren()].find(child => child.is('table'));
if (attributeNewValue) {
writer.setStyle(styleName, attributeNewValue, table);
//Apply style to cell td too
table._children[0]._children.forEach(row => {
row._children.forEach(td => {
writer.setStyle(styleName, attributeNewValue, td);
});
})
//Apply style to cell td too
} else {
writer.removeStyle(styleName, table);
table._children[0]._children.forEach(row => {
row._children.forEach(td => {
writer.removeStyle(styleName, td);
});
})
}
}));
}
Update
From the photo you can see that the field is not update corresponding to the real color of the table.
What is right way to do this?
Update:
I think you also want to setAttribute
(https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_view_downcastwriter-DowncastWriter.html#function-setAttribute), in addition to setStyle
.
It looks like that cell property editing modal is updated specifically from tableCell commands: https://github.com/ckeditor/ckeditor5-table/blob/ba852e31ef34132ac88a404e2df634667c33de7f/src/tablecellproperties/tablecellpropertiesui.js#L266
And those are updated from attributes: https://github.com/ckeditor/ckeditor5-table/blob/ba852e31ef34132ac88a404e2df634667c33de7f/src/tablecellproperties/commands/tablecellpropertycommand.js#L41 via https://github.com/ckeditor/ckeditor5-table/blob/ba852e31ef34132ac88a404e2df634667c33de7f/src/tablecellproperties/commands/tablecellpropertycommand.js#L103
Original answer:
If you're applying the same style to all, why not use use css if you have a fixed number of styles or a dynamically generated style tag if your styles are dynamically generated?
Аre you familiar with CKEditor? This is a text editor for the web and has a plugin system. In my case, I want to change the functionality of an existing plugin.
I was thinking it would still work for you to dynamically modify a
style
tag, but on second thought it won't override styles already set on the elements. So the issue is that the above code applies styles down totd
and you want it to also apply styles down to its child elements?I updated the question and added image to see the problem.. I apply background style to table cell, after that I apply background style to table and all cells the code above, and after that I open cell setting where is first applied and value is not updated in the field.
Got it, the change is properly applied to the document but the cell properties modal is not updated. I'll update my answer.
Updated my answer.