Skip to content

Commit

Permalink
fix(scroll): fix scroll offset calculation
Browse files Browse the repository at this point in the history
Fix offset calculation when first "line-start" attribute found is not 0.
  • Loading branch information
wallpants committed Oct 15, 2023
1 parent 1afd25b commit 8fb397e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/src/web/markdown/cursor-line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const CursorLine = ({ offsets, cursorLineElement, markdownContainerElemen
useEffect(() => {
if (!cursorLineElement || !markdownContainerElement) return;
scroll(markdownContainerElement, topOffsetPct, offsets, cursorLine, cursorLineElement);
}, [cursorLine, cursorLineElement, offsets, topOffsetPct, markdownContainerElement]);
}, [markdownContainerElement, topOffsetPct, offsets, cursorLine, cursorLineElement]);

return (
<div
Expand Down
2 changes: 1 addition & 1 deletion app/src/web/markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const Markdown = ({ className }: { className: string }) => {
if (!markdownElement || !markdownContainerElement) return;

const observer = new ResizeObserver(() => {
setOffsets(getScrollOffsets(markdownContainerElement));
setOffsets(getScrollOffsets(markdownContainerElement, markdownElement));
});

observer.observe(markdownElement);
Expand Down
7 changes: 5 additions & 2 deletions app/src/web/markdown/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ function getAttrs(element: HTMLElement): Attrs {

export type Offsets = [number, HTMLElement][];

export function getScrollOffsets(markdownContainerElement: HTMLElement): Offsets {
export function getScrollOffsets(
markdownContainerElement: HTMLElement,
markdownElement: HTMLElement,
): Offsets {
const elements: NodeListOf<HTMLElement> = document.querySelectorAll("[line-start]");
// HTMLElement kept arround for debugging purposes
const sourceLineOffsets: Offsets = [];
Expand All @@ -62,7 +65,7 @@ export function getScrollOffsets(markdownContainerElement: HTMLElement): Offsets
const { elemStartLine, elemEndLine, offsetTop, scrollHeight } = getAttrs(element);

if (currLine < elemStartLine) {
let acc = 0;
let acc = markdownElement.offsetTop + markdownElement.getBoundingClientRect().top;
let perLine = 0;

const prevElement = elements[index - 1];
Expand Down

0 comments on commit 8fb397e

Please sign in to comment.