@@ -620,9 +620,16 @@ function showHighlightsModal() {
620620 modal . classList . add ( 'active' ) ;
621621 document . body . style . overflow = 'hidden' ;
622622
623+ const filterButtons = document . querySelectorAll ( '.highlight-filter-btn' ) ;
624+ filterButtons . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
625+
626+ const allButton = document . querySelector ( '.highlight-filter-btn[data-color="all"]' ) ;
627+ if ( allButton ) {
628+ allButton . classList . add ( 'active' ) ;
629+ }
630+
623631 renderHighlights ( 'all' ) ;
624632
625- // Add filter button listeners
626633 document . querySelectorAll ( '.highlight-filter-btn' ) . forEach ( btn => {
627634 btn . addEventListener ( 'click' , function ( ) {
628635 document . querySelectorAll ( '.highlight-filter-btn' ) . forEach ( b => b . classList . remove ( 'active' ) ) ;
@@ -653,48 +660,77 @@ function renderHighlights(filterColor = 'all') {
653660 }
654661
655662 let html = '' ;
656- const versesByColor = { } ;
657-
658- // Group verses by color
659- Object . entries ( highlights ) . forEach ( ( [ reference , color ] ) => {
660- if ( ! versesByColor [ color ] ) versesByColor [ color ] = [ ] ;
661- versesByColor [ color ] . push ( reference ) ;
662- } ) ;
663663
664- // Sort verses within each color group
665- Object . keys ( versesByColor ) . forEach ( color => {
666- versesByColor [ color ] . sort ( ( a , b ) => {
667- // Sort by book, chapter, verse
668- const [ bookA , chapA , verseA ] = a . split ( / [ : ] / ) ;
669- const [ bookB , chapB , verseB ] = b . split ( / [ : ] / ) ;
664+ const sortableReferences = Object . keys ( highlights ) . map ( reference => {
665+ const match = reference . match ( / ^ ( \d * \s * \w + ) \s + ( \d + ) : ( \d + ) $ / ) ;
666+ if ( ! match ) return null ;
667+
668+ let bookName = match [ 1 ] . trim ( ) ;
669+ const chapter = parseInt ( match [ 2 ] ) ;
670+ const verse = parseInt ( match [ 3 ] ) ;
671+ const color = highlights [ reference ] ;
672+
673+ const bookParts = bookName . split ( ' ' ) ;
674+ let baseBookName = bookName ;
675+ let bookNumber = '' ;
676+
677+ if ( bookParts . length > 1 && / ^ \d + $ / . test ( bookParts [ 0 ] ) ) {
678+ bookNumber = bookParts [ 0 ] ;
679+ baseBookName = bookParts . slice ( 1 ) . join ( ' ' ) ;
680+ }
681+
682+ const bookIndex = BOOK_ORDER . findIndex ( book => {
683+ const orderParts = book . split ( ' ' ) ;
684+ let orderBaseName = book ;
685+ let orderNumber = '' ;
670686
671- const bookOrderA = BOOK_ORDER . indexOf ( bookA ) ;
672- const bookOrderB = BOOK_ORDER . indexOf ( bookB ) ;
687+ if ( orderParts . length > 1 && / ^ \d + $ / . test ( orderParts [ 0 ] ) ) {
688+ orderNumber = orderParts [ 0 ] ;
689+ orderBaseName = orderParts . slice ( 1 ) . join ( ' ' ) ;
690+ }
673691
674- if ( bookOrderA !== bookOrderB ) return bookOrderA - bookOrderB ;
675- if ( parseInt ( chapA ) !== parseInt ( chapB ) ) return parseInt ( chapA ) - parseInt ( chapB ) ;
676- return parseInt ( verseA ) - parseInt ( verseB ) ;
692+ return orderBaseName . toLowerCase ( ) === baseBookName . toLowerCase ( ) &&
693+ orderNumber === bookNumber ;
677694 } ) ;
695+
696+ return {
697+ reference,
698+ bookName,
699+ baseBookName,
700+ bookNumber,
701+ bookIndex,
702+ chapter,
703+ verse,
704+ color
705+ } ;
706+ } ) . filter ( ref => ref !== null && ref . bookIndex !== - 1 ) ;
707+
708+ sortableReferences . sort ( ( a , b ) => {
709+ if ( a . bookIndex !== b . bookIndex ) {
710+ return a . bookIndex - b . bookIndex ;
711+ }
712+ if ( a . chapter !== b . chapter ) {
713+ return a . chapter - b . chapter ;
714+ }
715+ return a . verse - b . verse ;
678716 } ) ;
679717
680- // Render verses
681- Object . entries ( versesByColor ) . forEach ( ( [ color , references ] ) => {
682- if ( filterColor !== 'all' && filterColor !== color ) return ;
718+ sortableReferences . forEach ( ref => {
719+ if ( filterColor !== 'all' && ref . color !== filterColor ) {
720+ return ;
721+ }
683722
684- references . forEach ( reference => {
685- const verseText = getVerseTextFromStorage ( reference ) || 'Verse text not available' ;
686- html += `
687- <div class="highlight-item ${ color } " data-reference="${ reference } " data-color="${ color } ">
688- <div class="highlight-ref">${ reference } </div>
689- <div class="highlight-text">${ verseText } </div>
690- </div>
691- ` ;
692- } ) ;
723+ const verseText = getVerseTextFromStorage ( ref . reference ) || 'Verse text not available' ;
724+ html += `
725+ <div class="highlight-item ${ ref . color } " data-reference="${ ref . reference } " data-color="${ ref . color } ">
726+ <div class="highlight-ref">${ ref . reference } </div>
727+ <div class="highlight-text">${ verseText } </div>
728+ </div>
729+ ` ;
693730 } ) ;
694731
695732 highlightsList . innerHTML = html || '<div class="no-highlights">No highlights match the selected filter</div>' ;
696733
697- // Add click handlers to navigate to verses
698734 document . querySelectorAll ( '.highlight-item' ) . forEach ( item => {
699735 item . addEventListener ( 'click' , ( ) => {
700736 const reference = item . dataset . reference ;
@@ -838,10 +874,19 @@ async function init() {
838874 const navigatedFromURL = navigateFromURL ( ) ;
839875 if ( ! navigatedFromURL ) {
840876 if ( window . location . pathname !== '/' ) {
877+ const defaultParams = {
878+ translation : 'BSB' ,
879+ book : 'Genesis' ,
880+ chapter : 1
881+ } ;
882+ window . history . replaceState (
883+ defaultParams ,
884+ '' ,
885+ `?p=bsb/gen/1`
886+ ) ;
841887 loadPassage ( ) ;
842888 }
843889 }
844-
845890 console . log ( 'App initialized successfully' ) ;
846891}
847892
0 commit comments