Skip to content

Commit 940f37d

Browse files
authored
Keep local storage keys as constants (#34)
1 parent 9cf5a2f commit 940f37d

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

index.html

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,20 @@
136136
</div>
137137

138138
<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+
139145
class Shiro {
140146
constructor() {
141147
this.editor = document.getElementById('editor');
142148
this.controls = document.querySelectorAll('.controls');
143149
this.countDisplay = document.getElementById('count');
144-
this.theme = localStorage.getItem('shiroTheme') ||
150+
this.theme = localStorage.getItem(SHIRO_THEME) ||
145151
(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';
147153
this.visible = true;
148154

149155
this.init();
@@ -248,21 +254,21 @@
248254
}
249255

250256
saveText() {
251-
localStorage.setItem('shiroSavedText', this.editor.value);
257+
localStorage.setItem(SHIRO_SAVED_TEXT, this.editor.value);
252258
}
253259

254260
loadText() {
255-
this.editor.value = localStorage.getItem('shiroSavedText') || '';
261+
this.editor.value = localStorage.getItem(SHIRO_SAVED_TEXT) || '';
256262
this.loadCursorPosition();
257263
this.updateCount();
258264
}
259265

260266
saveCursorPosition() {
261-
localStorage.setItem('shiroCursorPosition', this.editor.selectionStart);
267+
localStorage.setItem(SHIRO_CURSOR_POSITION, this.editor.selectionStart);
262268
}
263269

264270
loadCursorPosition() {
265-
const saved = localStorage.getItem('shiroCursorPosition');
271+
const saved = localStorage.getItem(SHIRO_CURSOR_POSITION);
266272
if (saved) {
267273
const position = parseInt(saved);
268274
setTimeout(() => {
@@ -289,7 +295,7 @@
289295

290296
toggleCountMode() {
291297
this.countMode = this.countMode === 'words' ? 'characters' : 'words';
292-
localStorage.setItem('shiroCountMode', this.countMode);
298+
localStorage.setItem(SHIRO_COUNT_MODE, this.countMode);
293299
this.updateCount();
294300
}
295301

@@ -311,7 +317,7 @@
311317
setTheme(theme = this.theme) {
312318
document.documentElement.setAttribute('data-theme', theme);
313319
document.querySelector('[data-action="theme"]').textContent = theme === 'dark' ? 'Light' : 'Dark';
314-
localStorage.setItem('shiroTheme', theme);
320+
localStorage.setItem(SHIRO_THEME, theme);
315321
this.theme = theme;
316322
}
317323

@@ -344,7 +350,7 @@
344350
setTimeout(() => {
345351
this.editor.value = '';
346352
this.editor.style.opacity = '1';
347-
localStorage.removeItem('shiroSavedText');
353+
localStorage.removeItem(SHIRO_SAVED_TEXT);
348354
this.updateCount();
349355
}, 300);
350356
}

0 commit comments

Comments
 (0)