Certainly! Here's the improved version of your HTML code with a more structured JavaScript implementation. The code has been refactored to use a NoteApp class instead of an Immediately Invoked Function Expression (IIFE). This enhances readability and maintainability. Additionally, all try-catch blocks have been removed as per your request since you're in the debugging stage.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Notes</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
:root {
--sidebar-width: 260px;
--primary-color: #2196F3;
--bg-color: #ffffff;
--text-color: #2c3e50;
--border-color: #eaeaea;
--hover-color: #f5f5f5;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
display: flex;
height: 100vh;
color: var(--text-color);
background: var(--bg-color);
}
.sidebar {
width: var(--sidebar-width);
border-right: 1px solid var(--border-color);
display: flex;
flex-direction: column;
height: 100%;
background: var(--bg-color);
}
.content {
flex: 1;
display: flex;
flex-direction: column;
height: 100%;
}
.toolbar {
padding: 10px;
border-bottom: 1px solid var(--border-color);
display: flex;
gap: 8px;
align-items: center;
justify-content: space-between;
}
.toolbar-group {
display: flex;
gap: 8px;
align-items: center;
}
.search-box {
flex: 1;
padding: 6px 12px;
border: 1px solid var(--border-color);
border-radius: 4px;
font-size: 14px;
outline: none;
}
.search-box:focus {
border-color: var(--primary-color);
}
.notes-list {
flex: 1;
overflow-y: auto;
padding: 8px;
}
.note-item {
padding: 8px 10px;
cursor: pointer;
border-radius: 4px;
margin-bottom: 2px;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 14px;
}
.note-item:hover {
background: var(--hover-color);
}
.note-item.active {
background: var(--primary-color);
color: white;
}
.note-actions {
opacity: 0;
display: flex;
gap: 4px;
}
.note-item:hover .note-actions {
opacity: 1;
}
.note-title {
padding: 10px 20px;
font-size: 16px;
border: none;
border-bottom: 1px solid var(--border-color);
outline: none;
width: 100%;
}
#editor {
flex: 1;
width: 100%;
padding: 20px;
border: none;
resize: none;
font-size: 15px;
line-height: 1.6;
outline: none;
}
.icon-button {
padding: 4px 8px;
background: transparent;
border: none;
cursor: pointer;
color: inherit;
border-radius: 4px;
font-size: 13px;
transition: background 0.2s ease;
}
.icon-button:hover {
background: rgba(0,0,0,0.1);
}
.tooltip {
position: relative;
}
.tooltip:hover::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
padding: 4px 8px;
background: rgba(0,0,0,0.8);
color: white;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
pointer-events: none;
margin-bottom: 5px;
}
.vault-selection {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.5);
display: flex;
align-items: center;
justify-content: center;
}
.vault-dialog {
background: white;
padding: 20px;
border-radius: 8px;
text-align: center;
}
.hidden {
display: none;
}
.empty-state {
text-align: center;
padding: 20px;
color: #666;
font-size: 14px;
}
@media (max-width: 768px) {
.sidebar {
width: 100%;
position: fixed;
transform: translateX(-100%);
transition: transform 0.3s ease;
}
.sidebar.show {
transform: translateX(0);
}
}
</style>
</head>
<body>
<div id="vault-selection" class="vault-selection">
<div class="vault-dialog">
<h2>Select Your Notes Folder</h2>
<button class="icon-button" id="select-vault">Choose Folder</button>
</div>
</div>
<div class="sidebar">
<div class="toolbar">
<div class="toolbar-group">
<input type="text" class="search-box" id="search-notes" placeholder="Search notes...">
<button class="icon-button tooltip" id="new-note" data-tooltip="New note (Ctrl+N)">New</button>
</div>
<div class="toolbar-group">
<button class="icon-button tooltip" id="change-vault" data-tooltip="Change vault folder">📁</button>
</div>
</div>
<div class="notes-list" id="notes-list">
<div class="empty-state">No notes yet</div>
</div>
</div>
<div class="content">
<input type="text" class="note-title" id="note-title" placeholder="Note title">
<textarea id="editor" placeholder="Start writing..."></textarea>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const app = new NoteApp();
app.init();
});
class NoteApp {
constructor() {
const { dialog, fs, path } = window.__TAURI__;
this.dialog = dialog;
this.fs = fs;
this.path = path;
this.vaultPath = null;
this.currentNote = null;
this.saveTimeout = null;
this.notes = [];
this.editor = document.getElementById('editor');
this.titleInput = document.getElementById('note-title');
this.notesList = document.getElementById('notes-list');
this.newNoteBtn = document.getElementById('new-note');
this.selectVaultBtn = document.getElementById('select-vault');
this.vaultSelection = document.getElementById('vault-selection');
this.searchInput = document.getElementById('search-notes');
this.changeVaultBtn = document.getElementById('change-vault');
}
async init() {
this.vaultPath = await localStorage.getItem('vaultPath');
console.log(this.vaultPath);
if (this.vaultPath) {
this.vaultSelection.classList.add('hidden');
await this.loadNotes();
}
this.addEventListeners();
}
addEventListeners() {
this.selectVaultBtn.addEventListener('click', () => this.selectVault());
this.changeVaultBtn.addEventListener('click', () => this.changeVault());
this.newNoteBtn.addEventListener('click', () => this.createNewNote());
this.editor.addEventListener('input', () => this.handleEditorInput());
this.titleInput.addEventListener('input', () => this.handleTitleInput());
document.addEventListener('keydown', (e) => this.handleKeyDown(e));
}
async selectVault() {
const selected = await this.dialog.open({ directory: true });
if (selected) {
this.vaultPath = selected;
console.log(await this.path.normalize(this.vaultPath));
await localStorage.setItem('vaultPath', this.vaultPath);
this.vaultSelection.classList.add('hidden');
await this.loadNotes();
}
}
async changeVault() {
const selected = await this.dialog.open({ directory: true });
if (selected) {
if (this.currentNote) {
await this.saveNote();
}
this.vaultPath = selected;
console.log(this.vaultPath);
await localStorage.setItem('vaultPath', this.vaultPath);
this.currentNote = null;
this.editor.value = '';
this.titleInput.value = '';
await this.loadNotes();
}
}
async createNewNote() {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const noteName = `Untitled Note.txt`;
const notePath = await this.path.join(this.vaultPath, noteName);
await this.fs.writeTextFile(notePath, '');
await this.loadNotes();
this.openNote(notePath);
}
handleEditorInput() {
if (this.currentNote) {
clearTimeout(this.saveTimeout);
this.saveTimeout = setTimeout(() => {
this.saveNote();
}, 300);
}
}
handleTitleInput() {
if (this.currentNote) {
clearTimeout(this.saveTimeout);
this.saveTimeout = setTimeout(() => {
this.renameNote();
}, 300);
}
}
async renameNote() {
const newTitle = this.titleInput.value.trim() || 'Untitled Note';
const newPath = await this.path.join(this.vaultPath, `${newTitle}.txt`);
if (newPath !== this.currentNote) {
await this.fs.rename(this.currentNote, newPath);
this.currentNote = newPath;
await this.loadNotes();
}
}
async saveNote() {
if (this.currentNote && this.editor.value !== undefined) {
await this.fs.writeTextFile(this.currentNote, this.editor.value);
}
}
async deleteNote(notePath) {
if (confirm('Are you sure you want to delete this note?')) {
await this.fs.remove(notePath);
if (this.currentNote === notePath) {
this.currentNote = null;
this.editor.value = '';
this.titleInput.value = '';
}
await this.loadNotes();
}
}
async loadNotes() {
this.notesList.innerHTML = '';
console.log(this.vaultPath);
const entries = await this.fs.readDir(this.vaultPath);
console.log(this.vaultPath);
this.notes = entries.filter(entry => !entry.children && entry.name.endsWith('.txt'));
if (this.notes.length === 0) {
this.notesList.innerHTML = `
<div class="empty-state">
No notes yet in:<br>
<small style="color: #999; word-break: break-all;">${this.vaultPath}</small>
</div>
`;
return;
}
this.notes.sort((a, b) => b.name.localeCompare(a.name));
for (const entry of this.notes) {
console.log(entry);
const entrypath = await this.path.join(this.vaultPath, entry.name);
const noteElement = document.createElement('div');
noteElement.className = 'note-item';
const noteName = document.createElement('span');
noteName.className = 'note-name';
noteName.textContent = entry.name.replace('.txt', '');
const actions = document.createElement('div');
actions.className = 'note-actions';
const deleteBtn = document.createElement('button');
deleteBtn.className = 'icon-button';
deleteBtn.textContent = 'Delete';
deleteBtn.onclick = (e) => {
e.stopPropagation();
this.deleteNote(entrypath);
};
actions.appendChild(deleteBtn);
noteElement.appendChild(noteName);
noteElement.appendChild(actions);
noteElement.addEventListener('click', () => this.openNote(entrypath));
this.notesList.appendChild(noteElement);
}
}
async openNote(notePath) {
const content = await this.fs.readTextFile(notePath);
this.editor.value = content;
this.currentNote = notePath;
const fileName = await this.path.basename(notePath);
this.titleInput.value = fileName.replace('.txt', '');
document.querySelectorAll('.note-item').forEach(item => {
item.classList.remove('active');
const noteName = item.querySelector('.note-name').textContent;
if (noteName === this.titleInput.value) {
item.classList.add('active');
}
});
}
handleKeyDown(e) {
if ((e.ctrlKey || e.metaKey) && e.key === 'n') {
e.preventDefault();
this.newNoteBtn.click();
}
}
}
</script>
</body>
</html>
Feel free to integrate this updated structure into your project. It should provide a cleaner and more maintainable codebase while retaining all existing functionalities.