【问题标题】:Wagtail RichTextBlock extend to pick colorWagtail RichTextBlock 扩展以选择颜色
【发布时间】:2020-01-21 18:29:00
【问题描述】:

我正在尝试在 wagtail RichTextBlock 中实现颜色选择器选项,以便我可以为所选文本选择任何颜色。

这是我的wagtail_hooks.py 文件:

@hooks.register("register_rich_text_features")
def register_colortext_feature(features):
    """Creates centered text in our richtext editor and page."""

    # Step 1
    feature_name = "Text Color Picker"
    type_ = "TEXTCOLOR"
    tag = "span"

    # Step 2
    control = {
        "type": type_,
        "label": "color",
        "description": "Color Text",
        # "style": {
        #     "display": "block",
        #     "text-align": "center",
        # },
    }

    # Step 3
    features.register_editor_plugin(
        "draftail", feature_name, draftail_features.InlineStyleFeature(control)
    )

    # Step 4
    db_conversion = {
        "from_database_format": {tag: InlineStyleElementHandler(type_)},
        "to_database_format": {
            "style_map": {
                type_: {
                    "element": tag,
                    "props": {
                        "style": "color:colorcode"
                    }
                }
            }
        }
    }

    # Step 5
    features.register_converter_rule("contentstate", feature_name, db_conversion)

    # Step 6, This is optional.
    features.default_features.append(feature_name)

有没有人可以帮助我解决这个问题?我只是尽力做到这一点,但没有运气。

【问题讨论】:

  • 你的代码到底哪里出错了?请包含正在发生的错误或意外行为,以便我们更轻松地诊断出发生了什么。
  • 代码没有破解,只是遇到了麻烦
  • 您必须更具体地说明您遇到的问题,以便我们理解和帮助您。

标签: wagtail wagtail-streamfield wagtail-snippet wagtail-pageurl wagtail-search


【解决方案1】:

您能否在模型或流块中添加一个字符域或字符块,在其中输入十六进制值并在模板中读取字段值并将文本颜色设置为该值?

【讨论】:

  • 我不明白你的意思,能给我举个例子吗?
  • 在你的models.py中:class MyModel(): text_color = blocks.CharBlock(help_text="输入你想要的文本颜色的十六进制值") text = blocks.RichTextBlock()在 html:
    {{value.text}}
    语法可能有点偏离,但请查看文档进行验证。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 2016-03-10
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
相关资源
最近更新 更多