mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 12:25:22 +00:00
improve scroll experience
This commit is contained in:
parent
23057d8a05
commit
e165d5e590
1 changed files with 13 additions and 7 deletions
|
|
@ -80,7 +80,6 @@ export const PureCodePreviewPanel = ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}, [highlightRangeQuery]);
|
}, [highlightRangeQuery]);
|
||||||
|
|
||||||
const extensions = useMemo(() => {
|
const extensions = useMemo(() => {
|
||||||
|
|
@ -108,20 +107,27 @@ export const PureCodePreviewPanel = ({
|
||||||
|
|
||||||
// Scroll the highlighted range into view.
|
// Scroll the highlighted range into view.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!highlightRange || !editorRef || !editorRef.state) {
|
if (!highlightRange || !editorRef || !editorRef.state || !editorRef.view) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const doc = editorRef.state.doc;
|
const doc = editorRef.state.doc;
|
||||||
const { start, end } = highlightRange;
|
const { start, end } = highlightRange;
|
||||||
const selection = EditorSelection.range(
|
|
||||||
doc.line(start.lineNumber).from,
|
const from = doc.line(start.lineNumber).from;
|
||||||
doc.line(end.lineNumber).from,
|
const to = doc.line(end.lineNumber).to;
|
||||||
);
|
const selection = EditorSelection.range(from, to);
|
||||||
|
|
||||||
|
// When the selection is in view, we don't want to perform any scrolling
|
||||||
|
// as it could be jarring for the user. If it is not in view, scroll to the
|
||||||
|
// center of the viewport.
|
||||||
|
const viewport = editorRef.view.viewport;
|
||||||
|
const isInView = from >= viewport.from && to <= viewport.to;
|
||||||
|
const scrollStrategy = isInView ? "nearest" : "center";
|
||||||
|
|
||||||
editorRef.view?.dispatch({
|
editorRef.view?.dispatch({
|
||||||
effects: [
|
effects: [
|
||||||
EditorView.scrollIntoView(selection, { y: "center" }),
|
EditorView.scrollIntoView(selection, { y: scrollStrategy }),
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
}, [editorRef, highlightRange]);
|
}, [editorRef, highlightRange]);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue