mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 11:37:02 -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
|
||||
*/
|
||||
|
||||
import React, { useContext, useMemo } from 'react'
|
||||
import React, { createContext, useContext, useMemo } from 'react'
|
||||
import { IframeEditorToRendererCommunicator } from '../../render-page/iframe-editor-to-renderer-communicator'
|
||||
|
||||
const IFrameEditorToRendererCommunicatorContext = React.createContext<IframeEditorToRendererCommunicator | undefined>(
|
||||
const IFrameEditorToRendererCommunicatorContext = createContext<IframeEditorToRendererCommunicator | 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 }) => {
|
||||
const currentIFrameCommunicator = useMemo<IframeEditorToRendererCommunicator>(
|
||||
|
|
|
@ -13,8 +13,19 @@ const IFrameRendererToEditorCommunicatorContext = createContext<IframeRendererTo
|
|||
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 }) => {
|
||||
const editorOrigin = useSelector((state: ApplicationState) => state.config.iframeCommunication.editorOrigin)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue