|
136 | 136 | </div> |
137 | 137 |
|
138 | 138 | <script> |
| 139 | + // LocalStorage keys |
| 140 | + const SHIRO_THEME = 'shiroTheme'; |
| 141 | + const SHIRO_COUNT_MODE = 'shiroCountMode'; |
| 142 | + const SHIRO_SAVED_TEXT = 'shiroSavedText'; |
| 143 | + const SHIRO_CURSOR_POSITION = 'shiroCursorPosition'; |
| 144 | + |
139 | 145 | class Shiro { |
140 | 146 | constructor() { |
141 | 147 | this.editor = document.getElementById('editor'); |
142 | 148 | this.controls = document.querySelectorAll('.controls'); |
143 | 149 | this.countDisplay = document.getElementById('count'); |
144 | | - this.theme = localStorage.getItem('shiroTheme') || |
| 150 | + this.theme = localStorage.getItem(SHIRO_THEME) || |
145 | 151 | (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'); |
146 | | - this.countMode = localStorage.getItem('shiroCountMode') || 'words'; |
| 152 | + this.countMode = localStorage.getItem(SHIRO_COUNT_MODE) || 'words'; |
147 | 153 | this.visible = true; |
148 | 154 |
|
149 | 155 | this.init(); |
|
248 | 254 | } |
249 | 255 |
|
250 | 256 | saveText() { |
251 | | - localStorage.setItem('shiroSavedText', this.editor.value); |
| 257 | + localStorage.setItem(SHIRO_SAVED_TEXT, this.editor.value); |
252 | 258 | } |
253 | 259 |
|
254 | 260 | loadText() { |
255 | | - this.editor.value = localStorage.getItem('shiroSavedText') || ''; |
| 261 | + this.editor.value = localStorage.getItem(SHIRO_SAVED_TEXT) || ''; |
256 | 262 | this.loadCursorPosition(); |
257 | 263 | this.updateCount(); |
258 | 264 | } |
259 | 265 |
|
260 | 266 | saveCursorPosition() { |
261 | | - localStorage.setItem('shiroCursorPosition', this.editor.selectionStart); |
| 267 | + localStorage.setItem(SHIRO_CURSOR_POSITION, this.editor.selectionStart); |
262 | 268 | } |
263 | 269 |
|
264 | 270 | loadCursorPosition() { |
265 | | - const saved = localStorage.getItem('shiroCursorPosition'); |
| 271 | + const saved = localStorage.getItem(SHIRO_CURSOR_POSITION); |
266 | 272 | if (saved) { |
267 | 273 | const position = parseInt(saved); |
268 | 274 | setTimeout(() => { |
|
289 | 295 |
|
290 | 296 | toggleCountMode() { |
291 | 297 | this.countMode = this.countMode === 'words' ? 'characters' : 'words'; |
292 | | - localStorage.setItem('shiroCountMode', this.countMode); |
| 298 | + localStorage.setItem(SHIRO_COUNT_MODE, this.countMode); |
293 | 299 | this.updateCount(); |
294 | 300 | } |
295 | 301 |
|
|
311 | 317 | setTheme(theme = this.theme) { |
312 | 318 | document.documentElement.setAttribute('data-theme', theme); |
313 | 319 | document.querySelector('[data-action="theme"]').textContent = theme === 'dark' ? 'Light' : 'Dark'; |
314 | | - localStorage.setItem('shiroTheme', theme); |
| 320 | + localStorage.setItem(SHIRO_THEME, theme); |
315 | 321 | this.theme = theme; |
316 | 322 | } |
317 | 323 |
|
|
344 | 350 | setTimeout(() => { |
345 | 351 | this.editor.value = ''; |
346 | 352 | this.editor.style.opacity = '1'; |
347 | | - localStorage.removeItem('shiroSavedText'); |
| 353 | + localStorage.removeItem(SHIRO_SAVED_TEXT); |
348 | 354 | this.updateCount(); |
349 | 355 | }, 300); |
350 | 356 | } |
|
0 commit comments