Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.

Commit 17da287

Browse files
committed
fix: fix iframe styles and height adjustments
1 parent 29ebfbd commit 17da287

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

render/components/Iframe.tsx

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, {
2-
useEffect, forwardRef, useCallback, MouseEventHandler
2+
useEffect, useCallback, MouseEventHandler, useState
33
} from 'react';
44
import { css } from '@emotion/react';
5+
import throttle from 'lodash/throttle';
56
import { useOvermindEffects } from '../store';
67

78
type IframeProps = {
@@ -10,7 +11,7 @@ type IframeProps = {
1011
html: string;
1112
width?: string;
1213
cssCode?: string;
13-
onResize?: () => void;
14+
onResize?: (iframe: HTMLIFrameElement | null) => void;
1415
}
1516

1617
const styles = {
@@ -20,27 +21,43 @@ const styles = {
2021
`
2122
};
2223

23-
const Iframe = forwardRef<HTMLIFrameElement, IframeProps>(({
24+
const Iframe = ({
2425
className, html, cssCode, onResize, name, width
25-
}: IframeProps, ref) => {
26+
}: IframeProps) => {
2627
const effects = useOvermindEffects();
28+
const [iframe, setIframe] = useState<HTMLIFrameElement | null>(null);
29+
const [isLoaded, setLoaded] = useState(false);
2730

28-
const handleResize = useCallback(() => {
29-
if (ref.current) {
30-
ref.current.height = ref.current.contentDocument.body.scrollHeight || 0;
31-
ref.current.width = width ? width : ref.current.contentDocument.body.scrollWidth || 0;
31+
const handleResize = useCallback(throttle(() => {
32+
if (iframe) {
33+
iframe.height = iframe.contentDocument.body.scrollHeight + 30;
34+
iframe.width = width ? width : iframe.contentDocument.body.scrollWidth || 0;
3235
}
3336

3437
if (onResize) {
35-
onResize();
38+
onResize(iframe);
3639
}
37-
}, [onResize, width]);
40+
}, 300), [onResize, width, html, iframe]);
3841

39-
useEffect(() => () => {
40-
window.removeEventListener('resize', handleResize);
41-
}, [handleResize]);
42+
const handleFocus = useCallback(() => {
43+
if (iframe) {
44+
iframe.contentWindow?.getSelection()?.empty();
45+
}
46+
}, [iframe]);
47+
48+
useEffect(() => {
49+
if (isLoaded) {
50+
handleResize();
51+
window.addEventListener('resize', handleResize);
52+
}
53+
54+
return () => {
55+
window.removeEventListener('resize', handleResize);
56+
};
57+
}, [isLoaded, handleResize]);
4258

4359
const onLoad = useCallback(() => {
60+
setLoaded(true);
4461
const interceptIframeInternalRedirect: MouseEventHandler<HTMLElement> = (e) => {
4562
e.preventDefault();
4663
e.stopPropagation();
@@ -57,37 +74,41 @@ const Iframe = forwardRef<HTMLIFrameElement, IframeProps>(({
5774
}
5875
};
5976

60-
if (ref.current) {
61-
for (const elem of ref.current.contentDocument?.body.querySelectorAll(['a', 'img', 'button', 'input']) ?? []) {
77+
if (iframe) {
78+
for (const elem of iframe.contentDocument?.body.querySelectorAll(['a', 'img', 'button', 'input']) ?? []) {
6279
elem.removeEventListener('click', interceptIframeInternalRedirect);
6380
elem.addEventListener('click', interceptIframeInternalRedirect);
6481
}
6582

83+
iframe.contentWindow?.addEventListener('blur', handleFocus);
84+
iframe.contentWindow?.onbeforeunload = () => {
85+
iframe.contentWindow?.removeEventListener('blur', handleFocus);
86+
};
87+
6688
if (cssCode) {
6789
const cssContainer = document.createElement('style');
6890
// styles.type = 'text/css';
6991
cssContainer.innerText = cssCode;
70-
ref.current.contentDocument?.head.appendChild(cssContainer);
92+
iframe.contentDocument?.head.appendChild(cssContainer);
7193
}
94+
7295
handleResize();
73-
window.addEventListener('resize', handleResize);
7496
}
75-
}, [cssCode, handleResize]);
97+
}, [cssCode, setLoaded, handleResize, handleFocus]);
7698

7799
return (
78100
<iframe
79101
css={styles.iframe}
80102
title={name}
81103
frameBorder="0"
82104
width={width}
83-
height={html ? undefined : 20}
84-
ref={ref}
105+
ref={setIframe}
85106
className={className}
86107
srcDoc={html}
87108
onLoad={onLoad}
88109
/>
89110
);
90-
});
111+
};
91112

92113
export {
93114
Iframe

render/components/MarkdownEditor.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,20 @@ type MarkdownTextProps = {
342342
}
343343

344344
const MarkdownText = ({ markdownText, name, className }: MarkdownTextProps) => {
345-
const iframeRef = createRef<HTMLIFrameElement>();
346345
const theme = useTheme() as typeof Theme;
347346

348347
const genericIframeStyles = `
349348
body {
350349
margin: 0;
351350
font-family: Roboto,-apple-system,BlinkMacSystemFont,'Helvetica Neue',Helvetica,sans-serif;
352-
font-size: 1rem;
351+
font-size: 14px;
353352
font-weight: 500;
354353
color: ${theme.normalText};
355354
}
356355
357356
a {
358357
color: ${theme.main};
359-
font-size: 1rem;
358+
font-size: 14px;
360359
padding: 2px;
361360
cursor: pointer;
362361
}
@@ -387,26 +386,15 @@ const MarkdownText = ({ markdownText, name, className }: MarkdownTextProps) => {
387386
}
388387
`;
389388

390-
const adjustIframeSize = () => {
391-
const iframe = iframeRef.current;
392-
if (iframe) {
393-
iframe.height = `${iframe.contentDocument?.body.scrollHeight || 0 + 35}`;
394-
}
395-
};
396-
397-
const throttledAdjustIframeSize = useCallback(throttle(adjustIframeSize, 300), []);
398-
399389
const markdown = xss(converter.makeHtml(markdownText));
400390

401391
return (
402392
<Iframe
403-
ref={iframeRef}
404393
className={className}
405394
width="100%"
406395
name={name}
407396
html={markdown}
408397
cssCode={genericIframeStyles}
409-
onResize={throttledAdjustIframeSize}
410398
/>
411399
);
412400
};

render/views/IssueDetailsPage.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ const IssueDetailsPage = () => {
123123
);
124124
}
125125

126-
console.log('currentIssue', JSON.stringify(currentIssue, null, 2));
127-
128126
return (
129127
<div
130128
css={styles.pageWrapper}

render/views/__tests__/IssueDetailsPage.spec.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import '@testing-library/jest-dom/extend-expect';
1212

1313
import { theme } from '../../theme';
14-
import IssueDetailsPage from '../IssueDetailsPage';
14+
import { IssueDetailsPage } from '../IssueDetailsPage';
1515
import { getInstance, reset, initialize } from '../../../common/request';
1616

1717
const mockStore = configureStore([thunk]);

0 commit comments

Comments
 (0)