mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 19:47:03 -04:00
Restructure Communicator (#1510)
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
e6830598d5
commit
f1e91b4574
31 changed files with 680 additions and 569 deletions
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import React, { createContext, useContext, useMemo } from 'react'
|
||||
import { EditorToRendererCommunicator } from '../../render-page/window-post-message-communicator/editor-to-renderer-communicator'
|
||||
|
||||
const EditorToRendererCommunicatorContext = createContext<EditorToRendererCommunicator | undefined>(undefined)
|
||||
|
||||
/**
|
||||
* Provides the {@link EditorToRendererCommunicator editor to renderer iframe communicator} that is set by a {@link EditorToRendererCommunicatorContextProvider context provider}.
|
||||
*
|
||||
* @return the received communicator
|
||||
* @throws {Error} if no communicator was received
|
||||
*/
|
||||
export const useEditorToRendererCommunicator: () => EditorToRendererCommunicator = () => {
|
||||
const communicatorFromContext = useContext(EditorToRendererCommunicatorContext)
|
||||
if (!communicatorFromContext) {
|
||||
throw new Error('No editor-to-renderer-iframe-communicator received. Did you forget to use the provider component?')
|
||||
}
|
||||
return communicatorFromContext
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a {@link EditorToRendererCommunicator editor to renderer communicator} for the child components via Context.
|
||||
*/
|
||||
export const EditorToRendererCommunicatorContextProvider: React.FC = ({ children }) => {
|
||||
const communicator = useMemo<EditorToRendererCommunicator>(() => new EditorToRendererCommunicator(), [])
|
||||
|
||||
return (
|
||||
<EditorToRendererCommunicatorContext.Provider value={communicator}>
|
||||
{children}
|
||||
</EditorToRendererCommunicatorContext.Provider>
|
||||
)
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import React, { createContext, useContext, useMemo } from 'react'
|
||||
import { IframeEditorToRendererCommunicator } from '../../render-page/iframe-editor-to-renderer-communicator'
|
||||
|
||||
const IFrameEditorToRendererCommunicatorContext = createContext<IframeEditorToRendererCommunicator | undefined>(
|
||||
undefined
|
||||
)
|
||||
|
||||
/**
|
||||
* 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>(
|
||||
() => new IframeEditorToRendererCommunicator(),
|
||||
[]
|
||||
)
|
||||
|
||||
return (
|
||||
<IFrameEditorToRendererCommunicatorContext.Provider value={currentIFrameCommunicator}>
|
||||
{children}
|
||||
</IFrameEditorToRendererCommunicatorContext.Provider>
|
||||
)
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import React, { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
import { IframeRendererToEditorCommunicator } from '../../render-page/iframe-renderer-to-editor-communicator'
|
||||
import { useSelector } from 'react-redux'
|
||||
import { ApplicationState } from '../../../redux'
|
||||
|
||||
const IFrameRendererToEditorCommunicatorContext = createContext<IframeRendererToEditorCommunicator | undefined>(
|
||||
undefined
|
||||
)
|
||||
|
||||
/**
|
||||
* 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)
|
||||
const currentIFrameCommunicator = useMemo<IframeRendererToEditorCommunicator>(() => {
|
||||
const newCommunicator = new IframeRendererToEditorCommunicator()
|
||||
newCommunicator.setMessageTarget(window.parent, editorOrigin)
|
||||
return newCommunicator
|
||||
}, [editorOrigin])
|
||||
|
||||
useEffect(() => {
|
||||
const currentIFrame = currentIFrameCommunicator
|
||||
currentIFrame?.sendRendererReady()
|
||||
return () => currentIFrame?.unregisterEventListener()
|
||||
}, [currentIFrameCommunicator])
|
||||
|
||||
return (
|
||||
<IFrameRendererToEditorCommunicatorContext.Provider value={currentIFrameCommunicator}>
|
||||
{children}
|
||||
</IFrameRendererToEditorCommunicatorContext.Provider>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import React, { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
import { useSelector } from 'react-redux'
|
||||
import { ApplicationState } from '../../../redux'
|
||||
import { RendererToEditorCommunicator } from '../../render-page/window-post-message-communicator/renderer-to-editor-communicator'
|
||||
import { CommunicationMessageType } from '../../render-page/window-post-message-communicator/rendering-message'
|
||||
|
||||
const RendererToEditorCommunicatorContext = createContext<RendererToEditorCommunicator | undefined>(undefined)
|
||||
|
||||
/**
|
||||
* Provides the {@link RendererToEditorCommunicator renderer to editor iframe communicator} that is set by a {@link RendererToEditorCommunicatorContextProvider context provider}.
|
||||
*
|
||||
* @return the received communicator
|
||||
* @throws {Error} if no communicator was received
|
||||
*/
|
||||
export const useRendererToEditorCommunicator: () => RendererToEditorCommunicator = () => {
|
||||
const communicatorFromContext = useContext(RendererToEditorCommunicatorContext)
|
||||
if (!communicatorFromContext) {
|
||||
throw new Error('No renderer-to-editor-iframe-communicator received. Did you forget to use the provider component?')
|
||||
}
|
||||
return communicatorFromContext
|
||||
}
|
||||
|
||||
export const RendererToEditorCommunicatorContextProvider: React.FC = ({ children }) => {
|
||||
const editorOrigin = useSelector((state: ApplicationState) => state.config.iframeCommunication.editorOrigin)
|
||||
const communicator = useMemo<RendererToEditorCommunicator>(() => {
|
||||
const newCommunicator = new RendererToEditorCommunicator()
|
||||
newCommunicator.setMessageTarget(window.parent, editorOrigin)
|
||||
return newCommunicator
|
||||
}, [editorOrigin])
|
||||
|
||||
useEffect(() => {
|
||||
const currentCommunicator = communicator
|
||||
currentCommunicator.enableCommunication()
|
||||
currentCommunicator.sendMessageToOtherSide({
|
||||
type: CommunicationMessageType.RENDERER_READY
|
||||
})
|
||||
return () => currentCommunicator?.unregisterEventListener()
|
||||
}, [communicator])
|
||||
|
||||
/**
|
||||
* Provides a {@link RendererToEditorCommunicator renderer to editor communicator} for the child components via Context.
|
||||
*/
|
||||
return (
|
||||
<RendererToEditorCommunicatorContext.Provider value={communicator}>
|
||||
{children}
|
||||
</RendererToEditorCommunicatorContext.Provider>
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue