skillbox/server/core/wagtail_hooks.py

91 lines
3.4 KiB
Python

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
from basicknowledge.models import BasicKnowledge
from books.models import ContentBlockSnapshot
# 1. Use the register_rich_text_features hook.
@hooks.register('register_rich_text_features')
def register_brand_feature(features):
"""
Registering the feature, which uses the `BRAND` Draft.js inline style type,
and is stored as HTML with a `<span class="brand">` tag.
"""
feature_name = 'brand'
type_ = 'BRAND'
# 2. Configure how Draftail handles the feature in its toolbar.
control = {
'type': type_,
'label': 'Grün',
'description': 'Grün',
'style': {
'color': '#17A887',
'font-weight': '600'
},
}
# 3. Call register_editor_plugin to register the configuration for Draftail.
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
# 4.configure the content transform from the DB to the editor and back.
db_conversion = {
'from_database_format': {'span[class="brand"]': InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: 'span class="brand""'}},
}
# 5. Call register_converter_rule to register the content transformation conversion.
features.register_converter_rule('contentstate', feature_name, db_conversion)
# 6. (optional) Add the feature to the default features list to make it available
# on rich text fields that do not specify an explicit 'features' list
features.default_features.append(feature_name)
@hooks.register('register_rich_text_features')
def register_secondary_feature(features):
"""
Registering the feature, which uses the `SECONDARY` Draft.js inline style type,
and is stored as HTML with a `<span class="secondary">` tag.
"""
feature_name = 'secondary'
type_ = 'SECONDARY'
# 2. Configure how Draftail handles the feature in its toolbar.
control = {
'type': type_,
'label': 'Blau',
'description': 'Blau',
'style': {
'color': '#078CC6',
'font-weight': '600'
},
}
# 3. Call register_editor_plugin to register the configuration for Draftail.
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
# 4.configure the content transform from the DB to the editor and back.
db_conversion = {
'from_database_format': {'span[class="secondary"]': InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: 'span class="secondary"'}},
}
# 5. Call register_converter_rule to register the content transformation conversion.
features.register_converter_rule('contentstate', feature_name, db_conversion)
# 6. (optional) Add the feature to the default features list to make it available
# on rich text fields that do not specify an explicit 'features' list
features.default_features.append(feature_name)
@hooks.register('construct_explorer_page_queryset')
def remove_page_types_from_menu(parent_page, pages, request):
return pages.not_type(ContentBlockSnapshot).not_type(BasicKnowledge).exclude(contentblock__user_created=True)