我必须修改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);
});
})
}
}));
}
更新资料
从照片中您可以看到,该字段未更新,与表格的真实颜色相对应。
什么是正确的方法?
更新:
我认为,除了,您还希望setAttribute
(https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_view_downcastwriter-DowncastWriter.html#function-setAttribute)setStyle
。
似乎是从tableCell命令专门更新了单元格属性编辑模式:https : //github.com/ckeditor/ckeditor5-table/blob/ba852e31ef34132ac88a404e2df634667c33de7f/src/tablecellproperties/tablecellpropertiesui.js#L266
并从属性更新:https : //github.com/ckeditor/ckeditor5-table/blob/ba852e31ef34132ac88a404e2df634667c33de7f/src/tablecellproperties/commands/tablecellpropertycommand.js#L41 通过 https://github.com/ckeditor/ckeditor5-table /blob/ba852e31ef34132ac88a404e2df634667c33de7f/src/tablecellproperties/commands/tablecellpropertycommand.js#L103
原始答案:
如果对所有对象都应用相同的样式,那么如果您具有固定数量的样式,或者为什么要动态生成样式,则为什么不使用css?
А您熟悉CKEditor吗?这是用于网络的文本编辑器,具有一个插件系统。就我而言,我想更改现有插件的功能。
我以为它可以动态地修改
style
标签,但第二个想法是它不会覆盖已经设置在元素上的样式。因此,问题在于上述代码将样式向下应用到样式中,td
并且您希望它也将样式向下应用于其子元素吗?我更新了问题,并添加了图像以查看问题。.我将背景样式应用于表格单元格,之后,我将背景样式应用于表格和上面代码中的所有单元格,然后打开单元格设置,其中第一个应用且值是在该字段中未更新。
知道了,所做的更改已正确应用于文档,但是单元格属性模态未更新。我将更新我的答案。
更新了我的答案。