Fix smooth scroll and other bugs (#1861)

This PR fixes:

- The wrong clean up of window post message communicators
- The smooth scroll bug in chrome (Fixes Anchor navigation in render view not working #1770)
- Scroll by using touch devices in renderer
- Lazy loading of the editor (code mirror doesn't need to be lazy loaded any more)
This commit is contained in:
Tilman Vatteroth 2022-02-14 17:37:34 +01:00 committed by GitHub
parent 0f3f7a82b5
commit 8b4e9191e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 260 additions and 213 deletions

View file

@ -5,12 +5,12 @@
*/
import type { RefObject } from 'react'
import { useMemo } from 'react'
import { useMemo, useRef } from 'react'
import { updateCursorPositions } from '../../../../redux/note-details/methods'
import type { ViewUpdate } from '@codemirror/view'
import { EditorView } from '@codemirror/view'
import { Logger } from '../../../../utils/logger'
import type { Extension } from '@codemirror/state'
import type { Extension, SelectionRange } from '@codemirror/state'
const logger = new Logger('useCursorActivityCallback')
@ -20,14 +20,20 @@ const logger = new Logger('useCursorActivityCallback')
* @return the generated callback
*/
export const useCursorActivityCallback = (editorFocused: RefObject<boolean>): Extension => {
const lastMainSelection = useRef<SelectionRange>()
return useMemo(
() =>
EditorView.updateListener.of((viewUpdate: ViewUpdate): void => {
const firstSelection = viewUpdate.state.selection.main
if (lastMainSelection.current === firstSelection) {
return
}
lastMainSelection.current = firstSelection
if (!editorFocused.current) {
logger.debug("Don't post updated cursor because editor isn't focused")
return
}
const firstSelection = viewUpdate.state.selection.main
const newCursorPos = {
from: firstSelection.from,
to: firstSelection.to === firstSelection.from ? undefined : firstSelection.to