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

🐛 Nightmare Bug – Scroll Event Listening Offset Calculation Bug #2230

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions pages/api/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//TODO – check if this is still needed
bradystroud marked this conversation as resolved.
Show resolved Hide resolved
import Airtable from 'airtable'

interface FeedbackRecord {
Expand Down
3 changes: 2 additions & 1 deletion pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as gqlPackage from '@tinacms/graphql-old'
//TODO – check if this is still needed, the graphiql playgrounds have already been replaced
import * as datalayerPackage from '@tinacms/datalayer-old'
import * as gqlPackage from '@tinacms/graphql-old'

bradystroud marked this conversation as resolved.
Show resolved Hide resolved
export default async function feedback(req, res) {
class InMemoryStore extends datalayerPackage.LevelStore {
Expand Down
2 changes: 1 addition & 1 deletion tina/tina-lock.json

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions utils/toc_helpers.ts
bradystroud marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,37 @@ export function createTocListener(
setActiveIds: (activeIds: string[]) => void
): () => void {
let tick = false
const BASE_OFFSET = 16
const THROTTLE_INTERVAL = 100
const headings = createHeadings(contentRef)
//Find the maximum pixel value from vertical scroll
const maxScrollY = document.documentElement.scrollHeight - window.innerHeight;

const relativePositionHeadingMap = headings.map((heading) => {
return {
...heading,
//Find the relative position of the heading in the page content.
relativePagePosition: (heading.offset / contentRef.current.scrollHeight),
}
});

const throttledScroll = () => {
//Find the current vertical scroll pixel value
const scrollPos = window.scrollY
const newActiveIds = []
const activeHeadingCandidates = headings.filter((heading) => {
return heading.offset - scrollPos < BASE_OFFSET
//Find the relative position on the page based on the scroll.
const relativeScrollPosition = scrollPos / maxScrollY
//Find the headings that are above the current scroll position
//This is adjusted to account for differences between min/max scroll values and content height
const activeHeadingCandidates = relativePositionHeadingMap.filter((heading) => {
return relativeScrollPosition >= heading.relativePagePosition
})

const activeHeading =
activeHeadingCandidates.length > 0
? activeHeadingCandidates.reduce((prev, current) =>
prev.offset > current.offset ? prev : current
)
: {}
: headings[0] ?? {}
newActiveIds.push(activeHeading.id)

if (activeHeading.level != 'H2') {
Expand All @@ -60,9 +74,9 @@ export function createTocListener(
? activeHeadingParentCandidates.reduce((prev, current) =>
prev.offset > current.offset ? prev : current
)
: {}
: null

if (activeHeadingParent.id) {
if (activeHeadingParent?.id) {
newActiveIds.push(activeHeadingParent.id)
}
}
Expand Down
Loading