鹡鸰草稿段落样式
Wagtail draftail paragraph style
我希望将自定义 BlockFeature 添加到 wagtail 草稿编辑器,该编辑器可转换为具有特定 class 的段落标记。
<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',
},
},
},
},
})
这会正确保存到数据库并生成正确的页面标记,但是,当我在 wagtail admin 中打开页面时,草稿编辑器将其误认为是普通段落。
通过查看鹡鸰来源,我在 html_ruleset.py 中注意到了这一点:
Look for a rule matching an HTML element with the given name and attribute dict, and return the corresponding result object. If no rule matches, return None.
If multiple rules match, the one chosen is undetermined.
因为有一个内置的 'p' 标签处理程序,这是否使识别 'p class="margin-0"' 变得不可能?
如果能够在编辑器的每个段落上编写您想要的自定义 class 就好了。
是的,遗憾的是,规则集系统目前并未优先考虑更具体的规则,因此无法定义取代默认 <p>
规则的规则。这是一个开放的功能请求:https://github.com/wagtail/wagtail/pull/4527
但是,请注意选择器 p[margin-0]
是不正确的,因为这将匹配具有 margin-0
属性而不是 class
属性的 <p>
元素 - 它应该是 p[class="margin-0"]
.
我希望将自定义 BlockFeature 添加到 wagtail 草稿编辑器,该编辑器可转换为具有特定 class 的段落标记。
<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',
},
},
},
},
})
这会正确保存到数据库并生成正确的页面标记,但是,当我在 wagtail admin 中打开页面时,草稿编辑器将其误认为是普通段落。
通过查看鹡鸰来源,我在 html_ruleset.py 中注意到了这一点:
Look for a rule matching an HTML element with the given name and attribute dict, and return the corresponding result object. If no rule matches, return None. If multiple rules match, the one chosen is undetermined.
因为有一个内置的 'p' 标签处理程序,这是否使识别 'p class="margin-0"' 变得不可能?
如果能够在编辑器的每个段落上编写您想要的自定义 class 就好了。
是的,遗憾的是,规则集系统目前并未优先考虑更具体的规则,因此无法定义取代默认 <p>
规则的规则。这是一个开放的功能请求:https://github.com/wagtail/wagtail/pull/4527
但是,请注意选择器 p[margin-0]
是不正确的,因为这将匹配具有 margin-0
属性而不是 class
属性的 <p>
元素 - 它应该是 p[class="margin-0"]
.