forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#125779 - GuillaumeGomez:copy-code, r=rustdoc-team [rustdoc] Add copy code feature This PR adds a "copy code" to code blocks. Since this is a JS only feature, the HTML is generated with JS when the user hovers the code block to prevent generating DOM unless needed. Two things to note: 1. I voluntarily kept the current behaviour of the run button (only when hovering a code block with a mouse) so it doesn't do anything on mobile. I plan to send a follow-up where the buttons would "expandable" or something. Still need to think which approach would be the best. 2. I used a picture and not text like the run button to remain consistent with the "copy path" button. I'd also prefer for the run button to use a picture (like what is used in mdbook) but again, that's something to be discussed later on. The rendering looks like this: ![Screenshot from 2024-06-03 21-29-48](https://github.com/rust-lang/rust/assets/3050060/a0b18f9c-b3dd-4a65-89a7-5a7a303b5c2b) ![Screenshot from 2024-06-03 21-30-20](https://github.com/rust-lang/rust/assets/3050060/b3b084ff-2716-4160-820b-d4774681a961) It can be tested [here](https://guillaume-gomez.fr/rustdoc/bar/struct.Bar.html) (without the run button) and [here](https://guillaume-gomez.fr/rustdoc/foo/struct.Bar.html) (with the run button). Fixes rust-lang#86851. r? ``@notriddle``
- Loading branch information
Showing
4 changed files
with
193 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Checks that the "copy code" button is not triggering JS error and its display | ||
// isn't broken. | ||
go-to: "file://" + |DOC_PATH| + "/test_docs/fn.foo.html" | ||
|
||
define-function: ( | ||
"check-copy-button", | ||
[], | ||
block { | ||
// First we ensure that there are no "copy code" currently existing. | ||
assert-count: (".example-wrap .copy-button", 0) | ||
move-cursor-to: ".example-wrap" | ||
assert-count: (".example-wrap .copy-button", 1) | ||
// We now ensure it's only displayed when the example is hovered. | ||
assert-css: (".example-wrap .copy-button", { "visibility": "visible" }) | ||
move-cursor-to: ".search-input" | ||
assert-css: (".example-wrap .copy-button", { "visibility": "hidden" }) | ||
// Checking that the copy button has the same size as the "copy path" button. | ||
compare-elements-size: ( | ||
"#copy-path", | ||
".example-wrap:nth-of-type(1) .copy-button", | ||
["height", "width"], | ||
) | ||
}, | ||
) | ||
|
||
call-function: ("check-copy-button", {}) | ||
// Checking that the run button and the copy button have the same height. | ||
compare-elements-size: ( | ||
".example-wrap:nth-of-type(1) .test-arrow", | ||
".example-wrap:nth-of-type(1) .copy-button", | ||
["height"], | ||
) | ||
// ... and the same y position. | ||
compare-elements-position: ( | ||
".example-wrap:nth-of-type(1) .test-arrow", | ||
".example-wrap:nth-of-type(1) .copy-button", | ||
["y"], | ||
) | ||
store-size: (".example-wrap:nth-of-type(1) .copy-button", { | ||
"height": copy_height, | ||
"width": copy_width, | ||
}) | ||
assert: |copy_height| > 0 && |copy_width| > 0 | ||
|
||
// Checking same things for the copy button when there is no run button. | ||
go-to: "file://" + |DOC_PATH| + "/lib2/sub_mod/struct.Foo.html" | ||
call-function: ("check-copy-button", {}) | ||
// Ensure there is no run button. | ||
assert-count: (".example-wrap .test-arrow", 0) | ||
// Check it's the same size without a run button. | ||
assert-size: (".example-wrap:nth-of-type(1) .copy-button", { | ||
"height": |copy_height|, | ||
"width": |copy_width|, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters