-
Notifications
You must be signed in to change notification settings - Fork 0
/
wagtail_hooks.py
237 lines (179 loc) · 7.76 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
import json
import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from django.utils.html import format_html, format_html_join
from wagtail.admin.rich_text import HalloPlugin
from wagtail.admin.rich_text.converters.html_to_contentstate import InlineStyleElementHandler
from wagtail.core import hooks
from wagtail.admin.rich_text.converters.editor_html import WhitelistRule
from wagtail.core.whitelist import allow_without_attributes, attribute_rule, check_url
from django.templatetags.static import static
from django.utils.safestring import mark_safe
from wagtail.core import hooks
from draftjs_exporter.dom import DOM
from wagtail.admin.rich_text.converters.html_to_contentstate import InlineEntityElementHandler
# Testing annotation replacement
@hooks.register('register_rich_text_features')
def register_annotation_feature(features):
features.default_features.append('annotate')
"""
Registering the `annotate` feature, which uses the `DANNOTATE` Draft.js entity type,
and is stored as HTML with a `<span data-annotation>` tag.
"""
feature_name = 'annotate'
type_ = 'ANNOTATION'
control = {
'type': type_,
'label': 'Lesa yfir',
'description': 'Færa leiðréttingar\ninn í skjalið',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.EntityFeature(
control,
js=['js/annotate-bundle.js'],
css={'all': ['css/grammar-annotation.css']}
)
)
features.register_converter_rule('contentstate', feature_name, {
# Note here that the conversion is more complicated than for blocks and inline styles.
'from_database_format': {'span[data-annotation]': AnnotationEntityElementHandler(type_)},
'to_database_format': {'entity_decorators': {type_: annotation_entity_decorator}},
})
# Testing annotation replacement
def annotation_entity_decorator(props):
"""
Draft.js ContentState to database HTML.
Converts the ANNOTATE entities into a span tag.
"""
return DOM.create_element('span', {
'data-annotation': props['annotation'],
}, props['children'])
class AnnotationEntityElementHandler(InlineEntityElementHandler):
"""
Database HTML to Draft.js ContentState.
Converts the span tag into a ANNOTATE entity, with the right data.
"""
mutability = 'MUTABLE'
def get_attribute_data(self, attrs):
"""
Take the ``annotation`` value from the ``data-annotation`` HTML attribute.
"""
return {
'annotation': attrs['data-annotation'],
}
@hooks.register('register_rich_text_features')
def register_remove_annotation_feature(features):
features.default_features.append('remove-annotation')
"""
"""
feature_name = 'remove-annotation'
type_ = 'RMVANN'
control = {
'type': type_,
'label': 'Ljúka',
'description': 'Ljúka yfirferð\nog eyða leiðréttingum',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.EntityFeature(
control,
js=['js/remove-anns-bundle.js'],
css={'all': ['css/grammar-annotation.css']}
)
)
features.register_converter_rule('contentstate', feature_name, {
# Note here that the conversion is more complicated than for blocks and inline styles.
'from_database_format': {'span[data-remove-annotation]': RemoveAnnotationElementHandler(type_)},
'to_database_format': {'entity_decorators': {type_: remove_annotations_decorator}},
})
# Testing annotation replacement
def remove_annotations_decorator(props):
"""
Draft.js ContentState to database HTML.
Converts the RMVANN entities into a span tag.
"""
return DOM.create_element('span', {
'data-remove-annotation': props['remove-annotation'],
}, props['children'])
class RemoveAnnotationElementHandler(InlineEntityElementHandler):
"""
Database HTML to Draft.js ContentState.
Converts the span tag into a RMVANN entity, with the right data.
"""
mutability = 'IMMUTABLE'
def get_attribute_data(self, attrs):
"""
"""
return {
'remove-annotation': attrs['data-remove-annotation'],
}
@hooks.register('register_rich_text_features')
def register_html_feature(features):
features.register_editor_plugin(
'hallo', 'html',
HalloPlugin(
name='hallohtml',
js=[],
)
)
features.register_converter_rule('editorhtml', 'html', [
WhitelistRule('blockquote', allow_without_attributes),
WhitelistRule('table', attribute_rule({'class': True})),
WhitelistRule('tr', attribute_rule({'class': True})),
WhitelistRule('th', attribute_rule({'class': True, 'colspan': True})),
WhitelistRule('td', attribute_rule({'class': True, 'colspan': True})),
WhitelistRule('tbody', attribute_rule({'class': True})),
WhitelistRule('thead', attribute_rule({'class': True})),
WhitelistRule('tfoot', attribute_rule({'class': True})),
WhitelistRule('caption', attribute_rule({'class': True})),
WhitelistRule('h1', attribute_rule({'class': True, 'id': True, 'data': True})),
WhitelistRule('h2', attribute_rule({'class': True, 'id': True, 'data': True})),
WhitelistRule('h3', attribute_rule({'class': True, 'id': True, 'data': True})),
WhitelistRule('h4', attribute_rule({'class': True, 'id': True, 'data': True})),
WhitelistRule('h5', attribute_rule({'class': True, 'id': True, 'data': True})),
WhitelistRule('a', attribute_rule({'class': True, 'href': True})),
WhitelistRule('div', attribute_rule({'class':True, 'id':True, 'data':True, 'style': True})),
WhitelistRule('canvas', attribute_rule({'class':True, 'id': True, 'height': True, 'width': True, 'data': True})),
WhitelistRule('iframe', attribute_rule({'src':True, 'id': True, 'type': True, 'width': True, 'height': True, 'frameborder': True, 'scrolling': True, 'style': True})),
])
features.default_features.append('html')
@hooks.register('insert_editor_js')
def editor_js():
return format_html("""
<script>
function initSlugAutoPopulate() {{
if (!$('body').hasClass('page-is-live')) {{
var now = new Date();
var d = date_format(now);
var slugFollowsTitle = false;
$('#id_title').on('focus', function() {{
/* slug should only follow the title field if its value matched the title's value at the time of focus */
var currentSlug = $('#id_slug').val();
var slugifiedTitle = cleanForSlug(this.value, true);
slugFollowsTitle = (currentSlug.replace(d, "") == slugifiedTitle);
}});
$('#id_title').on('keyup keydown keypress blur', function() {{
if (slugFollowsTitle) {{
var slugifiedTitle = d + cleanForSlug(this.value, true);
$('#id_slug').val(slugifiedTitle);
}}
}});
}}
}}
$(function(){{
$("input#url_to_copy").on("click", function () {{
$(this).select();
}});
}});
function pad_2(number) {{
return (number < 10 ? '0' : '') + number;
}}
function date_format(date) {{
return date.getFullYear() + '-' +
pad_2(date.getMonth()+1) + '-' +
pad_2(date.getDate()) + '-';
}}
registerHalloPlugin('hallohtml');
delete halloPlugins['halloreundo'];
// registerHalloPlugin('hallocorrect')
</script>
""")