feat(redux): add print-mode

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2024-11-27 20:17:47 +01:00
parent b75a8db251
commit 2bb453451c
9 changed files with 111 additions and 12 deletions

View file

@ -4,17 +4,36 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { useIsRendererReady } from '../../render-page/window-post-message-communicator/hooks/use-is-renderer-ready'
import { useCallback } from 'react'
import { CommunicationMessageType } from '../../render-page/window-post-message-communicator/rendering-message'
import { useEditorToRendererCommunicator } from '../render-context/editor-to-renderer-communicator-context-provider'
/** /**
* Prints the content of the renderer iframe. * Prints the content of the renderer iframe.
*/ */
export const printIframe = (): void => { export const usePrintIframe = (): (() => void) => {
const iframe = document.getElementById('editor-renderer-iframe') as HTMLIFrameElement const iframeCommunicator = useEditorToRendererCommunicator()
<<<<<<< HEAD const rendererReady = useIsRendererReady()
if (!iframe) {
======= return useCallback(() => {
if (!iframe || !iframe.contentWindow) { if (!rendererReady) {
>>>>>>> 279f6cd89 (feat(frontend): add basic print functionality) return
return }
} const iframe = document.getElementById('editor-renderer-iframe') as HTMLIFrameElement
iframe.contentWindow.print() if (!iframe || !iframe.contentWindow) {
return
}
iframeCommunicator.sendMessageToOtherSide({
type: CommunicationMessageType.SET_PRINT_MODE,
printMode: true
})
setTimeout(() => {
iframe.contentWindow?.print()
iframeCommunicator.sendMessageToOtherSide({
type: CommunicationMessageType.SET_PRINT_MODE,
printMode: false
})
}, 50)
}, [rendererReady, iframeCommunicator])
} }

View file

@ -17,6 +17,7 @@ import { countWords } from './word-counter'
import type { SlideOptions } from '@hedgedoc/commons' import type { SlideOptions } from '@hedgedoc/commons'
import { EventEmitter2 } from 'eventemitter2' import { EventEmitter2 } from 'eventemitter2'
import React, { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState } from 'react' import React, { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState } from 'react'
import { setPrintMode } from '../../redux/print-mode/methods'
/** /**
* Wraps the markdown rendering in an iframe. * Wraps the markdown rendering in an iframe.
@ -78,6 +79,13 @@ export const RenderPageContent: React.FC = () => {
}, [communicator]) }, [communicator])
) )
useRendererReceiveHandler(
CommunicationMessageType.SET_PRINT_MODE,
useCallback(({ printMode }) => {
setPrintMode(printMode)
}, [])
)
const onMakeScrollSource = useCallback(() => { const onMakeScrollSource = useCallback(() => {
sendScrolling.current = true sendScrolling.current = true
communicator.sendMessageToOtherSide({ communicator.sendMessageToOtherSide({

View file

@ -20,13 +20,19 @@ export enum CommunicationMessageType {
ON_WORD_COUNT_CALCULATED = 'ON_WORD_COUNT_CALCULATED', ON_WORD_COUNT_CALCULATED = 'ON_WORD_COUNT_CALCULATED',
SET_SLIDE_OPTIONS = 'SET_SLIDE_OPTIONS', SET_SLIDE_OPTIONS = 'SET_SLIDE_OPTIONS',
IMAGE_UPLOAD = 'IMAGE_UPLOAD', IMAGE_UPLOAD = 'IMAGE_UPLOAD',
EXTENSION_EVENT = 'EXTENSION_EVENT' EXTENSION_EVENT = 'EXTENSION_EVENT',
SET_PRINT_MODE = 'SET_PRINT_MODE'
} }
export interface NoPayloadMessage<TYPE extends CommunicationMessageType> { export interface NoPayloadMessage<TYPE extends CommunicationMessageType> {
type: TYPE type: TYPE
} }
export interface SetPrintModeConfigurationMessage {
type: CommunicationMessageType.SET_PRINT_MODE
printMode: boolean
}
export interface SetAdditionalConfigurationMessage { export interface SetAdditionalConfigurationMessage {
type: CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION type: CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION
darkModePreference: DarkModePreference darkModePreference: DarkModePreference
@ -101,6 +107,7 @@ export type CommunicationMessages =
| OnWordCountCalculatedMessage | OnWordCountCalculatedMessage
| ImageUploadMessage | ImageUploadMessage
| ExtensionEvent | ExtensionEvent
| SetPrintModeConfigurationMessage
export type EditorToRendererMessageType = export type EditorToRendererMessageType =
| CommunicationMessageType.SET_MARKDOWN_CONTENT | CommunicationMessageType.SET_MARKDOWN_CONTENT
@ -110,6 +117,7 @@ export type EditorToRendererMessageType =
| CommunicationMessageType.GET_WORD_COUNT | CommunicationMessageType.GET_WORD_COUNT
| CommunicationMessageType.SET_SLIDE_OPTIONS | CommunicationMessageType.SET_SLIDE_OPTIONS
| CommunicationMessageType.DISABLE_RENDERER_SCROLL_SOURCE | CommunicationMessageType.DISABLE_RENDERER_SCROLL_SOURCE
| CommunicationMessageType.SET_PRINT_MODE
export type RendererToEditorMessageType = export type RendererToEditorMessageType =
| CommunicationMessageType.RENDERER_READY | CommunicationMessageType.RENDERER_READY

View file

@ -6,6 +6,9 @@
import { DarkModePreference } from '../../redux/dark-mode/types' import { DarkModePreference } from '../../redux/dark-mode/types'
import { useApplicationState } from '../common/use-application-state' import { useApplicationState } from '../common/use-application-state'
import useMediaQuery from '@restart/hooks/useMediaQuery' import useMediaQuery from '@restart/hooks/useMediaQuery'
import { Logger } from '../../utils/logger'
const logger = new Logger('useDarkModeState')
/** /**
* Uses the user settings and the browser preference to determine if dark mode should be used. * Uses the user settings and the browser preference to determine if dark mode should be used.
@ -14,7 +17,14 @@ import useMediaQuery from '@restart/hooks/useMediaQuery'
*/ */
export const useDarkModeState = (): boolean => { export const useDarkModeState = (): boolean => {
const preference = useApplicationState((state) => state.darkMode.darkModePreference) const preference = useApplicationState((state) => state.darkMode.darkModePreference)
const printModeEnabled = useApplicationState((state) => state.printMode.printModeEnabled)
const isBrowserPreferringDark = useMediaQuery('(prefers-color-scheme: dark)') const isBrowserPreferringDark = useMediaQuery('(prefers-color-scheme: dark)')
logger.info(`SET_PRINT_MODE ${printModeEnabled}`)
if (printModeEnabled) {
return false
}
return preference === DarkModePreference.DARK || (preference === DarkModePreference.AUTO && isBrowserPreferringDark) return preference === DarkModePreference.DARK || (preference === DarkModePreference.AUTO && isBrowserPreferringDark)
} }

View file

@ -12,6 +12,7 @@ import { rendererStatusReducer } from './renderer-status/slice'
import { realtimeStatusReducer } from './realtime/slice' import { realtimeStatusReducer } from './realtime/slice'
import { historyReducer } from './history/slice' import { historyReducer } from './history/slice'
import { noteDetailsReducer } from './note-details/slice' import { noteDetailsReducer } from './note-details/slice'
import { printModeReducer } from './print-mode/slice'
export const store = configureStore({ export const store = configureStore({
reducer: { reducer: {
@ -21,7 +22,8 @@ export const store = configureStore({
rendererStatus: rendererStatusReducer, rendererStatus: rendererStatusReducer,
realtimeStatus: realtimeStatusReducer, realtimeStatus: realtimeStatusReducer,
history: historyReducer, history: historyReducer,
noteDetails: noteDetailsReducer noteDetails: noteDetailsReducer,
printMode: printModeReducer
}, },
devTools: isDevMode devTools: isDevMode
}) })

View file

@ -0,0 +1,10 @@
/*
* SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { PrintModeConfig } from './types'
export const initialState: PrintModeConfig = {
printModeEnabled: false
}

View file

@ -0,0 +1,12 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { store } from '..'
import { printModeActionsCreator } from './slice'
export const setPrintMode = (printMode: boolean): void => {
const action = printModeActionsCreator.setPrintMode(printMode)
store.dispatch(action)
}

View file

@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { PayloadAction } from '@reduxjs/toolkit'
import { createSlice } from '@reduxjs/toolkit'
import { initialState } from './initial-state'
import type { PrintModeConfig } from './types'
const printModeSlice = createSlice({
name: 'printMode',
initialState,
reducers: {
setPrintMode: (state, action: PayloadAction<PrintModeConfig['printModeEnabled']>) => {
state.printModeEnabled = action.payload
}
}
})
export const printModeActionsCreator = printModeSlice.actions
export const printModeReducer = printModeSlice.reducer

View file

@ -0,0 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
export interface PrintModeConfig {
printModeEnabled: boolean
}