我已经在一个项目中将 Draft JS 实现为一个简单的编辑器,但是我在设置无序列表的样式时遇到了问题,特别是更改了项目符号的颜色以匹配文本颜色。
文档中似乎没有关于如何将样式应用于li
包装unordered-list-item
项目的信息。我可以选择文本并应用颜色,但这会产生如下编辑器状态:
{
"entityMap": {},
"blocks": [
{
"key": "bcci",
"text": "Heading",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 7,
"style": "red"
}
],
"entityRanges": []
},
{
"key": "28tv7",
"text": "One",
"type": "unordered-list-item",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 3,
"style": "yellow"
}
],
"entityRanges": []
},
{
"key": "85hig",
"text": "Two",
"type": "unordered-list-item",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 3,
"style": "red"
}
],
"entityRanges": []
},
{
"key": "6fkt5",
"text": "Three",
"type": "unordered-list-item",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 5,
"style": "red"
}
],
"entityRanges": []
},
{
"key": "ah3co",
"text": "End",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 3,
"style": "red"
}
],
"entityRanges": []
}
]
}
有没有人有经验/可以指出如何为子弹添加颜色的方向?
更新
经过一番调查并一遍又一遍地阅读文档后,我能够通过添加自定义blockStyleFn
并将自定义类添加到li
块中来达到预期的结果:
_getBlockStyle (block) {
const blockStyles = [];
const styleMap = Object.keys(colorStyleMap);
switch (block.getType()) {
case 'unordered-list-item':
// With draft JS we cannot output different styles for the same block type.
// We can however customise the css classes:
block.findStyleRanges((item) => {
const itemStyles = item.getStyle();
return _.some(styleMap, (styleKey) => itemStyles.includes(styleKey));
}, (startCharacter) => {
if (startCharacter === 0) {
// Apply the same styling to the block as the first character
_.each(block.getInlineStyleAt(startCharacter).toArray(), (styleKey) => {
blockStyles.push(`block-style-${styleKey}`);
});
}
});
return blockStyles.join(' ');
default:
return null;
}
}
这也需要为块编写额外的 css 类以匹配颜色块的样式(例如.block-style-yellow { color: rgb(180, 180, 0, 1.0) }
)。
此小提琴中提供了此工作的示例