From 69861453919bdc35eb808ba5300a219ff3e86661 Mon Sep 17 00:00:00 2001 From: Mikey Stengel Date: Fri, 1 Mar 2024 16:35:05 +0100 Subject: [PATCH 1/2] fix(images): Pixabay image resizing does not overflow --- .../plugins/edusharing-asset/renderer.tsx | 92 ++++++++++++++----- 1 file changed, 70 insertions(+), 22 deletions(-) diff --git a/src/frontend/plugins/edusharing-asset/renderer.tsx b/src/frontend/plugins/edusharing-asset/renderer.tsx index 78a0e11c..411de9c7 100644 --- a/src/frontend/plugins/edusharing-asset/renderer.tsx +++ b/src/frontend/plugins/edusharing-asset/renderer.tsx @@ -104,18 +104,31 @@ export function EdusharingAssetRenderer(props: { 'edusharing_rendering_content_footer { display: none;', ) - if ( - content?.mimeType === 'image/jpeg' && + const parser = new DOMParser() + const htmlDocument = parser.parseFromString(detailsSnippet, 'text/html') + + const image = getImageOrUndefined(htmlDocument) + + const isPixabayImage = + hasImageMimeType(content?.mimeType || '') && content?.node?.remote?.repository?.repositoryType === 'PIXABAY' - ) { - // Positions the button to the right, makes it smaller and removes bg - // color + padding + if (isPixabayImage) { + const imageSnippet = buildImageSnippet(image) + + // fetch the src link of the image (usually says "Zur Originalseite + // springen"). Note that not all pixabay images contain a source link. + const sourceLink = htmlDocument.querySelector( + '#edusharing_rendering_content_href', + ) + + // Positions the button to the left, makes it smaller and removes bg + // color + padding. const shrinkPixabaySourceButton = ` ` + const emptyStringOrJumpToSource = sourceLink + ? sourceLink.outerHTML + shrinkPixabaySourceButton + : '' + return { - html: detailsSnippet + shrinkPixabaySourceButton, + html: imageSnippet + emptyStringOrJumpToSource, renderMethod: 'dangerously-set-inner-html', defineContainerHeight: false, } } - const parser = new DOMParser() - const htmlDocument = parser.parseFromString(detailsSnippet, 'text/html') - - // Image - const image = htmlDocument.querySelector( - '.edusharing_rendering_content_wrapper > img', - ) - const isImageSnippet = image && !image.classList.contains('edusharing_rendering_content_preview') if (isImageSnippet) { // Create completely new element because patching the existing one is more work/error-prone - const imageSnippet = ` - ${image.getAttribute('alt')} - ` + const imageSnippet = buildImageSnippet(image) return { html: imageSnippet, renderMethod: 'dangerously-set-inner-html', @@ -256,6 +259,9 @@ export function EdusharingAssetRenderer(props: { return (
( + '.edusharing_rendering_content_wrapper > img', + ) ?? + htmlDocument.querySelector( + '.edusharing_rendering_content', + ) + + if (image && image.nodeName !== 'IMG') { + return undefined + } + + return image +} + +function buildImageSnippet(image: HTMLImageElement): string { + return ` + ${image.getAttribute('alt')} + ` +} + +const imageMimeTypes = [ + 'image/jpeg', + 'image/png', + 'image/gif', + 'image/webp', + 'image/svg+xml', + 'image/bmp', + 'image/tiff', +] + +function hasImageMimeType(mimeType: string): boolean { + return imageMimeTypes.includes(mimeType) +} + // 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, From 14c43335835487b91ac3da8d416efb39d609ec98 Mon Sep 17 00:00:00 2001 From: Mikey Stengel Date: Fri, 1 Mar 2024 16:38:14 +0100 Subject: [PATCH 2/2] chore(release): Release version 2.1.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7785bb2a..0bf4537a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@serlo/serlo-editor-for-edusharing", - "version": "2.1.4", + "version": "2.1.5", "private": true, "bugs": { "url": "https://github.com/serlo/serlo-editor-for-edusharing/issues"