mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 19:47:03 -04:00
Add context for iframe communicator in editor page (#1146)
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
6e43ec99a3
commit
af887877aa
3 changed files with 58 additions and 27 deletions
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import React, { useContext, useMemo } from 'react'
|
||||
import { IframeEditorToRendererCommunicator } from '../../render-page/iframe-editor-to-renderer-communicator'
|
||||
|
||||
const IFrameEditorToRendererCommunicatorContext = React.createContext<IframeEditorToRendererCommunicator | undefined>(undefined)
|
||||
|
||||
export const useIFrameCommunicator: () => IframeEditorToRendererCommunicator | undefined = () => useContext(IFrameEditorToRendererCommunicatorContext)
|
||||
|
||||
export const useContextOrStandaloneIframeCommunicator: () => IframeEditorToRendererCommunicator = () => {
|
||||
const contextCommunicator = useIFrameCommunicator()
|
||||
return useMemo(() => contextCommunicator ? contextCommunicator : new IframeEditorToRendererCommunicator(), [contextCommunicator])
|
||||
}
|
||||
|
||||
export const IframeCommunicatorContextProvider: React.FC = ({ children }) => {
|
||||
const currentIFrameCommunicator = useMemo<IframeEditorToRendererCommunicator>(() => new IframeEditorToRendererCommunicator(), [])
|
||||
|
||||
return <IFrameEditorToRendererCommunicatorContext.Provider value={ currentIFrameCommunicator }>
|
||||
{ children }
|
||||
</IFrameEditorToRendererCommunicatorContext.Provider>
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue