fix: send scroll state if current renderer is ready instead of main renderer

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-04-11 14:00:57 +02:00
parent 00be642c26
commit 34b433d4ab
No known key found for this signature in database
GPG key ID: 42498463316F048B
2 changed files with 14 additions and 18 deletions

View file

@ -3,32 +3,29 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useApplicationState } from '../../../../hooks/common/use-application-state'
import { useSendToRenderer } from '../../../render-page/window-post-message-communicator/hooks/use-send-to-renderer'
import { CommunicationMessageType } from '../../../render-page/window-post-message-communicator/rendering-message'
import { useEditorToRendererCommunicator } from '../../render-context/editor-to-renderer-communicator-context-provider'
import type { ScrollState } from '../../synced-scroll/scroll-props'
import equal from 'fast-deep-equal'
import { useEffect, useRef } from 'react'
import { useMemo, useRef } from 'react'
/**
* Sends the given {@link ScrollState scroll state} to the renderer if the content changed.
*
* @param scrollState The scroll state to send
* @param rendererReady Defines if the target renderer is ready
*/
export const useSendScrollState = (scrollState: ScrollState | undefined): void => {
const iframeCommunicator = useEditorToRendererCommunicator()
export const useSendScrollState = (scrollState: ScrollState | undefined, rendererReady: boolean): void => {
const oldScrollState = useRef<ScrollState | undefined>(undefined)
const rendererReady = useApplicationState((state) => state.rendererStatus.rendererReady)
useEffect(() => {
if (
iframeCommunicator.isCommunicationEnabled() &&
rendererReady &&
scrollState &&
!equal(scrollState, oldScrollState.current)
) {
useSendToRenderer(
useMemo(() => {
if (!scrollState || equal(scrollState, oldScrollState.current)) {
return
}
oldScrollState.current = scrollState
iframeCommunicator.sendMessageToOtherSide({ type: CommunicationMessageType.SET_SCROLL_STATE, scrollState })
}
}, [iframeCommunicator, rendererReady, scrollState])
return { type: CommunicationMessageType.SET_SCROLL_STATE, scrollState }
}, [scrollState]),
rendererReady
)
}

View file

@ -146,8 +146,7 @@ export const RenderIframe: React.FC<RenderIframeProps> = ({
useEffectOnRenderTypeChange(rendererType, onIframeLoad)
useSendDarkModeStatusToRenderer(forcedDarkMode, rendererReady)
useSendMarkdownToRenderer(markdownContentLines, rendererReady)
useSendScrollState(scrollState)
useSendScrollState(scrollState, rendererReady)
useEditorReceiveHandler(
CommunicationMessageType.SET_SCROLL_STATE,