Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
79 changes: 57 additions & 22 deletions src/components/LeftSidebar/helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const addExpandCollapseImages = (
nav.innerHTML = navContent;
nav.classList.add('navWrapper');

nav.querySelectorAll('li').forEach((el, i) => {
nav.querySelectorAll('li').forEach((el, index) => {
if (el.children.length === 2) {
const paragraphElement = el.children[0];
if (paragraphElement.childNodes.length < 2) {
Expand All @@ -37,23 +37,33 @@ export const addExpandCollapseImages = (
const spanElementParent = document.createElement('span');
spanElementParent.classList.add('iconSpan');
const spanElementChild = document.createElement('span');
if (tabsClosed[text] === undefined || !tabsClosed[text]) {

// Default state is collapsed (i.e., closed)
spanElementChild.innerHTML = ArrowForwardHTML;
el.children[1].classList.add('displayNone');

// Only open if explicitly marked as open in tabsClosed
if (tabsClosed[text] === true) {
spanElementChild.innerHTML = ArrowDownHTML;
} else {
spanElementChild.innerHTML = ArrowForwardHTML;
el.children[1].classList.add('displayNone');
el.children[1].classList.remove('displayNone');
}

// Checking if this div contains the active link
// Check if this div contains the active link
let containsActivePage = false;
const allLinks = el.children[1].querySelectorAll('a');
for (let i = 0; i < allLinks.length; i++) {
const splitArr = allLinks[i].href.split('=');
for (let j = 0; j < allLinks.length; j++) {
const splitArr = allLinks[j].href.split('=');
if (splitArr.length > 1 && splitArr[1] === pageId) {
spanElementChild.innerHTML = ArrowDownHTML;
el.children[1].classList.remove('displayNone');
containsActivePage = true;
break;
}
}

// If this section contains the active page, always expand it
if (containsActivePage) {
spanElementChild.innerHTML = ArrowDownHTML;
el.children[1].classList.remove('displayNone');
}

// Adding arrow icon to the p tags
spanElementParent.appendChild(spanElementChild);
Expand Down Expand Up @@ -90,20 +100,23 @@ const isLinkMatching = (

// Tutorials module pages have pageids like {subdirectory}_{real_url_ending}, must be split to generate matching URL
const pageIdSplit = pageid.split('__');
if (pageIdSplit.length > 1){
return (
href.includes(`pageid=${pageid}`) ||
href.includes(`/${encodeURI(pageIdSplit[0])}/${encodeURI(pageIdSplit[1])}#`) ||
href.endsWith(`/${encodeURI(pageIdSplit[0])}/${encodeURI(pageIdSplit[1])}`)
);
}
else {
if (pageIdSplit.length > 1) {
return (
href.includes(`pageid=${pageid}`) ||
href.includes(`/${encodeURI(pageid)}#`) ||
href.endsWith(`/${encodeURI(pageid)}`)
href.includes(
`/${encodeURI(pageIdSplit[0])}/${encodeURI(pageIdSplit[1])}#`,
) ||
href.endsWith(
`/${encodeURI(pageIdSplit[0])}/${encodeURI(pageIdSplit[1])}`,
)
);
}

return (
href.includes(`pageid=${pageid}`) ||
href.includes(`/${encodeURI(pageid)}#`) ||
href.endsWith(`/${encodeURI(pageid)}`)
);
};

const isCurrentNavOpen = (liEle: HTMLLIElement, activePageid: string) => {
Expand Down Expand Up @@ -145,6 +158,25 @@ export const collapseAndExpandLeftNav = (
doc?.querySelectorAll(selectors.links).forEach((link) => {
link.addEventListener('click', () => {
setLeftNavOpen(false);

// When a link is clicked, store its parent's expanded state
// This will prevent the menu from collapsing during navigation
const parentLi = link.closest('li');
if (
parentLi &&
parentLi.parentElement &&
parentLi.parentElement.parentElement
) {
const grandparentLi = parentLi.parentElement.parentElement;
if (
grandparentLi.tagName === 'LI' &&
grandparentLi.children.length === 2
) {
const headerText = (grandparentLi
.children[0] as HTMLParagraphElement).innerText;
toggleExpandOnTab(headerText);
}
}
});
});

Expand All @@ -157,8 +189,11 @@ export const collapseAndExpandLeftNav = (

const isOpen = isCurrentNavOpen(el, activePageid);
const divElement = el.children[1];
if (!isOpen) {
divElement.classList.toggle('displayNone');

// REMOVED: The code that was auto-collapsing menus here
// Only expand menus containing the active page, don't collapse others
if (isOpen) {
divElement.classList.remove('displayNone');
}

if (spanElement && (spanElement.children[0] as HTMLImageElement)) {
Expand Down
12 changes: 0 additions & 12 deletions src/components/LeftSidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ const LeftSideBar = (props: {
tag.classList.add('active');
}

divElement.querySelectorAll('a').forEach((link) => {
const href = link.getAttribute('href');
if (href && !href.includes('://') && !href.startsWith('#')) {
// Only modify internal links (not external or anchor links)
const hasParams = href.includes('?');
const separator = hasParams ? '&' : '?';
link.setAttribute(
'href',
`${href}${separator}isDarkMode=${props.isDarkMode}`,
);
}
});
const updatedHTML = addExpandCollapseImages(
divElement.innerHTML,
params[TS_PAGE_ID_PARAM],
Expand Down
Loading