Skip to content

Commit

Permalink
Take advantage of querySelector overload
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Feb 1, 2025
1 parent 01509e0 commit 1b4b78d
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1790,14 +1790,12 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
*/
let pendingSidebarResizingFrame = false;

/** @type {HTMLElement} */
// @ts-expect-error
/** @type {HTMLElement|null} */
const resizer = document.querySelector(".sidebar-resizer");
/** @type {HTMLElement} */
// @ts-expect-error
/** @type {HTMLElement|null} */
const sidebar = document.querySelector(".sidebar");
// If this page has no sidebar at all, bail out.
if (!(resizer instanceof HTMLElement) || !(sidebar instanceof HTMLElement)) {
if (!resizer || !sidebar) {
return;
}

Expand All @@ -1813,7 +1811,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
// from settings.js, which uses a separate function. It's done here because
// the minimum sidebar size is rather uncomfortable, and it must pass
// through that size when using the shrink-to-nothing gesture.
function hideSidebar() {
const hideSidebar = function hideSidebar() {
if (isSrcPage) {
window.rustdocCloseSourceSidebar();
updateLocalStorage("src-sidebar-width", null);
Expand All @@ -1838,30 +1836,30 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
sidebar.style.removeProperty("--desktop-sidebar-width");
resizer.style.removeProperty("--desktop-sidebar-width");
}
}
};

// Call this function to show the sidebar from the resize handle.
// On docs pages, this can only happen if the user has grabbed the resize
// handle, shrunk the sidebar down to nothing, and then pulls back into
// the visible range without releasing it. You can, however, grab the
// resize handle on a source page with the sidebar closed, because it
// remains visible all the time on there.
function showSidebar() {
const showSidebar = function showSidebar() {
if (isSrcPage) {
window.rustdocShowSourceSidebar();
} else {
removeClass(document.documentElement, "hide-sidebar");
updateLocalStorage("hide-sidebar", "false");
}
}
};

/**
* Call this to set the correct CSS variable and setting.
* This function doesn't enforce size constraints. Do that before calling it!
*
* @param {number} size - CSS px width of the sidebar.
*/
function changeSidebarSize(size) {
const changeSidebarSize = function changeSidebarSize(size) {
if (isSrcPage) {
updateLocalStorage("src-sidebar-width", size.toString());
// [RUSTDOCIMPL] CSS variable fast path
Expand All @@ -1877,7 +1875,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
sidebar.style.setProperty("--desktop-sidebar-width", size + "px");
resizer.style.setProperty("--desktop-sidebar-width", size + "px");
}
}
};

// Check if the sidebar is hidden. Since src pages and doc pages have
// different settings, this function has to check that.
Expand Down Expand Up @@ -1942,7 +1940,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
/**
* @param {PointerEvent=} e
*/
function stopResize(e) {
const stopResize = function stopResize(e) {
if (currentPointerId === null) {
return;
}
Expand All @@ -1959,12 +1957,12 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
resizer.releasePointerCapture(currentPointerId);
currentPointerId = null;
}
}
};

/**
* @param {PointerEvent} e
*/
function initResize(e) {
const initResize = function initResize(e) {
if (currentPointerId !== null || e.altKey || e.ctrlKey || e.metaKey || e.button !== 0) {
return;
}
Expand All @@ -1988,7 +1986,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
const pos = e.clientX - sidebar.offsetLeft - 3;
document.documentElement.style.setProperty( "--resizing-sidebar-width", pos + "px");
desiredSidebarSize = null;
}
};
resizer.addEventListener("pointerdown", initResize, false);
}());

Expand Down

0 comments on commit 1b4b78d

Please sign in to comment.