Warm tip: This article is reproduced from stackoverflow.com, please click
ckeditor ckeditor5 javascript

How to apply styles to table and all cells in same time when applying it to table

发布于 2020-06-06 12:25:42

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

enter image description here

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?

Questioner
Dimitar Manovski
Viewed
28
Ozan Bellik 2020-03-25 03:59

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?