问问题
1554 次
1 回答
4
我遇到了同样的问题,并在Wagtail Slack支持频道上提问。我得到了注册一个新的富文本功能的建议。该文档显示了一个删除线示例。这里是粗体(强)和斜体(em):
import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.rich_text.converters.html_to_contentstate import (
InlineStyleElementHandler,
)
@hooks.register('register_rich_text_features')
def register_strong_feature(features):
"""
Registering the `strong` feature. It will render bold text with `strong` tag.
Default Wagtail uses the `b` tag.
"""
feature_name = 'strong'
type_ = 'BOLD'
tag = 'strong'
# Configure how Draftail handles the feature in its toolbar.
control = {
'type': type_,
'icon': 'bold',
'description': 'Bold',
}
# Call register_editor_plugin to register the configuration for Draftail.
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
# Configure the content transform from the DB to the editor and back.
db_conversion = {
'from_database_format': {tag: InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: tag}},
}
# Call register_converter_rule to register the content transformation conversion.
features.register_converter_rule('contentstate', feature_name, db_conversion)
<em>
以及带有标签的斜体:
@hooks.register('register_rich_text_features')
def register_em_feature(features):
"""
Registering the `em` feature. It will render italic text with `em` tag.
Default Wagtail uses the `i` tag.
"""
feature_name = 'em'
type_ = 'ITALIC'
tag = 'em'
control = {
'type': type_,
'icon': 'italic',
'description': 'Italic',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
db_conversion = {
'from_database_format': {tag: InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: tag}},
}
features.register_converter_rule('contentstate', feature_name, db_conversion)
指定富文本字段的功能。不要忘记删除旧的“粗体”和“斜体”:
from wagtail.core.fields import RichTextField
class FooPage(Page):
body = RichTextField(features=['strong', 'em'])
于 2018-07-02T12:47:40.980 回答