|
13 | 13 | // ★boot() window.onload直後の処理 |
14 | 14 | //--------------------------------------------------------------------------- |
15 | 15 | pzpr.on("load", function boot() { |
16 | | - if (importData()) { |
17 | | - startPuzzle(); |
| 16 | + var pzl; |
| 17 | + var storedGame = null; |
| 18 | + |
| 19 | + if (pzpr.env.localStorageAvailable) { |
| 20 | + // If localStorage is available and autosave is enabled: |
| 21 | + // Get URL search hash and check localStorage to see if a board state is saved |
| 22 | + |
| 23 | + // ui.menuconfig is not yet populated, so need to manually check |
| 24 | + var json_menu = localStorage.getItem("pzprv3_config:ui"); |
| 25 | + if (json_menu && JSON.parse(json_menu)["autosave"]) { |
| 26 | + var key = "pzpr_" + getPuzzleString(); |
| 27 | + storedGame = localStorage.getItem(key); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (storedGame) { |
| 32 | + var valObject = JSON.parse(storedGame); |
| 33 | + pzl = importData(valObject.pzl); |
18 | 34 | } else { |
| 35 | + pzl = importData(); |
| 36 | + } |
| 37 | + if (!pzl) { |
19 | 38 | setTimeout(boot, 0); |
20 | 39 | } |
| 40 | + startPuzzle(); |
21 | 41 | }); |
22 | 42 |
|
23 | | - function importData() { |
| 43 | + function importData(string) { |
24 | 44 | if (!onload_pzl) { |
25 | 45 | /* 1) 盤面複製・index.htmlからのファイル入力/Database入力か */ |
26 | 46 | /* 2) URL(?以降)をチェック */ |
27 | | - onload_pzl = importURL(); |
| 47 | + if (!string) { |
| 48 | + onload_pzl = importURL(); |
| 49 | + } else { |
| 50 | + onload_pzl = importFromString(string); |
| 51 | + } |
28 | 52 |
|
29 | 53 | /* 指定されたパズルがない場合はさようなら~ */ |
30 | 54 | if (!onload_pzl || !onload_pzl.pid) { |
|
90 | 114 | //--------------------------------------------------------------------------- |
91 | 115 | function importURL() { |
92 | 116 | /* index.htmlからURLが入力されていない場合は現在のURLの?以降をとってくる */ |
| 117 | + var puzString = getPuzzleString(); |
| 118 | + return importFromString(puzString); |
| 119 | + } |
| 120 | + //Splitting functionality from above for flexibility. |
| 121 | + |
| 122 | + //Return the string associated with the puzzle |
| 123 | + function getPuzzleString() { |
93 | 124 | var search = location.search; |
94 | 125 | if (!search) { |
95 | 126 | return null; |
96 | 127 | } |
97 | | - |
98 | | - /* 一旦先頭の?記号を取り除く */ |
99 | 128 | if (search.charAt(0) === "?") { |
100 | | - search = search.substr(1); |
| 129 | + search = search.slice(1); |
101 | 130 | } |
102 | 131 |
|
103 | 132 | while (search.match(/^(\w+)\=(\w+)\&(.*)/)) { |
104 | 133 | onload_option[RegExp.$1] = RegExp.$2; |
105 | 134 | search = RegExp.$3; |
106 | 135 | } |
| 136 | + return search; |
| 137 | + } |
| 138 | + //Import from a puzzle string. This can come from the URL or from localStorage |
| 139 | + function importFromString(string) { |
| 140 | + if (!string) { |
| 141 | + return null; |
| 142 | + } |
107 | 143 |
|
108 | | - onload_pzv = search; |
109 | | - var pzl = pzpr.parser.parseURL(search); |
| 144 | + onload_pzv = string; |
| 145 | + var pzl = pzpr.parser.parseURL(string); |
110 | 146 | var startmode = pzl.mode || (!pzl.body ? "editor" : "player"); |
111 | 147 | onload_option.type = onload_option.type || startmode; |
112 | 148 |
|
113 | 149 | return pzl; |
114 | 150 | } |
| 151 | + |
| 152 | + //--------------------------------------------------------------------------- |
| 153 | + // Functionality to support browser caching |
| 154 | + //--------------------------------------------------------------------------- |
| 155 | + |
| 156 | + //Save board state. Creates an entry in localStorage whose key is a 'pzpr_' identifier plus the current board state puzzle string. |
| 157 | + //Board state puzzle string is the same thing you get from duplicating the board state |
| 158 | + //Auto-exits if the correct setting is not set, so safe to call from anywhere without checking |
| 159 | + function saveBoardState() { |
| 160 | + if (!ui.menuconfig.get("autosave")) { |
| 161 | + return; |
| 162 | + } |
| 163 | + var key = "pzpr_" + getPuzzleString(); |
| 164 | + var url = ui.puzzle.getURL( |
| 165 | + pzpr.parser.URL_PZPRFILE, |
| 166 | + ui.puzzle.playeronly ? "player" : "editor" |
| 167 | + ); |
| 168 | + //Strip url to the last option. This is the "puzzle string" we want |
| 169 | + url = url.substring(url.indexOf("?") + 1); //Skip to the search parameters part of the url |
| 170 | + while (url.match(/^(\w+)\=(\w+)\&(.*)/)) { |
| 171 | + url = RegExp.$3; |
| 172 | + } |
| 173 | + //Add a time signifier so that we can sort and delete oldest if setting fails |
| 174 | + var valObject = { |
| 175 | + t: Date.now(), |
| 176 | + pzl: url |
| 177 | + // bufferToForceStorageLimitErrors: "0".repeat(1700000) //Include for testing to force out-of-storage errors |
| 178 | + }; |
| 179 | + pzpr.util.store(key, JSON.stringify(valObject)); |
| 180 | + } |
| 181 | + |
| 182 | + //Events that trigger a board state save |
| 183 | + document.addEventListener("visibilitychange", function() { |
| 184 | + if (document.visibilityState === "hidden") { |
| 185 | + saveBoardState(); |
| 186 | + } |
| 187 | + }); |
115 | 188 | })(); |
0 commit comments