如何在将样式应用于 table 时同时将样式应用于 table 和所有单元格
How to apply styles to table and all cells in same time when applying it to table
我必须修改 Ckeditor 5 table 插件才能同时将样式应用于 table 和所有单元格。
https://ckeditor.com/docs/ckeditor5/latest/api/table.html
有什么简单的方法吗?
目前,我遍历 table children 并应用相同的样式,但这不是正确的方法。因为该值不会在单元格的字段中更新。
以下是 table 属性 更改时触发的一些代码。
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);
});
})
}
}));
}
更新
从照片中可以看出,table.
的真实颜色对应的字段没有更新
正确的做法是什么?
更新:
我想你也想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 5 table 插件才能同时将样式应用于 table 和所有单元格。 https://ckeditor.com/docs/ckeditor5/latest/api/table.html
有什么简单的方法吗? 目前,我遍历 table children 并应用相同的样式,但这不是正确的方法。因为该值不会在单元格的字段中更新。 以下是 table 属性 更改时触发的一些代码。
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);
});
})
}
}));
}
更新
从照片中可以看出,table.
的真实颜色对应的字段没有更新正确的做法是什么?
更新:
我想你也想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 如果您有固定数量的样式,或者如果您的样式是动态生成的则使用动态生成的样式标签?