Desktop: Fixes #5918: Scroll jumps when images are rendered in Markdown Editor (#5929)

pull/5984/head
Kenichi Kobayashi 2022-01-09 20:26:03 +09:00 committed by GitHub
parent 5c77317735
commit 70e623e741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 5 deletions

View File

@ -7,6 +7,7 @@ export default function useScrollHandler(editorRef: any, webviewRef: any, onScro
const ignoreNextEditorScrollTime_ = useRef(Date.now());
const ignoreNextEditorScrollEventCount_ = useRef(0);
const delayedSetEditorPercentScrollTimeoutID_ = useRef(null);
const lastResizeHeight_ = useRef(NaN);
// Ignores one next scroll event for a short time.
const ignoreNextEditorScrollEvent = () => {
@ -90,8 +91,7 @@ export default function useScrollHandler(editorRef: any, webviewRef: any, onScro
}, [scheduleOnScroll]);
const editor_scroll = useCallback(() => {
if (isNextEditorScrollEventIgnored()) return;
const ignored = isNextEditorScrollEventIgnored();
const cm = editorRef.current;
if (isCodeMirrorReady(cm)) {
const editorPercent = Math.max(0, Math.min(1, cm.getScrollPercent()));
@ -104,7 +104,9 @@ export default function useScrollHandler(editorRef: any, webviewRef: any, onScro
// calculates GUI-independent line-based percent
const percent = translateScrollPercentE2L(cm, editorPercent);
scrollPercent_.current = percent;
setViewerPercentScroll(percent);
if (!ignored) {
setViewerPercentScroll(percent);
}
}
}
}, [setViewerPercentScroll]);
@ -117,8 +119,14 @@ export default function useScrollHandler(editorRef: any, webviewRef: any, onScro
}, []);
const editor_resize = useCallback((cm) => {
if (cm) {
restoreEditorPercentScroll();
if (isCodeMirrorReady(cm)) {
// Only when resized, the scroll position is restored.
const info = cm.getScrollInfo();
const height = info.height - info.clientHeight;
if (height !== lastResizeHeight_.current) {
restoreEditorPercentScroll();
lastResizeHeight_.current = height;
}
}
}, []);