Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/docs-page-url-handling #598

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/src/components/DocumentControlButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function DocumentControlButtons (props: Props): JSX.Element {
<Select
className={styles['version-select']}
onChange={(e) => { props.onVersionChange(e.target.value) }}
value={props.versions.length > 0 ? props.version : ''}
value={props.versions.find(v => v.name === props.version) !== undefined ? props.version : ''}
>
{props.versions
.filter((v) => !v.hidden || v.name === props.version)
Expand Down
95 changes: 95 additions & 0 deletions web/src/components/IFrame.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React, { useRef } from 'react'
import { uniqueId } from 'lodash'

import styles from '../style/components/IFrame.module.css'
interface Props {
src: string
onPageChanged: (page: string, hash: string) => void
onHashChanged: (hash: string) => void
}

export default function IFrame(props: Props): JSX.Element {
const iFrameRef = useRef<HTMLIFrameElement>(null)

const onIframeLoad = (): void => {
if (iFrameRef.current == null) {
console.error('iFrameRef is null')
return
}

// remove the hashchange event listener to prevent memory leaks
iFrameRef.current.contentWindow?.removeEventListener('hashchange', hashChangeEventListener)

const url = iFrameRef.current?.contentDocument?.location.href
if (url == null) {
console.warn('IFrame onload event triggered, but url is null')
return
}

// make all external links in iframe open in new tab
// and make internal links replace the iframe url so that change
// doesn't show up in the page history (we'd need to click back twice)
iFrameRef.current.contentDocument
?.querySelectorAll('a')
.forEach((a: HTMLAnchorElement) => {
if (!a.href.startsWith(window.location.origin)) {
a.setAttribute('target', '_blank')
return
}

if (a.href.trim() === '') {
// ignore empty links, may be handled with js internally,
// so we'll ignore it. Will inevitably cause the user to have to click back
// multiple times to get back to the previous page.
return
}

// From here: https://www.ozzu.com/questions/358584/how-do-you-ignore-iframes-javascript-history
a.onclick = () => {
iFrameRef.current?.contentWindow?.location.replace(a.href)
return false
}
})

// Add the event listener again
iFrameRef.current.contentWindow?.addEventListener('hashchange', hashChangeEventListener)

const parts = url.split('/doc/').slice(1).join('/doc/').split('/')
const urlPageAndHash = parts.slice(2).join('/')
const hashIndex = urlPageAndHash.includes('#') ? urlPageAndHash.indexOf('#') : urlPageAndHash.length
const urlPage = urlPageAndHash.slice(0, hashIndex)
const urlHash = urlPageAndHash.slice(hashIndex)

props.onPageChanged(urlPage, urlHash)
}

const hashChangeEventListener = (): void => {
if (iFrameRef.current == null) {
console.error('hashChangeEvent from iframe but iFrameRef is null')
return
}

const url = iFrameRef.current?.contentDocument?.location.href
if (url == null) {
return
}

let hash = url.split('#')[1]
if (hash !== null) {
hash = `#${hash}`
} else {
hash = ''
}

props.onHashChanged(hash)
}

return (<iframe
ref={iFrameRef}
key={uniqueId()}
className={styles['docs-iframe']}
src={props.src}
title="docs"
onLoad={onIframeLoad}
/>)
}
18 changes: 17 additions & 1 deletion web/src/data-providers/MessageBannerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ export interface Message {

interface MessageBannerState {
showMessage: (message: Message) => void
clearMessages: () => void
}

export const Context = React.createContext<MessageBannerState>({
showMessage: (): void => {
console.warn('MessageBannerProvider not initialized')
},
clearMessages: (): void => {
console.warn('MessageBannerProvider not initialized')
}
})

Expand Down Expand Up @@ -59,8 +63,20 @@ export function MessageBannerProvider ({ children }: any): JSX.Element {
setLastTimeout(newTimeout)
}, [])

const clearMessages = useCallback(() => {
if (lastTimeout !== undefined) {
clearTimeout(lastTimeout)
}

setMessage({
content: undefined,
type: 'success',
showMs: 6000
})
}, [])

return (
<Context.Provider value={{ showMessage }}>
<Context.Provider value={{ showMessage, clearMessages }}>
<Banner message={message} />
{children}
</Context.Provider>
Expand Down
Loading