Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(code-view): correctly scroll to selected line #9160

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/ui/code-view/code-view.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
background-color: rgb(97, 82, 207, 0.2) !important; // Ensure that this color is applied to all children as well
}

.highlightedRow {
scroll-margin-bottom: 100px;
scroll-margin-top: 50px;
}

:global {
// after we set the line number to be inline we need to override the styles to make it look like it used to
.linenumber {
Expand Down
63 changes: 52 additions & 11 deletions components/ui/code-view/code-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ function joinPaths(base: string, relative: string) {
return newPath.startsWith('/') ? newPath : `/${newPath}`;
}

function useInViewport(ref: React.RefObject<HTMLElement>) {
const [isInViewport, setIsInViewport] = React.useState(false);

React.useEffect(() => {
if (!ref.current) return;

const observer = new IntersectionObserver(
([entry]) => {
setIsInViewport(entry.isIntersecting);
},
{
root: null,
threshold: 0.1,
}
);

observer.observe(ref.current);

return () => {
if (ref.current) {
observer.unobserve(ref.current);
}
};
}, [ref]);

return isInViewport;
}

export function CodeView({
className,
componentId,
Expand All @@ -97,8 +125,10 @@ export function CodeView({
const loading = loadingFromProps || loadingFileContent;
const location = useLocation();
const navigate = useNavigate();
const [scrollBlock, setScrollBlock] = React.useState<'nearest' | 'center'>('center');
const [isHighlightedState, setIsHighlightedState] = React.useState(false);
const highlightedLineRef = React.useRef<HTMLDivElement>(null);
const isInViewport = useInViewport(highlightedLineRef);

const fileContent = currentFileContent || downloadedFileContent;
const title = useMemo(() => currentFile?.split('/').pop(), [currentFile]);
const lang = useMemo(() => {
Expand All @@ -114,15 +144,15 @@ export function CodeView({
const searchKeyword = extractSearchKeyword(location?.hash);
const lineRange = extractLineRange(location?.hash);

React.useEffect(() => {
if (highlightedLineRef?.current) {
highlightedLineRef?.current.scrollIntoView({ behavior: 'smooth', block: scrollBlock });
React.useLayoutEffect(() => {
if (highlightedLineRef?.current && isHighlightedState && !isInViewport) {
highlightedLineRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}, [highlightedLineRef?.current, scrollBlock]);
}, [isHighlightedState, isInViewport]);

const onLineClicked = React.useCallback(
(_lineNumber: number) => () => {
setScrollBlock('nearest');
// setScrollBlock('nearest');
// If the line number is already highlighted, remove the hash
if (
lineNumber === _lineNumber ||
Expand All @@ -139,9 +169,9 @@ export function CodeView({
const customRenderer = React.useCallback(
({ rows, stylesheet, useInlineStyles }) => {
let isKeywordHighlighted = false;
let refAssigned = false;

return rows.map((node, index) => {
// console.log('🚀 ~ returnrows.map ~ node:', node);
const lineText = node.children
.map((child) => child.children?.[0]?.value ?? '')
.join('')
Expand Down Expand Up @@ -205,9 +235,11 @@ export function CodeView({
key: `line-number-${index}`,
});

const highlightedRef = !isInRange
? (isHighlighted && highlightedLineRef) || null
: (lineNum === lineRange.start && highlightedLineRef) || null;
const highlightedRef = !refAssigned && isHighlighted ? ((refAssigned = true), highlightedLineRef) : null;

if (isHighlighted && !isHighlightedState && highlightedRef) {
setIsHighlightedState(true);
}

return (
<div
Expand All @@ -227,7 +259,16 @@ export function CodeView({
);
});
},
[onLineClicked, searchKeyword, lineNumber, lineRange, coreAspects, depsByPackageName.size, currentFile]
[
onLineClicked,
searchKeyword,
lineNumber,
lineRange,
coreAspects,
depsByPackageName.size,
currentFile,
isHighlightedState,
]
);

const getSelectedLineRange = () => {
Expand Down