Editor Wagtail Draftail:将类添加到div

Editor Wagtail Draftail:将类添加到div,editor,wagtail,Editor,Wagtail,我想在draftail编辑器中添加一个自定义元素。添加div元素不是问题。但是我也想把我自己的类添加到div中 我该怎么做?这是我当前的代码: @hooks.register('register_rich_text_features') def register_infobox_feature(features): """ Registering the `mark` feature, which uses the `MARK` Draft.js inline style typ

我想在draftail编辑器中添加一个自定义元素。添加div元素不是问题。但是我也想把我自己的类添加到div中

我该怎么做?这是我当前的代码:

@hooks.register('register_rich_text_features')
def register_infobox_feature(features):
    """
    Registering the `mark` feature, which uses the `MARK` Draft.js inline style type,
    and is stored as HTML with a `<mark>` tag.
    """
    feature_name = 'infobox'
    type_ = 'div'
    tag = 'div'

    # 2. Configure how Draftail handles the feature in its toolbar.
    control = {
        'type': type_,
        'label': 'InfoBox',
        'description': 'Infobox',
        # This isn’t even required – Draftail has predefined styles for MARK.
        # 'style': {'textDecoration': 'line-through'},
    }

    # 3. Call register_editor_plugin to register the configuration for Draftail.
    features.register_editor_plugin(
        'draftail', feature_name, draftail_features.InlineStyleFeature(control)
    )

    # 4.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, 'props': {'class' : 'test'}}},
    }

    # 5. Call register_converter_rule to register the content transformation conversion.
    features.register_converter_rule('contentstate', feature_name, db_conversion)

    # 6. (optional) Add the feature to the default features list to make it available
    # on rich text fields that do not specify an explicit 'features' list
    features.default_features.append('infobox')
@hooks.register('register\u rich\u text\u features'))
def寄存器信息盒功能(功能):
"""
注册“mark”功能,该功能使用“mark”Draft.js内联样式类型,
并存储为带有``标记的HTML。
"""
功能名称='infobox'
类型='div'
标记='div'
# 2. 配置Draftail在其工具栏中处理功能的方式。
控制={
“类型”:类型,
“标签”:“信息框”,
'description':'Infobox',
#这甚至不是必需的–Draftail为MARK预定义了样式。
#'style':{'textdearching':'line through'},
}
# 3. 调用register_editor_plugin来注册Draftail的配置。
features.register\u编辑器\u插件(
“draftail”,功能名称,draftail功能。InlineStyleFeature(控件)
)
#4.配置从数据库到编辑器的内容转换。
db_转换={
'from_database_format':{tag:InlineStyleElementHandler(type_)},
'to_database_format':{'style_map':{type_uu:tag,'props':{'class':'test'},
}
# 5. 调用register\u converter\u rule来注册内容转换。
features.register\u converter\u规则('contentstate',feature\u名称,db\u转换)
# 6. (可选)将该功能添加到默认功能列表以使其可用
#在未指定显式“功能”列表的富文本字段上
features.default_ufeatures.append('infobox')
看起来新的块元素是可能的,但实体是不可能的。为什么?

今天也遇到了同样的问题,我在任何文档中都找不到解决方案,但我只是补充了以下内容:

tag = 'div class="myClass"'
它成功了

我正在发布我的全部功能,以防有人会寻找这样的解决方案:

import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.rich_text.converters.html_to_contentstate import InlineStyleElementHandler
from wagtail.core import hooks

# 1. Use the register_rich_text_features hook.
@hooks.register('register_rich_text_features')
def register_flashgreen_feature(features):
    """
    Registering the `mark` feature, which uses the `MARK` Draft.js inline style type,
    and is stored as HTML with a `<mark>` tag.
    """
    feature_name = 'FlashGreen'
    type_ = 'FG'
    tag = 'div class="FG"'

    # 2. Configure how Draftail handles the feature in its toolbar.
    control = {
        'type': type_,
        'label': 'Flash_Green',
        'description': 'Flash Green',
        'style': {'color': '#00F000'},
    }

    # 3. Call register_editor_plugin to register the configuration for Draftail.
    features.register_editor_plugin(
        'draftail', feature_name, draftail_features.InlineStyleFeature(control)
    )

    # 4.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}},
    }

    # 5. Call register_converter_rule to register the content transformation conversion.
    features.register_converter_rule(
        'contentstate', feature_name, db_conversion)

    # 6. (optional) Add the feature to the default features list to make it available
    # on rich text fields that do not specify an explicit 'features' list
    features.default_features.append('FlashGreen')
将wagtail.admin.rich_text.editors.draftail.features导入为draftail_features
从wagtail.admin.rich_text.converters.html_到_contentstate导入InlineStyleElementHandler
从wagtail.core导入钩子
# 1. 使用register_rich_text_features钩子。
@hooks.register('register\u rich\u text\u features')
def寄存器_flashgreen_功能(功能):
"""
注册“mark”功能,该功能使用“mark”Draft.js内联样式类型,
并存储为带有``标记的HTML。
"""
功能名称='FlashGreen'
类型='FG'
标记='div class=“FG”'
# 2. 配置Draftail在其工具栏中处理功能的方式。
控制={
“类型”:类型,
“标签”:“闪绿”,
'说明':'闪烁绿色',
'style':{'color':'#00F000'},
}
# 3. 调用register_editor_plugin来注册Draftail的配置。
features.register\u编辑器\u插件(
“draftail”,功能名称,draftail功能。InlineStyleFeature(控件)
)
#4.配置从数据库到编辑器的内容转换。
db_转换={
'from_database_format':{tag:InlineStyleElementHandler(type_)},
'to_database_format':{'style_map':{type_uu:tag},
}
# 5. 调用register\u converter\u rule来注册内容转换。
features.register\u转换器\u规则(
“内容状态”、功能名称、数据库转换)
# 6. (可选)将该功能添加到默认功能列表以使其可用
#在未指定显式“功能”列表的富文本字段上
features.default\u features.append('FlashGreen')