wagtail_hooks.py
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
@hooks.register("register_rich_text_features")
def register_code_styling(features):
"""Add the <code> to the richtext editor."""
# Step 1
feature_name = "code"
type_ = "CODE"
tag = "code"
# Step 2
control = {
"type": type_,
"label": "</>",
"description": "Code"
}
# Step 3
features.register_editor_plugin(
"draftail", feature_name, draftail_features.InlineStyleFeature(control)
)
# Step 4
db_conversion = {
"from_database_format": {tag: InlineStyleElementHandler(type_)},
"to_database_format": {"style_map": {type_: {"element": tag}}},
}
# Step 5
features.register_converter_rule("contentstate", feature_name, db_conversion)
# Step 6 optional
# This will register this feature with all richtext editors by default
features.default_features.append(feature_name)
@hooks.register("register_rich_text_features")
def register_centertext_feature(features):
"""Creates centered text in our richtext"""
feature_name = "center"
type_ = "CENTERTEXT"
tag = "div"
control = {
"type": type_,
"label": "Center", # 라벨
"description": "Center Text", # 설명
"style": { # css
"display": "block",
"text-align": "center",
},
}
features.register_editor_plugin(
"draftail", feature_name, draftail_features.InlineStyleFeature(control)
)
db_conversion = {
"from_database_format": {tag: InlineStyleElementHandler(type_)},
"to_database_format": {
"style_map": {
type_: {
"element": tag,
"props": {
"class": "d-block text-center",
}
}
}
},
}
features.register_converter_rule("contentstate", feature_name, db_conversion)
features.default_features.append(feature_name)
control의 style은 Admin Page에서 보이는 RichEditor, db_conversion의 props는 Front-end 단계의 Style
'Back-End > Wagtail, Django' 카테고리의 다른 글
Wagtail Cache (0) | 2021.11.28 |
---|---|
Wagtail Recaptcha (0) | 2021.11.28 |
Wagtail API v2 Serializing Rich Text (0) | 2021.11.25 |
Wagtail API v2 Serializing a QuerySet (0) | 2021.11.25 |
Wagtail API v2 Custom Page Properties (0) | 2021.11.25 |