From dc0b3e1e6009f44fa8acfcf905faa9ef721eb7b2 Mon Sep 17 00:00:00 2001 From: Victor Miti Date: Tue, 20 Jul 2021 22:45:28 +0200 Subject: [PATCH] feat: add CustomRichTextBlock with customized richtext options --- .../config/settings/base.py | 23 +++++++++++++++++++ .../base/blocks.py | 23 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/config/settings/base.py b/{{cookiecutter.project_slug}}/config/settings/base.py index f593e25a..dabb1db3 100644 --- a/{{cookiecutter.project_slug}}/config/settings/base.py +++ b/{{cookiecutter.project_slug}}/config/settings/base.py @@ -268,3 +268,26 @@ MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +# Omitting the 'default' editor now leaves the original default editor intact, +# so it is no longer necessary to redefine 'default' when adding alternative editors. +WAGTAILADMIN_RICH_TEXT_EDITORS = { + "simple": { + "WIDGET": "wagtail.admin.rich_text.DraftailRichTextArea", + "OPTIONS": { + "features": [ + "bold", + "italic", + "ol", + "ul", + "link", + "superscript", + "subscript", + "strikethrough", + "blockquote", + "hr", + "code", + ] + }, + } +} diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/base/blocks.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/base/blocks.py index 42b85f0b..930fc8ff 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/base/blocks.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/base/blocks.py @@ -78,7 +78,28 @@ class BaseStreamBlock(StreamBlock): icon="media", template="blocks/embed_block.html", ) - table = TableBlock(template="blocks/table_block.html",) + table = TableBlock( + template="blocks/table_block.html", + ) class Meta: required = False + + +class CustomRichTextBlock(StreamBlock): + """ + Define the custom blocks that `StreamField` will utilize + """ + + paragraph_block = RichTextBlock( + icon="fa-paragraph", template="blocks/paragraph_block.html", editor="simple" + ) + + def get_api_representation(self, value, context=None): + # this hack based on https://github.com/wagtail/wagtail/issues/2695#issuecomment-457392434 + api_representation = super().get_api_representation(value, context) + # we don't want the 'id + key_to_be_deleted = "id" + api_representation.pop(key_to_be_deleted, None) + api_representation["paragraph_block"] = str(value["paragraph_block"]) + return api_representation