mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-19 02:21:39 +03:00
fix iframe
This commit is contained in:
parent
c777c3c71d
commit
9708f0dc0d
@ -4,7 +4,14 @@ import {
|
|||||||
useLazyIframe,
|
useLazyIframe,
|
||||||
} from '@toeverything/components/editor-core';
|
} from '@toeverything/components/editor-core';
|
||||||
import { styled } from '@toeverything/components/ui';
|
import { styled } from '@toeverything/components/ui';
|
||||||
import { FC, useRef } from 'react';
|
import {
|
||||||
|
FC,
|
||||||
|
ReactElement,
|
||||||
|
ReactNode,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
import { SCENE_CONFIG } from '../../blocks/group/config';
|
import { SCENE_CONFIG } from '../../blocks/group/config';
|
||||||
import { BlockPreview } from './BlockView';
|
import { BlockPreview } from './BlockView';
|
||||||
import { formatUrl } from './format-url';
|
import { formatUrl } from './format-url';
|
||||||
@ -88,18 +95,54 @@ const SourceViewContainer = styled('div')<{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const IframeContainer = styled('div')<{ show: boolean }>(({ show }) => {
|
|
||||||
return {
|
const LazyIframe = ({
|
||||||
height: '100%',
|
src,
|
||||||
display: show ? 'block' : 'none',
|
delay = 3000,
|
||||||
|
fallback,
|
||||||
|
}: {
|
||||||
|
src: string;
|
||||||
|
delay?: number;
|
||||||
|
fallback?: ReactNode;
|
||||||
|
}) => {
|
||||||
|
const [show, setShow] = useState(false);
|
||||||
|
const timer = useRef<number>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Hide iframe when the src changed
|
||||||
|
setShow(false);
|
||||||
|
}, [src]);
|
||||||
|
|
||||||
|
const onLoad = () => {
|
||||||
|
clearTimeout(timer.current);
|
||||||
|
timer.current = window.setTimeout(() => {
|
||||||
|
// Prevent iframe scrolling parent container
|
||||||
|
// Remove the delay after the issue is resolved
|
||||||
|
// See W3C https://github.com/w3c/csswg-drafts/issues/7134
|
||||||
|
// See https://forum.figma.com/t/prevent-figmas-embed-code-from-automatically-scrolling-to-it-on-page-load/26029/6
|
||||||
|
setShow(true);
|
||||||
|
}, delay);
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div
|
||||||
|
onMouseDown={e => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
}}
|
||||||
|
style={{ display: show ? 'block' : 'none', height: '100%' }}
|
||||||
|
>
|
||||||
|
<iframe src={src} onLoad={onLoad} />
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const SourceView: FC<Props> = props => {
|
export const SourceView: FC<Props> = props => {
|
||||||
const { link, isSelected, block, editorElement } = props;
|
const { link, isSelected, block, editorElement } = props;
|
||||||
const src = formatUrl(link);
|
const src = formatUrl(link);
|
||||||
|
// let iframeShow = useLazyIframe(src, 3000, iframeContainer);
|
||||||
const iframeContainer = useRef(null);
|
|
||||||
let iframeShow = useLazyIframe(src, 3000, iframeContainer);
|
|
||||||
const [currentView] = useCurrentView();
|
const [currentView] = useCurrentView();
|
||||||
const { type } = currentView;
|
const { type } = currentView;
|
||||||
if (src?.startsWith('http')) {
|
if (src?.startsWith('http')) {
|
||||||
@ -107,14 +150,8 @@ export const SourceView: FC<Props> = props => {
|
|||||||
<div style={{ display: 'flex' }}>
|
<div style={{ display: 'flex' }}>
|
||||||
<SourceViewContainer isSelected={isSelected} scene={type}>
|
<SourceViewContainer isSelected={isSelected} scene={type}>
|
||||||
<MouseMaskContainer />
|
<MouseMaskContainer />
|
||||||
<IframeContainer
|
|
||||||
onMouseDown={e => {
|
<LazyIframe src={src} fallback={'Loading……'}></LazyIframe>
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
}}
|
|
||||||
show={iframeShow}
|
|
||||||
ref={iframeContainer}
|
|
||||||
></IframeContainer>
|
|
||||||
</SourceViewContainer>
|
</SourceViewContainer>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user