Fix conversion between db-format attributes and contentState data for images

Draftail expects 'alt' and 'format' to become 'altText' and 'alignment'.

Also ensure that the image chooser continues to the alt-text/format selection stage.
This commit is contained in:
Matt Westcott 2017-12-14 17:16:26 +00:00 committed by Thibaud Colas
parent b0f8cd12b2
commit ce0aa1f716
3 changed files with 21 additions and 5 deletions

View file

@ -9,9 +9,12 @@ class ImageSource extends ModalSource {
}
parseData(imageData) {
this.onConfirmAtomicBlock(Object.assign({}, imageData, {
this.onConfirmAtomicBlock({
src: imageData.preview.url,
}));
altText: imageData.alt,
id: imageData.id,
alignment: imageData.format,
});
}
componentDidMount() {
@ -20,7 +23,7 @@ class ImageSource extends ModalSource {
// eslint-disable-next-line new-cap
window.ModalWorkflow({
url: imageChooser,
url: imageChooser + '?select_format=true',
responses: {
imageChosen: this.parseData,
},

View file

@ -340,7 +340,7 @@ class TestHtmlToContentState(TestCase):
],
'entityMap': {
'0': {
'data': {'alignment': 'left', 'altText': 'an image', 'id': '1'},
'data': {'alignment': 'left', 'altText': 'an image', 'id': '1', 'src': '/media/not-found'},
'mutability': 'IMMUTABLE', 'type': 'IMAGE'
}
}

View file

@ -7,6 +7,7 @@ from wagtail.admin.rich_text.converters.html_to_contentstate import AtomicBlockE
from wagtail.admin.rich_text.editors.draftail.features import EntityFeature
from wagtail.images import get_image_model
from wagtail.images.formats import get_image_format, get_image_formats
from wagtail.images.shortcuts import get_rendition_or_not_found
# Front-end conversion
@ -113,7 +114,19 @@ class ImageElementHandler(AtomicBlockEntityElementHandler):
to contentstate
"""
def create_entity(self, name, attrs, state, contentstate):
return Entity('IMAGE', 'IMMUTABLE', {'altText': attrs.get('alt'), 'id': attrs['id'], 'alignment': attrs['format']})
Image = get_image_model()
try:
image = Image.objects.get(id=attrs['id'])
image_format = get_image_format(attrs['format'])
rendition = get_rendition_or_not_found(image, image_format.filter_spec)
src = rendition.url
except Image.DoesNotExist:
src = ''
return Entity('IMAGE', 'IMMUTABLE', {
'src': src,
'altText': attrs.get('alt'), 'id': attrs['id'], 'alignment': attrs['format']
})
ContentstateImageConversionRule = {