Console.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { Tab } from '../ui/Tab.js';
  2. class Console extends Tab {
  3. constructor() {
  4. super( 'Console' );
  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 ( type === 'warn' ) {
  68. icon = '⚠️';
  69. } else if ( type === 'error' ) {
  70. icon = '🔴';
  71. } else if ( type === 'info' ) {
  72. icon = 'ℹ️';
  73. }
  74. return icon;
  75. }
  76. _formatMessage( type, text ) {
  77. const fragment = document.createDocumentFragment();
  78. const prefixMatch = text.match( /^([\w\.]+:\s)/ );
  79. let content = text;
  80. if ( prefixMatch ) {
  81. const fullPrefix = prefixMatch[ 0 ];
  82. const parts = fullPrefix.slice( 0, - 2 ).split( '.' );
  83. const shortPrefix = ( parts.length > 1 ? parts[ parts.length - 1 ] : parts[ 0 ] ) + ':';
  84. const icon = this._getIcon( type, shortPrefix.split( ':' )[ 0 ].toLowerCase() );
  85. fragment.appendChild( document.createTextNode( icon + ' ' ) );
  86. const prefixSpan = document.createElement( 'span' );
  87. prefixSpan.className = 'log-prefix';
  88. prefixSpan.textContent = shortPrefix;
  89. fragment.appendChild( prefixSpan );
  90. content = text.substring( fullPrefix.length );
  91. }
  92. const parts = content.split( /(".*?"|'.*?'|`.*?`)/g ).map( p => p.trim() ).filter( Boolean );
  93. parts.forEach( ( part, index ) => {
  94. if ( /^("|'|`)/.test( part ) ) {
  95. const codeSpan = document.createElement( 'span' );
  96. codeSpan.className = 'log-code';
  97. codeSpan.textContent = part.slice( 1, - 1 );
  98. fragment.appendChild( codeSpan );
  99. } else {
  100. if ( index > 0 ) part = ' ' + part; // add space before parts except the first
  101. if ( index < parts.length - 1 ) part += ' '; // add space between parts
  102. fragment.appendChild( document.createTextNode( part ) );
  103. }
  104. } );
  105. return fragment;
  106. }
  107. addMessage( type, text ) {
  108. const msg = document.createElement( 'div' );
  109. msg.className = `log-message ${type}`;
  110. msg.dataset.type = type;
  111. msg.dataset.rawText = text;
  112. msg.appendChild( this._formatMessage( type, text ) );
  113. const showByType = this.filters[ type ];
  114. const showByText = text.toLowerCase().includes( this.filterText );
  115. msg.classList.toggle( 'hidden', ! ( showByType && showByText ) );
  116. this.logContainer.appendChild( msg );
  117. this.logContainer.scrollTop = this.logContainer.scrollHeight;
  118. if ( this.logContainer.children.length > 200 ) {
  119. this.logContainer.removeChild( this.logContainer.firstChild );
  120. }
  121. }
  122. }
  123. export { Console };
粤ICP备19079148号