File size: 1,562 Bytes
07bbbbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
export function scrollToAnchor(doc: Document, anchor: string): void {
if (doc) {
const selectors = [
`a[name="${anchor}"]`,
`a[id="${anchor}"]`,
`[id="${anchor}"]`,
];
let targetElement: HTMLElement | null = null;
for (const selector of selectors) {
targetElement = doc.querySelector(selector);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'center' //maybe 'start' is better?
});
break;
}
}
}
}
// For search and highlight
// @ts-ignore - Reserved for future use
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function dimScreen(iframe: HTMLIFrameElement): void {
const curtain = document.createElement("div");
Object.assign(curtain.style, {
position: 'fixed',
left: '0',
top: '0',
width: '100%',
height: '100%',
background: 'black',
opacity: '0.2',
zIndex: '999'
});
curtain.id = "__mdx_overlay_id";
iframe.contentDocument?.body.appendChild(curtain);
}
// @ts-ignore - Reserved for future use
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function undimScreen(iframe: HTMLIFrameElement): void {
const element = iframe.contentDocument?.getElementById("__mdx_overlay_id");
if (element && element.parentNode)
element.parentNode.removeChild(element);
} |