Skip to content

Commit

Permalink
Merge pull request #39 from prezly/fix/qrf-113-embeds-vanish-in-edito…
Browse files Browse the repository at this point in the history
…r-in-published

[QRF-113] Fix - Iframely Embeds not visible on first page load
  • Loading branch information
e1himself authored Aug 29, 2022
2 parents 940b404 + b53ce50 commit 4378c44
Show file tree
Hide file tree
Showing 30 changed files with 52 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
.yalc/
.history
build/
coverage/
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_Elements_Gallery_Expanded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_Elements_Gallery_Full_Width.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_Elements_Image_Left_Aligned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_Elements_Image_Right_Aligned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_Elements_Image_Width_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_Elements_Image_Width_45.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Gallery_Expanded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Gallery_Full_Width.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Image_Left_Aligned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Image_Right_Aligned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Image_Width_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Image_Width_45.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Gallery_Expanded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Gallery_Full_Width.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Image_Left_Aligned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Image_Right_Aligned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Image_Width_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Image_Width_45.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 40 additions & 19 deletions src/components/HtmlInjection.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import React, { FunctionComponent, useEffect, useRef } from 'react';
import { useSyncedRef } from '@react-hookz/web';
import { injectHtmlMarkup } from '../lib';
import React, { HTMLAttributes, ScriptHTMLAttributes, useEffect } from 'react';

interface Props {
interface Props extends HTMLAttributes<HTMLDivElement> {
html: string;
id?: string;
className?: string;
onError: () => void;
}

export const HtmlInjection: FunctionComponent<Props> = (props) => {
const { html, className, id } = props;
const freshProps = useSyncedRef<Props>(props);
const ref = useRef<HTMLDivElement>(null);
export function HtmlInjection(props: Props) {
const { html, onError, ...attrs } = props;

useScripts(html, onError);

return <div {...attrs} dangerouslySetInnerHTML={{ __html: html }} />;
}

function useScripts(html: Props['html'], onError: Props['onError']) {
useEffect(() => {
if (ref.current) {
injectHtmlMarkup({
html,
onError: () => freshProps.current.onError(),
target: ref.current,
});
}
const container = document.createElement('div');
container.innerHTML = html;

const scripts: ScriptHTMLAttributes<HTMLScriptElement>[] = Array.from(
container.getElementsByTagName('script'),
).map((script) => {
return Array.from(script.attributes).reduce((agg, { name, value }) => ({ ...agg, [name]: value }), {});
});

container.remove();

scripts.forEach((attributes) => {
if (attributes.src && document.querySelector(`script[src="${attributes.src}"]`)) {
return;
}

const script = document.createElement('script');
setScriptAttributes(script, attributes);

script.addEventListener('error', onError);
document.body.appendChild(script);
});

iframely?.load();

return;
}, [html]);
}

return <div id={id} className={className} ref={ref} />;
};
function setScriptAttributes(script: HTMLScriptElement, attributes: ScriptHTMLAttributes<HTMLScriptElement>): void {
Object.entries(attributes).forEach(([name, value]) => script.setAttribute(name, value));
}
2 changes: 1 addition & 1 deletion src/components/Media/Media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function Media({ className, image, style, title }: Props) {
playsInline
style={style}
title={title}
webkit-playsinline
webkit-playsinline="true"
>
<source src={sourceWebm} type="video/webm" />
<source src={sourceMp4} type="video/mp4" />
Expand Down
4 changes: 4 additions & 0 deletions src/elements/Embed/IframelyEmbed.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
border: 1px solid $color-error;
border-radius: $border-radius-base;
}

blockquote {
border-left: none;
}
}
5 changes: 5 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Iframely {
load(): void;
}

declare const iframely: Iframely | undefined;
1 change: 0 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export { applyTransformations } from './applyTransformations';
export { describeNode } from './describeNode';
export { formatBytes } from './formatBytes';
export { identity } from './identity';
export { injectHtmlMarkup } from './injectHtmlMarkup';
export { noop } from './noop';
export { openWindow } from './openWindow';
export { stringifyNode } from './stringifyNode';
Expand Down
31 changes: 0 additions & 31 deletions src/lib/injectHtmlMarkup.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@

"include": ["./src"],
"exclude": ["node_modules"],
"files": ["./src/@types/scss.d.ts", "./src/@types/svg.d.ts"]
"files": ["./src/@types/scss.d.ts", "./src/@types/svg.d.ts", "./src/globals.d.ts"]
}

0 comments on commit 4378c44

Please sign in to comment.