@@ -40,7 +40,49 @@ function loadGrblBackupFile(f) {
4040 }
4141}
4242
43+ function populateRestoreMenu ( ) {
44+ // Retrieve backups from localStorage
45+ const backups = JSON . parse ( localStorage . getItem ( 'grblParamsBackups' ) ) || [ ] ;
46+
47+ // Get the dropdown menu element
48+ const backupMenu = document . getElementById ( 'restoreBackupMenu' ) ;
49+
50+ // Clear existing menu items (in case you're calling this multiple times)
51+ backupMenu . innerHTML = '' ;
52+
53+ // Loop through each backup and create a list item for it
54+ backups . forEach ( ( backup , index ) => {
55+ const backupItem = document . createElement ( 'li' ) ;
56+
57+ // Format the timestamp (you can format it as needed)
58+ const formattedTimestamp = new Date ( backup . timestamp ) . toLocaleString ( ) ; // Adjust formatting as needed
59+
60+ // Create the list item HTML content
61+ backupItem . innerHTML = `
62+ <a href="#" onclick="restoreAutoBackup(${ index } )">
63+ <i class="fas fa-clock fa-fw"></i>
64+ Restore AutoBackup: ${ formattedTimestamp } (${ backup . note || 'No note' } )
65+ </a>
66+ ` ;
67+
68+ // Append the list item to the dropdown menu
69+ backupMenu . appendChild ( backupItem ) ;
70+ } ) ;
71+ }
72+
73+ function restoreAutoBackup ( index ) {
74+ const backups = JSON . parse ( localStorage . getItem ( 'grblParamsBackups' ) ) || [ ] ;
75+ const selectedBackup = backups [ index ] ;
76+
77+ // You can now access selectedBackup.grblParams and apply it as needed
78+ console . log ( 'Restoring backup:' , selectedBackup ) ;
79+ // Call your function to restore the backup here, e.g., update grblParams
80+ // Example: grblParams = selectedBackup.grblParams;
81+ }
82+
83+
4384function backupGrblSettings ( ) {
85+ autoBackup ( "Manual Backup" )
4486 var grblBackup = ""
4587 for ( key in grblParams ) {
4688 var key2 = key . split ( '=' ) [ 0 ] . substr ( 1 ) ;
@@ -50,7 +92,6 @@ function backupGrblSettings() {
5092 } else {
5193 var descr = "unknown"
5294 }
53-
5495 grblBackup += key + "=" + grblParams [ key ] + " ; " + descr + "\n"
5596 }
5697 if ( laststatus . machine . name . length > 0 ) {
@@ -65,7 +106,6 @@ function backupGrblSettings() {
65106 } else {
66107 invokeSaveAsDialog ( blob , 'grbl-settings-backup-' + date . yyyymmdd ( ) + '.txt' ) ;
67108 }
68-
69109}
70110
71111function grblSettings ( data ) {
@@ -409,6 +449,8 @@ function grblPopulate() {
409449 setTimeout ( function ( ) {
410450 setMachineButton ( laststatus . machine . name )
411451 } , 500 )
452+
453+ populateRestoreMenu ( ) ;
412454 }
413455
414456}
@@ -519,7 +561,38 @@ function checkifchanged() {
519561}
520562
521563
564+ function autoBackup ( note ) {
565+
566+ const timestamp = new Date ( ) . toISOString ( ) ; // Generate current timestamp
567+ const currentParams = {
568+ machinetype : laststatus . machine . name ,
569+ note : note ,
570+ timestamp : timestamp ,
571+ grblParams : {
572+ ...grblParams // Spread Operator copy
573+ }
574+ } ; // Add timestamp to the current parameters
575+
576+ // Retrieve existing backups from localStorage or initialize an empty array
577+ let backups = JSON . parse ( localStorage . getItem ( 'grblParamsBackups' ) ) || [ ] ;
578+
579+ // Add the current backup to the beginning of the array
580+ backups . unshift ( currentParams ) ;
581+
582+ // Trim backups to keep only the last 20
583+ if ( backups . length > 20 ) {
584+ backups = backups . slice ( 0 , 20 ) ;
585+ }
586+
587+ // Save the updated backups array back to localStorage
588+ localStorage . setItem ( 'grblParamsBackups' , JSON . stringify ( backups ) ) ;
589+
590+ // Optionally, add your existing save functionality here
591+ console . log ( 'Settings saved and backup created.' ) ;
592+ }
593+
522594function grblSaveSettings ( ) {
595+ autoBackup ( "Updated Grbl Settings" )
523596 var toSaveCommands = [ ] ;
524597 var saveProgressBar = $ ( "#grblSaveProgress" ) . data ( "progress" ) ;
525598 for ( var key in grblParams ) {
0 commit comments