-
Notifications
You must be signed in to change notification settings - Fork 13
/
rich_text.py
64 lines (53 loc) · 1.93 KB
/
rich_text.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
from draftjs_exporter.dom import DOM
from wagtail.admin.rich_text.converters.html_to_contentstate import InlineEntityElementHandler
def anchor_entity_decorator(props):
"""
Draft.js ContentState to database HTML.
Converts the ANCHOR entities into <a> tags.
"""
return DOM.create_element('a', {
'data-anchor': True,
# 'href': props['fragment'],
'href': '#{}'.format(props['fragment'].lstrip('#')),
}, props['children'])
class AnchorEntityElementHandler(InlineEntityElementHandler):
"""
Database HTML to Draft.js ContentState.
Converts the <a> tags into ANCHOR entities, with the right data.
"""
# In Draft.js entity terms, anchors are "mutable".
# We can alter the anchor's text, but it's still an anchor.
mutability = 'MUTABLE'
def get_attribute_data(self, attrs):
"""
Take the ``fragment`` value from the ``href`` HTML attribute.
"""
return {
'fragment': attrs['href'],
}
def anchor_identifier_entity_decorator(props):
"""
Draft.js ContentState to database HTML.
Converts the ANCHOR entities into <a> tags.
"""
return DOM.create_element('a', {
# 'data-anchor': True,
'data-id':props['fragment'].lstrip('#'),
'id':props['fragment'].lstrip('#'),
'href': '#{}'.format(props['fragment'].lstrip('#')),
}, props['children'])
class AnchorIndentifierEntityElementHandler(InlineEntityElementHandler):
"""
Database HTML to Draft.js ContentState.
Converts the <a> tags into ANCHOR IDENTIFIER entities, with the right data.
"""
# In Draft.js entity terms, anchors identifier are "mutable".
mutability = 'MUTABLE'
def get_attribute_data(self, attrs):
"""
Take the ``fragment`` value from the ``href`` HTML attribute.
"""
return {
'fragment': attrs['href'].lstrip('#'),
'data-id': attrs['id'],
}