Add copy code button · rust-lang/rust@9946a68 (original) (raw)

`@@ -1769,9 +1769,37 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm

`

1769

1769

`}());

`

1770

1770

``

1771

1771

`// This section handles the copy button that appears next to the path breadcrumbs

`

``

1772

`+

// and the copy buttons on the code examples.

`

1772

1773

`(function() {

`

1773

``

`-

let reset_button_timeout = null;

`

``

1774

`+

// Common functions to copy buttons.

`

``

1775

`+

function copyContentToClipboard(content) {

`

``

1776

`+

const el = document.createElement("textarea");

`

``

1777

`+

el.value = content;

`

``

1778

`+

el.setAttribute("readonly", "");

`

``

1779

`+

// To not make it appear on the screen.

`

``

1780

`+

el.style.position = "absolute";

`

``

1781

`+

el.style.left = "-9999px";

`

1774

1782

``

``

1783

`+

document.body.appendChild(el);

`

``

1784

`+

el.select();

`

``

1785

`+

document.execCommand("copy");

`

``

1786

`+

document.body.removeChild(el);

`

``

1787

`+

}

`

``

1788

+

``

1789

`+

function copyButtonAnimation(button) {

`

``

1790

`+

button.classList.add("clicked");

`

``

1791

+

``

1792

`+

if (button.reset_button_timeout !== undefined) {

`

``

1793

`+

window.clearTimeout(button.reset_button_timeout);

`

``

1794

`+

}

`

``

1795

+

``

1796

`+

button.reset_button_timeout = window.setTimeout(() => {

`

``

1797

`+

button.reset_button_timeout = undefined;

`

``

1798

`+

button.classList.remove("clicked");

`

``

1799

`+

}, 1000);

`

``

1800

`+

}

`

``

1801

+

``

1802

`+

// Copy button that appears next to the path breadcrumbs.

`

1775

1803

`const but = document.getElementById("copy-path");

`

1776

1804

`if (!but) {

`

1777

1805

`return;

`

`@@ -1786,29 +1814,49 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm

`

1786

1814

`}

`

1787

1815

`});

`

1788

1816

``

1789

``

`-

const el = document.createElement("textarea");

`

1790

``

`-

el.value = path.join("::");

`

1791

``

`-

el.setAttribute("readonly", "");

`

1792

``

`-

// To not make it appear on the screen.

`

1793

``

`-

el.style.position = "absolute";

`

1794

``

`-

el.style.left = "-9999px";

`

1795

``

-

1796

``

`-

document.body.appendChild(el);

`

1797

``

`-

el.select();

`

1798

``

`-

document.execCommand("copy");

`

1799

``

`-

document.body.removeChild(el);

`

1800

``

-

1801

``

`-

but.classList.add("clicked");

`

``

1817

`+

copyContentToClipboard(path.join("::"));

`

``

1818

`+

copyButtonAnimation(but);

`

``

1819

`+

};

`

1802

1820

``

1803

``

`-

if (reset_button_timeout !== null) {

`

1804

``

`-

window.clearTimeout(reset_button_timeout);

`

``

1821

`+

// Copy buttons on code examples.

`

``

1822

`+

function copyCode(codeElem) {

`

``

1823

`+

if (!codeElem) {

`

``

1824

`+

// Should never happen, but the world is a dark and dangerous place.

`

``

1825

`+

return;

`

1805

1826

`}

`

``

1827

`+

copyContentToClipboard(codeElem.textContent);

`

``

1828

`+

}

`

1806

1829

``

1807

``

`-

function reset_button() {

`

1808

``

`-

reset_button_timeout = null;

`

1809

``

`-

but.classList.remove("clicked");

`

``

1830

`+

function addCopyButton(event) {

`

``

1831

`+

let elem = event.target;

`

``

1832

`+

while (!hasClass(elem, "example-wrap")) {

`

``

1833

`+

elem = elem.parentElement;

`

``

1834

`+

if (elem.tagName === "body" || hasClass(elem, "docblock")) {

`

``

1835

`+

return;

`

``

1836

`+

}

`

``

1837

`+

}

`

``

1838

`+

// Since the button will be added, no need to keep this listener around.

`

``

1839

`+

elem.removeEventListener("mouseover", addCopyButton);

`

``

1840

+

``

1841

`+

const parent = document.createElement("div");

`

``

1842

`+

parent.className = "button-holder";

`

``

1843

`+

const runButton = elem.querySelector(".test-arrow");

`

``

1844

`+

if (runButton !== null) {

`

``

1845

`+

// If there is a run button, we move it into the same div.

`

``

1846

`+

parent.appendChild(runButton);

`

1810

1847

`}

`

``

1848

`+

elem.appendChild(parent);

`

``

1849

`+

const copyButton = document.createElement("button");

`

``

1850

`+

copyButton.className = "copy-button";

`

``

1851

`+

copyButton.title = "Copy code to clipboard";

`

``

1852

`+

copyButton.addEventListener("click", () => {

`

``

1853

`+

copyCode(elem.querySelector("pre > code"));

`

``

1854

`+

copyButtonAnimation(copyButton);

`

``

1855

`+

});

`

``

1856

`+

parent.appendChild(copyButton);

`

``

1857

`+

}

`

1811

1858

``

1812

``

`-

reset_button_timeout = window.setTimeout(reset_button, 1000);

`

1813

``

`-

};

`

``

1859

`+

onEachLazy(document.querySelectorAll(".docblock .example-wrap"), elem => {

`

``

1860

`+

elem.addEventListener("mouseover", addCopyButton);

`

``

1861

`+

});

`

1814

1862

`}());

`