@@ -439,9 +439,19 @@ BookLibService.Borrow(id) {
439439
440440 async setCurrentItem ( item ) {
441441 const d = deferred ( ) ;
442+ // TODO: remove later
443+ item . htmlMode =
444+ item . htmlMode || this . state . prefs . htmlMode || HtmlModes . HTML ;
445+ item . cssMode = item . cssMode || this . state . prefs . cssMode || CssModes . CSS ;
446+ item . jsMode = item . jsMode || this . state . prefs . jsMode || JsModes . JS ;
442447 // Migrate the item to the new pages format if needed
443448 const migratedItem = migrateItemToPages ( item ) ;
444449 await this . setState ( { currentItem : migratedItem } , d . resolve ) ;
450+ this . saveItem ( ) ;
451+
452+ // Reset unsaved count, in UI also.
453+ await this . setState ( { unsavedEditCount : 0 } ) ;
454+ currentBrowserTab . setTitle ( item . title ) ;
445455 return d . promise ;
446456 }
447457
@@ -901,7 +911,7 @@ BookLibService.Borrow(id) {
901911 }
902912
903913 if (
904- isUserChange &&
914+ isUserChange &&
905915 this . state . unsavedEditCount % UNSAVED_WARNING_COUNT === 0 &&
906916 this . state . unsavedEditCount >= UNSAVED_WARNING_COUNT
907917 ) {
@@ -912,7 +922,7 @@ BookLibService.Borrow(id) {
912922 window . saveBtn . classList . remove ( 'wobble' ) ;
913923 } ) ;
914924 }
915-
925+
916926 if ( this . state . prefs . isJs13kModeOn ) {
917927 // Throttling codesize calculation
918928 if ( this . codeSizeCalculationTimeout ) {
@@ -1512,7 +1522,7 @@ BookLibService.Borrow(id) {
15121522 if ( ! currentItem || ! currentItem . pages || ! currentItem . currentPageId ) {
15131523 return null ;
15141524 }
1515-
1525+
15161526 return currentItem . pages . find ( page => page . id === currentItem . currentPageId ) || null ;
15171527 }
15181528
@@ -1533,40 +1543,40 @@ BookLibService.Borrow(id) {
15331543 const pageCount = currentItem . pages . length + 1 ;
15341544 title = `Page ${ pageCount } ` ;
15351545 }
1536-
1546+
15371547 const newPage = {
15381548 id : generateRandomId ( ) ,
15391549 title,
15401550 js : '' ,
15411551 css : '' ,
15421552 isDefault : false
15431553 } ;
1544-
1554+
15451555 const updatedItem = {
15461556 ...currentItem ,
15471557 pages : [ ...currentItem . pages , newPage ]
15481558 } ;
1549-
1559+
15501560 this . setState ( { currentItem : updatedItem } ) ;
1551-
1561+
15521562 // Switch to the new page
15531563 this . switchToPage ( newPage . id ) ;
1554-
1564+
15551565 return newPage . id ;
15561566 }
15571567
15581568 switchToPage ( pageId ) {
15591569 const { currentItem } = this . state ;
15601570 if ( ! currentItem ) return ;
1561-
1571+
15621572 const pageExists = currentItem . pages . some ( page => page . id === pageId ) ;
15631573 if ( ! pageExists ) return ;
1564-
1574+
15651575 const updatedItem = {
15661576 ...currentItem ,
15671577 currentPageId : pageId
15681578 } ;
1569-
1579+
15701580 this . setState ( { currentItem : updatedItem } , ( ) => {
15711581 // Refresh the editor to show the new page content
15721582 if ( this . contentWrap ) {
@@ -1578,21 +1588,21 @@ BookLibService.Borrow(id) {
15781588 updatePage ( pageId , updates ) {
15791589 const { currentItem } = this . state ;
15801590 if ( ! currentItem ) return ;
1581-
1591+
15821592 const pageIndex = currentItem . pages . findIndex ( page => page . id === pageId ) ;
15831593 if ( pageIndex === - 1 ) return ;
1584-
1594+
15851595 const updatedPages = [ ...currentItem . pages ] ;
15861596 updatedPages [ pageIndex ] = {
15871597 ...updatedPages [ pageIndex ] ,
15881598 ...updates
15891599 } ;
1590-
1600+
15911601 const updatedItem = {
15921602 ...currentItem ,
15931603 pages : updatedPages
15941604 } ;
1595-
1605+
15961606 // If we're updating the current page's js or css, also update the item's js/css for backward compatibility
15971607 if ( pageId === currentItem . currentPageId ) {
15981608 if ( updates . js !== undefined ) {
@@ -1602,36 +1612,36 @@ BookLibService.Borrow(id) {
16021612 updatedItem . css = updates . css ;
16031613 }
16041614 }
1605-
1615+
16061616 this . setState ( { currentItem : updatedItem } ) ;
16071617 }
16081618
16091619 deletePage ( pageId ) {
16101620 const { currentItem } = this . state ;
16111621 if ( ! currentItem ) return ;
1612-
1622+
16131623 // Don't allow deleting the last page
16141624 if ( currentItem . pages . length <= 1 ) return ;
1615-
1625+
16161626 const pageIndex = currentItem . pages . findIndex ( page => page . id === pageId ) ;
16171627 if ( pageIndex === - 1 ) return ;
1618-
1628+
16191629 const updatedPages = currentItem . pages . filter ( page => page . id !== pageId ) ;
1620-
1630+
16211631 // If we're deleting the current page, switch to another page
16221632 let updatedCurrentPageId = currentItem . currentPageId ;
16231633 if ( pageId === currentItem . currentPageId ) {
16241634 // Find the nearest page to switch to
16251635 const newPageIndex = Math . min ( pageIndex , updatedPages . length - 1 ) ;
16261636 updatedCurrentPageId = updatedPages [ newPageIndex ] . id ;
16271637 }
1628-
1638+
16291639 const updatedItem = {
16301640 ...currentItem ,
16311641 pages : updatedPages ,
16321642 currentPageId : updatedCurrentPageId
16331643 } ;
1634-
1644+
16351645 this . setState ( { currentItem : updatedItem } , ( ) => {
16361646 // If we switched pages, refresh the editor
16371647 if ( pageId === currentItem . currentPageId && this . contentWrap ) {
0 commit comments