Created
October 4, 2019 16:55
-
-
Save KalobTaulien/d17f5aa50d138f6b09eb03af86ce72f6 to your computer and use it in GitHub Desktop.
Wagtail/Draftail button links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Draftail hooks. | |
Adds a .css file to the /admin/ and register a Block Element for Draftail. | |
""" | |
import wagtail.admin.rich_text.editors.draftail.features as draftail_features | |
from wagtail.admin.rich_text.converters.html_to_contentstate import BlockElementHandler | |
from wagtail.core import hooks | |
from django.contrib.staticfiles.templatetags.staticfiles import static | |
from django.utils.html import format_html | |
@hooks.register('register_rich_text_features') | |
def register_button_section_feature(features): | |
"""Register the section of buttons with Draftail.""" | |
feature_name = 'button-block' | |
type_ = 'button-block' | |
tag = 'div' | |
control = { | |
'type': type_, | |
'label': ' ', | |
'description': 'Button Section', | |
'element': 'div', | |
'icon': 'icon icon-grip', | |
} | |
features.register_editor_plugin( | |
'draftail', feature_name, draftail_features.BlockFeature(control) | |
) | |
features.register_converter_rule('contentstate', feature_name, { | |
'from_database_format': {tag: BlockElementHandler(type_)}, | |
'to_database_format': {'block_map': {type_: {'element': 'div', 'props': {'class': 'rich-text-buttons'}}}}, | |
}) | |
# Auto append feature to Draftail | |
features.default_features.append('button-block') | |
# Register a custom css file for the wagtail admin. | |
@hooks.register('insert_global_admin_css', order=100) | |
def global_admin_css(): | |
"""Add /static/css/button-block.css to the Wagtail /admin/.""" | |
return format_html( | |
'<link rel="stylesheet" href="{}">', | |
static('css/button-block.css')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load wagtailcore_tags %} | |
<style> | |
.rich-text-buttons a { | |
display: inline-block; | |
border-radius: 3px; | |
border: 1px solid #007d7e; | |
} | |
</style> | |
{{ self.richtextfield|richtext }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment