Created
April 3, 2018 15:38
-
-
Save johnfraney/207004a78b4abd5615ba3a0bcb3cda53 to your computer and use it in GitHub Desktop.
Wagtail Draftail code block
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
import wagtail.admin.rich_text.editors.draftail.features as draftail_features | |
from wagtail.admin.rich_text.converters.html_to_contentstate import ( | |
BlockElementHandler, InlineStyleElementHandler | |
) | |
from wagtail.core import hooks | |
@hooks.register('register_rich_text_features') | |
def register_code_block_feature(features): | |
""" | |
Registering the `code-block` feature, which uses the `code-block` Draft.js block type, | |
and is stored as HTML with `<pre><code>` tags. | |
""" | |
feature_name = 'code-block' | |
type_ = 'code-block' | |
control = { | |
'type': type_, | |
'label': '{}', | |
'description': 'Code', | |
} | |
features.register_editor_plugin( | |
'draftail', feature_name, draftail_features.BlockFeature(control) | |
) | |
features.register_converter_rule('contentstate', feature_name, { | |
'from_database_format': { | |
'pre': BlockElementHandler(type_), | |
'code': InlineStyleElementHandler('CODE'), | |
}, | |
'to_database_format': { | |
'block_map': {'code-block': {'element': 'code', 'wrapper': 'pre'}} | |
}, | |
}) | |
features.default_features.append(feature_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment