|
| 1 | +import React, { useRef } from 'react' |
| 2 | +import { uniqueId } from 'lodash' |
| 3 | + |
| 4 | +import styles from '../style/components/IFrame.module.css' |
| 5 | +interface Props { |
| 6 | + src: string |
| 7 | + onPageChanged: (page: string, hash: string) => void |
| 8 | + onHashChanged: (hash: string) => void |
| 9 | +} |
| 10 | + |
| 11 | +export default function IFrame(props: Props): JSX.Element { |
| 12 | + const iFrameRef = useRef<HTMLIFrameElement>(null) |
| 13 | + |
| 14 | + const onIframeLoad = (): void => { |
| 15 | + if (iFrameRef.current == null) { |
| 16 | + console.error('iFrameRef is null') |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + // remove the hashchange event listener to prevent memory leaks |
| 21 | + iFrameRef.current.contentWindow?.removeEventListener('hashchange', hashChangeEventListener) |
| 22 | + |
| 23 | + const url = iFrameRef.current?.contentDocument?.location.href |
| 24 | + if (url == null) { |
| 25 | + console.warn('IFrame onload event triggered, but url is null') |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + // make all external links in iframe open in new tab |
| 30 | + // and make internal links replace the iframe url so that change |
| 31 | + // doesn't show up in the page history (we'd need to click back twice) |
| 32 | + iFrameRef.current.contentDocument |
| 33 | + ?.querySelectorAll('a') |
| 34 | + .forEach((a: HTMLAnchorElement) => { |
| 35 | + if (!a.href.startsWith(window.location.origin)) { |
| 36 | + a.setAttribute('target', '_blank') |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if (a.href.trim() === '') { |
| 41 | + // ignore empty links, may be handled with js internally, |
| 42 | + // so we'll ignore it. Will inevitably cause the user to have to click back |
| 43 | + // multiple times to get back to the previous page. |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // From here: https://www.ozzu.com/questions/358584/how-do-you-ignore-iframes-javascript-history |
| 48 | + a.onclick = () => { |
| 49 | + iFrameRef.current?.contentWindow?.location.replace(a.href) |
| 50 | + return false |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + // Add the event listener again |
| 55 | + iFrameRef.current.contentWindow?.addEventListener('hashchange', hashChangeEventListener) |
| 56 | + |
| 57 | + const parts = url.split('/doc/').slice(1).join('/doc/').split('/') |
| 58 | + const urlPageAndHash = parts.slice(2).join('/') |
| 59 | + const hashIndex = urlPageAndHash.includes('#') ? urlPageAndHash.indexOf('#') : urlPageAndHash.length |
| 60 | + const urlPage = urlPageAndHash.slice(0, hashIndex) |
| 61 | + const urlHash = urlPageAndHash.slice(hashIndex) |
| 62 | + |
| 63 | + props.onPageChanged(urlPage, urlHash) |
| 64 | + } |
| 65 | + |
| 66 | + const hashChangeEventListener = (): void => { |
| 67 | + if (iFrameRef.current == null) { |
| 68 | + console.error('hashChangeEvent from iframe but iFrameRef is null') |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + const url = iFrameRef.current?.contentDocument?.location.href |
| 73 | + if (url == null) { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + let hash = url.split('#')[1] |
| 78 | + if (hash !== null) { |
| 79 | + hash = `#${hash}` |
| 80 | + } else { |
| 81 | + hash = '' |
| 82 | + } |
| 83 | + |
| 84 | + props.onHashChanged(hash) |
| 85 | + } |
| 86 | + |
| 87 | + return (<iframe |
| 88 | + ref={iFrameRef} |
| 89 | + key={uniqueId()} |
| 90 | + className={styles['docs-iframe']} |
| 91 | + src={props.src} |
| 92 | + title="docs" |
| 93 | + onLoad={onIframeLoad} |
| 94 | + />) |
| 95 | +} |
0 commit comments