11import { showNotification } from './notification.js' ;
22
3+ const HASH_TO_TAB : Record < string , string > = {
4+ scaler : 'scalerTab' ,
5+ slicer : 'slicerTab' ,
6+ audio : 'audioTab' ,
7+ palette : 'paletteTab' ,
8+ csv : 'csvTab' ,
9+ qrcode : 'qrcodeTab' ,
10+ brainfuck : 'brainfuckTab' ,
11+ pagnai : 'pagnaiTab' ,
12+ } ;
13+
14+ function tabIdToHash ( tabId : string ) : string {
15+ const entry = Object . entries ( HASH_TO_TAB ) . find ( ( [ , id ] ) => id === tabId ) ;
16+ return entry ? entry [ 0 ] : '' ;
17+ }
18+
319export function openTab ( tabId : string ) : void {
420 try {
521 const allTabs = document . querySelectorAll < HTMLElement > ( '.tabcontent' ) ;
@@ -13,15 +29,16 @@ export function openTab(tabId: string): void {
1329 const allButtons = document . querySelectorAll ( '.tab-button' ) ;
1430 allButtons . forEach ( ( btn ) => btn . classList . remove ( 'active' ) ) ;
1531
16- const targetButton = document . querySelector ( `[data-tab="${ tabId } "]` ) ;
17- if ( targetButton ) {
18- targetButton . classList . add ( 'active' ) ;
19- }
32+ document . querySelectorAll ( `[data-tab="${ tabId } "]` ) . forEach ( ( btn ) => btn . classList . add ( 'active' ) ) ;
2033
2134 const selectedTab = document . getElementById ( tabId ) ;
2235 if ( selectedTab ) {
2336 selectedTab . style . display = 'block' ;
2437 selectedTab . classList . add ( 'active' ) ;
38+ const hash = tabIdToHash ( tabId ) ;
39+ if ( hash ) {
40+ history . replaceState ( null , '' , `#${ hash } ` ) ;
41+ }
2542 } else {
2643 console . error ( `Tab with id "${ tabId } " not found` ) ;
2744 showNotification ( 'Tab not found' , 'error' ) ;
@@ -36,14 +53,20 @@ export function selectMobileTab(tabId: string): void {
3653 openTab ( tabId ) ;
3754 const mobileMenu = document . getElementById ( 'mobileMenu' ) ;
3855 if ( mobileMenu ) mobileMenu . style . display = 'none' ;
39- const button = document . querySelector ( `[data-tab="${ tabId } "]` ) ;
40- if ( button ) {
41- document . querySelectorAll ( '.tab-button' ) . forEach ( ( btn ) => btn . classList . remove ( 'active' ) ) ;
42- button . classList . add ( 'active' ) ;
43- }
4456}
4557
4658export function initTabs ( ) : void {
59+ function openFromHash ( ) : void {
60+ const hash = window . location . hash . slice ( 1 ) . toLowerCase ( ) ;
61+ const tabId = hash ? HASH_TO_TAB [ hash ] : null ;
62+ if ( tabId ) {
63+ openTab ( tabId ) ;
64+ }
65+ }
66+
67+ openFromHash ( ) ;
68+ window . addEventListener ( 'hashchange' , openFromHash ) ;
69+
4770 document . querySelectorAll < HTMLElement > ( '#tabs .tab-button[data-tab]' ) . forEach ( ( btn ) => {
4871 const tabId = btn . dataset . tab ;
4972 if ( tabId ) {
0 commit comments