|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | + <title>Editor</title> |
| 7 | + <script src="https://cdn.jsdelivr.net/npm/remotestoragejs@1.2.3/release/remotestorage.min.js"></script> |
| 8 | + <script src="https://cdn.jsdelivr.net/npm/remotestorage-widget@1.4.0/build/widget.js"></script> |
| 9 | + <style> |
| 10 | + * { box-sizing: border-box; margin: 0; padding: 0; } |
| 11 | + body { font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif; background: #f5f5f5; height: 100vh; display: flex; flex-direction: column; } |
| 12 | + header { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem 1rem; background: white; border-bottom: 1px solid #e0e0e0; } |
| 13 | + header h1 { font-size: 1.1rem; flex-shrink: 0; } |
| 14 | + #rs-widget { flex-shrink: 0; } |
| 15 | + .spacer { flex: 1; } |
| 16 | + #save-status { font-size: 0.8rem; color: #999; flex-shrink: 0; } |
| 17 | + .main { display: flex; flex: 1; min-height: 0; } |
| 18 | + .sidebar { width: 220px; background: white; border-right: 1px solid #e0e0e0; overflow-y: auto; flex-shrink: 0; } |
| 19 | + .sidebar .actions { padding: 0.5rem; border-bottom: 1px solid #eee; } |
| 20 | + .sidebar .actions button { width: 100%; padding: 0.4rem; background: #4a9eff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 0.85rem; } |
| 21 | + .sidebar .actions button:hover { background: #3a8eef; } |
| 22 | + .sidebar .actions button:disabled { background: #ccc; } |
| 23 | + .sidebar ul { list-style: none; } |
| 24 | + .sidebar li { padding: 0.6rem 0.75rem; cursor: pointer; font-size: 0.85rem; border-bottom: 1px solid #f0f0f0; display: flex; justify-content: space-between; align-items: center; } |
| 25 | + .sidebar li:hover { background: #f5f5f5; } |
| 26 | + .sidebar li.active { background: #e8f0fe; font-weight: 500; } |
| 27 | + .sidebar li .del { background: none; border: none; color: #ccc; cursor: pointer; font-size: 1rem; padding: 0 0.2rem; } |
| 28 | + .sidebar li .del:hover { color: #dc3545; } |
| 29 | + .editor-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; } |
| 30 | + #editor { flex: 1; width: 100%; border: none; outline: none; resize: none; padding: 1.5rem; font-family: 'Georgia', serif; font-size: 1.1rem; line-height: 1.7; background: #fafafa; } |
| 31 | + #editor:disabled { background: #f0f0f0; } |
| 32 | + .empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: #999; font-size: 1rem; } |
| 33 | + @media (max-width: 600px) { |
| 34 | + .sidebar { width: 160px; } |
| 35 | + #editor { padding: 1rem; font-size: 1rem; } |
| 36 | + } |
| 37 | + </style> |
| 38 | +</head> |
| 39 | +<body> |
| 40 | + <header> |
| 41 | + <h1>Editor</h1> |
| 42 | + <div id="rs-widget"></div> |
| 43 | + <div class="spacer"></div> |
| 44 | + <span id="save-status"></span> |
| 45 | + </header> |
| 46 | + <div class="main"> |
| 47 | + <div class="sidebar"> |
| 48 | + <div class="actions"> |
| 49 | + <button id="new-btn" disabled>New Document</button> |
| 50 | + </div> |
| 51 | + <ul id="doc-list"></ul> |
| 52 | + </div> |
| 53 | + <div class="editor-wrap"> |
| 54 | + <textarea id="editor" disabled placeholder="Select or create a document..."></textarea> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + |
| 58 | + <script> |
| 59 | + const rs = new RemoteStorage() |
| 60 | + rs.access.claim('documents', 'rw') |
| 61 | + rs.caching.enable('/documents/') |
| 62 | + |
| 63 | + const widget = new Widget(rs) |
| 64 | + widget.attach('rs-widget') |
| 65 | + |
| 66 | + const client = rs.scope('/documents/texts/') |
| 67 | + const editor = document.getElementById('editor') |
| 68 | + const docList = document.getElementById('doc-list') |
| 69 | + const newBtn = document.getElementById('new-btn') |
| 70 | + const saveStatus = document.getElementById('save-status') |
| 71 | + |
| 72 | + let currentDoc = null |
| 73 | + let saveTimer = null |
| 74 | + |
| 75 | + rs.on('connected', () => { |
| 76 | + newBtn.disabled = false |
| 77 | + loadDocList() |
| 78 | + }) |
| 79 | + |
| 80 | + rs.on('disconnected', () => { |
| 81 | + newBtn.disabled = true |
| 82 | + editor.disabled = true |
| 83 | + editor.value = '' |
| 84 | + currentDoc = null |
| 85 | + docList.innerHTML = '' |
| 86 | + }) |
| 87 | + |
| 88 | + async function loadDocList () { |
| 89 | + const listing = await client.getListing('') |
| 90 | + docList.innerHTML = '' |
| 91 | + |
| 92 | + if (!listing) return |
| 93 | + |
| 94 | + const files = Object.keys(listing).filter(k => !k.endsWith('/')).sort().reverse() |
| 95 | + for (const name of files) { |
| 96 | + addDocToSidebar(name) |
| 97 | + } |
| 98 | + |
| 99 | + // Auto-select first doc |
| 100 | + if (files.length > 0) selectDoc(files[0]) |
| 101 | + } |
| 102 | + |
| 103 | + function addDocToSidebar (name) { |
| 104 | + const li = document.createElement('li') |
| 105 | + li.dataset.name = name |
| 106 | + |
| 107 | + const label = document.createElement('span') |
| 108 | + label.textContent = docTitle(name) |
| 109 | + label.onclick = () => selectDoc(name) |
| 110 | + |
| 111 | + const del = document.createElement('button') |
| 112 | + del.className = 'del' |
| 113 | + del.textContent = '\u00d7' |
| 114 | + del.onclick = async (e) => { |
| 115 | + e.stopPropagation() |
| 116 | + await client.remove(name) |
| 117 | + li.remove() |
| 118 | + if (currentDoc === name) { |
| 119 | + currentDoc = null |
| 120 | + editor.value = '' |
| 121 | + editor.disabled = true |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + li.append(label, del) |
| 126 | + docList.appendChild(li) |
| 127 | + } |
| 128 | + |
| 129 | + function docTitle (name) { |
| 130 | + // name is like "1741234567890.txt" — show date |
| 131 | + const ts = parseInt(name.replace('.txt', '')) |
| 132 | + if (!isNaN(ts)) return new Date(ts).toLocaleDateString() + ' ' + new Date(ts).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) |
| 133 | + return name.replace('.txt', '') |
| 134 | + } |
| 135 | + |
| 136 | + async function selectDoc (name) { |
| 137 | + // Save current doc before switching |
| 138 | + if (currentDoc && saveTimer) { |
| 139 | + clearTimeout(saveTimer) |
| 140 | + await saveCurrentDoc() |
| 141 | + } |
| 142 | + |
| 143 | + currentDoc = name |
| 144 | + editor.disabled = false |
| 145 | + |
| 146 | + // Highlight in sidebar |
| 147 | + docList.querySelectorAll('li').forEach(li => { |
| 148 | + li.classList.toggle('active', li.dataset.name === name) |
| 149 | + }) |
| 150 | + |
| 151 | + const file = await client.getFile(name) |
| 152 | + editor.value = (file && file.data) || '' |
| 153 | + editor.focus() |
| 154 | + saveStatus.textContent = '' |
| 155 | + } |
| 156 | + |
| 157 | + async function saveCurrentDoc () { |
| 158 | + if (!currentDoc) return |
| 159 | + saveStatus.textContent = 'Saving...' |
| 160 | + await client.storeFile('text/plain', currentDoc, editor.value) |
| 161 | + saveStatus.textContent = 'Saved' |
| 162 | + setTimeout(() => { if (saveStatus.textContent === 'Saved') saveStatus.textContent = '' }, 2000) |
| 163 | + } |
| 164 | + |
| 165 | + // Auto-save on typing (debounced 1s) |
| 166 | + editor.addEventListener('input', () => { |
| 167 | + if (!currentDoc) return |
| 168 | + saveStatus.textContent = 'Editing...' |
| 169 | + clearTimeout(saveTimer) |
| 170 | + saveTimer = setTimeout(saveCurrentDoc, 1000) |
| 171 | + }) |
| 172 | + |
| 173 | + // New document |
| 174 | + newBtn.onclick = async () => { |
| 175 | + const name = Date.now() + '.txt' |
| 176 | + await client.storeFile('text/plain', name, '') |
| 177 | + addDocToSidebar(name) |
| 178 | + selectDoc(name) |
| 179 | + } |
| 180 | + </script> |
| 181 | +</body> |
| 182 | +</html> |
0 commit comments