|
|
|
|
|
|
|
|
|
|
|
import { scrollToAnchor } from './utils'; |
|
|
import { useSystemStore } from '../../store/useSystemStore'; |
|
|
|
|
|
interface NavigationMessage { |
|
|
type: 'navigate'; |
|
|
url: string; |
|
|
} |
|
|
|
|
|
function mdxLinkHandler(anchor: HTMLAnchorElement, win: Window, event: Event): boolean { |
|
|
console.debug("MDX link element found:", anchor.href); |
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
|
let service_path = anchor.href.substring(useSystemStore.getState().baseUrl.length); |
|
|
if (service_path.startsWith('sound')) { |
|
|
console.debug("Audio link detected, playing audio:", anchor.href); |
|
|
|
|
|
const audio = new Audio(anchor.href); |
|
|
audio.play().catch(function(error: Error) { |
|
|
console.debug("Audio play failed:", error); |
|
|
}); |
|
|
} else { |
|
|
|
|
|
console.debug("Sending navigation message:", anchor.href); |
|
|
const message: NavigationMessage = { |
|
|
type: 'navigate', |
|
|
url: anchor.href |
|
|
}; |
|
|
win.parent.postMessage(message, '*'); |
|
|
console.debug("Navigation message sent:", message); |
|
|
} |
|
|
return true; |
|
|
} |
|
|
|
|
|
export function clickHandler(event: Event, cur_doc:Document, cur_win:Window): boolean { |
|
|
console.debug("Click event detected", event.target); |
|
|
|
|
|
let element = event.target as Element | null; |
|
|
while (element && element.tagName !== 'A') { |
|
|
element = element.parentElement; |
|
|
} |
|
|
|
|
|
if (element && element.tagName.toLowerCase() === 'a') { |
|
|
const anchor = element as HTMLAnchorElement; |
|
|
|
|
|
if (anchor.href) { |
|
|
|
|
|
const href = anchor.getAttribute('href'); |
|
|
if (href && href.startsWith('#')) { |
|
|
event.preventDefault(); |
|
|
console.debug("Local fragment link detected, allowing default behavior:", href); |
|
|
scrollToAnchor(cur_doc, href.substring(1)); |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
const currentUrl = cur_win.location.href; |
|
|
const linkUrl = anchor.href; |
|
|
|
|
|
|
|
|
const currentUrlWithoutFragment = currentUrl.split('#')[0]; |
|
|
const linkUrlWithoutFragment = linkUrl.split('#')[0]; |
|
|
|
|
|
|
|
|
if (currentUrlWithoutFragment === linkUrlWithoutFragment && linkUrl.indexOf('#') !== -1) { |
|
|
console.debug("Same-page fragment link detected, allowing default behavior:", linkUrl); |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
if (anchor.href.startsWith(useSystemStore.getState().baseUrl)) { |
|
|
return mdxLinkHandler(anchor, cur_win, event); |
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|