Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed media in rich text fields using Draftail #90

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

glitchwolfe
Copy link

This was mainly reverse-engineered from Wagtail's existing Image chooser. I still think it needs some work before putting it in the hands of all users, but it is functional.

Some notes:

Signed URLs

  • Does not work with the boto3 backend if you are using signed urls. Since the JSON representation of the thumbnail and file URLs are saved directly to the database, this means their signed URLs will be saved once and never refreshed. If your URLs expire after a few days, this means your media links will break and need to be re-embedded in the editor.
  • My workaround for this in my local environment is to add two URLs to my app: /custommedia/thumbnails/<MEDIA_ID> and /custommedia/files/<MEDIA_ID>. These views redirect to the correct URL, and the alias can be relied upon to always be the same.
  • Since wagtailmedia only contains a admin_url hook into Wagtail (and not regular URLs), I am not certain of the best way to approach this without requiring all users to add these URLs to their project manually.

Styles

This code won't look the same as it did in my preview screenshots from my feature request.

I noticed this project doesn't have any CSS, and I didn't want to be the first to include it. I did this for the sake of keeping this PR simple - if there are existing Wagtail components we can use to improve things without custom HTML/CSS, I'm all for it (if there's a desire for anything specific from those screenshots, I'll happily go back in and port that code over as well).

The only styles I've added are an inline min height/width of 100px for the media file preview in the text editor. Everything else uses existing class names within Wagtail. Well, except for...

Icons

  • I'm currently using Wagtailfontawesome for the video/audio icons. I'm not sure what the best replacement would be for these, since wagtail doesn't provide any icons that would work well by default.
  • The button in the rich text editor uses the exact same icon as the "Embed" button. The same issue as above applies - I'm not sure what the best replacement would be here.

- Update tests for rich text chooser
- Address inconsistent tab/spacing issues
- Address more CI issues
- Properly sort imports
- For wagtail versions <2.5, use MediaEmbedHandler instead of EmbedHandler
- In Wagtail <2.5, specify embed type and handler explicitly for register_embed_type
Copy link
Member

@thibaudcolas thibaudcolas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! It’s quite a lot of work, and as you said there are a few caveats / rough edges, but getting this up and running locally it works as expected.

Signed URLs feels like the most problematic of all issues you’ve faced – but to be honest I’m not sure I understand why that’s not a problem regardless of your PR? Aren’t the URLs stored at the time of saving the media regardless?

  • For styles, I think further work is needed, but you’re right in wanting to keep this to a minimum. I think we could reuse Wagtail’s EmbedBlock for the rich text editor, and that might solve a few problems for us.
  • For icons, we should use SVGs (inline might be the best for the current purpose). That’s what Wagtail is going to switch to, which should make wagtailfontawesome redundant.

Finally, one important question – currently you have this as a single "Media" feature. Why not two separate "Audio" and "Video" features? I can see how it would be convenient to be able to say that a field should contain audio, or video, or both. It also seems like a good idea to keep it together for consistency with the rest of wagtailmedia, so not too sure what to think of this.

And finally – you’ve made those things block-level entities in Draftail. Would there ever be a case where it would be valuable to have these as inline entities (that can be inserted inside text)?


'autoplay': props.get('autoplay'),
'mute': props.get('mute'),
'loop': props.get('loop'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be better to just serialize all the props as a single JSON-encoded attribute, rather than have to store them as separate HTML attributes like this. Then you wouldn’t have to do the annoying True if attrs.get('autoplay') == 'true' else False conversion back the other way around.

Form for customizing media player behavior (e.g. autoplay by default)
prior to insertion into a rich text area
"""
autoplay = forms.BooleanField(required=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn’t ever recommend anyone make anything autoplay, although I know some people might. How can we make the presence of this field configurable?

if(data.autoplay) options.push("Autoplay");
if(data.mute) options.push("Mute");
if(data.loop) options.push("Loop");
const options_str = options.length ? options.join(", ") : "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to worry about right now but these strings should be translate-able. We can implement this similarly to Wagtail.

}
else if(data.type == 'audio'){
icon = React.createElement('span', { class:"icon icon-fa-music", 'aria-hidden':"true" });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Draftail supports providing icons as SVG so we don’t need font awesome for these at least.


return React.createElement('button',
{
class: 'MediaBlock WagtailMediaBlock '+data.type,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it looks like we should just reuse MediaBlock.

{ class:"MediaBlock__icon-wrapper", 'aria-hidden': "true" },
React.createElement('span', {}, icon)
),
React.createElement('img', { src: data.thumbnail }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an alt="" attribute to be valid HTML

[
React.createElement('span',
{ class:"MediaBlock__icon-wrapper", 'aria-hidden': "true" },
React.createElement('span', {}, icon)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn’t need aria-hidden if it’s already set on the icon.

{% if loop %}loop{% endif %}
controls>
</audio>
{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a stupid question but this project didn’t have anything like this to define the medias’ output before?

{% if autoplay %}autoplay{% endif %}
{% if loop %}loop{% endif %}
{% if thumbnail %}poster="{{thumbnail}}"{% endif %}>
<source src="{{file}}" data-title={{title}}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is data-title for?

ContentstateMediaConversionRule
)

features.default_features.append(feature_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think it should be enabled by default for all fields.

Base automatically changed from master to main March 3, 2021 17:33
@zerolab zerolab marked this pull request as draft July 28, 2021 15:21
@zerolab zerolab added this to the Version 2 milestone Feb 5, 2024
@ordigital
Copy link

Is this still in progress or it's already usable?

@zerolab
Copy link
Member

zerolab commented Apr 16, 2024

@ordigital this most definitely needs improvements. and bringing the code up-to-date with package and Wagtail changes

@ordigital
Copy link

@ordigital this most definitely needs improvements. and bringing the code up-to-date with package and Wagtail changes

But are there any working solutions for inserting wagtailmedia files to richtext editor?

@zerolab
Copy link
Member

zerolab commented Apr 16, 2024

But are there any working solutions for inserting wagtailmedia files to richtext editor?

No. Most people use this in StreamField with a rich text block followed by a media block, followed by any other block, be it rich text or custom.

@ordigital
Copy link

But are there any working solutions for inserting wagtailmedia files to richtext editor?

No. Most people use this in StreamField with a rich text block followed by a media block, followed by any other block, be it rich text or custom.

Thanks. So I guess Draftail integration feature has been completely dropped?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants