| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"><head><meta charset="utf-8"> |
| 3 | <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 4 | <title>notes</title> |
| 5 | <!--TOKENS--> |
| 6 | <style> |
| 7 | *{box-sizing:border-box} |
| 8 | html,body{margin:0;height:100%} |
| 9 | body{background:var(--bg);color:var(--text);font-family:var(--font); |
| 10 | font-size:14px;line-height:1.5;-webkit-font-smoothing:antialiased;overflow:hidden} |
| 11 | #app{display:grid;grid-template-columns:288px minmax(0,1fr);height:100%;min-height:0} |
| 12 | .pane{min-width:0;min-height:0;display:flex;flex-direction:column} |
| 13 | #side{background:var(--bg-2);border-right:1px solid var(--line)} |
| 14 | .side-hd{display:flex;align-items:center;gap:8px;padding:12px 12px 8px} |
| 15 | .brand{font-size:12px;font-weight:700;letter-spacing:.09em;text-transform:uppercase; |
| 16 | color:var(--text-2);flex:1} |
| 17 | .side-ft{display:flex;align-items:center;gap:8px;padding:8px 12px; |
| 18 | border-top:1px solid var(--line);font-size:11px;color:var(--text-2)} |
| 19 | .side-ft .grow{flex:1} |
| 20 | .icon{display:inline-flex;align-items:center;justify-content:center;gap:6px; |
| 21 | background:var(--surface-2);border:1px solid var(--line);color:var(--text); |
| 22 | border-radius:9px;width:30px;height:30px;padding:0;cursor:pointer;font-family:inherit; |
| 23 | font-size:12px;font-weight:600} |
| 24 | .icon:hover{background:var(--surface-hover);border-color:var(--line-2)} |
| 25 | .icon:active{transform:scale(.96)} |
| 26 | .icon svg{width:15px;height:15px;display:block;fill:none;stroke:currentColor; |
| 27 | stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round} |
| 28 | .icon.danger{color:var(--red)} |
| 29 | .btn{background:var(--blue);color:#fff;border:0;border-radius:980px;padding:6px 14px; |
| 30 | font-weight:600;font-size:12.5px;cursor:pointer;font-family:inherit} |
| 31 | .btn:active{transform:scale(.98)} |
| 32 | .search{margin:0 12px 8px;padding:8px 11px;border-radius:9px;border:1px solid var(--line); |
| 33 | background:var(--input-bg);color:var(--text);font-family:inherit;font-size:13px} |
| 34 | .search:focus{outline:2px solid var(--blue);outline-offset:-1px} |
| 35 | .list{flex:1 1 auto;overflow-y:auto;padding:0 8px 8px} |
| 36 | .item{display:block;width:100%;text-align:left;background:transparent;border:0; |
| 37 | border-radius:10px;padding:9px 10px;color:var(--text);cursor:pointer;font-family:inherit; |
| 38 | margin-bottom:2px;position:relative} |
| 39 | .item:hover{background:var(--surface-hover)} |
| 40 | .item.sel{background:var(--surface-2);box-shadow:inset 2px 0 0 var(--blue)} |
| 41 | .item .t{font-size:13.5px;font-weight:600;white-space:nowrap;overflow:hidden; |
| 42 | text-overflow:ellipsis;padding-right:18px} |
| 43 | .item .m{font-size:11px;color:var(--text-2);white-space:nowrap;overflow:hidden; |
| 44 | text-overflow:ellipsis;margin-top:2px} |
| 45 | .item .pinmark{position:absolute;top:9px;right:9px;width:11px;height:11px;color:var(--blue); |
| 46 | display:none} |
| 47 | .item.pinned .pinmark{display:block} |
| 48 | .item .pinmark svg{width:11px;height:11px;fill:currentColor;stroke:none} |
| 49 | .empty{padding:18px 12px;color:var(--text-2);font-size:12.5px;text-align:center} |
| 50 | .hidden{display:none!important} |
| 51 | #main{background:var(--bg)} |
| 52 | .toolbar{display:flex;align-items:center;gap:8px;padding:10px 14px; |
| 53 | border-bottom:1px solid var(--line);flex:0 0 auto;min-width:0} |
| 54 | .toolbar .title{flex:1 1 auto;min-width:0;background:transparent;border:0;color:var(--text); |
| 55 | font-family:inherit;font-size:16px;font-weight:650;padding:4px 2px;border-radius:6px} |
| 56 | .toolbar .title:focus{outline:0;background:var(--input-bg);padding:4px 8px} |
| 57 | .state{font-size:11px;font-weight:600;color:var(--text-2);white-space:nowrap; |
| 58 | padding:2px 8px;border-radius:999px;background:var(--surface-2)} |
| 59 | .state.dirty{color:var(--orange)} |
| 60 | .state.ok{color:var(--green)} |
| 61 | .back{display:none} |
| 62 | .split{flex:1 1 auto;display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr); |
| 63 | min-height:0} |
| 64 | .editor{width:100%;height:100%;resize:none;border:0;border-right:1px solid var(--line); |
| 65 | background:var(--bg);color:var(--text);font-family:var(--mono);font-size:13px; |
| 66 | line-height:1.65;padding:16px 18px;tab-size:2} |
| 67 | .editor:focus{outline:0} |
| 68 | .preview{overflow-y:auto;padding:16px 20px 40px;min-width:0} |
| 69 | .preview h1,.preview h2,.preview h3,.preview h4,.preview h5,.preview h6{ |
| 70 | line-height:1.25;margin:18px 0 8px;font-weight:650} |
| 71 | .preview h1{font-size:24px;letter-spacing:-.01em} |
| 72 | .preview h2{font-size:19px} |
| 73 | .preview h3{font-size:16px} |
| 74 | .preview h4,.preview h5,.preview h6{font-size:14px;color:var(--text-2)} |
| 75 | .preview h1:first-child,.preview h2:first-child,.preview p:first-child{margin-top:0} |
| 76 | .preview p{margin:0 0 10px} |
| 77 | .preview ul,.preview ol{margin:0 0 10px;padding-left:22px} |
| 78 | .preview li{margin:2px 0} |
| 79 | .preview blockquote{margin:0 0 10px;padding:2px 12px;border-left:3px solid var(--line-2); |
| 80 | color:var(--text-2)} |
| 81 | .preview code{font-family:var(--mono);font-size:12px;background:var(--code-bg); |
| 82 | padding:1px 5px;border-radius:5px} |
| 83 | .preview pre{background:var(--code-bg);border:1px solid var(--line);border-radius:10px; |
| 84 | padding:11px 13px;overflow-x:auto;margin:0 0 12px} |
| 85 | .preview pre code{background:transparent;padding:0;font-size:12px;line-height:1.55} |
| 86 | .preview hr{border:0;border-top:1px solid var(--line);margin:16px 0} |
| 87 | .preview a{color:var(--blue);text-decoration:none;font-weight:600} |
| 88 | .preview a:hover{text-decoration:underline} |
| 89 | .preview table{border-collapse:collapse;margin:0 0 12px;font-size:13px} |
| 90 | .preview th,.preview td{border:1px solid var(--line);padding:5px 9px;text-align:left} |
| 91 | .preview th{color:var(--text-2);font-size:11.5px;text-transform:uppercase;letter-spacing:.04em} |
| 92 | .welcome{flex:1 1 auto;display:flex;flex-direction:column;align-items:center; |
| 93 | justify-content:center;gap:10px;color:var(--text-2);font-size:13px;padding:24px;text-align:center} |
| 94 | .welcome svg{width:34px;height:34px;fill:none;stroke:currentColor;stroke-width:1.4; |
| 95 | stroke-linecap:round;stroke-linejoin:round;opacity:.55} |
| 96 | .toast{position:fixed;left:50%;bottom:18px;transform:translateX(-50%); |
| 97 | background:var(--modal-bg);border:1px solid var(--line-2);color:var(--text); |
| 98 | padding:8px 14px;border-radius:999px;font-size:12.5px;box-shadow:var(--shadow-lg); |
| 99 | opacity:0;pointer-events:none;transition:opacity .16s var(--ease);z-index:9} |
| 100 | .toast.show{opacity:1} |
| 101 | @media (max-width:700px){ |
| 102 | body{overflow:hidden} |
| 103 | #app{grid-template-columns:minmax(0,1fr)} |
| 104 | #side,#main{grid-column:1;grid-row:1;width:100%} |
| 105 | #app[data-view="edit"] #side{display:none} |
| 106 | #app[data-view="list"] #main{display:none} |
| 107 | #side{border-right:0} |
| 108 | .back{display:inline-flex} |
| 109 | .split{grid-template-columns:minmax(0,1fr)} |
| 110 | .editor{border-right:0} |
| 111 | #app:not([data-view="preview"]) .preview{display:none} |
| 112 | .toolbar{padding:8px 10px;gap:6px} |
| 113 | .toolbar .title{font-size:15px} |
| 114 | .editor{padding:12px} |
| 115 | .preview{padding:12px 14px 32px} |
| 116 | } |
| 117 | </style> |
| 118 | </head><body> |
| 119 | <div id="app" data-view="list"> |
| 120 | <aside id="side" class="pane"> |
| 121 | <div class="side-hd"> |
| 122 | <span class="brand">notes</span> |
| 123 | <button id="btnNew" class="icon" title="New note" aria-label="New note"> |
| 124 | <svg viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg></button> |
| 125 | </div> |
| 126 | <input id="q" class="search" placeholder="Search notes" autocomplete="off" spellcheck="false"> |
| 127 | <div id="list" class="list"></div> |
| 128 | <div id="listEmpty" class="empty hidden">No notes yet. Create one with the plus button.</div> |
| 129 | <div class="side-ft"> |
| 130 | <span id="count">0 notes</span><span class="grow"></span> |
| 131 | <button id="btnTheme" class="icon embed-hide" title="Toggle appearance" aria-label="Toggle appearance"> |
| 132 | <svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="4.5"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M19.1 4.9l-1.4 1.4M6.3 17.7l-1.4 1.4"/></svg></button> |
| 133 | </div> |
| 134 | </aside> |
| 135 | <main id="main" class="pane"> |
| 136 | <div id="editorWrap" class="pane hidden"> |
| 137 | <div class="toolbar"> |
| 138 | <button id="btnBack" class="icon back" title="All notes" aria-label="All notes"> |
| 139 | <svg viewBox="0 0 24 24"><path d="M15 5l-7 7 7 7"/></svg></button> |
| 140 | <input id="title" class="title" placeholder="Untitled note" spellcheck="false"> |
| 141 | <span id="state" class="state">saved</span> |
| 142 | <button id="btnPin" class="icon" title="Pin note" aria-label="Pin note"> |
| 143 | <svg viewBox="0 0 24 24"><path d="M9 3h6l-1 6 3 3v2H7v-2l3-3-1-6zM12 14v7"/></svg></button> |
| 144 | <button id="btnPreview" class="icon back" title="Toggle preview" aria-label="Toggle preview"> |
| 145 | <svg viewBox="0 0 24 24"><path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7S1 12 1 12z"/><circle cx="12" cy="12" r="3"/></svg></button> |
| 146 | <button id="btnSave" class="btn" title="Save (Ctrl/Cmd+S)">Save</button> |
| 147 | <button id="btnDel" class="icon danger" title="Delete note" aria-label="Delete note"> |
| 148 | <svg viewBox="0 0 24 24"><path d="M4 7h16M9 7V5h6v2M6 7l1 13h10l1-13M10 11v6M14 11v6"/></svg></button> |
| 149 | </div> |
| 150 | <div class="split"> |
| 151 | <textarea id="body" class="editor" spellcheck="false" placeholder="Write markdown — # heading, **bold**, - list, `code`"></textarea> |
| 152 | <div id="preview" class="preview"></div> |
| 153 | </div> |
| 154 | </div> |
| 155 | <div id="welcome" class="welcome"> |
| 156 | <svg viewBox="0 0 24 24"><path d="M6 3h9l5 5v13H6z"/><path d="M14 3v5h5M9 13h7M9 17h5"/></svg> |
| 157 | <div>Select a note or create a new one.</div> |
| 158 | <div style="font-size:11.5px">Ctrl/Cmd+S saves · Ctrl/Cmd+K searches</div> |
| 159 | </div> |
| 160 | </main> |
| 161 | </div> |
| 162 | <div id="toast" class="toast"></div> |
| 163 | <script> |
| 164 | (function(){ |
| 165 | "use strict"; |
| 166 | var app=document.getElementById('app'); |
| 167 | var listEl=document.getElementById('list'); |
| 168 | var listEmpty=document.getElementById('listEmpty'); |
| 169 | var countEl=document.getElementById('count'); |
| 170 | var qEl=document.getElementById('q'); |
| 171 | var titleEl=document.getElementById('title'); |
| 172 | var bodyEl=document.getElementById('body'); |
| 173 | var previewEl=document.getElementById('preview'); |
| 174 | var stateEl=document.getElementById('state'); |
| 175 | var editorWrap=document.getElementById('editorWrap'); |
| 176 | var welcome=document.getElementById('welcome'); |
| 177 | var toastEl=document.getElementById('toast'); |
| 178 | |
| 179 | var q=new URLSearchParams(location.search); |
| 180 | var node=q.get('node')||''; |
| 181 | var sid=q.get('id')||''; |
| 182 | var pwd=q.get('pwd')||(node&&localStorage.getItem('bot_pwd_'+node))|| |
| 183 | localStorage.getItem('bot_active_pwd')||''; |
| 184 | var HUB=q.get('plugin')||''; |
| 185 | var SCROLLKEY='bot_notes_scroll'; |
| 186 | |
| 187 | /* Two call routes, and getting this wrong is the most common plugin bug: |
| 188 | * page served through the hub (bot.scorpiox.net/plugins?plugin=plugin_notes&node=..) |
| 189 | * -> call the hub proxy back: /plugins?plugin=plugin_notes&action=.. |
| 190 | * opened directly on the node |
| 191 | * -> the plugin itself is the route: /plugin_notes?action=.. |
| 192 | * Calling /plugins?plugin= on the node returns the discovery list, not us. */ |
| 193 | function url(action,extra){ |
| 194 | var u=(HUB?'/plugins?plugin='+encodeURIComponent(HUB):'/plugin_notes')+ |
| 195 | '?action='+encodeURIComponent(action)+ |
| 196 | (extra?('&'+extra):'')+ |
| 197 | (node?'&node='+encodeURIComponent(node):'')+ |
| 198 | (sid?'&id='+encodeURIComponent(sid):'')+ |
| 199 | (pwd?'&pwd='+encodeURIComponent(pwd):''); |
| 200 | return u; |
| 201 | } |
| 202 | function hdr(extra){ |
| 203 | var h=extra||{}; |
| 204 | if(pwd)h['X-Bot-Password']=pwd; |
| 205 | return h; |
| 206 | } |
| 207 | function api(action,extra){ |
| 208 | return fetch(url(action,extra),{headers:hdr({})}).then(function(r){ |
| 209 | if(!r.ok)throw new Error('HTTP '+r.status); |
| 210 | return r.json(); |
| 211 | }); |
| 212 | } |
| 213 | function apiPost(action,extra,payload){ |
| 214 | return fetch(url(action,extra),{method:'POST', |
| 215 | headers:hdr({'Content-Type':'application/json'}), |
| 216 | body:JSON.stringify(payload||{})}).then(function(r){ |
| 217 | if(!r.ok)throw new Error('HTTP '+r.status); |
| 218 | return r.json(); |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | var notes=[]; /* list rows */ |
| 223 | var cur=null; /* {id,title,body,pinned} */ |
| 224 | var dirty=false; |
| 225 | var saveTimer=null, prevTimer=null, searchTimer=null; |
| 226 | var saving=false; |
| 227 | |
| 228 | function toast(msg){ |
| 229 | toastEl.textContent=msg; |
| 230 | toastEl.classList.add('show'); |
| 231 | clearTimeout(toast._t); |
| 232 | toast._t=setTimeout(function(){toastEl.classList.remove('show');},1800); |
| 233 | } |
| 234 | function setState(kind,text){ |
| 235 | stateEl.className='state'+(kind?(' '+kind):''); |
| 236 | stateEl.textContent=text; |
| 237 | } |
| 238 | function fmtDate(iso){ |
| 239 | if(!iso)return ''; |
| 240 | try{ |
| 241 | var d=new Date(iso);var now=new Date(); |
| 242 | var diff=(now-d)/1000; |
| 243 | if(diff<60)return 'just now'; |
| 244 | if(diff<3600)return Math.floor(diff/60)+' min ago'; |
| 245 | if(diff<86400)return Math.floor(diff/3600)+' h ago'; |
| 246 | if(diff<604800)return Math.floor(diff/86400)+' d ago'; |
| 247 | return d.toLocaleDateString(); |
| 248 | }catch(e){return ''} |
| 249 | } |
| 250 | |
| 251 | function renderList(){ |
| 252 | while(listEl.firstChild)listEl.removeChild(listEl.firstChild); |
| 253 | var shown=0; |
| 254 | notes.forEach(function(n){ |
| 255 | var b=document.createElement('button'); |
| 256 | b.className='item'+(n.pinned?' pinned':'')+(cur&&cur.id===n.id?' sel':''); |
| 257 | b.setAttribute('data-id',n.id); |
| 258 | var t=document.createElement('div');t.className='t';t.textContent=n.title||'Untitled'; |
| 259 | var m=document.createElement('div');m.className='m'; |
| 260 | m.textContent=(n.pinned?'Pinned · ':'')+fmtDate(n.updated); |
| 261 | var pm=document.createElement('span');pm.className='pinmark'; |
| 262 | pm.innerHTML='<svg viewBox="0 0 24 24"><path d="M9 3h6l-1 6 3 3v2H7v-2l3-3-1-6z"/></svg>'; |
| 263 | b.appendChild(t);b.appendChild(m);b.appendChild(pm); |
| 264 | b.onclick=function(){openNote(n.id);}; |
| 265 | listEl.appendChild(b);shown++; |
| 266 | }); |
| 267 | listEmpty.classList.toggle('hidden',shown>0); |
| 268 | countEl.textContent=shown+(shown===1?' note':' notes'); |
| 269 | if(cur){ |
| 270 | var sel=listEl.querySelector('.item.sel'); |
| 271 | if(sel&&sel.scrollIntoView)sel.scrollIntoView({block:'nearest'}); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | function refresh(){ |
| 276 | return api('list').then(function(d){ |
| 277 | notes=(d&&d.notes)||[]; |
| 278 | renderList(); |
| 279 | }).catch(function(e){ |
| 280 | toast('Could not load notes: '+e.message); |
| 281 | }); |
| 282 | } |
| 283 | |
| 284 | function setView(v){app.setAttribute('data-view',v);} |
| 285 | |
| 286 | function openNote(id){ |
| 287 | return api('get','id='+encodeURIComponent(id)).then(function(d){ |
| 288 | if(!d||!d.note){toast('Note not found');return;} |
| 289 | cur={id:d.note.id,title:d.note.title||'',body:d.note.body||'', |
| 290 | pinned:!!d.note.pinned}; |
| 291 | titleEl.value=cur.title; |
| 292 | bodyEl.value=cur.body; |
| 293 | editorWrap.classList.remove('hidden'); |
| 294 | welcome.classList.add('hidden'); |
| 295 | setState('','saved'); |
| 296 | dirty=false; |
| 297 | setView('edit'); |
| 298 | document.getElementById('btnPin').classList.toggle('active',cur.pinned); |
| 299 | renderList(); |
| 300 | if(window.matchMedia('(min-width:701px)').matches){ |
| 301 | bodyEl.focus(); |
| 302 | } |
| 303 | renderPreview(); |
| 304 | }).catch(function(e){toast('Could not open note: '+e.message);}); |
| 305 | } |
| 306 | |
| 307 | function newNote(){ |
| 308 | cur={id:'',title:'',body:'',pinned:false}; |
| 309 | titleEl.value='';bodyEl.value=''; |
| 310 | editorWrap.classList.remove('hidden'); |
| 311 | welcome.classList.add('hidden'); |
| 312 | setState('dirty','unsaved'); |
| 313 | dirty=true; |
| 314 | setView('edit'); |
| 315 | previewEl.innerHTML=''; |
| 316 | titleEl.focus(); |
| 317 | } |
| 318 | |
| 319 | function scheduleSave(){ |
| 320 | dirty=true; |
| 321 | setState('dirty','unsaved'); |
| 322 | clearTimeout(saveTimer); |
| 323 | saveTimer=setTimeout(function(){save();},800); |
| 324 | } |
| 325 | |
| 326 | function save(force){ |
| 327 | if(!cur)return Promise.resolve(); |
| 328 | if(!dirty&&!force)return Promise.resolve(); |
| 329 | if(saving)return Promise.resolve(); |
| 330 | saving=true; |
| 331 | setState('','saving'); |
| 332 | var payload={title:titleEl.value,body:bodyEl.value,pinned:cur.pinned?1:0}; |
| 333 | if(cur.id)payload.id=cur.id; |
| 334 | return apiPost('save','',payload).then(function(d){ |
| 335 | saving=false; |
| 336 | if(d&&d.note){ |
| 337 | cur.id=d.note.id;cur.title=d.note.title;cur.body=bodyEl.value; |
| 338 | dirty=false; |
| 339 | setState('ok','saved'); |
| 340 | return refresh(); |
| 341 | } |
| 342 | throw new Error(d&&d.error?d.error:'save failed'); |
| 343 | }).catch(function(e){ |
| 344 | saving=false; |
| 345 | setState('dirty','unsaved'); |
| 346 | toast('Save failed: '+e.message); |
| 347 | }); |
| 348 | } |
| 349 | |
| 350 | function removeNote(){ |
| 351 | if(!cur||!cur.id){newNote();return;} |
| 352 | if(!window.confirm('Move "'+(cur.title||'Untitled')+'" to the trash?'))return; |
| 353 | apiPost('delete','id='+encodeURIComponent(cur.id),{}).then(function(){ |
| 354 | cur=null; |
| 355 | editorWrap.classList.add('hidden'); |
| 356 | welcome.classList.remove('hidden'); |
| 357 | setView('list'); |
| 358 | toast('Moved to trash'); |
| 359 | return refresh(); |
| 360 | }).catch(function(e){toast('Delete failed: '+e.message);}); |
| 361 | } |
| 362 | |
| 363 | function togglePin(){ |
| 364 | if(!cur)return; |
| 365 | cur.pinned=!cur.pinned; |
| 366 | document.getElementById('btnPin').classList.toggle('active',cur.pinned); |
| 367 | if(!cur.id){scheduleSave();return;} |
| 368 | apiPost('pin','id='+encodeURIComponent(cur.id),{pin:cur.pinned?1:0}) |
| 369 | .then(function(){return refresh();}) |
| 370 | .catch(function(e){toast('Pin failed: '+e.message);}); |
| 371 | } |
| 372 | |
| 373 | function renderPreview(){ |
| 374 | if(!cur)return; |
| 375 | var text=bodyEl.value; |
| 376 | clearTimeout(prevTimer); |
| 377 | prevTimer=setTimeout(function(){ |
| 378 | apiPost('render','',{body:text}).then(function(d){ |
| 379 | if(d&&typeof d.html==='string')previewEl.innerHTML=d.html; |
| 380 | }).catch(function(){/* preview is best-effort */}); |
| 381 | },220); |
| 382 | } |
| 383 | |
| 384 | function doSearch(){ |
| 385 | var term=qEl.value.trim(); |
| 386 | clearTimeout(searchTimer); |
| 387 | searchTimer=setTimeout(function(){ |
| 388 | if(!term){refresh();return;} |
| 389 | api('search','q='+encodeURIComponent(term)).then(function(d){ |
| 390 | notes=(d&&d.notes)||[]; |
| 391 | renderList(); |
| 392 | }).catch(function(e){toast('Search failed: '+e.message);}); |
| 393 | },180); |
| 394 | } |
| 395 | |
| 396 | document.getElementById('btnNew').onclick=newNote; |
| 397 | document.getElementById('btnSave').onclick=function(){save(true);}; |
| 398 | document.getElementById('btnDel').onclick=removeNote; |
| 399 | document.getElementById('btnPin').onclick=togglePin; |
| 400 | document.getElementById('btnBack').onclick=function(){ |
| 401 | setView('list');renderList(); |
| 402 | }; |
| 403 | document.getElementById('btnPreview').onclick=function(){ |
| 404 | setView(app.getAttribute('data-view')==='preview'?'edit':'preview'); |
| 405 | }; |
| 406 | qEl.addEventListener('input',doSearch); |
| 407 | titleEl.addEventListener('input',scheduleSave); |
| 408 | bodyEl.addEventListener('input',function(){scheduleSave();renderPreview();}); |
| 409 | titleEl.addEventListener('keydown',function(e){ |
| 410 | if(e.key==='Enter'){e.preventDefault();bodyEl.focus();} |
| 411 | }); |
| 412 | bodyEl.addEventListener('keydown',function(e){ |
| 413 | if(e.key==='Tab'){ |
| 414 | e.preventDefault(); |
| 415 | var s=bodyEl.selectionStart,en=bodyEl.selectionEnd; |
| 416 | bodyEl.value=bodyEl.value.slice(0,s)+' '+bodyEl.value.slice(en); |
| 417 | bodyEl.selectionStart=bodyEl.selectionEnd=s+2; |
| 418 | scheduleSave();renderPreview(); |
| 419 | } |
| 420 | }); |
| 421 | document.addEventListener('keydown',function(e){ |
| 422 | var mod=e.metaKey||e.ctrlKey; |
| 423 | if(!mod)return; |
| 424 | var k=(e.key||'').toLowerCase(); |
| 425 | if(k==='s'){e.preventDefault();save(true);} |
| 426 | else if(k==='k'){e.preventDefault();qEl.focus();qEl.select();} |
| 427 | else if(k==='n'&&e.altKey){e.preventDefault();newNote();} |
| 428 | }); |
| 429 | |
| 430 | var themeBtn=document.getElementById('btnTheme'); |
| 431 | if(themeBtn&&window.sxApplyTheme){ |
| 432 | themeBtn.onclick=function(){ |
| 433 | window.sxApplyTheme(window.sxTheme&&window.sxTheme()==='dark'?'light':'dark'); |
| 434 | }; |
| 435 | } |
| 436 | |
| 437 | refresh().then(function(){ |
| 438 | if(notes.length&&window.matchMedia('(min-width:701px)').matches){ |
| 439 | /* stay on the list — the user picks; predictable beats clever */ |
| 440 | } |
| 441 | }); |
| 442 | })(); |
| 443 | </script> |
| 444 | </body></html> |