mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 15:14:56 -04:00
Throw error if no communicator was found in a react context
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
57f46f489b
commit
31ca77ebfa
6 changed files with 58 additions and 38 deletions
|
@ -4,15 +4,26 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useContext, useMemo } from 'react'
|
import React, { createContext, useContext, useMemo } from 'react'
|
||||||
import { IframeEditorToRendererCommunicator } from '../../render-page/iframe-editor-to-renderer-communicator'
|
import { IframeEditorToRendererCommunicator } from '../../render-page/iframe-editor-to-renderer-communicator'
|
||||||
|
|
||||||
const IFrameEditorToRendererCommunicatorContext = React.createContext<IframeEditorToRendererCommunicator | undefined>(
|
const IFrameEditorToRendererCommunicatorContext = createContext<IframeEditorToRendererCommunicator | undefined>(
|
||||||
undefined
|
undefined
|
||||||
)
|
)
|
||||||
|
|
||||||
export const useIFrameEditorToRendererCommunicator: () => IframeEditorToRendererCommunicator | undefined = () =>
|
/**
|
||||||
useContext(IFrameEditorToRendererCommunicatorContext)
|
* Provides the {@link IframeEditorToRendererCommunicator editor to renderer iframe communicator} that is set by a {@link IframeEditorToRendererCommunicatorContextProvider context provider}.
|
||||||
|
*
|
||||||
|
* @return the received communicator
|
||||||
|
* @throws Error if no communicator was received
|
||||||
|
*/
|
||||||
|
export const useIFrameEditorToRendererCommunicator: () => IframeEditorToRendererCommunicator = () => {
|
||||||
|
const communicatorFromContext = useContext(IFrameEditorToRendererCommunicatorContext)
|
||||||
|
if (!communicatorFromContext) {
|
||||||
|
throw new Error('No editor-to-renderer-iframe-communicator received. Did you forget to use the provider component?')
|
||||||
|
}
|
||||||
|
return communicatorFromContext
|
||||||
|
}
|
||||||
|
|
||||||
export const IframeEditorToRendererCommunicatorContextProvider: React.FC = ({ children }) => {
|
export const IframeEditorToRendererCommunicatorContextProvider: React.FC = ({ children }) => {
|
||||||
const currentIFrameCommunicator = useMemo<IframeEditorToRendererCommunicator>(
|
const currentIFrameCommunicator = useMemo<IframeEditorToRendererCommunicator>(
|
||||||
|
|
|
@ -13,8 +13,19 @@ const IFrameRendererToEditorCommunicatorContext = createContext<IframeRendererTo
|
||||||
undefined
|
undefined
|
||||||
)
|
)
|
||||||
|
|
||||||
export const useIFrameRendererToEditorCommunicator: () => IframeRendererToEditorCommunicator | undefined = () =>
|
/**
|
||||||
useContext(IFrameRendererToEditorCommunicatorContext)
|
* Provides the {@link IframeRendererToEditorCommunicator renderer to editor iframe communicator} that is set by a {@link IframeRendererToEditorCommunicatorContextProvider context provider}.
|
||||||
|
*
|
||||||
|
* @return the received communicator
|
||||||
|
* @throws Error if no communicator was received
|
||||||
|
*/
|
||||||
|
export const useIFrameRendererToEditorCommunicator: () => IframeRendererToEditorCommunicator = () => {
|
||||||
|
const communicatorFromContext = useContext(IFrameRendererToEditorCommunicatorContext)
|
||||||
|
if (!communicatorFromContext) {
|
||||||
|
throw new Error('No renderer-to-editor-iframe-communicator received. Did you forget to use the provider component?')
|
||||||
|
}
|
||||||
|
return communicatorFromContext
|
||||||
|
}
|
||||||
|
|
||||||
export const IframeRendererToEditorCommunicatorContextProvider: React.FC = ({ children }) => {
|
export const IframeRendererToEditorCommunicatorContextProvider: React.FC = ({ children }) => {
|
||||||
const editorOrigin = useSelector((state: ApplicationState) => state.config.iframeCommunication.editorOrigin)
|
const editorOrigin = useSelector((state: ApplicationState) => state.config.iframeCommunication.editorOrigin)
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { IframeEditorToRendererCommunicator } from '../../../render-page/iframe-
|
||||||
|
|
||||||
export const useOnIframeLoad = (
|
export const useOnIframeLoad = (
|
||||||
frameReference: RefObject<HTMLIFrameElement>,
|
frameReference: RefObject<HTMLIFrameElement>,
|
||||||
iframeCommunicator: IframeEditorToRendererCommunicator | undefined,
|
iframeCommunicator: IframeEditorToRendererCommunicator,
|
||||||
rendererOrigin: string,
|
rendererOrigin: string,
|
||||||
renderPageUrl: string,
|
renderPageUrl: string,
|
||||||
onNavigateAway: () => void
|
onNavigateAway: () => void
|
||||||
|
@ -19,12 +19,12 @@ export const useOnIframeLoad = (
|
||||||
return useCallback(() => {
|
return useCallback(() => {
|
||||||
const frame = frameReference.current
|
const frame = frameReference.current
|
||||||
if (!frame || !frame.contentWindow) {
|
if (!frame || !frame.contentWindow) {
|
||||||
iframeCommunicator?.unsetOtherSide()
|
iframeCommunicator.unsetOtherSide()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sendToRenderPage.current) {
|
if (sendToRenderPage.current) {
|
||||||
iframeCommunicator?.setOtherSide(frame.contentWindow, rendererOrigin)
|
iframeCommunicator.setOtherSide(frame.contentWindow, rendererOrigin)
|
||||||
sendToRenderPage.current = false
|
sendToRenderPage.current = false
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -58,39 +58,39 @@ export const RenderIframe: React.FC<RenderIframeProps> = ({
|
||||||
onRendererReadyChange?.(rendererReady)
|
onRendererReadyChange?.(rendererReady)
|
||||||
}, [onRendererReadyChange, rendererReady])
|
}, [onRendererReadyChange, rendererReady])
|
||||||
|
|
||||||
useEffect(() => () => iframeCommunicator?.unregisterEventListener(), [iframeCommunicator])
|
useEffect(() => () => iframeCommunicator.unregisterEventListener(), [iframeCommunicator])
|
||||||
useEffect(
|
useEffect(
|
||||||
() => iframeCommunicator?.onFirstHeadingChange(onFirstHeadingChange),
|
() => iframeCommunicator.onFirstHeadingChange(onFirstHeadingChange),
|
||||||
[iframeCommunicator, onFirstHeadingChange]
|
[iframeCommunicator, onFirstHeadingChange]
|
||||||
)
|
)
|
||||||
useEffect(
|
useEffect(
|
||||||
() => iframeCommunicator?.onFrontmatterChange(onFrontmatterChange),
|
() => iframeCommunicator.onFrontmatterChange(onFrontmatterChange),
|
||||||
[iframeCommunicator, onFrontmatterChange]
|
[iframeCommunicator, onFrontmatterChange]
|
||||||
)
|
)
|
||||||
useEffect(() => iframeCommunicator?.onSetScrollState(onScroll), [iframeCommunicator, onScroll])
|
useEffect(() => iframeCommunicator.onSetScrollState(onScroll), [iframeCommunicator, onScroll])
|
||||||
useEffect(
|
useEffect(
|
||||||
() => iframeCommunicator?.onSetScrollSourceToRenderer(onMakeScrollSource),
|
() => iframeCommunicator.onSetScrollSourceToRenderer(onMakeScrollSource),
|
||||||
[iframeCommunicator, onMakeScrollSource]
|
[iframeCommunicator, onMakeScrollSource]
|
||||||
)
|
)
|
||||||
useEffect(
|
useEffect(
|
||||||
() => iframeCommunicator?.onTaskCheckboxChange(onTaskCheckedChange),
|
() => iframeCommunicator.onTaskCheckboxChange(onTaskCheckedChange),
|
||||||
[iframeCommunicator, onTaskCheckedChange]
|
[iframeCommunicator, onTaskCheckedChange]
|
||||||
)
|
)
|
||||||
useEffect(() => iframeCommunicator?.onImageClicked(setLightboxDetails), [iframeCommunicator])
|
useEffect(() => iframeCommunicator.onImageClicked(setLightboxDetails), [iframeCommunicator])
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
iframeCommunicator?.onRendererReady(() => {
|
iframeCommunicator.onRendererReady(() => {
|
||||||
iframeCommunicator?.sendSetBaseConfiguration({
|
iframeCommunicator.sendSetBaseConfiguration({
|
||||||
baseUrl: window.location.toString(),
|
baseUrl: window.location.toString(),
|
||||||
rendererType
|
rendererType
|
||||||
})
|
})
|
||||||
setRendererReady(true)
|
setRendererReady(true)
|
||||||
})
|
})
|
||||||
}, [darkMode, rendererType, iframeCommunicator, rendererReady, scrollState])
|
}, [darkMode, rendererType, iframeCommunicator, rendererReady, scrollState])
|
||||||
useEffect(() => iframeCommunicator?.onHeightChange(setFrameHeight), [iframeCommunicator])
|
useEffect(() => iframeCommunicator.onHeightChange(setFrameHeight), [iframeCommunicator])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (rendererReady) {
|
if (rendererReady) {
|
||||||
iframeCommunicator?.sendSetDarkmode(darkMode)
|
iframeCommunicator.sendSetDarkmode(darkMode)
|
||||||
}
|
}
|
||||||
}, [darkMode, iframeCommunicator, rendererReady])
|
}, [darkMode, iframeCommunicator, rendererReady])
|
||||||
|
|
||||||
|
@ -98,13 +98,13 @@ export const RenderIframe: React.FC<RenderIframeProps> = ({
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (rendererReady && !equal(scrollState, oldScrollState.current)) {
|
if (rendererReady && !equal(scrollState, oldScrollState.current)) {
|
||||||
oldScrollState.current = scrollState
|
oldScrollState.current = scrollState
|
||||||
iframeCommunicator?.sendScrollState(scrollState)
|
iframeCommunicator.sendScrollState(scrollState)
|
||||||
}
|
}
|
||||||
}, [iframeCommunicator, rendererReady, scrollState])
|
}, [iframeCommunicator, rendererReady, scrollState])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (rendererReady) {
|
if (rendererReady) {
|
||||||
iframeCommunicator?.sendSetMarkdownContent(markdownContent)
|
iframeCommunicator.sendSetMarkdownContent(markdownContent)
|
||||||
}
|
}
|
||||||
}, [iframeCommunicator, markdownContent, rendererReady])
|
}, [iframeCommunicator, markdownContent, rendererReady])
|
||||||
|
|
||||||
|
|
|
@ -8,16 +8,14 @@ import React, { useCallback } from 'react'
|
||||||
import { ImageClickHandler } from '../../markdown-renderer/replace-components/image/image-replacer'
|
import { ImageClickHandler } from '../../markdown-renderer/replace-components/image/image-replacer'
|
||||||
import { IframeRendererToEditorCommunicator } from '../iframe-renderer-to-editor-communicator'
|
import { IframeRendererToEditorCommunicator } from '../iframe-renderer-to-editor-communicator'
|
||||||
|
|
||||||
export const useImageClickHandler = (
|
export const useImageClickHandler = (iframeCommunicator: IframeRendererToEditorCommunicator): ImageClickHandler => {
|
||||||
iframeCommunicator: IframeRendererToEditorCommunicator | undefined
|
|
||||||
): ImageClickHandler => {
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
(event: React.MouseEvent<HTMLImageElement, MouseEvent>) => {
|
(event: React.MouseEvent<HTMLImageElement, MouseEvent>) => {
|
||||||
const image = event.target as HTMLImageElement
|
const image = event.target as HTMLImageElement
|
||||||
if (image.src === '') {
|
if (image.src === '') {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
iframeCommunicator?.sendClickedImageUrl({
|
iframeCommunicator.sendClickedImageUrl({
|
||||||
src: image.src,
|
src: image.src,
|
||||||
alt: image.alt,
|
alt: image.alt,
|
||||||
title: image.title
|
title: image.title
|
||||||
|
|
|
@ -26,17 +26,17 @@ export const IframeMarkdownRenderer: React.FC = () => {
|
||||||
const countWordsInRenderedDocument = useCallback(() => {
|
const countWordsInRenderedDocument = useCallback(() => {
|
||||||
const documentContainer = document.querySelector('.markdown-body')
|
const documentContainer = document.querySelector('.markdown-body')
|
||||||
if (!documentContainer) {
|
if (!documentContainer) {
|
||||||
iframeCommunicator?.sendWordCountCalculated(0)
|
iframeCommunicator.sendWordCountCalculated(0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const wordCount = countWords(documentContainer)
|
const wordCount = countWords(documentContainer)
|
||||||
iframeCommunicator?.sendWordCountCalculated(wordCount)
|
iframeCommunicator.sendWordCountCalculated(wordCount)
|
||||||
}, [iframeCommunicator])
|
}, [iframeCommunicator])
|
||||||
|
|
||||||
useEffect(() => iframeCommunicator?.onSetBaseConfiguration(setBaseConfiguration), [iframeCommunicator])
|
useEffect(() => iframeCommunicator.onSetBaseConfiguration(setBaseConfiguration), [iframeCommunicator])
|
||||||
useEffect(() => iframeCommunicator?.onSetMarkdownContent(setMarkdownContent), [iframeCommunicator])
|
useEffect(() => iframeCommunicator.onSetMarkdownContent(setMarkdownContent), [iframeCommunicator])
|
||||||
useEffect(() => iframeCommunicator?.onSetDarkMode(setDarkMode), [iframeCommunicator])
|
useEffect(() => iframeCommunicator.onSetDarkMode(setDarkMode), [iframeCommunicator])
|
||||||
useEffect(() => iframeCommunicator?.onSetScrollState(setScrollState), [iframeCommunicator, scrollState])
|
useEffect(() => iframeCommunicator.onSetScrollState(setScrollState), [iframeCommunicator, scrollState])
|
||||||
useEffect(
|
useEffect(
|
||||||
() => iframeCommunicator?.onGetWordCount(countWordsInRenderedDocument),
|
() => iframeCommunicator?.onGetWordCount(countWordsInRenderedDocument),
|
||||||
[iframeCommunicator, countWordsInRenderedDocument]
|
[iframeCommunicator, countWordsInRenderedDocument]
|
||||||
|
@ -44,33 +44,33 @@ export const IframeMarkdownRenderer: React.FC = () => {
|
||||||
|
|
||||||
const onTaskCheckedChange = useCallback(
|
const onTaskCheckedChange = useCallback(
|
||||||
(lineInMarkdown: number, checked: boolean) => {
|
(lineInMarkdown: number, checked: boolean) => {
|
||||||
iframeCommunicator?.sendTaskCheckBoxChange(lineInMarkdown, checked)
|
iframeCommunicator.sendTaskCheckBoxChange(lineInMarkdown, checked)
|
||||||
},
|
},
|
||||||
[iframeCommunicator]
|
[iframeCommunicator]
|
||||||
)
|
)
|
||||||
|
|
||||||
const onFirstHeadingChange = useCallback(
|
const onFirstHeadingChange = useCallback(
|
||||||
(firstHeading?: string) => {
|
(firstHeading?: string) => {
|
||||||
iframeCommunicator?.sendFirstHeadingChanged(firstHeading)
|
iframeCommunicator.sendFirstHeadingChanged(firstHeading)
|
||||||
},
|
},
|
||||||
[iframeCommunicator]
|
[iframeCommunicator]
|
||||||
)
|
)
|
||||||
|
|
||||||
const onMakeScrollSource = useCallback(() => {
|
const onMakeScrollSource = useCallback(() => {
|
||||||
iframeCommunicator?.sendSetScrollSourceToRenderer()
|
iframeCommunicator.sendSetScrollSourceToRenderer()
|
||||||
}, [iframeCommunicator])
|
}, [iframeCommunicator])
|
||||||
|
|
||||||
const onFrontmatterChange = useCallback(
|
const onFrontmatterChange = useCallback(
|
||||||
(frontmatter?: NoteFrontmatter) => {
|
(frontmatter?: NoteFrontmatter) => {
|
||||||
setNoteFrontmatter(frontmatter)
|
setNoteFrontmatter(frontmatter)
|
||||||
iframeCommunicator?.sendSetFrontmatter(frontmatter)
|
iframeCommunicator.sendSetFrontmatter(frontmatter)
|
||||||
},
|
},
|
||||||
[iframeCommunicator]
|
[iframeCommunicator]
|
||||||
)
|
)
|
||||||
|
|
||||||
const onScroll = useCallback(
|
const onScroll = useCallback(
|
||||||
(scrollState: ScrollState) => {
|
(scrollState: ScrollState) => {
|
||||||
iframeCommunicator?.sendSetScrollState(scrollState)
|
iframeCommunicator.sendSetScrollState(scrollState)
|
||||||
},
|
},
|
||||||
[iframeCommunicator]
|
[iframeCommunicator]
|
||||||
)
|
)
|
||||||
|
@ -79,7 +79,7 @@ export const IframeMarkdownRenderer: React.FC = () => {
|
||||||
|
|
||||||
const onHeightChange = useCallback(
|
const onHeightChange = useCallback(
|
||||||
(height: number) => {
|
(height: number) => {
|
||||||
iframeCommunicator?.sendHeightChange(height)
|
iframeCommunicator.sendHeightChange(height)
|
||||||
},
|
},
|
||||||
[iframeCommunicator]
|
[iframeCommunicator]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue