diff --git a/app.py b/app.py index c9bf710..89763cd 100644 --- a/app.py +++ b/app.py @@ -33,7 +33,9 @@ LLM_CONFIG = { def load_notes(): """加载所有笔记""" if NOTES_FILE.exists(): - return json.loads(NOTES_FILE.read_text(encoding='utf-8')) + notes = json.loads(NOTES_FILE.read_text(encoding='utf-8')) + # 按置顶和更新时间排序 + return sorted(notes, key=lambda x: (not x.get('pinned', False), x.get('updated_at', '')), reverse=True) return [] def save_notes(notes): @@ -118,6 +120,7 @@ def api_notes(): 'updated_at': n['updated_at'], 'created_at': n['created_at'], 'preview': n['content'][:50] if n['content'] else '', + 'pinned': n.get('pinned', False), } for n in notes]) @app.route('/api/notes/') @@ -193,9 +196,11 @@ def api_regenerate_title(note_id): note['title'] = title note['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + # 重新排序 + notes = sorted(notes, key=lambda x: (not x.get('pinned', False), x.get('updated_at', '')), reverse=True) save_notes(notes) - return jsonify({'success': True, 'title': title}) + return jsonify({'success': True, 'title': title, 'note': note}) @app.route('/api/notes/', methods=['DELETE']) def api_delete_note(note_id): @@ -206,6 +211,23 @@ def api_delete_note(note_id): return jsonify({'success': True}) +@app.route('/api/notes//pin', methods=['POST']) +def api_pin_note(note_id): + """置顶/取消置顶笔记""" + notes = load_notes() + note = next((n for n in notes if n['id'] == note_id), None) + + if not note: + return jsonify({'error': 'Note not found'}), 404 + + note['pinned'] = not note.get('pinned', False) + + # 重新排序 + notes = sorted(notes, key=lambda x: (not x.get('pinned', False), x.get('updated_at', '')), reverse=True) + save_notes(notes) + + return jsonify({'success': True, 'pinned': note['pinned'], 'note': note}) + @app.route('/api/search') def api_search(): """搜索笔记""" @@ -217,13 +239,12 @@ def api_search(): notes = load_notes() results = [n for n in notes if keyword in n.get('title', '').lower() or keyword in n.get('content', '').lower()] - results = sorted(results, key=lambda x: x.get('updated_at', ''), reverse=True) - return jsonify([{ 'id': n['id'], 'title': n['title'], 'updated_at': n['updated_at'], 'preview': n['content'][:100] if n['content'] else '', + 'pinned': n.get('pinned', False), } for n in results]) if __name__ == '__main__': diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 diff --git a/templates/index.html b/templates/index.html index 5a6dcea..3ba0f7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,10 +9,15 @@ @@ -27,6 +32,15 @@ class="w-full pl-10 pr-4 py-2 border border-gray-200 rounded-lg focus:outline-none focus:border-purple-400" oninput="searchNotes()"> + + +
+ +
+ @@ -46,10 +60,18 @@