lesson.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Licensed under a BSD license. See license.html for license
  2. /* eslint-disable strict */
  3. 'use strict'; // eslint-disable-line
  4. ( function () {
  5. if ( window.frameElement ) {
  6. // in iframe
  7. document.querySelectorAll( 'a' ).forEach( a => {
  8. // we have to send all links to the parent
  9. // otherwise we'll end up with 3rd party
  10. // sites under the frame.
  11. a.addEventListener( 'click', e => {
  12. // opening a new tab?
  13. if ( a.target === '_blank' ) {
  14. return;
  15. }
  16. // change changing hashes?
  17. if ( a.origin !== window.location.origin || a.pathname !== window.location.pathname ) {
  18. e.preventDefault();
  19. }
  20. window.parent.setUrl( a.href );
  21. } );
  22. } );
  23. window.parent.setTitle( document.title );
  24. } else {
  25. if ( window.location.protocol !== 'file:' ) {
  26. const re = /^(.*?\/manual\/)(.*?)$/;
  27. const [ , baseURL, articlePath ] = re.exec( window.location.href );
  28. const href = `${baseURL}#${articlePath.replace( '.html', '' )}`;
  29. window.location.replace( href ); // lgtm[js/client-side-unvalidated-url-redirection]
  30. }
  31. }
  32. const parts = window.location.href.split( '/' );
  33. const filename = parts[ parts.length - 1 ];
  34. if ( filename !== 'primitives.html' ) {
  35. let text = document.body.innerHTML;
  36. text = text.replace( /\[link:([\w\:\/\.\-\_\(\)\?\#\=\!\~]+)\]/gi, '<a href="$1" target="_blank">$1</a>' ); // [link:url]
  37. text = text.replace( /\[link:([\w:/.\-_()?#=!~]+) ([\w\p{L}:/.\-_'\s]+)\]/giu, '<a href="$1" target="_blank">$2</a>' ); // [link:url title]
  38. text = text.replace( /\[example:([\w\_]+)\]/gi, '[example:$1 $1]' ); // [example:name] to [example:name title]
  39. text = text.replace( /\[example:([\w\_]+) ([\w\:\/\.\-\_ \s]+)\]/gi, '<a href="../../examples/#$1" target="_blank">$2</a>' ); // [example:name title]
  40. text = text.replace( /\`(.*?)\`/gs, '<code class="notranslate" translate="no">$1</code>' ); // `code`
  41. document.body.innerHTML = text;
  42. }
  43. if ( window.prettyPrint ) {
  44. window.prettyPrint();
  45. }
  46. // help translation services translate comments.
  47. document.querySelectorAll( 'span[class=com]' ).forEach( elem => {
  48. elem.classList.add( 'translate', 'yestranslate' );
  49. elem.setAttribute( 'translate', 'yes' );
  50. } );
  51. if ( window.threejsLessonUtils ) {
  52. window.threejsLessonUtils.afterPrettify();
  53. }
  54. }() );
  55. // ios needs this to allow touch events in an iframe
  56. window.addEventListener( 'touchstart', {} );
粤ICP备19079148号