温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - How to apply styles to table and all cells in same time when applying it to table
ckeditor ckeditor5 javascript

javascript - 将样式应用于表格时,如何同时将样式应用于表格和所有单元格

发布于 2020-06-07 11:31:46

我必须修改Ckeditor 5表格插件,以便能够将样式同时应用于表格和所有单元格。 https://ckeditor.com/docs/ckeditor5/latest/api/table.html

有任何简单的方法吗?当前,我遍历表子项并应用相同的样式,但这不是正确的方法。因为该值不会在单元格的字段中更新。这是更改表属性时触发的一些代码。

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

更新资料

在此处输入图片说明

从照片中您可以看到,该字段未更新,与表格的真实颜色相对应。

什么是正确的方法?

查看更多

提问者
Dimitar Manovski
被浏览
7
Ozan Bellik 2020-03-25 03:59