-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwagtail_hooks.py
89 lines (73 loc) · 3.3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
from django.utils.html import format_html_join
from django.conf import settings
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 .rich_text import AnchorEntityElementHandler, anchor_entity_decorator, AnchorIndentifierEntityElementHandler, anchor_identifier_entity_decorator
@hooks.register('register_rich_text_features')
def register_rich_text_anchor_feature(features):
features.default_features.append('anchor')
"""
Registering the `anchor` feature, which uses the `ANCHOR` Draft.js entity type,
and is stored as HTML with a `<a data-anchor href="#my-anchor">` tag.
"""
feature_name = 'anchor'
type_ = 'ANCHOR'
control = {
'type': type_,
'label': '#',
'description': 'Anchor',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.EntityFeature(control)
)
features.register_converter_rule('contentstate', feature_name, {
# Note here that the conversion is more complicated than for blocks and inline styles.
'from_database_format': {'a[data-anchor]': AnchorEntityElementHandler(type_)},
'to_database_format': {'entity_decorators': {type_: anchor_entity_decorator}},
})
@hooks.register('insert_editor_js')
def insert_editor_js_anchor():
js_files = [
# We require this file here to make sure it is loaded before the other.
'wagtailadmin/js/draftail.js',
'wagtail_draftail_anchor.js',
]
js_includes = format_html_join('\n', '<script src="{0}{1}"></script>',
((settings.STATIC_URL, filename) for filename in js_files)
)
return js_includes
@hooks.register('register_rich_text_features')
def register_rich_text_anchor_identifier_feature(features):
features.default_features.append('anchor-identifier')
"""
Registering the `anchor-identifier` feature, which uses the `ANCHOR-IDENTIFIER` Draft.js entity type,
and is stored as HTML with a `<a data-anchor href="#my-anchor" id="my-anchor">` tag.
"""
feature_name = 'anchor-identifier'
type_ = 'ANCHOR-IDENTIFIER'
control = {
'type': type_,
'label': '<#id>',
'description': 'Anchor Identifier',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.EntityFeature(control)
)
features.register_converter_rule('contentstate', feature_name, {
# Note here that the conversion is more complicated than for blocks and inline styles.
# 'from_database_format': {'a[data-anchor][id]': AnchorIndentifierEntityElementHandler(type_)},
'from_database_format': {'a[data-id]': AnchorIndentifierEntityElementHandler(type_)},
'to_database_format': {'entity_decorators': {type_: anchor_identifier_entity_decorator}},
})
# @hooks.register('insert_editor_js')
# def insert_editor_js():
# js_files = [
# # We require this file here to make sure it is loaded before the other.
# 'wagtailadmin/js/draftail.js',
# 'wagtail_draftail_anchor.js',
# ]
# js_includes = format_html_join('\n', '<script src="{0}{1}"></script>',
# ((settings.STATIC_URL, filename) for filename in js_files)
# )
# return js_includes