在 Wagtail 2.0 中为 Draftail 添加上标 [英] Adding superscript to Draftail in Wagtail 2.0

查看:29
本文介绍了在 Wagtail 2.0 中为 Draftail 添加上标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Wagtail 中的新编辑器添加上标.

I'm trying to add superscript to the new editor in Wagtail.

我在这里看到文档:http://docs.wagtail.io/en/v2.0/advanced_topics/customisation/extending_draftail.html

我应该在哪里添加示例代码?我是否正确假设我将能够将示例从 feature_name = 'strikethrough'type_ = 'STRIKETHROUGH' 更改为 superscript> 它会起作用吗?

Where am I supposed to add the example code? And am I correct in assuming that I will be able to just change the example from feature_name = 'strikethrough' and type_ = 'STRIKETHROUGH' to superscript and it will work?

一旦注册,我是否必须修改我必须将其包含在 features 设置中的每个 RichTextField,或者有没有办法将其添加到我的应用程序中的所有 RTF 中?

Once this is registered, do I have to modify each RichTextField that I have to include it in the features setting, or is there a way to add this to all RTF in my application?

推荐答案

我相信我已经想出了如何做到这一点,希望如果有更好的方法,有人会纠正我!

I believe I have figured out how to do this, hopefully, someone will correct me if there is a better way!

  1. 在您注册的应用目录之一(在 INSTALLED_APPS 中)创建一个名为 wagtail_hooks.py 的文件.
  2. 在文件中加入以下代码:

  1. Create a file in one of your registered (in INSTALLED_APPS) app directories called wagtail_hooks.py.
  2. Put the following code in the file:

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

@hooks.register('register_rich_text_features')
def register_strikethrough_feature(features):
    feature_name = 'superscript'
    type_ = 'SUPERSCRIPT'
    tag = 'sup'

    control = {
        'type': type_,
        'label': '^',
        'description': 'Superscript',
    }

    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.default_features.append(feature_name)
    features.register_converter_rule('contentstate', feature_name, db_conversion)

  • features.default_features.append(feature_name) 回答了我问题的最后一部分 - 文档中没有(嗯,它在那里,但不在这个上下文中)).这会将功能添加到所有 RichTextFields,而无需将 features=[] 设置添加到每个现有和/或新 RTF.

  • The line features.default_features.append(feature_name) is what answers the last part of my question - and is missing from the docs (well, it's there, but not in this context). This adds the feature to all RichTextFields without having to add the features=[] setting to each existing and/or new RTF.

    要修改它以与另一个内置 Draftail 功能一起使用,请修改 feature_nametype_taglabeldescription 字段.Draftail 支持以下类型:

    To modify this to work with another built in Draftail feature, modify the feature_name, type_, tag, label, and description fields. Draftail supports the following types:

    • 区块类型:H1、H2、H3、H4、H5、H6、Blockquote、Code、UL、OL、P
    • 内联样式:粗体、斜体、下划线、代码、删除线、标记、键盘、上标、下标
    • 还有人力资源部,BR

    粗体斜体h2h3h4ulolhrbr 已经在 RichTextField 的 Wagtail 默认设置中.

    With bold, italic, h2, h3, h4, ul, ol, hr, and br already being in the Wagtail default set for a RichTextField.

    这篇关于在 Wagtail 2.0 中为 Draftail 添加上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