Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate scraped examples buttons in JS #130069

Merged
merged 1 commit into from
Sep 19, 2024
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
1 change: 0 additions & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,6 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
&cx.root_path(),
highlight::DecorationInfo(decoration_info),
sources::SourceContext::Embedded(sources::ScrapedInfo {
needs_prev_next_buttons: line_ranges.len() > 1,
needs_expansion,
offset: line_min,
name: &call_data.display_name,
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ where

pub(crate) struct ScrapedInfo<'a> {
pub(crate) offset: usize,
pub(crate) needs_prev_next_buttons: bool,
pub(crate) name: &'a str,
pub(crate) url: &'a str,
pub(crate) title: &'a str,
Expand Down
14 changes: 8 additions & 6 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1855,12 +1855,8 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
// Since the button will be added, no need to keep this listener around.
elem.removeEventListener("mouseover", addCopyButton);

// If this is a scrapped example, there will already be a "button-holder" element.
let parent = elem.querySelector(".button-holder");
if (!parent) {
parent = document.createElement("div");
parent.className = "button-holder";
}
const parent = document.createElement("div");
parent.className = "button-holder";

const runButton = elem.querySelector(".test-arrow");
if (runButton !== null) {
Expand All @@ -1876,6 +1872,12 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
copyButtonAnimation(copyButton);
});
parent.appendChild(copyButton);

if (!elem.parentElement.classList.contains("scraped-example")) {
return;
}
const scrapedWrapped = elem.parentElement;
window.updateScrapedExample(scrapedWrapped, parent);
}

function showHideCodeExampleButtons(event) {
Expand Down
51 changes: 34 additions & 17 deletions src/librustdoc/html/static/js/scrape-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,30 @@
elt.querySelector(".rust").scrollTo(0, scrollOffset);
}

function updateScrapedExample(example, isHidden) {
const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
function createScrapeButton(parent, className, content) {
const button = document.createElement("button");
button.className = className;
button.innerText = content;
parent.insertBefore(button, parent.firstChild);
return button;
}

window.updateScrapedExample = (example, buttonHolder) => {
let locIndex = 0;
const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
const link = example.querySelector(".scraped-example-title a");
let expandButton = null;

if (!example.classList.contains("expanded")) {
expandButton = createScrapeButton(buttonHolder, "expand", "↕");
}
const isHidden = example.parentElement.classList.contains("more-scraped-examples");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed something while hunting through the code behind this variable.

image

Notice something wrong with this screenshot?

According to this code, the isHidden variable is actually used because the number of lines is supposed to be different depending on whether it's hidden or not. It looks like this was silently broken and nobody noticed.

Is this important, or should it just be simplified away?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a question for @willcrichton. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an issue with this PR? If I glance at the Bevy docs, they seem to show the correct behavior:
Screenshot 2024-09-07 at 3 33 24 PM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link you gave is for 0.14.1. Try 0.14.2.

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm planning to send a follow-up PR to revert the height change since it's not linked to the current PR. Does it sound good to you @notriddle ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. 👍


const locs = example.locs;
if (locs.length > 1) {
const next = createScrapeButton(buttonHolder, "next", "≻");
const prev = createScrapeButton(buttonHolder, "prev", "≺");

// Toggle through list of examples in a given file
const onChangeLoc = changeIndex => {
removeClass(highlights[locIndex], "focus");
Expand All @@ -57,22 +74,19 @@
link.innerHTML = title;
};

example.querySelector(".prev")
.addEventListener("click", () => {
onChangeLoc(() => {
locIndex = (locIndex - 1 + locs.length) % locs.length;
});
prev.addEventListener("click", () => {
onChangeLoc(() => {
locIndex = (locIndex - 1 + locs.length) % locs.length;
});
});

example.querySelector(".next")
.addEventListener("click", () => {
onChangeLoc(() => {
locIndex = (locIndex + 1) % locs.length;
});
next.addEventListener("click", () => {
onChangeLoc(() => {
locIndex = (locIndex + 1) % locs.length;
});
});
}

const expandButton = example.querySelector(".expand");
if (expandButton) {
expandButton.addEventListener("click", () => {
if (hasClass(example, "expanded")) {
Expand All @@ -83,13 +97,16 @@
}
});
}
};

function setupLoc(example, isHidden) {
example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
// Start with the first example in view
scrollToLoc(example, locs[0][0], isHidden);
scrollToLoc(example, example.locs[0][0], isHidden);
}

const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example");
onEachLazy(firstExamples, el => updateScrapedExample(el, false));
onEachLazy(firstExamples, el => setupLoc(el, false));
onEachLazy(document.querySelectorAll(".more-examples-toggle"), toggle => {
// Allow users to click the left border of the <details> section to close it,
// since the section can be large and finding the [+] button is annoying.
Expand All @@ -102,11 +119,11 @@
const moreExamples = toggle.querySelectorAll(".scraped-example");
toggle.querySelector("summary").addEventListener("click", () => {
// Wrapping in setTimeout ensures the update happens after the elements are actually
// visible. This is necessary since updateScrapedExample calls scrollToLoc which
// visible. This is necessary since setupLoc calls scrollToLoc which
// depends on offsetHeight, a property that requires an element to be visible to
// compute correctly.
setTimeout(() => {
onEachLazy(moreExamples, el => updateScrapedExample(el, true));
onEachLazy(moreExamples, el => setupLoc(el, true));
});
}, {once: true});
});
Expand Down
15 changes: 2 additions & 13 deletions src/librustdoc/html/templates/scraped_source.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="scraped-example{% if !info.needs_expansion +%} expanded{% endif %}" data-locs="{{info.locations}}"> {# #}
<div class="scraped-example-title">
{{info.name +}} (<a href="{{info.url}}">{{info.title}}</a>) {# #}
</div>
<div class="example-wrap"> {# #}
</div> {# #}
<div class="example-wrap">
{# https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
Do not show "1 2 3 4 5 ..." in web search results. #}
<div class="src-line-numbers" data-nosnippet> {# #}
Expand All @@ -18,16 +18,5 @@
{{code_html|safe}}
</code> {# #}
</pre> {# #}
{% if info.needs_prev_next_buttons || info.needs_expansion %}
<div class="button-holder">
{% if info.needs_prev_next_buttons %}
<button class="prev">&pr;</button> {# #}
<button class="next">&sc;</button>
{% endif %}
{% if info.needs_expansion %}
<button class="expand">&varr;</button>
{% endif %}
</div>
{% endif %}
</div> {# #}
</div> {# #}
Loading