Console.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.unreadErrors = 0;
  8. this.unreadWarns = 0;
  9. this.tabBadgeContainer = document.createElement( 'span' );
  10. this.tabBadgeContainer.className = 'tab-badge-container';
  11. this.tabErrorBadge = document.createElement( 'span' );
  12. this.tabErrorBadge.className = 'tab-badge error';
  13. this.tabErrorBadge.style.display = 'none';
  14. this.tabWarnBadge = document.createElement( 'span' );
  15. this.tabWarnBadge.className = 'tab-badge warn';
  16. this.tabWarnBadge.style.display = 'none';
  17. this.tabBadgeContainer.appendChild( this.tabErrorBadge );
  18. this.tabBadgeContainer.appendChild( this.tabWarnBadge );
  19. this.button.appendChild( this.tabBadgeContainer );
  20. this.buildHeader();
  21. this.logContainer = document.createElement( 'div' );
  22. this.logContainer.classList.add( 'console-log' );
  23. this.content.appendChild( this.logContainer );
  24. }
  25. buildHeader() {
  26. const header = document.createElement( 'div' );
  27. header.className = 'toolbar';
  28. const filterInput = document.createElement( 'input' );
  29. filterInput.type = 'text';
  30. filterInput.className = 'console-filter-input';
  31. filterInput.placeholder = 'Filter...';
  32. filterInput.addEventListener( 'input', ( e ) => {
  33. this.filterText = e.target.value.toLowerCase();
  34. this.applyFilters();
  35. } );
  36. const copyButton = document.createElement( 'button' );
  37. copyButton.className = 'console-copy-button';
  38. copyButton.title = 'Copy all';
  39. 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>';
  40. copyButton.addEventListener( 'click', () => this.copyAll( copyButton ) );
  41. const buttonsGroup = document.createElement( 'div' );
  42. buttonsGroup.className = 'console-buttons-group';
  43. Object.keys( this.filters ).forEach( type => {
  44. const label = document.createElement( 'label' );
  45. label.className = 'custom-checkbox';
  46. label.style.color = `var(--${type === 'info' ? 'text-primary' : 'color-' + ( type === 'warn' ? 'yellow' : 'red' )})`;
  47. const checkbox = document.createElement( 'input' );
  48. checkbox.type = 'checkbox';
  49. checkbox.checked = this.filters[ type ];
  50. checkbox.dataset.type = type;
  51. const checkmark = document.createElement( 'span' );
  52. checkmark.className = 'checkmark';
  53. label.appendChild( checkbox );
  54. label.appendChild( checkmark );
  55. label.append( type.charAt( 0 ).toUpperCase() + type.slice( 1 ) );
  56. buttonsGroup.appendChild( label );
  57. } );
  58. buttonsGroup.addEventListener( 'change', ( e ) => {
  59. const type = e.target.dataset.type;
  60. if ( type in this.filters ) {
  61. this.filters[ type ] = e.target.checked;
  62. this.applyFilters();
  63. }
  64. } );
  65. buttonsGroup.appendChild( copyButton );
  66. header.appendChild( filterInput );
  67. header.appendChild( buttonsGroup );
  68. this.content.appendChild( header );
  69. }
  70. applyFilters() {
  71. const messages = this.logContainer.querySelectorAll( '.log-message' );
  72. messages.forEach( msg => {
  73. const type = msg.dataset.type;
  74. const text = msg.dataset.rawText.toLowerCase();
  75. const showByType = this.filters[ type ];
  76. const showByText = text.includes( this.filterText );
  77. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  78. } );
  79. }
  80. copyAll( button ) {
  81. const win = this.logContainer.ownerDocument.defaultView;
  82. const selection = win.getSelection();
  83. const selectedText = selection.toString();
  84. const textInConsole = selectedText && this.logContainer.contains( selection.anchorNode );
  85. let text;
  86. if ( textInConsole ) {
  87. text = selectedText;
  88. } else {
  89. const messages = this.logContainer.querySelectorAll( '.log-message:not(.hidden)' );
  90. text = Array.from( messages ).map( msg => msg.dataset.rawText ).join( '\n' );
  91. }
  92. navigator.clipboard.writeText( text );
  93. button.classList.add( 'copied' );
  94. setTimeout( () => button.classList.remove( 'copied' ), 350 );
  95. }
  96. _getIcon( type, subType ) {
  97. let icon;
  98. if ( subType === 'tip' ) {
  99. icon = '💭';
  100. } else if ( subType === 'tsl' ) {
  101. icon = '✨';
  102. } else if ( subType === 'webgpurenderer' ) {
  103. icon = '🎨';
  104. } else if ( type === 'warn' ) {
  105. icon = '⚠️';
  106. } else if ( type === 'error' ) {
  107. icon = '🔴';
  108. } else if ( type === 'info' ) {
  109. icon = 'ℹ️';
  110. }
  111. return icon;
  112. }
  113. _formatMessage( type, text ) {
  114. const fragment = document.createDocumentFragment();
  115. const prefixMatch = text.match( /^([\w\.]+:\s)/ );
  116. let content = text;
  117. if ( prefixMatch ) {
  118. const fullPrefix = prefixMatch[ 0 ];
  119. const parts = fullPrefix.slice( 0, - 2 ).split( '.' );
  120. const shortPrefix = ( parts.length > 1 ? parts[ parts.length - 1 ] : parts[ 0 ] ) + ':';
  121. const icon = this._getIcon( type, shortPrefix.split( ':' )[ 0 ].toLowerCase() );
  122. fragment.appendChild( document.createTextNode( icon + ' ' ) );
  123. const prefixSpan = document.createElement( 'span' );
  124. prefixSpan.className = 'log-prefix';
  125. prefixSpan.textContent = shortPrefix;
  126. fragment.appendChild( prefixSpan );
  127. content = text.substring( fullPrefix.length );
  128. }
  129. const parts = content.split( /(".*?"|'.*?'|`.*?`)/g ).map( p => p.trim() ).filter( Boolean );
  130. parts.forEach( ( part, index ) => {
  131. if ( /^("|'|`)/.test( part ) ) {
  132. const codeSpan = document.createElement( 'span' );
  133. codeSpan.className = 'log-code';
  134. codeSpan.textContent = part.slice( 1, - 1 );
  135. fragment.appendChild( codeSpan );
  136. } else {
  137. if ( index > 0 ) part = ' ' + part; // add space before parts except the first
  138. if ( index < parts.length - 1 ) part += ' '; // add space between parts
  139. fragment.appendChild( document.createTextNode( part ) );
  140. }
  141. } );
  142. return fragment;
  143. }
  144. setActive( isActive ) {
  145. super.setActive( isActive );
  146. if ( isActive && this.profiler && this.profiler.panel.classList.contains( 'visible' ) ) {
  147. this.clearUnread();
  148. }
  149. }
  150. clearUnread() {
  151. this.unreadErrors = 0;
  152. this.unreadWarns = 0;
  153. this.updateBadges();
  154. }
  155. updateBadges() {
  156. if ( ! this.profiler ) return;
  157. const errorBadge = this.profiler.toggleButton.querySelector( '.console-badge.error' );
  158. const warnBadge = this.profiler.toggleButton.querySelector( '.console-badge.warn' );
  159. if ( errorBadge ) {
  160. if ( this.unreadErrors > 0 ) {
  161. errorBadge.textContent = this.unreadErrors > 99 ? '+99' : this.unreadErrors;
  162. errorBadge.style.display = '';
  163. } else {
  164. errorBadge.style.display = 'none';
  165. }
  166. }
  167. if ( warnBadge ) {
  168. if ( this.unreadWarns > 0 ) {
  169. warnBadge.textContent = this.unreadWarns > 99 ? '+99' : this.unreadWarns;
  170. warnBadge.style.display = '';
  171. } else {
  172. warnBadge.style.display = 'none';
  173. }
  174. }
  175. if ( this.tabErrorBadge ) {
  176. if ( this.unreadErrors > 0 ) {
  177. this.tabErrorBadge.textContent = this.unreadErrors > 99 ? '+99' : this.unreadErrors;
  178. this.tabErrorBadge.style.display = '';
  179. } else {
  180. this.tabErrorBadge.style.display = 'none';
  181. }
  182. }
  183. if ( this.tabWarnBadge ) {
  184. if ( this.unreadWarns > 0 ) {
  185. this.tabWarnBadge.textContent = this.unreadWarns > 99 ? '+99' : this.unreadWarns;
  186. this.tabWarnBadge.style.display = '';
  187. } else {
  188. this.tabWarnBadge.style.display = 'none';
  189. }
  190. }
  191. }
  192. addMessage( type, text ) {
  193. const msg = document.createElement( 'div' );
  194. msg.className = `log-message ${type}`;
  195. msg.dataset.type = type;
  196. msg.dataset.rawText = text;
  197. msg.appendChild( this._formatMessage( type, text ) );
  198. const showByType = this.filters[ type ];
  199. const showByText = text.toLowerCase().includes( this.filterText );
  200. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  201. this.logContainer.appendChild( msg );
  202. this.logContainer.scrollTop = this.logContainer.scrollHeight;
  203. if ( this.logContainer.children.length > 200 ) {
  204. this.logContainer.removeChild( this.logContainer.firstChild );
  205. }
  206. // Update unread counts if the console is not active/visible
  207. const isUnread = ! this.isActive;
  208. if ( isUnread ) {
  209. if ( type === 'error' ) {
  210. this.unreadErrors ++;
  211. this.updateBadges();
  212. } else if ( type === 'warn' ) {
  213. this.unreadWarns ++;
  214. this.updateBadges();
  215. }
  216. }
  217. }
  218. }
  219. export { Console };
粤ICP备19079148号