Console.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 filtersGroup = document.createElement( 'div' );
  24. filtersGroup.className = 'console-filters-group';
  25. Object.keys( this.filters ).forEach( type => {
  26. const label = document.createElement( 'label' );
  27. label.className = 'custom-checkbox';
  28. label.style.color = `var(--${type === 'info' ? 'text-primary' : 'color-' + ( type === 'warn' ? 'yellow' : 'red' )})`;
  29. const checkbox = document.createElement( 'input' );
  30. checkbox.type = 'checkbox';
  31. checkbox.checked = this.filters[ type ];
  32. checkbox.dataset.type = type;
  33. const checkmark = document.createElement( 'span' );
  34. checkmark.className = 'checkmark';
  35. label.appendChild( checkbox );
  36. label.appendChild( checkmark );
  37. label.append( type.charAt( 0 ).toUpperCase() + type.slice( 1 ) );
  38. filtersGroup.appendChild( label );
  39. } );
  40. filtersGroup.addEventListener( 'change', ( e ) => {
  41. const type = e.target.dataset.type;
  42. if ( type in this.filters ) {
  43. this.filters[ type ] = e.target.checked;
  44. this.applyFilters();
  45. }
  46. } );
  47. header.appendChild( filterInput );
  48. header.appendChild( filtersGroup );
  49. this.content.appendChild( header );
  50. }
  51. applyFilters() {
  52. const messages = this.logContainer.querySelectorAll( '.log-message' );
  53. messages.forEach( msg => {
  54. const type = msg.dataset.type;
  55. const text = msg.dataset.rawText.toLowerCase();
  56. const showByType = this.filters[ type ];
  57. const showByText = text.includes( this.filterText );
  58. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  59. } );
  60. }
  61. _getIcon( type, subType ) {
  62. let icon;
  63. if ( subType === 'tip' ) {
  64. icon = '💭';
  65. } else if ( subType === 'tsl' ) {
  66. icon = '✨';
  67. } else if ( subType === 'webgpurenderer' ) {
  68. icon = '🎨';
  69. } else if ( type === 'warn' ) {
  70. icon = '⚠️';
  71. } else if ( type === 'error' ) {
  72. icon = '🔴';
  73. } else if ( type === 'info' ) {
  74. icon = 'ℹ️';
  75. }
  76. return icon;
  77. }
  78. _formatMessage( type, text ) {
  79. const fragment = document.createDocumentFragment();
  80. const prefixMatch = text.match( /^([\w\.]+:\s)/ );
  81. let content = text;
  82. if ( prefixMatch ) {
  83. const fullPrefix = prefixMatch[ 0 ];
  84. const parts = fullPrefix.slice( 0, - 2 ).split( '.' );
  85. const shortPrefix = ( parts.length > 1 ? parts[ parts.length - 1 ] : parts[ 0 ] ) + ':';
  86. const icon = this._getIcon( type, shortPrefix.split( ':' )[ 0 ].toLowerCase() );
  87. fragment.appendChild( document.createTextNode( icon + ' ' ) );
  88. const prefixSpan = document.createElement( 'span' );
  89. prefixSpan.className = 'log-prefix';
  90. prefixSpan.textContent = shortPrefix;
  91. fragment.appendChild( prefixSpan );
  92. content = text.substring( fullPrefix.length );
  93. }
  94. const parts = content.split( /(".*?"|'.*?'|`.*?`)/g ).map( p => p.trim() ).filter( Boolean );
  95. parts.forEach( ( part, index ) => {
  96. if ( /^("|'|`)/.test( part ) ) {
  97. const codeSpan = document.createElement( 'span' );
  98. codeSpan.className = 'log-code';
  99. codeSpan.textContent = part.slice( 1, - 1 );
  100. fragment.appendChild( codeSpan );
  101. } else {
  102. if ( index > 0 ) part = ' ' + part; // add space before parts except the first
  103. if ( index < parts.length - 1 ) part += ' '; // add space between parts
  104. fragment.appendChild( document.createTextNode( part ) );
  105. }
  106. } );
  107. return fragment;
  108. }
  109. addMessage( type, text ) {
  110. const msg = document.createElement( 'div' );
  111. msg.className = `log-message ${type}`;
  112. msg.dataset.type = type;
  113. msg.dataset.rawText = text;
  114. msg.appendChild( this._formatMessage( type, text ) );
  115. const showByType = this.filters[ type ];
  116. const showByText = text.toLowerCase().includes( this.filterText );
  117. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  118. this.logContainer.appendChild( msg );
  119. this.logContainer.scrollTop = this.logContainer.scrollHeight;
  120. if ( this.logContainer.children.length > 200 ) {
  121. this.logContainer.removeChild( this.logContainer.firstChild );
  122. }
  123. }
  124. }
  125. export { Console };
粤ICP备19079148号