如何扩展Wagtail图像格式模态?

问题描述 投票:0回答:2

我们有一个内容编辑想要的用例

  1. 右对齐图像,使文本环绕图像并且
  2. 设置图像宽度,使其在文章中能够很好地缩放。

虽然富文本编辑器允许我们选择右对齐格式的图像,但没有指定图像宽度的选项。

如何自定义 Wagtail“选择格式”模式来添加图像宽度的新字段并在渲染输出中使用它?我希望避免创建额外的图像格式以及可能的图像对齐/宽度排列,因为这是两个独立的属性。

现有技术

请参阅 WordPress 古腾堡图像设置 下面的 UI 了解现有技术。

wagtail wagtail-admin
2个回答
1
投票

文档:https://docs.wagtail.org/en/stable/advanced_topics/images/changing_rich_text_representation.html#changing-rich-text-representation

# image_formats.py
from wagtail.images.formats import Format, register_image_format


class SubclassedImageFormat(Format):

    def image_to_html(self, image, alt_text, extra_attributes=None):
        original_html = super().image_to_html(image, alt_text, extra_attributes)
        return original_html.replace('class="', 'class="foo')


register_image_format(
    SubclassedImageFormat('subclassed_format', 'Subclassed Format', classnames, filter_spec)
)

-1
投票

全部大写的答案是错误的。 Brylie Christopher Oxley 明确表示:“我希望避免创建额外的图像格式以及可能的图像对齐/宽度排列,因为这是两个独立的属性。”我有同样的问题。如何添加额外的 css 类选择字段,而不是为每个可能的尺寸和对齐组合乘以格式?

© www.soinside.com 2019 - 2024. All rights reserved.