mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-29 22:35:50 -04:00
Add prettier for codestyle and re-format everything (#1294)
This commit is contained in:
parent
8b78154075
commit
0aae1f70d2
319 changed files with 4809 additions and 3936 deletions
|
@ -8,7 +8,11 @@ import { RefObject, useCallback } from 'react'
|
|||
import { LineMarkerPosition } from '../../../markdown-renderer/types'
|
||||
import { ScrollState } from '../scroll-props'
|
||||
|
||||
export const useOnUserScroll = (lineMarks: LineMarkerPosition[] | undefined, scrollContainer: RefObject<HTMLElement>, onScroll: ((newScrollState: ScrollState) => void) | undefined): () => void => {
|
||||
export const useOnUserScroll = (
|
||||
lineMarks: LineMarkerPosition[] | undefined,
|
||||
scrollContainer: RefObject<HTMLElement>,
|
||||
onScroll: ((newScrollState: ScrollState) => void) | undefined
|
||||
): (() => void) => {
|
||||
return useCallback(() => {
|
||||
if (!scrollContainer.current || !lineMarks || lineMarks.length === 0 || !onScroll) {
|
||||
return
|
||||
|
@ -16,31 +20,31 @@ export const useOnUserScroll = (lineMarks: LineMarkerPosition[] | undefined, scr
|
|||
|
||||
const scrollTop = scrollContainer.current.scrollTop
|
||||
|
||||
const lineMarksBeforeScrollTop = lineMarks.filter(lineMark => lineMark.position <= scrollTop)
|
||||
const lineMarksBeforeScrollTop = lineMarks.filter((lineMark) => lineMark.position <= scrollTop)
|
||||
if (lineMarksBeforeScrollTop.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const lineMarksAfterScrollTop = lineMarks.filter(lineMark => lineMark.position > scrollTop)
|
||||
const lineMarksAfterScrollTop = lineMarks.filter((lineMark) => lineMark.position > scrollTop)
|
||||
if (lineMarksAfterScrollTop.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const beforeLineMark = lineMarksBeforeScrollTop
|
||||
.reduce((prevLineMark, currentLineMark) =>
|
||||
prevLineMark.line >= currentLineMark.line ? prevLineMark : currentLineMark)
|
||||
const beforeLineMark = lineMarksBeforeScrollTop.reduce((prevLineMark, currentLineMark) =>
|
||||
prevLineMark.line >= currentLineMark.line ? prevLineMark : currentLineMark
|
||||
)
|
||||
|
||||
const afterLineMark = lineMarksAfterScrollTop
|
||||
.reduce((prevLineMark, currentLineMark) =>
|
||||
prevLineMark.line < currentLineMark.line ? prevLineMark : currentLineMark)
|
||||
const afterLineMark = lineMarksAfterScrollTop.reduce((prevLineMark, currentLineMark) =>
|
||||
prevLineMark.line < currentLineMark.line ? prevLineMark : currentLineMark
|
||||
)
|
||||
|
||||
const componentHeight = afterLineMark.position - beforeLineMark.position
|
||||
const distanceToBefore = scrollTop - beforeLineMark.position
|
||||
const percentageRaw = (distanceToBefore / componentHeight)
|
||||
const percentageRaw = distanceToBefore / componentHeight
|
||||
const lineCount = afterLineMark.line - beforeLineMark.line
|
||||
const line = Math.floor(lineCount * percentageRaw + beforeLineMark.line)
|
||||
const lineHeight = componentHeight / lineCount
|
||||
const innerScrolling = Math.floor((distanceToBefore % lineHeight) / lineHeight * 100)
|
||||
const innerScrolling = Math.floor(((distanceToBefore % lineHeight) / lineHeight) * 100)
|
||||
|
||||
const newScrollState: ScrollState = { firstLineInView: line, scrolledPercentage: innerScrolling }
|
||||
onScroll(newScrollState)
|
||||
|
|
|
@ -9,18 +9,26 @@ import { LineMarkerPosition } from '../../../markdown-renderer/types'
|
|||
import { ScrollState } from '../scroll-props'
|
||||
import { findLineMarks } from '../utils'
|
||||
|
||||
export const useScrollToLineMark = (scrollState: ScrollState | undefined, lineMarks: LineMarkerPosition[] | undefined, contentLineCount: number, scrollContainer: RefObject<HTMLElement>): void => {
|
||||
export const useScrollToLineMark = (
|
||||
scrollState: ScrollState | undefined,
|
||||
lineMarks: LineMarkerPosition[] | undefined,
|
||||
contentLineCount: number,
|
||||
scrollContainer: RefObject<HTMLElement>
|
||||
): void => {
|
||||
const lastScrollPosition = useRef<number>()
|
||||
|
||||
const scrollTo = useCallback((targetPosition: number): void => {
|
||||
if (!scrollContainer.current || targetPosition === lastScrollPosition.current) {
|
||||
return
|
||||
}
|
||||
lastScrollPosition.current = targetPosition
|
||||
scrollContainer.current.scrollTo({
|
||||
top: targetPosition
|
||||
})
|
||||
}, [scrollContainer])
|
||||
const scrollTo = useCallback(
|
||||
(targetPosition: number): void => {
|
||||
if (!scrollContainer.current || targetPosition === lastScrollPosition.current) {
|
||||
return
|
||||
}
|
||||
lastScrollPosition.current = targetPosition
|
||||
scrollContainer.current.scrollTo({
|
||||
top: targetPosition
|
||||
})
|
||||
},
|
||||
[scrollContainer]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!scrollContainer.current || !lineMarks || lineMarks.length === 0 || !scrollState) {
|
||||
|
@ -42,7 +50,10 @@ export const useScrollToLineMark = (scrollState: ScrollState | undefined, lineMa
|
|||
const linesBetweenMarkers = firstMarkAfterLine - lastMarkBeforeLine
|
||||
const blockHeight = positionAfter - positionBefore
|
||||
const lineHeight = blockHeight / linesBetweenMarkers
|
||||
const position = positionBefore + (scrollState.firstLineInView - lastMarkBeforeLine) * lineHeight + scrollState.scrolledPercentage / 100 * lineHeight
|
||||
const position =
|
||||
positionBefore +
|
||||
(scrollState.firstLineInView - lastMarkBeforeLine) * lineHeight +
|
||||
(scrollState.scrolledPercentage / 100) * lineHeight
|
||||
const correctedPosition = Math.floor(position)
|
||||
scrollTo(correctedPosition)
|
||||
}, [contentLineCount, lineMarks, scrollContainer, scrollState, scrollTo])
|
||||
|
|
|
@ -10,26 +10,31 @@ import { ScrollState } from '../scroll-props'
|
|||
import { useOnUserScroll } from './use-on-user-scroll'
|
||||
import { useScrollToLineMark } from './use-scroll-to-line-mark'
|
||||
|
||||
export const useSyncedScrolling = (outerContainerRef: React.RefObject<HTMLElement>,
|
||||
export const useSyncedScrolling = (
|
||||
outerContainerRef: React.RefObject<HTMLElement>,
|
||||
rendererRef: React.RefObject<HTMLElement>,
|
||||
numberOfLines: number,
|
||||
scrollState?: ScrollState,
|
||||
onScroll?: (scrollState: ScrollState) => void): [(lineMarkers: LineMarkerPosition[]) => void, () => void] => {
|
||||
onScroll?: (scrollState: ScrollState) => void
|
||||
): [(lineMarkers: LineMarkerPosition[]) => void, () => void] => {
|
||||
const [lineMarks, setLineMarks] = useState<LineMarkerPosition[]>()
|
||||
|
||||
const onLineMarkerPositionChanged = useCallback((linkMarkerPositions: LineMarkerPosition[]) => {
|
||||
if (!outerContainerRef.current || !rendererRef.current) {
|
||||
return
|
||||
}
|
||||
const documentRenderPaneTop = (outerContainerRef.current.offsetTop ?? 0)
|
||||
const rendererTop = (rendererRef.current.offsetTop ?? 0)
|
||||
const offset = rendererTop - documentRenderPaneTop
|
||||
const adjustedLineMakerPositions = linkMarkerPositions.map(oldMarker => ({
|
||||
line: oldMarker.line,
|
||||
position: oldMarker.position + offset
|
||||
}))
|
||||
setLineMarks(adjustedLineMakerPositions)
|
||||
}, [outerContainerRef, rendererRef])
|
||||
const onLineMarkerPositionChanged = useCallback(
|
||||
(linkMarkerPositions: LineMarkerPosition[]) => {
|
||||
if (!outerContainerRef.current || !rendererRef.current) {
|
||||
return
|
||||
}
|
||||
const documentRenderPaneTop = outerContainerRef.current.offsetTop ?? 0
|
||||
const rendererTop = rendererRef.current.offsetTop ?? 0
|
||||
const offset = rendererTop - documentRenderPaneTop
|
||||
const adjustedLineMakerPositions = linkMarkerPositions.map((oldMarker) => ({
|
||||
line: oldMarker.line,
|
||||
position: oldMarker.position + offset
|
||||
}))
|
||||
setLineMarks(adjustedLineMakerPositions)
|
||||
},
|
||||
[outerContainerRef, rendererRef]
|
||||
)
|
||||
|
||||
const onUserScroll = useOnUserScroll(lineMarks, outerContainerRef, onScroll)
|
||||
useScrollToLineMark(scrollState, lineMarks, numberOfLines, outerContainerRef)
|
||||
|
|
|
@ -6,7 +6,10 @@
|
|||
|
||||
import { LineMarkerPosition } from '../../markdown-renderer/types'
|
||||
|
||||
export const findLineMarks = (lineMarks: LineMarkerPosition[], lineNumber: number): { lastMarkBefore: LineMarkerPosition | undefined, firstMarkAfter: LineMarkerPosition | undefined } => {
|
||||
export const findLineMarks = (
|
||||
lineMarks: LineMarkerPosition[],
|
||||
lineNumber: number
|
||||
): { lastMarkBefore: LineMarkerPosition | undefined; firstMarkAfter: LineMarkerPosition | undefined } => {
|
||||
let lastMarkBefore
|
||||
let firstMarkAfter
|
||||
for (let i = 0; i < lineMarks.length; i++) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue