Console.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { Tab } from '../ui/Tab.js';
  2. class Console extends Tab {
  3. constructor( options = {} ) {
  4. super( 'Console', options );
  5. this.filters = { info: true, warn: true, error: true };
  6. this.filterText = '';
  7. this.buildHeader();
  8. this.logContainer = document.createElement( 'div' );
  9. this.logContainer.id = 'console-log';
  10. this.content.appendChild( this.logContainer );
  11. }
  12. buildHeader() {
  13. const header = document.createElement( 'div' );
  14. header.className = 'console-header';
  15. const filterInput = document.createElement( 'input' );
  16. filterInput.type = 'text';
  17. filterInput.className = 'console-filter-input';
  18. filterInput.placeholder = 'Filter...';
  19. filterInput.addEventListener( 'input', ( e ) => {
  20. this.filterText = e.target.value.toLowerCase();
  21. this.applyFilters();
  22. } );
  23. const copyButton = document.createElement( 'button' );
  24. copyButton.className = 'console-copy-button';
  25. copyButton.title = 'Copy all';
  26. copyButton.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
  27. copyButton.addEventListener( 'click', () => this.copyAll( copyButton ) );
  28. const buttonsGroup = document.createElement( 'div' );
  29. buttonsGroup.className = 'console-buttons-group';
  30. Object.keys( this.filters ).forEach( type => {
  31. const label = document.createElement( 'label' );
  32. label.className = 'custom-checkbox';
  33. label.style.color = `var(--${type === 'info' ? 'text-primary' : 'color-' + ( type === 'warn' ? 'yellow' : 'red' )})`;
  34. const checkbox = document.createElement( 'input' );
  35. checkbox.type = 'checkbox';
  36. checkbox.checked = this.filters[ type ];
  37. checkbox.dataset.type = type;
  38. const checkmark = document.createElement( 'span' );
  39. checkmark.className = 'checkmark';
  40. label.appendChild( checkbox );
  41. label.appendChild( checkmark );
  42. label.append( type.charAt( 0 ).toUpperCase() + type.slice( 1 ) );
  43. buttonsGroup.appendChild( label );
  44. } );
  45. buttonsGroup.addEventListener( 'change', ( e ) => {
  46. const type = e.target.dataset.type;
  47. if ( type in this.filters ) {
  48. this.filters[ type ] = e.target.checked;
  49. this.applyFilters();
  50. }
  51. } );
  52. buttonsGroup.appendChild( copyButton );
  53. header.appendChild( filterInput );
  54. header.appendChild( buttonsGroup );
  55. this.content.appendChild( header );
  56. }
  57. applyFilters() {
  58. const messages = this.logContainer.querySelectorAll( '.log-message' );
  59. messages.forEach( msg => {
  60. const type = msg.dataset.type;
  61. const text = msg.dataset.rawText.toLowerCase();
  62. const showByType = this.filters[ type ];
  63. const showByText = text.includes( this.filterText );
  64. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  65. } );
  66. }
  67. copyAll( button ) {
  68. const win = this.logContainer.ownerDocument.defaultView;
  69. const selection = win.getSelection();
  70. const selectedText = selection.toString();
  71. const textInConsole = selectedText && this.logContainer.contains( selection.anchorNode );
  72. let text;
  73. if ( textInConsole ) {
  74. text = selectedText;
  75. } else {
  76. const messages = this.logContainer.querySelectorAll( '.log-message:not(.hidden)' );
  77. text = Array.from( messages ).map( msg => msg.dataset.rawText ).join( '\n' );
  78. }
  79. navigator.clipboard.writeText( text );
  80. button.classList.add( 'copied' );
  81. setTimeout( () => button.classList.remove( 'copied' ), 350 );
  82. }
  83. _getIcon( type, subType ) {
  84. let icon;
  85. if ( subType === 'tip' ) {
  86. icon = '💭';
  87. } else if ( subType === 'tsl' ) {
  88. icon = '✨';
  89. } else if ( subType === 'webgpurenderer' ) {
  90. icon = '🎨';
  91. } else if ( type === 'warn' ) {
  92. icon = '⚠️';
  93. } else if ( type === 'error' ) {
  94. icon = '🔴';
  95. } else if ( type === 'info' ) {
  96. icon = 'ℹ️';
  97. }
  98. return icon;
  99. }
  100. _formatMessage( type, text ) {
  101. const fragment = document.createDocumentFragment();
  102. const prefixMatch = text.match( /^([\w\.]+:\s)/ );
  103. let content = text;
  104. if ( prefixMatch ) {
  105. const fullPrefix = prefixMatch[ 0 ];
  106. const parts = fullPrefix.slice( 0, - 2 ).split( '.' );
  107. const shortPrefix = ( parts.length > 1 ? parts[ parts.length - 1 ] : parts[ 0 ] ) + ':';
  108. const icon = this._getIcon( type, shortPrefix.split( ':' )[ 0 ].toLowerCase() );
  109. fragment.appendChild( document.createTextNode( icon + ' ' ) );
  110. const prefixSpan = document.createElement( 'span' );
  111. prefixSpan.className = 'log-prefix';
  112. prefixSpan.textContent = shortPrefix;
  113. fragment.appendChild( prefixSpan );
  114. content = text.substring( fullPrefix.length );
  115. }
  116. const parts = content.split( /(".*?"|'.*?'|`.*?`)/g ).map( p => p.trim() ).filter( Boolean );
  117. parts.forEach( ( part, index ) => {
  118. if ( /^("|'|`)/.test( part ) ) {
  119. const codeSpan = document.createElement( 'span' );
  120. codeSpan.className = 'log-code';
  121. codeSpan.textContent = part.slice( 1, - 1 );
  122. fragment.appendChild( codeSpan );
  123. } else {
  124. if ( index > 0 ) part = ' ' + part; // add space before parts except the first
  125. if ( index < parts.length - 1 ) part += ' '; // add space between parts
  126. fragment.appendChild( document.createTextNode( part ) );
  127. }
  128. } );
  129. return fragment;
  130. }
  131. addMessage( type, text ) {
  132. const msg = document.createElement( 'div' );
  133. msg.className = `log-message ${type}`;
  134. msg.dataset.type = type;
  135. msg.dataset.rawText = text;
  136. msg.appendChild( this._formatMessage( type, text ) );
  137. const showByType = this.filters[ type ];
  138. const showByText = text.toLowerCase().includes( this.filterText );
  139. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  140. this.logContainer.appendChild( msg );
  141. this.logContainer.scrollTop = this.logContainer.scrollHeight;
  142. if ( this.logContainer.children.length > 200 ) {
  143. this.logContainer.removeChild( this.logContainer.firstChild );
  144. }
  145. }
  146. }
  147. export { Console };
粤ICP备19079148号