@@ -12,6 +12,8 @@ import {
12
12
tools_files ,
13
13
} from "./utils.js" ;
14
14
15
+ import { save_story , get_story } from "./storage.js" ;
16
+
15
17
const data_url_regexp = / ^ d a t a : i m a g e \/ ( [ a - z ] * ) ; b a s e 6 4 , ( .* ) $ / ;
16
18
17
19
var story = { } ;
@@ -712,15 +714,20 @@ async function download_graph_split() {
712
714
713
715
folder . file ( get_file_safe_title ( ) + ".json" , JSON . stringify ( story_deep_copy ) ) ;
714
716
715
- add_stroy_adventure_files ( zip ) . then ( ( ) => {
716
- toast_ok ( "Generating Zip" ) ;
717
- zip . generateAsync ( { type : "base64" } ) . then ( function ( content ) {
718
- trigger_data_dl (
719
- "data:application/zip;base64," + content ,
720
- get_file_safe_title ( ) + ".zip"
721
- ) ;
717
+ add_stroy_adventure_files ( zip )
718
+ . then ( ( ) => {
719
+ toast_ok ( "Generating Zip" ) ;
720
+ zip . generateAsync ( { type : "base64" } ) . then ( function ( content ) {
721
+ trigger_data_dl (
722
+ "data:application/zip;base64," + content ,
723
+ get_file_safe_title ( ) + ".zip"
724
+ ) ;
725
+ } ) ;
726
+ } )
727
+ . catch ( ( err ) => {
728
+ console . error ( "Error generating story adventure zip" , err ) ;
729
+ toast_alert ( "Error generating bundle." ) ;
722
730
} ) ;
723
- } ) ;
724
731
}
725
732
726
733
async function add_to_zip ( zip , folder , global_path = "../" ) {
@@ -996,53 +1003,10 @@ function add_action() {
996
1003
text_editor_load ( active_section ) ;
997
1004
}
998
1005
999
- text_area . addEventListener ( "change" , handle_text_change ) ;
1000
-
1001
- text_area . addEventListener ( "paste" , paste_image ) ;
1002
-
1003
- delete_button . addEventListener ( "click" , handle_delete ) ;
1004
- add_node_button . addEventListener ( "click" , handle_add_node ) ;
1005
- add_edge_button . addEventListener ( "click" , handle_add_edge ) ;
1006
-
1007
- document
1008
- . getElementById ( "download_as_is_button" )
1009
- . addEventListener ( "click" , download_as_is ) ;
1010
- document
1011
- . getElementById ( "download_in_one_button" )
1012
- . addEventListener ( "click" , download_graph_in_one ) ;
1013
- document
1014
- . getElementById ( "download_split_button" )
1015
- . addEventListener ( "click" , download_graph_split ) ;
1016
- load_button . addEventListener ( "click" , load_graph ) ;
1017
- document
1018
- . getElementById ( "clear_all_button" )
1019
- . addEventListener ( "click" , new_story ) ;
1020
- add_media_button . addEventListener ( "click" , add_or_remove_media ) ;
1021
- document
1022
- . getElementById ( "redraw_button" )
1023
- . addEventListener ( "click" , redraw_adventure_graph ) ;
1024
-
1025
- document
1026
- . getElementById ( "linearize_button" )
1027
- . addEventListener ( "click" , create_linear_story ) ;
1028
-
1029
- document . addEventListener ( "keydown" , handle_global_key_down ) ;
1030
-
1031
- document
1032
- . getElementById ( "story_modal" )
1033
- . addEventListener ( "shown.bs.modal" , ( ) => {
1034
- document . getElementById ( "story_code" ) . innerHTML = JSON . stringify (
1035
- story ,
1036
- null ,
1037
- 2
1038
- ) ;
1039
- } ) ;
1040
-
1041
1006
async function load_last_story_or_example ( ) {
1042
1007
try {
1043
- const storyJson = localStorage . getItem ( current_editor_story_key ) ;
1044
- if ( storyJson ) {
1045
- story = JSON . parse ( storyJson ) ;
1008
+ story = await get_story ( current_editor_story_key ) ;
1009
+ if ( story ) {
1046
1010
return ;
1047
1011
}
1048
1012
} catch ( err ) {
@@ -1141,7 +1105,10 @@ async function depth_first_search(linearized_history, end_at, passing_through) {
1141
1105
console . debug ( "dfs reached target" , end_at ) ;
1142
1106
if ( passing_through ) {
1143
1107
for ( const passing of passing_through ) {
1144
- if ( ! linearized_history . includes ( String ( passing ) ) && ! linearized_history . includes ( Number ( passing ) ) ) {
1108
+ if (
1109
+ ! linearized_history . includes ( String ( passing ) ) &&
1110
+ ! linearized_history . includes ( Number ( passing ) )
1111
+ ) {
1145
1112
console . debug ( linearized_history , "not passing through" , passing ) ;
1146
1113
return null ;
1147
1114
}
@@ -1182,12 +1149,22 @@ async function depth_first_search(linearized_history, end_at, passing_through) {
1182
1149
return null ;
1183
1150
}
1184
1151
1185
- function local_save ( ) {
1186
- console . debug ( "local save" ) ;
1187
- localStorage . setItem (
1188
- current_editor_story_key ,
1189
- JSON . stringify ( story , null , 2 )
1190
- ) ;
1152
+ var error_in_autosave_reported = false ;
1153
+ async function local_save ( ) {
1154
+ try {
1155
+ console . debug ( "local save" ) ;
1156
+ await save_story ( current_editor_story_key , story ) ;
1157
+ if ( error_in_autosave_reported ) {
1158
+ toast_ok ( "Autosaving is working." ) ;
1159
+ }
1160
+ error_in_autosave_reported = false ;
1161
+ } catch ( err ) {
1162
+ console . error ( "Error in autosave" , err ) ;
1163
+ if ( ! error_in_autosave_reported ) {
1164
+ toast_alert ( "Error auto-saving the story." ) ;
1165
+ }
1166
+ error_in_autosave_reported = true ;
1167
+ }
1191
1168
}
1192
1169
1193
1170
function set_save_interval ( ) {
@@ -1200,8 +1177,53 @@ function on_load() {
1200
1177
}
1201
1178
1202
1179
async function init ( ) {
1180
+ add_listeners ( ) ;
1203
1181
load_last_story_or_example ( ) . then ( on_load ) ;
1204
1182
set_save_interval ( ) ;
1205
1183
}
1206
1184
1185
+ function add_listeners ( ) {
1186
+ text_area . addEventListener ( "change" , handle_text_change ) ;
1187
+
1188
+ text_area . addEventListener ( "paste" , paste_image ) ;
1189
+
1190
+ delete_button . addEventListener ( "click" , handle_delete ) ;
1191
+ add_node_button . addEventListener ( "click" , handle_add_node ) ;
1192
+ add_edge_button . addEventListener ( "click" , handle_add_edge ) ;
1193
+
1194
+ document
1195
+ . getElementById ( "download_as_is_button" )
1196
+ . addEventListener ( "click" , download_as_is ) ;
1197
+ document
1198
+ . getElementById ( "download_in_one_button" )
1199
+ . addEventListener ( "click" , download_graph_in_one ) ;
1200
+ document
1201
+ . getElementById ( "download_split_button" )
1202
+ . addEventListener ( "click" , download_graph_split ) ;
1203
+ load_button . addEventListener ( "click" , load_graph ) ;
1204
+ document
1205
+ . getElementById ( "clear_all_button" )
1206
+ . addEventListener ( "click" , new_story ) ;
1207
+ add_media_button . addEventListener ( "click" , add_or_remove_media ) ;
1208
+ document
1209
+ . getElementById ( "redraw_button" )
1210
+ . addEventListener ( "click" , redraw_adventure_graph ) ;
1211
+
1212
+ document
1213
+ . getElementById ( "linearize_button" )
1214
+ . addEventListener ( "click" , create_linear_story ) ;
1215
+
1216
+ document . addEventListener ( "keydown" , handle_global_key_down ) ;
1217
+
1218
+ document
1219
+ . getElementById ( "story_modal" )
1220
+ . addEventListener ( "shown.bs.modal" , ( ) => {
1221
+ document . getElementById ( "story_code" ) . innerHTML = JSON . stringify (
1222
+ story ,
1223
+ null ,
1224
+ 2
1225
+ ) ;
1226
+ } ) ;
1227
+ }
1228
+
1207
1229
init ( ) ;
0 commit comments