fix: send "new lines as breaks" option to the renderer

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-04-11 17:18:11 +02:00
parent 9aa0363359
commit f6f57d5e06
No known key found for this signature in database
GPG key ID: 42498463316F048B
5 changed files with 32 additions and 13 deletions

View file

@ -20,6 +20,7 @@ export const useSendAdditionalConfigurationToRenderer = (
forcedDarkMode: DarkModePreference = DarkModePreference.AUTO forcedDarkMode: DarkModePreference = DarkModePreference.AUTO
): void => { ): void => {
const darkModePreference = useApplicationState((state) => state.darkMode.darkModePreference) const darkModePreference = useApplicationState((state) => state.darkMode.darkModePreference)
const newlinesAreBreaks = useApplicationState((state) => state.noteDetails.frontmatter.newlinesAreBreaks)
const darkMode = useMemo(() => { const darkMode = useMemo(() => {
return forcedDarkMode === DarkModePreference.AUTO ? darkModePreference : forcedDarkMode return forcedDarkMode === DarkModePreference.AUTO ? darkModePreference : forcedDarkMode
@ -29,9 +30,10 @@ export const useSendAdditionalConfigurationToRenderer = (
useMemo( useMemo(
() => ({ () => ({
type: CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION, type: CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION,
darkModePreference: darkMode darkModePreference: darkMode,
newLinesAreBreaks: newlinesAreBreaks
}), }),
[darkMode] [darkMode, newlinesAreBreaks]
), ),
rendererReady rendererReady
) )

View file

@ -25,6 +25,7 @@ export const IframeMarkdownRenderer: React.FC = () => {
const [scrollState, setScrollState] = useState<ScrollState>({ firstLineInView: 1, scrolledPercentage: 0 }) const [scrollState, setScrollState] = useState<ScrollState>({ firstLineInView: 1, scrolledPercentage: 0 })
const [baseConfiguration, setBaseConfiguration] = useState<BaseConfiguration | undefined>(undefined) const [baseConfiguration, setBaseConfiguration] = useState<BaseConfiguration | undefined>(undefined)
const [slideOptions, setSlideOptions] = useState<SlideOptions>() const [slideOptions, setSlideOptions] = useState<SlideOptions>()
const [newLinesAreBreaks, setNewLinesAreBreaks] = useState<boolean>(true)
const communicator = useRendererToEditorCommunicator() const communicator = useRendererToEditorCommunicator()
@ -54,7 +55,10 @@ export const IframeMarkdownRenderer: React.FC = () => {
useRendererReceiveHandler( useRendererReceiveHandler(
CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION, CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION,
useCallback((values) => setDarkModePreference(values.darkModePreference), []) useCallback((values) => {
setNewLinesAreBreaks(values.newLinesAreBreaks)
setDarkModePreference(values.darkModePreference)
}, [])
) )
useRendererReceiveHandler( useRendererReceiveHandler(
@ -123,6 +127,7 @@ export const IframeMarkdownRenderer: React.FC = () => {
onScroll={onScroll} onScroll={onScroll}
baseUrl={baseConfiguration.baseUrl} baseUrl={baseConfiguration.baseUrl}
onHeightChange={onHeightChange} onHeightChange={onHeightChange}
newLinesAreBreaks={newLinesAreBreaks}
/> />
) )
case RendererType.SLIDESHOW: case RendererType.SLIDESHOW:
@ -132,6 +137,7 @@ export const IframeMarkdownRenderer: React.FC = () => {
baseUrl={baseConfiguration.baseUrl} baseUrl={baseConfiguration.baseUrl}
scrollState={scrollState} scrollState={scrollState}
slideOptions={slideOptions} slideOptions={slideOptions}
newlinesAreBreaks={newLinesAreBreaks}
/> />
) )
case RendererType.SIMPLE: case RendererType.SIMPLE:
@ -142,12 +148,22 @@ export const IframeMarkdownRenderer: React.FC = () => {
baseUrl={baseConfiguration.baseUrl} baseUrl={baseConfiguration.baseUrl}
disableToc={true} disableToc={true}
onHeightChange={onHeightChange} onHeightChange={onHeightChange}
newLinesAreBreaks={newLinesAreBreaks}
/> />
) )
default: default:
return null return null
} }
}, [baseConfiguration, markdownContentLines, onHeightChange, onMakeScrollSource, onScroll, scrollState, slideOptions]) }, [
baseConfiguration,
markdownContentLines,
newLinesAreBreaks,
onHeightChange,
onMakeScrollSource,
onScroll,
scrollState,
slideOptions
])
const extensionEventEmitter = useMemo(() => new EventEmitter2({ wildcard: true }), []) const extensionEventEmitter = useMemo(() => new EventEmitter2({ wildcard: true }), [])

View file

