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

Docs: Add code copy button. #30611

Merged
merged 1 commit into from
Feb 25, 2025
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
5 changes: 5 additions & 0 deletions src/cameras/OrthographicCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { Camera } from './Camera.js';
* constant regardless of its distance from the camera. This can be useful
* for rendering 2D scenes and UI elements, amongst other things.
*
* ```js
* const camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 1, 1000 );
* scene.add( camera );
* ```
*
* @augments Camera
*/
class OrthographicCamera extends Camera {
Expand Down
5 changes: 5 additions & 0 deletions src/cameras/PerspectiveCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const _maxTarget = /*@__PURE__*/ new Vector2();
* This projection mode is designed to mimic the way the human eye sees. It
* is the most common projection mode used for rendering a 3D scene.
*
* ```js
* const camera = new THREE.PerspectiveCamera( 45, width / height, 1, 1000 );
* scene.add( camera );
* ```
*
* @augments Camera
*/
class PerspectiveCamera extends Camera {
Expand Down
42 changes: 42 additions & 0 deletions utils/docs/template/static/scripts/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,48 @@ const expandButton = document.getElementById( 'expandButton' );
const clearSearchButton = document.getElementById( 'clearSearchButton' );
const filterInput = document.getElementById( 'filterInput' );

// code copy buttons

const elements = document.getElementsByTagName( 'pre' );

for ( let i = 0; i < elements.length; i ++ ) {

const element = elements[ i ];

if ( element.classList.contains( 'linenums' ) === false ) {

addCopyButton( element );

}

}

function addCopyButton( element ) {

const copyButton = document.createElement( 'button' );
copyButton.className = 'copy-btn';

element.appendChild( copyButton );

copyButton.addEventListener( 'click', function () {

const codeContent = element.textContent;
navigator.clipboard.writeText( codeContent ).then( () => {

copyButton.classList.add( 'copied' );

setTimeout( () => {

copyButton.classList.remove( 'copied' );

}, 1000 );

} );

} );

}

// Functionality for hamburger button (on small devices)

expandButton.onclick = function ( event ) {
Expand Down
27 changes: 27 additions & 0 deletions utils/docs/template/static/styles/page.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ h3 {
white-space: pre-wrap;
font-size: calc(var(--font-size) - 1px);
line-height: calc(var(--line-height) - 1px);
position: relative;
}

pre code {
Expand Down Expand Up @@ -210,6 +211,32 @@ h3 {
color: var(--text-color);
}

.copy-btn {
cursor: pointer;
position: absolute;
top: 16px;
right: 16px;
width: 24px;
height: 24px;
background-color: transparent;
background-image: url('/files/ic_copy_grey_24dp.svg');
background-size: contain;
background-position: center;
background-repeat: no-repeat;
opacity: 0.9;
border: none;
}

.copy-btn:hover {
opacity: 1;
}

.copy-btn.copied {
pointer-events: none;
opacity: 1;
background-image: url('/files/ic_tick_green_24dp.svg');
}

}

@media (prefers-color-scheme: dark) {
Expand Down
Loading