Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

fix(edusharing-asset): Fix handling of video embeds #362

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/frontend/plugins/edusharing-asset/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export function EdusharingAssetRenderer(props: {
}
}

// Video & audio
// Audio
const isEmbedThatNeedsToFetchContent =
detailsSnippet.includes('get_resource')
if (isEmbedThatNeedsToFetchContent) {
Expand All @@ -221,12 +221,31 @@ export function EdusharingAssetRenderer(props: {
'function get_resource(authstring)',
)

// Add iframe resizer script
const newEmbedHtml =
return {
html: appendIframeResizer(detailsSnippet),
renderMethod: 'iframe',
defineContainerHeight: false,
}
}

// Video
const isVideo: boolean = htmlDocument.querySelector('video') !== null
if (isVideo) {
// Add style overwrites
detailsSnippet =
detailsSnippet +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.contentWindow.min.js"></script>'
`
<style>
.edusharing_rendering_content_video_wrapper {
display: block !important;
}
.edusharing_rendering_content_video_wrapper video {
width: 100% !important;
}
</style>
`
return {
html: newEmbedHtml,
html: appendIframeResizer(detailsSnippet),
renderMethod: 'iframe',
defineContainerHeight: false,
}
Expand All @@ -238,9 +257,7 @@ export function EdusharingAssetRenderer(props: {
const isH5P = iframe && iframe.getAttribute('src')?.includes('h5p')
if (isH5P) {
return {
html:
detailsSnippet +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.contentWindow.min.js"></script>',
html: appendIframeResizer(detailsSnippet),
renderMethod: 'iframe',
defineContainerHeight: false,
}
Expand Down Expand Up @@ -280,9 +297,7 @@ export function EdusharingAssetRenderer(props: {
// Backup when content type could not be determined above -> Render in iframe with iframe-resizer.
// This will make sure <script> tags execute. They would not if using 'dangerously-set-inner-html'
return {
html:
detailsSnippet +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.contentWindow.min.js"></script>',
html: appendIframeResizer(detailsSnippet),
renderMethod: 'iframe',
defineContainerHeight: false,
}
Expand Down Expand Up @@ -375,6 +390,13 @@ function buildImageSnippet(image: HTMLImageElement): string {
`
}

function appendIframeResizer(htmlSnippet: string) {
return (
htmlSnippet +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.contentWindow.min.js"></script>'
)
}

// Only re-render if `srcDoc` prop changed. We do not want to re-render the Iframe every time when EdusharingAssetRenderer is re-rendered because the state within the iframe is lost.
const MemoizedIframeResizer = memo(
IframeResizer,
Expand Down
Loading