From 6c776d6b68f9cb89158247bc855be2b23362176e Mon Sep 17 00:00:00 2001 From: YR Chen Date: Wed, 23 Oct 2024 15:46:10 +0800 Subject: [PATCH 1/3] fix(web): keep title in sync with iframe --- web/src/components/IFrame.tsx | 24 ++++++++++++++++++++++-- web/src/pages/Docs.tsx | 10 +++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/web/src/components/IFrame.tsx b/web/src/components/IFrame.tsx index 3a5e51108..4f2a6c897 100644 --- a/web/src/components/IFrame.tsx +++ b/web/src/components/IFrame.tsx @@ -4,8 +4,9 @@ import { uniqueId } from 'lodash' import styles from '../style/components/IFrame.module.css' interface Props { src: string - onPageChanged: (page: string, hash: string) => void + onPageChanged: (page: string, hash: string, title?: string) => void onHashChanged: (hash: string) => void + onTitleChanged: (title: string) => void onNotFound: () => void } @@ -69,6 +70,10 @@ export default function IFrame(props: Props): JSX.Element { 'hashchange', hashChangeEventListener ) + iFrameRef.current.contentWindow?.addEventListener( + 'titlechange', + titleChangeEventListener + ) const parts = url.split('/doc/').slice(1).join('/doc/').split('/') const urlPageAndHash = parts.slice(2).join('/') @@ -77,8 +82,9 @@ export default function IFrame(props: Props): JSX.Element { : urlPageAndHash.length const urlPage = urlPageAndHash.slice(0, hashIndex) const urlHash = urlPageAndHash.slice(hashIndex) + const title = iFrameRef.current?.contentDocument?.title - props.onPageChanged(urlPage, urlHash) + props.onPageChanged(urlPage, urlHash, title) } const hashChangeEventListener = (): void => { @@ -102,6 +108,20 @@ export default function IFrame(props: Props): JSX.Element { props.onHashChanged(hash) } + const titleChangeEventListener = (): void => { + if (iFrameRef.current == null) { + console.error('titleChangeEvent from iframe but iFrameRef is null') + return + } + + const title = iFrameRef.current?.contentDocument?.title + if (title == null) { + return + } + + props.onTitleChanged(title) + } + return (