Added NEXT_PUBLIC_IGNORE_IFRAME_ORIGIN_CONFIG (#1738)

* Added NEXT_PUBLIC_IGNORE_IFRAME_ORIGIN_CONFIG

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2021-12-31 00:05:02 +01:00 committed by GitHub
parent acd368813d
commit ca49eb957d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 14 deletions

View file

@ -5,10 +5,9 @@
*/
import React, { createContext, useContext, useEffect, useMemo } from 'react'
import { useSelector } from 'react-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'
import type { ApplicationState } from '../../../redux/application-state'
import { ORIGIN_TYPE, useOriginFromConfig } from './use-origin-from-config'
const RendererToEditorCommunicatorContext = createContext<RendererToEditorCommunicator | undefined>(undefined)
@ -27,7 +26,7 @@ export const useRendererToEditorCommunicator: () => RendererToEditorCommunicator
}
export const RendererToEditorCommunicatorContextProvider: React.FC = ({ children }) => {
const editorOrigin = useSelector((state: ApplicationState) => state.config.iframeCommunication.editorOrigin)
const editorOrigin = useOriginFromConfig(ORIGIN_TYPE.EDITOR)
const communicator = useMemo<RendererToEditorCommunicator>(() => {
const newCommunicator = new RendererToEditorCommunicator()
newCommunicator.setMessageTarget(window.parent, editorOrigin)

View file

@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useApplicationState } from '../../../hooks/common/use-application-state'
import { useMemo } from 'react'
export enum ORIGIN_TYPE {
EDITOR,
RENDERER
}
/**
* Returns the url origin of the editor or the renderer
*/
export const useOriginFromConfig = (originType: ORIGIN_TYPE): string => {
const originFromConfig = useApplicationState((state) =>
originType === ORIGIN_TYPE.EDITOR
? state.config.iframeCommunication.editorOrigin
: state.config.iframeCommunication.rendererOrigin
)
return useMemo(() => {
return process.env.NEXT_PUBLIC_IGNORE_IFRAME_ORIGIN_CONFIG !== undefined
? window.location.origin + '/'
: originFromConfig
}, [originFromConfig])
}