Extract code into hook

Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
This commit is contained in:
Tilman Vatteroth 2021-03-06 15:06:54 +01:00 committed by Tilman Vatteroth
parent 6876aaa856
commit a6c80ac1f0
2 changed files with 28 additions and 14 deletions

View file

@ -4,15 +4,13 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import React, { Fragment, useCallback, useEffect, useRef, useState } from 'react' import React, { Fragment, useCallback, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useSelector } from 'react-redux' import { useSelector } from 'react-redux'
import { useLocation } from 'react-router'
import { useApplyDarkMode } from '../../hooks/common/use-apply-dark-mode' import { useApplyDarkMode } from '../../hooks/common/use-apply-dark-mode'
import { useDocumentTitleWithNoteTitle } from '../../hooks/common/use-document-title-with-note-title' import { useDocumentTitleWithNoteTitle } from '../../hooks/common/use-document-title-with-note-title'
import { useNoteMarkdownContent } from '../../hooks/common/use-note-markdown-content' import { useNoteMarkdownContent } from '../../hooks/common/use-note-markdown-content'
import { ApplicationState } from '../../redux' import { ApplicationState } from '../../redux'
import { setEditorMode } from '../../redux/editor/methods'
import { import {
SetCheckboxInMarkdownContent, SetCheckboxInMarkdownContent,
setNoteFrontmatter, setNoteFrontmatter,
@ -33,6 +31,7 @@ import { Sidebar } from './sidebar/sidebar'
import { Splitter } from './splitter/splitter' import { Splitter } from './splitter/splitter'
import { DualScrollState, ScrollState } from './synced-scroll/scroll-props' import { DualScrollState, ScrollState } from './synced-scroll/scroll-props'
import { RendererType } from '../render-page/rendering-message' import { RendererType } from '../render-page/rendering-message'
import { useEditorModeFromUrl } from './hooks/useEditorModeFromUrl'
export interface EditorPagePathParams { export interface EditorPagePathParams {
id: string id: string
@ -45,7 +44,6 @@ export enum ScrollSource {
export const EditorPage: React.FC = () => { export const EditorPage: React.FC = () => {
useTranslation() useTranslation()
const { search } = useLocation()
const markdownContent = useNoteMarkdownContent() const markdownContent = useNoteMarkdownContent()
const scrollSource = useRef<ScrollSource>(ScrollSource.EDITOR) const scrollSource = useRef<ScrollSource>(ScrollSource.EDITOR)
@ -57,15 +55,6 @@ export const EditorPage: React.FC = () => {
rendererScrollState: { firstLineInView: 1, scrolledPercentage: 0 } rendererScrollState: { firstLineInView: 1, scrolledPercentage: 0 }
})) }))
useEffect(() => {
const requestedMode = search.substr(1)
const mode = Object.values(EditorMode)
.find(mode => mode === requestedMode)
if (mode) {
setEditorMode(mode)
}
}, [search])
const onMarkdownRendererScroll = useCallback((newScrollState: ScrollState) => { const onMarkdownRendererScroll = useCallback((newScrollState: ScrollState) => {
if (scrollSource.current === ScrollSource.RENDERER && editorSyncScroll) { if (scrollSource.current === ScrollSource.RENDERER && editorSyncScroll) {
setScrollState((old) => ({ editorScrollState: newScrollState, rendererScrollState: old.rendererScrollState })) setScrollState((old) => ({ editorScrollState: newScrollState, rendererScrollState: old.rendererScrollState }))
@ -81,6 +70,8 @@ export const EditorPage: React.FC = () => {
useViewModeShortcuts() useViewModeShortcuts()
useApplyDarkMode() useApplyDarkMode()
useDocumentTitleWithNoteTitle() useDocumentTitleWithNoteTitle()
useEditorModeFromUrl()
const [error, loading] = useLoadNoteFromServer() const [error, loading] = useLoadNoteFromServer()
const setRendererToScrollSource = useCallback(() => { const setRendererToScrollSource = useCallback(() => {

View file

@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useEffect } from 'react'
import { EditorMode } from '../app-bar/editor-view-mode'
import { setEditorMode } from '../../../redux/editor/methods'
import { useLocation } from 'react-router'
export const useEditorModeFromUrl = (): void => {
const { search } = useLocation()
useEffect(() => {
const requestedMode = search.substr(1)
const mode = Object.values(EditorMode)
.find(mode => mode === requestedMode)
if (mode) {
setEditorMode(mode)
}
}, [search])
}