page.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. window.history.scrollRestoration = 'manual';
  21. window.addEventListener( 'pageshow', function () {
  22. const element = document.getElementById( hash );
  23. if ( element ) element.scrollIntoView();
  24. }, { once: true } );
  25. }
  26. } )();
  27. // Update URL hash when clicking on method/property links
  28. ( function () {
  29. const h1 = document.querySelector( 'h1' );
  30. const className = h1 ? h1.textContent.trim() : null;
  31. if ( ! className ) return;
  32. document.addEventListener( 'click', function ( event ) {
  33. const target = event.target.closest( 'a' );
  34. if ( ! target || ! target.hash ) return;
  35. // Check if it's a same-page link (either starting with # or pointing to current page)
  36. const href = target.getAttribute( 'href' );
  37. const isSamePageLink = href.startsWith( '#' ) || ( target.hostname === window.location.hostname && target.pathname === window.location.pathname );
  38. if ( ! isSamePageLink ) return;
  39. const hash = target.hash.substring( 1 );
  40. const newHash = ( hash !== className ) ? `#${className}.${hash}` : `#${hash}`;
  41. const targetWindow = ( window.parent !== window ) ? window.parent : window;
  42. targetWindow.history.pushState( null, '', newHash );
  43. } );
  44. } )();
  45. // Add code copy buttons
  46. ( function addCopyButtons() {
  47. const elements = document.getElementsByTagName( 'pre' );
  48. for ( let i = 0; i < elements.length; i ++ ) {
  49. const element = elements[ i ];
  50. if ( element.classList.contains( 'linenums' ) === false ) {
  51. const copyButton = document.createElement( 'button' );
  52. copyButton.className = 'copy-btn';
  53. element.appendChild( copyButton );
  54. copyButton.addEventListener( 'click', function () {
  55. const codeContent = element.textContent;
  56. navigator.clipboard.writeText( codeContent ).then( () => {
  57. copyButton.classList.add( 'copied' );
  58. setTimeout( () => {
  59. copyButton.classList.remove( 'copied' );
  60. }, 1000 );
  61. } );
  62. } );
  63. }
  64. }
  65. } )();
粤ICP备19079148号