@ -24,6 +24,7 @@ export interface MarkdownDocumentProps extends RendererProps {
additionalRendererClasses?: string additionalRendererClasses?: string
disableToc?: boolean disableToc?: boolean
baseUrl: string baseUrl: string
newLinesAreBreaks?: boolean
} }
/** /**
@ -38,6 +39,7 @@ export interface MarkdownDocumentProps extends RendererProps {
* @param scrollState The current {@link ScrollState} * @param scrollState The current {@link ScrollState}
* @param onHeightChange The callback to call if the height of the document changes * @param onHeightChange The callback to call if the height of the document changes
* @param disableToc If the table of contents should be disabled. * @param disableToc If the table of contents should be disabled.
* @param newLinesAreBreaks Defines if the provided markdown content should treat new lines as breaks
* @see https://markdown-it.github.io/ * @see https://markdown-it.github.io/
*/ */
export const MarkdownDocument: React.FC<MarkdownDocumentProps> = ({ export const MarkdownDocument: React.FC<MarkdownDocumentProps> = ({
@ -49,7 +51,8 @@ export const MarkdownDocument: React.FC<MarkdownDocumentProps> = ({
onScroll, onScroll,
scrollState, scrollState,
onHeightChange, onHeightChange,
disableToc disableToc,
newLinesAreBreaks
}) => { }) => {
const rendererRef = useRef<HTMLDivElement | null>(null) const rendererRef = useRef<HTMLDivElement | null>(null)
const [rendererSize, setRendererSize] = useState<DOMRectReadOnly>() const [rendererSize, setRendererSize] = useState<DOMRectReadOnly>()
@ -64,8 +67,6 @@ export const MarkdownDocument: React.FC<MarkdownDocumentProps> = ({
setInternalDocumentRenderPaneSize(entry.contentRect) setInternalDocumentRenderPaneSize(entry.contentRect)
) )
const newlinesAreBreaks = useApplicationState((state) => state.noteDetails.frontmatter.newlinesAreBreaks)
const contentLineCount = useMemo(() => markdownContentLines.length, [markdownContentLines]) const contentLineCount = useMemo(() => markdownContentLines.length, [markdownContentLines])
const [recalculateLineMarkers, onUserScroll] = useDocumentSyncScrolling( const [recalculateLineMarkers, onUserScroll] = useDocumentSyncScrolling(
internalDocumentRenderPaneRef, internalDocumentRenderPaneRef,
@ -91,7 +92,7 @@ export const MarkdownDocument: React.FC<MarkdownDocumentProps> = ({
markdownContentLines={markdownContentLines} markdownContentLines={markdownContentLines}
onLineMarkerPositionChanged={recalculateLineMarkers} onLineMarkerPositionChanged={recalculateLineMarkers}
baseUrl={baseUrl} baseUrl={baseUrl}
newlinesAreBreaks={newlinesAreBreaks} newlinesAreBreaks={newLinesAreBreaks}
/> />
</div> </div>
<DocumentTocSidebar <DocumentTocSidebar

View file

@ -3,10 +3,9 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import type { DarkModePreference } from '../../../redux/dark-mode/types'; import type { DarkModePreference } from '../../../redux/dark-mode/types'
import type { ScrollState } from '../../editor-page/synced-scroll/scroll-props'; import type { ScrollState } from '../../editor-page/synced-scroll/scroll-props'
import type { SlideOptions } from '@hedgedoc/commons'; import type { SlideOptions } from '@hedgedoc/commons'
export enum CommunicationMessageType { export enum CommunicationMessageType {
SET_MARKDOWN_CONTENT = 'SET_MARKDOWN_CONTENT', SET_MARKDOWN_CONTENT = 'SET_MARKDOWN_CONTENT',
@ -31,6 +30,7 @@ export interface NoPayloadMessage<TYPE extends CommunicationMessageType> {
export interface SetAdditionalConfigurationMessage { export interface SetAdditionalConfigurationMessage {
type: CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION type: CommunicationMessageType.SET_ADDITIONAL_CONFIGURATION
darkModePreference: DarkModePreference darkModePreference: DarkModePreference
newLinesAreBreaks: boolean
} }
export interface ExtensionEvent { export interface ExtensionEvent {

View file

@ -38,7 +38,7 @@ const parseRawNoteFrontmatter = (rawData: RawNoteFrontmatter): NoteFrontmatter =
title: rawData.title ?? initialState.frontmatter.title, title: rawData.title ?? initialState.frontmatter.title,
description: rawData.description ?? initialState.frontmatter.description, description: rawData.description ?? initialState.frontmatter.description,
robots: rawData.robots ?? initialState.frontmatter.robots, robots: rawData.robots ?? initialState.frontmatter.robots,
newlinesAreBreaks: rawData.breaks ?? initialState.frontmatter.newlinesAreBreaks, newlinesAreBreaks: parseBoolean(rawData.breaks) ?? initialState.frontmatter.newlinesAreBreaks,
GA: rawData.GA ?? initialState.frontmatter.GA, GA: rawData.GA ?? initialState.frontmatter.GA,
disqus: rawData.disqus ?? initialState.frontmatter.disqus, disqus: rawData.disqus ?? initialState.frontmatter.disqus,
lang: parseLanguage(rawData), lang: parseLanguage(rawData),