Console.js 9.6 KB

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