style: fix code formatting with Prettier

- Format popover.inline.ts and util.ts according to project style guidelines
- Ensure consistent code style across navigation enhancement files
This commit is contained in:
neerajadhav 2025-07-07 19:19:44 +05:30
parent 9322c199da
commit 1f6cbcbb71
2 changed files with 57 additions and 47 deletions

View File

@ -39,32 +39,32 @@ async function mouseEnterHandler(
if (popoverInner) { if (popoverInner) {
const hashWithoutPound = targetHash.slice(1) const hashWithoutPound = targetHash.slice(1)
const targetAnchor = `popover-internal-${hashWithoutPound}` const targetAnchor = `popover-internal-${hashWithoutPound}`
// Try to find the element by ID first // Try to find the element by ID first
let heading = popoverInner.querySelector(`#${targetAnchor}`) as HTMLElement | null let heading = popoverInner.querySelector(`#${targetAnchor}`) as HTMLElement | null
// If not found by ID, try to find by text content (fallback for headings) // If not found by ID, try to find by text content (fallback for headings)
if (!heading) { if (!heading) {
const headings = popoverInner.querySelectorAll('h1, h2, h3, h4, h5, h6') const headings = popoverInner.querySelectorAll("h1, h2, h3, h4, h5, h6")
for (const h of headings) { for (const h of headings) {
const headingElement = h as HTMLElement const headingElement = h as HTMLElement
const headingText = headingElement.textContent?.trim().toLowerCase() || '' const headingText = headingElement.textContent?.trim().toLowerCase() || ""
const targetText = hashWithoutPound.toLowerCase().replace(/-/g, ' ') const targetText = hashWithoutPound.toLowerCase().replace(/-/g, " ")
// More strict matching: avoid matching very short headings unless they're exact matches // More strict matching: avoid matching very short headings unless they're exact matches
const isExactMatch = headingText === targetText const isExactMatch = headingText === targetText
const isSubstringMatch = headingText.length >= 3 && ( const isSubstringMatch =
headingText.includes(targetText) || headingText.length >= 3 &&
(targetText.length >= 3 && targetText.includes(headingText)) (headingText.includes(targetText) ||
) (targetText.length >= 3 && targetText.includes(headingText)))
if (isExactMatch || isSubstringMatch) { if (isExactMatch || isSubstringMatch) {
heading = headingElement heading = headingElement
break break
} }
} }
} }
if (heading) { if (heading) {
// Use utility function to scroll with buffer and highlight // Use utility function to scroll with buffer and highlight
scrollInContainerToElement(popoverInner, heading, 20, true, "instant") scrollInContainerToElement(popoverInner, heading, 20, true, "instant")
@ -168,8 +168,8 @@ document.addEventListener("nav", () => {
link.addEventListener("mouseenter", mouseEnterHandler) link.addEventListener("mouseenter", mouseEnterHandler)
link.addEventListener("mouseleave", clearActivePopoverAndHighlights) link.addEventListener("mouseleave", clearActivePopoverAndHighlights)
// Use type assertion to avoid TypeScript error when checking individual files // Use type assertion to avoid TypeScript error when checking individual files
if (typeof (window as any).addCleanup === 'function') { if (typeof (window as any).addCleanup === "function") {
(window as any).addCleanup(() => { ;(window as any).addCleanup(() => {
link.removeEventListener("mouseenter", mouseEnterHandler) link.removeEventListener("mouseenter", mouseEnterHandler)
link.removeEventListener("mouseleave", clearActivePopoverAndHighlights) link.removeEventListener("mouseleave", clearActivePopoverAndHighlights)
}) })

View File

@ -15,12 +15,12 @@ export function registerEscapeHandler(outsideContainer: HTMLElement | null, cb:
outsideContainer?.addEventListener("click", click) outsideContainer?.addEventListener("click", click)
// Use type assertion to avoid TypeScript error when checking individual files // Use type assertion to avoid TypeScript error when checking individual files
if (typeof (window as any).addCleanup === 'function') { if (typeof (window as any).addCleanup === "function") {
(window as any).addCleanup(() => outsideContainer?.removeEventListener("click", click)) ;(window as any).addCleanup(() => outsideContainer?.removeEventListener("click", click))
} }
document.addEventListener("keydown", esc) document.addEventListener("keydown", esc)
if (typeof (window as any).addCleanup === 'function') { if (typeof (window as any).addCleanup === "function") {
(window as any).addCleanup(() => document.removeEventListener("keydown", esc)) ;(window as any).addCleanup(() => document.removeEventListener("keydown", esc))
} }
} }
@ -52,9 +52,9 @@ export async function fetchCanonical(url: URL): Promise<Response> {
// Keep track of active highlights to clean them up // Keep track of active highlights to clean them up
let activeHighlights: Set<{ let activeHighlights: Set<{
element: HTMLElement, element: HTMLElement
originalBackground: string, originalBackground: string
originalTransition: string, originalTransition: string
timeoutId: number timeoutId: number
}> = new Set() }> = new Set()
@ -69,16 +69,18 @@ export function clearAllHighlights() {
element.style.transition = originalTransition element.style.transition = originalTransition
}) })
activeHighlights.clear() activeHighlights.clear()
// Also clear any highlights that might not be tracked (backup cleanup) // Also clear any highlights that might not be tracked (backup cleanup)
// This catches highlights in popovers or other edge cases // This catches highlights in popovers or other edge cases
const allHighlightedElements = document.querySelectorAll('[style*="background-color"]') const allHighlightedElements = document.querySelectorAll('[style*="background-color"]')
allHighlightedElements.forEach((el) => { allHighlightedElements.forEach((el) => {
const element = el as HTMLElement const element = el as HTMLElement
if (element.style.backgroundColor.includes('var(--highlight') || if (
element.style.backgroundColor.includes('#ffeb3b')) { element.style.backgroundColor.includes("var(--highlight") ||
element.style.backgroundColor = '' element.style.backgroundColor.includes("#ffeb3b")
element.style.transition = '' ) {
element.style.backgroundColor = ""
element.style.transition = ""
} }
}) })
} }
@ -89,7 +91,11 @@ export function clearAllHighlights() {
* @param duration - How long to show the highlight in milliseconds (default: 2000) * @param duration - How long to show the highlight in milliseconds (default: 2000)
* @param color - The highlight color (default: uses CSS variable --highlight) * @param color - The highlight color (default: uses CSS variable --highlight)
*/ */
export function highlightElement(el: HTMLElement, duration: number = 2000, color: string = 'var(--highlight, #ffeb3b40)') { export function highlightElement(
el: HTMLElement,
duration: number = 2000,
color: string = "var(--highlight, #ffeb3b40)",
) {
// Clear any existing highlight on this element // Clear any existing highlight on this element
activeHighlights.forEach((highlight) => { activeHighlights.forEach((highlight) => {
if (highlight.element === el) { if (highlight.element === el) {
@ -97,15 +103,15 @@ export function highlightElement(el: HTMLElement, duration: number = 2000, color
activeHighlights.delete(highlight) activeHighlights.delete(highlight)
} }
}) })
// Store original styles // Store original styles
const originalBackground = el.style.backgroundColor const originalBackground = el.style.backgroundColor
const originalTransition = el.style.transition const originalTransition = el.style.transition
// Apply highlight styles // Apply highlight styles
el.style.transition = 'background-color 0.3s ease' el.style.transition = "background-color 0.3s ease"
el.style.backgroundColor = color el.style.backgroundColor = color
// Set up cleanup // Set up cleanup
const timeoutId = window.setTimeout(() => { const timeoutId = window.setTimeout(() => {
el.style.backgroundColor = originalBackground el.style.backgroundColor = originalBackground
@ -120,13 +126,13 @@ export function highlightElement(el: HTMLElement, duration: number = 2000, color
}) })
}, 300) }, 300)
}, duration) }, duration)
// Track this highlight // Track this highlight
activeHighlights.add({ activeHighlights.add({
element: el, element: el,
originalBackground, originalBackground,
originalTransition, originalTransition,
timeoutId timeoutId,
}) })
} }
@ -136,22 +142,26 @@ export function highlightElement(el: HTMLElement, duration: number = 2000, color
* @param buffer - Additional buffer space in pixels (default: 20) * @param buffer - Additional buffer space in pixels (default: 20)
* @param highlight - Whether to highlight the element after scrolling (default: true) * @param highlight - Whether to highlight the element after scrolling (default: true)
*/ */
export function scrollToElementWithBuffer(el: HTMLElement, buffer: number = 20, highlight: boolean = true) { export function scrollToElementWithBuffer(
el: HTMLElement,
buffer: number = 20,
highlight: boolean = true,
) {
// Get the height of the header to calculate buffer // Get the height of the header to calculate buffer
const header = document.querySelector('.page-header') as HTMLElement const header = document.querySelector(".page-header") as HTMLElement
const headerHeight = header ? header.offsetHeight : 0 const headerHeight = header ? header.offsetHeight : 0
const totalOffset = headerHeight + buffer const totalOffset = headerHeight + buffer
// Calculate the target position // Calculate the target position
const elementTop = el.offsetTop const elementTop = el.offsetTop
const targetPosition = elementTop - totalOffset const targetPosition = elementTop - totalOffset
// Scroll to the calculated position // Scroll to the calculated position
window.scrollTo({ window.scrollTo({
top: Math.max(0, targetPosition), top: Math.max(0, targetPosition),
behavior: 'smooth' behavior: "smooth",
}) })
// Add highlight effect if requested // Add highlight effect if requested
if (highlight) { if (highlight) {
highlightElement(el) highlightElement(el)
@ -167,20 +177,20 @@ export function scrollToElementWithBuffer(el: HTMLElement, buffer: number = 20,
* @param behavior - Scroll behavior (default: 'instant') * @param behavior - Scroll behavior (default: 'instant')
*/ */
export function scrollInContainerToElement( export function scrollInContainerToElement(
container: HTMLElement, container: HTMLElement,
target: HTMLElement, target: HTMLElement,
buffer: number = 20, buffer: number = 20,
highlight: boolean = true, highlight: boolean = true,
behavior: ScrollBehavior = 'instant' behavior: ScrollBehavior = "instant",
) { ) {
// Use requestAnimationFrame to ensure content is rendered before scrolling // Use requestAnimationFrame to ensure content is rendered before scrolling
requestAnimationFrame(() => { requestAnimationFrame(() => {
const targetPosition = target.offsetTop - buffer const targetPosition = target.offsetTop - buffer
container.scroll({ container.scroll({
top: Math.max(0, targetPosition), top: Math.max(0, targetPosition),
behavior behavior,
}) })
// Add highlight effect if requested // Add highlight effect if requested
if (highlight) { if (highlight) {
// Small delay to ensure scroll completes before highlighting // Small delay to ensure scroll completes before highlighting