page.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. if ( ! window.frameElement && window.location.protocol !== 'file:' ) {
  2. // navigates to docs home if direct access, e.g.
  3. // https://threejs.org/docs/pages/BoxGeometry.html
  4. // ->https://threejs.org/docs/#BoxGeometry
  5. const url = new URL( window.location.href );
  6. // hash route, e.g. #BoxGeometry
  7. url.hash = url.pathname.replace( /\/docs\/pages\/(.*?)(?:\.html)?$/, '$1' );
  8. // docs home, e.g. https://threejs.org/docs/
  9. url.pathname = url.pathname.replace( /(\/docs\/).*$/, '$1' );
  10. window.location.replace( url );
  11. }
  12. // Initialize Highlight.js for syntax highlighting
  13. if ( typeof hljs !== 'undefined' ) {
  14. hljs.highlightAll();
  15. }
  16. // Scroll to hash on page load
  17. ( function () {
  18. const hash = window.location.hash.substring( 1 );
  19. if ( hash ) {
  20. const element = document.getElementById( hash );
  21. if ( element ) element.scrollIntoView();
  22. }
  23. } )();
  24. // Update URL hash when clicking on method/property links
  25. ( function () {
  26. const h1 = document.querySelector( 'h1' );
  27. const className = h1 ? h1.textContent.trim() : null;
  28. if ( ! className ) return;
  29. document.addEventListener( 'click', function ( event ) {
  30. const target = event.target.closest( 'a' );
  31. if ( ! target || ! target.hash ) return;
  32. // Check if it's a same-page link (either starting with # or pointing to current page)
  33. const href = target.getAttribute( 'href' );
  34. const isSamePageLink = href.startsWith( '#' ) || ( target.hostname === window.location.hostname && target.pathname === window.location.pathname );
  35. if ( ! isSamePageLink ) return;
  36. const hash = target.hash.substring( 1 );
  37. const newHash = ( hash !== className ) ? `#${className}.${hash}` : `#${hash}`;
  38. const targetWindow = ( window.parent !== window ) ? window.parent : window;
  39. targetWindow.history.pushState( null, '', newHash );
  40. } );
  41. } )();
  42. // Add code copy buttons
  43. ( function addCopyButtons() {
  44. const elements = document.getElementsByTagName( 'pre' );
  45. for ( let i = 0; i < elements.length; i ++ ) {
  46. const element = elements[ i ];
  47. if ( element.classList.contains( 'linenums' ) === false ) {
  48. const copyButton = document.createElement( 'button' );
  49. copyButton.className = 'copy-btn';
  50. element.appendChild( copyButton );
  51. copyButton.addEventListener( 'click', function () {
  52. const codeContent = element.textContent;
  53. navigator.clipboard.writeText( codeContent ).then( () => {
  54. copyButton.classList.add( 'copied' );
  55. setTimeout( () => {
  56. copyButton.classList.remove( 'copied' );
  57. }, 1000 );
  58. } );
  59. } );
  60. }
  61. }
  62. } )();
粤ICP备19079148号