-
Notifications
You must be signed in to change notification settings - Fork 3
/
wagtail_hooks.py
76 lines (65 loc) · 2.18 KB
/
wagtail_hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from django.templatetags.static import static
from django.utils.html import format_html_join
from draftjs_exporter.dom import DOM
from wagtail.admin.rich_text.converters.html_to_contentstate import (
InlineEntityElementHandler,
)
from wagtail.core import hooks
@hooks.register("register_rich_text_features")
def register_placeholders_feature(features):
"""
Registering the `placeholders` feature, which uses the `PLACEHOLDERS` Draft.js
entity type, and is stored as HTML with a
`<placeholders id="">short-id</placeholders>` tag.
"""
feature_name = "placeholders"
type_ = "PLACEHOLDERS"
control = {
"type": type_,
"label": "<Tt>",
"description": "Template Tags"
}
features.register_editor_plugin(
"draftail",
feature_name,
draftail_features.EntityFeature(
control,
js = [
# make sure this loads first
'wagtailadmin/js/draftail.js',
'placeholders/js/placeholders.js'
]
),
)
features.register_converter_rule(
"contentstate",
feature_name,
{
"from_database_format": {
"placeholder[id]": PlaceholdersEntityElementHandler(type_)
},
"to_database_format": {
"entity_decorators": {
type_: placeholders_entity_decorator
}
},
},
)
def placeholders_entity_decorator(props):
"""
Draft.js ContentState to database HTML.
Converts the PLACEHOLDERS entities into a placeholder tag.
"""
return DOM.create_element("placeholder", {"id": props["placeholder"]}, props["children"])
class PlaceholdersEntityElementHandler(InlineEntityElementHandler):
"""
Database HTML to Draft.js ContentState.
Converts the placeholder tag into a PLACEHOLDERS entity, with the right data.
"""
mutability = "IMMUTABLE"
def get_attribute_data(self, attrs):
"""
Take the ``placeholder tag`` value from the ``id`` HTML attribute.
"""
return {"placeholder": attrs["id"]}