Skip to content

Commit e907e6b

Browse files
authored
Improve file open behavior and saved file name (#50)
1 parent aac1bd1 commit e907e6b

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

index.html

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -255,26 +255,7 @@
255255
// Open file: Ctrl+O (Windows/Linux) or Cmd+O (Mac)
256256
if (modKey && e.key === 'o') {
257257
e.preventDefault();
258-
if (!this.editor.value.trim()) {
259-
const input = document.createElement('input');
260-
input.type = 'file';
261-
input.accept = '.txt';
262-
input.onchange = (event) => {
263-
const file = event.target.files[0];
264-
if (file) {
265-
const reader = new FileReader();
266-
reader.onload = () => {
267-
this.editor.value = reader.result;
268-
this.saveText();
269-
this.updateCount();
270-
this.updateFade();
271-
};
272-
reader.readAsText(file);
273-
}
274-
input.value = null;
275-
};
276-
input.click();
277-
}
258+
this.openFile();
278259
return;
279260
}
280261

@@ -305,16 +286,14 @@
305286
this.fullscreen();
306287
return;
307288
}
289+
290+
// Focus editor on any other keydown
291+
this.editor.focus();
308292
});
309293

310294
// Update text area fade on scroll
311295
this.editor.addEventListener('scroll', () => this.updateFade(), { passive: true });
312296

313-
// Always focus on editor on keydown
314-
window.addEventListener('keydown', () => {
315-
this.editor.focus();
316-
});
317-
318297
window.addEventListener('resize', () => this.updateFade(), { passive: true });
319298

320299
// Control buttons
@@ -425,6 +404,7 @@
425404
return `~${days} day${days !== 1 ? 's' : ''}`;
426405
}
427406
}
407+
428408
updateCount() {
429409
const text = this.editor.value;
430410
const wordCount = this.getWordsCount(text);
@@ -491,11 +471,35 @@
491471
}
492472

493473
// Actions
474+
openFile() {
475+
const input = document.createElement('input');
476+
input.type = 'file';
477+
input.accept = '.txt';
478+
input.onchange = (event) => {
479+
const file = event.target.files[0];
480+
if (file) {
481+
if (this.editor.value.trim() && !confirm('Are you sure you want to clear all text and load the selected file?')) {
482+
return;
483+
}
484+
const reader = new FileReader();
485+
reader.onload = () => {
486+
this.editor.value = reader.result;
487+
this.saveText();
488+
this.updateCount();
489+
this.updateFade();
490+
};
491+
reader.readAsText(file);
492+
}
493+
};
494+
input.click();
495+
input.remove();
496+
}
497+
494498
download() {
495-
const timestamp = new Date().toISOString().slice(0, 16).replace(/[:T]/g, m => m === 'T' ? '_' : '-');
499+
const timestamp = new Date().toISOString().slice(0, 16).replace(/[:T]/g, m => m === 'T' ? '_at_' : '-');
496500
const a = document.createElement('a');
497501
a.href = URL.createObjectURL(new Blob([this.editor.value], { type: 'text/plain' }));
498-
a.download = `shiro_${timestamp}.txt`;
502+
a.download = `shiro_notes_on_${timestamp}.txt`;
499503
a.click();
500504
URL.revokeObjectURL(a.href);
501505
}

0 commit comments

Comments
 (0)