Restructure the max-length-warning and the status bar (#1654)

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2021-11-28 22:21:02 +01:00 committed by GitHub
parent 7182f22e52
commit 4023acc9d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 291 additions and 92 deletions

View file

@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { StatusBarInfo } from '../status-bar/status-bar'
import { defaultState } from '../status-bar/status-bar'
import type { Editor } from 'codemirror'
import { useCallback, useState } from 'react'
import { useApplicationState } from '../../../../hooks/common/use-application-state'
/**
* Provides a {@link StatusBarInfo} object and a function that can update this object using a {@link CodeMirror code mirror instance}.
*/
export const useCreateStatusBarInfo = (): [
statusBarInfo: StatusBarInfo,
updateStatusBarInfo: (editor: Editor) => void
] => {
const maxDocumentLength = useApplicationState((state) => state.config.maxDocumentLength)
const [statusBarInfo, setStatusBarInfo] = useState(defaultState)
const updateStatusBarInfo = useCallback(
(editor: Editor): void => {
setStatusBarInfo({
position: editor.getCursor(),
charactersInDocument: editor.getValue().length,
remainingCharacters: maxDocumentLength - editor.getValue().length,
linesInDocument: editor.lineCount(),
selectedColumns: editor.getSelection().length,
selectedLines: editor.getSelection().split('\n').length
})
},
[maxDocumentLength]
)
return [statusBarInfo, updateStatusBarInfo]
}