kopia lustrzana https://github.com/wagtail/wagtail
implement media embeds
rodzic
d395b09849
commit
ff0e601c61
|
@ -213,6 +213,11 @@ class ImageElementHandler(AtomicBlockEntityElementHandler):
|
|||
return Entity('IMAGE', 'IMMUTABLE', {'altText': attrs.get('alt'), 'id': attrs['id'], 'format': attrs['format']})
|
||||
|
||||
|
||||
class MediaEmbedElementHandler(AtomicBlockEntityElementHandler):
|
||||
def create_entity(self, name, attrs, state, contentstate):
|
||||
return Entity('EMBED', 'IMMUTABLE', {'url': attrs['url']})
|
||||
|
||||
|
||||
ELEMENT_HANDLERS_BY_FEATURE = {
|
||||
'ol': {
|
||||
'ol': ListElementHandler('ordered-list-item'),
|
||||
|
@ -258,6 +263,9 @@ ELEMENT_HANDLERS_BY_FEATURE = {
|
|||
'image': {
|
||||
'embed[embedtype="image"]': ImageElementHandler(),
|
||||
},
|
||||
'embed': {
|
||||
'embed[embedtype="media"]': MediaEmbedElementHandler(),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -345,3 +345,26 @@ class TestHtmlToContentState(TestCase):
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
def test_media_embed(self):
|
||||
converter = ContentstateConverter(features=['embed'])
|
||||
result = json.loads(converter.from_database_format(
|
||||
'''
|
||||
<p>before</p>
|
||||
<embed embedtype="media" url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw" />
|
||||
<p>after</p>
|
||||
'''
|
||||
))
|
||||
self.assertContentStateEqual(result, {
|
||||
'blocks': [
|
||||
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'before', 'type': 'unstyled'},
|
||||
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
|
||||
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'after', 'type': 'unstyled'}
|
||||
],
|
||||
'entityMap': {
|
||||
'0': {
|
||||
'data': {'url': 'https://www.youtube.com/watch?v=Kh0Y2hVe_bw'},
|
||||
'mutability': 'IMMUTABLE', 'type': 'EMBED'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
Ładowanie…
Reference in New Issue