我希望将一个自定义的BlockFeature添加到wagtail草稿编辑器中,该编辑器将转换为带有特定类的段落标记。
<p>A normal paragraph</p>
<p class="margin-0">A special paragraph using my custom feature</p>这是我的尝试:
@hooks.register('register_rich_text_features')
def register_margin0_feature(features):
"""
Registering the `margin-0` feature, which uses the `blockquote` Draft.js block type,
and is stored as HTML with a `<p class="margin-0">` tag.
"""
feature_name = 'margin-0'
type_ = 'custom'
tag = 'p'
classname = "margin-0"
control = {
'type': type_,
'label': '❝',
'description': 'Paragraph with margin-0',
# Optionally, we can tell Draftail what element to use when displaying those blocks in the editor.
'element': 'blockquote',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.BlockFeature(control)
)
features.register_converter_rule('contentstate', feature_name, {
'from_database_format': {'p[margin-0]': BlockElementHandler(type_)},
'to_database_format': {
'block_map': {
type_: {
'element': tag,
'props': {
'class': 'margin-0',
},
},
},
},
})这将正确地保存到数据库中,并生成正确的页面标记,但是,当我在摇尾管理中打开页面时,草稿编辑器会将它错误为一个正常的段落。
通过摇尾源,我在html_ruleset.py中注意到了这一点:
查找与给定名称和属性dict匹配的HTML元素的规则,并返回相应的结果对象。如果没有匹配的规则,则不返回任何规则。如果多个规则匹配,则选择的规则未定。
由于有一个内置的'p‘标记处理程序,这是否使识别'p class=’边距-0‘变得不可能?
如果能够在编辑器的每个段落上编写您想要的自定义类,那就太好了。
发布于 2018-10-11 14:37:35
是的,不幸的是,规则集系统目前没有赋予更多特定规则优先权,因此无法定义取代默认<p>规则的规则。这是一个开放的特性请求:https://github.com/wagtail/wagtail/pull/4527
但是,请注意,选择器p[margin-0]是不正确的,因为这将匹配一个<p>元素与一个margin-0属性,而不是一个class属性--应该是p[class="margin-0"]。
https://stackoverflow.com/questions/52762486
复制相似问题