refactor: use Asia/Shanghai timezone for note timestamps
Browse files
app.py
CHANGED
|
@@ -1,404 +1,409 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
from huggingface_hub import HfApi, hf_hub_download
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
import shutil
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
from uuid import uuid4
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
--
|
| 23 |
-
--
|
| 24 |
-
--
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
::-webkit-scrollbar
|
| 90 |
-
::-webkit-scrollbar-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
base = os.environ.get("
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
"
|
| 136 |
-
"
|
| 137 |
-
"
|
| 138 |
-
"
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
continue
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
return "", "", ""
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
"
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
gr.
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import shutil
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from uuid import uuid4
|
| 9 |
+
from zoneinfo import ZoneInfo
|
| 10 |
+
|
| 11 |
+
# --- 配置 (优先从环境变量读取) ---
|
| 12 |
+
DATASET_REPO_ID = os.environ.get("DATASET_REPO_ID", "mingyang22/huggingface-notes")
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # 必须在 Space 设置中配置
|
| 14 |
+
REMOTE_NOTES_PATH = "db/notes.json"
|
| 15 |
+
BEIJING_TZ = ZoneInfo("Asia/Shanghai")
|
| 16 |
+
|
| 17 |
+
PWA_HEAD = """
|
| 18 |
+
<meta name="theme-color" content="#3b82f6" />
|
| 19 |
+
<style>
|
| 20 |
+
/* 玻璃质感与高级深色主题 CSS */
|
| 21 |
+
:root {
|
| 22 |
+
--bg-dark: #0a0a0a;
|
| 23 |
+
--accent: #3b82f6;
|
| 24 |
+
--border: #333333;
|
| 25 |
+
--text-main: #eeeeee;
|
| 26 |
+
--text-dim: #888888;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
body, .gradio-container {
|
| 30 |
+
background-color: var(--bg-dark) !important;
|
| 31 |
+
color: var(--text-main) !important;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/* 隐藏 Gradio 默认页脚 */
|
| 35 |
+
footer { display: none !important; }
|
| 36 |
+
|
| 37 |
+
/* 侧边栏按钮美化 */
|
| 38 |
+
.nav-btn button {
|
| 39 |
+
background: transparent !important;
|
| 40 |
+
border: none !important;
|
| 41 |
+
text-align: left !important;
|
| 42 |
+
padding-left: 20px !important;
|
| 43 |
+
font-size: 16px !important;
|
| 44 |
+
color: var(--text-dim) !important;
|
| 45 |
+
}
|
| 46 |
+
.nav-btn button:hover {
|
| 47 |
+
background: #1a1a1a !important;
|
| 48 |
+
color: white !important;
|
| 49 |
+
}
|
| 50 |
+
.active-nav button {
|
| 51 |
+
background: #252525 !important;
|
| 52 |
+
color: white !important;
|
| 53 |
+
border-left: 3px solid var(--accent) !important;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/* 列表美化 */
|
| 57 |
+
.note-list-item {
|
| 58 |
+
border-bottom: 1px solid #1a1a1a !important;
|
| 59 |
+
padding: 15px !important;
|
| 60 |
+
cursor: pointer;
|
| 61 |
+
}
|
| 62 |
+
.note-list-item:hover {
|
| 63 |
+
background: #1a1a1a !important;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
/* 编辑器美化 */
|
| 67 |
+
#note_title textarea {
|
| 68 |
+
font-size: 24px !important;
|
| 69 |
+
font-weight: bold !important;
|
| 70 |
+
background: transparent !important;
|
| 71 |
+
border: none !important;
|
| 72 |
+
color: #e0e0e0 !important;
|
| 73 |
+
}
|
| 74 |
+
#note_content textarea {
|
| 75 |
+
font-size: 16px !important;
|
| 76 |
+
background: transparent !important;
|
| 77 |
+
border: none !important;
|
| 78 |
+
color: #cccccc !important;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
/* AI 按钮渐变 */
|
| 82 |
+
#ai_btn {
|
| 83 |
+
background: linear-gradient(135deg, #7c3aed 0%, #db2777 100%) !important;
|
| 84 |
+
border: none !important;
|
| 85 |
+
font-weight: bold !important;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/* 滚动条美化 */
|
| 89 |
+
::-webkit-scrollbar { width: 6px; }
|
| 90 |
+
::-webkit-scrollbar-track { background: transparent; }
|
| 91 |
+
::-webkit-scrollbar-thumb { background: #333; border-radius: 3px; }
|
| 92 |
+
::-webkit-scrollbar-thumb:hover { background: #444; }
|
| 93 |
+
</style>
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
def get_default_data_dir():
|
| 97 |
+
# 如果在 Space 环境,优先使用当前目录下的 cache_data 文件夹,避免 /root 权限问题
|
| 98 |
+
if os.environ.get("SPACE_ID") or os.environ.get("HF_SPACE"):
|
| 99 |
+
return str(Path.cwd() / "cache_data")
|
| 100 |
+
|
| 101 |
+
custom_dir = os.environ.get("HF_NOTES_DATA_DIR")
|
| 102 |
+
if custom_dir: return custom_dir
|
| 103 |
+
if os.name == "nt":
|
| 104 |
+
base = os.environ.get("LOCALAPPDATA") or str(Path.home() / "AppData" / "Local")
|
| 105 |
+
else:
|
| 106 |
+
base = os.environ.get("XDG_DATA_HOME") or str(Path.home() / ".local" / "share")
|
| 107 |
+
return str(Path(base) / "hf-note-app-pro")
|
| 108 |
+
|
| 109 |
+
DATA_DIR = get_default_data_dir()
|
| 110 |
+
LOCAL_NOTES_PATH = str(Path(DATA_DIR) / "notes.json")
|
| 111 |
+
|
| 112 |
+
def ensure_local_notes():
|
| 113 |
+
Path(DATA_DIR).mkdir(parents=True, exist_ok=True)
|
| 114 |
+
p = Path(LOCAL_NOTES_PATH)
|
| 115 |
+
if not p.exists():
|
| 116 |
+
p.write_text("[]", encoding="utf-8")
|
| 117 |
+
|
| 118 |
+
def read_notes():
|
| 119 |
+
ensure_local_notes()
|
| 120 |
+
try:
|
| 121 |
+
data = json.loads(Path(LOCAL_NOTES_PATH).read_text(encoding="utf-8-sig"))
|
| 122 |
+
if isinstance(data, list):
|
| 123 |
+
valid_notes = []
|
| 124 |
+
for item in data:
|
| 125 |
+
if not isinstance(item, dict): continue
|
| 126 |
+
# 关键修复:同时支持 C# 风格 (Uppercase) 和 Python 风格 (Lowercase) 的键名
|
| 127 |
+
n_id = item.get("Id") or item.get("id", "")
|
| 128 |
+
n_title = item.get("Title") or item.get("title", "")
|
| 129 |
+
n_content = item.get("Content") or item.get("content", "")
|
| 130 |
+
n_updated = item.get("UpdatedAt") or item.get("updated_at", "")
|
| 131 |
+
n_pinned = item.get("IsPinned") if "IsPinned" in item else item.get("is_pinned", False)
|
| 132 |
+
n_deleted = item.get("IsDeleted") if "IsDeleted" in item else item.get("is_deleted", False)
|
| 133 |
+
|
| 134 |
+
valid_notes.append({
|
| 135 |
+
"id": str(n_id),
|
| 136 |
+
"title": str(n_title),
|
| 137 |
+
"content": str(n_content),
|
| 138 |
+
"updated_at": str(n_updated),
|
| 139 |
+
"is_pinned": bool(n_pinned),
|
| 140 |
+
"is_deleted": bool(n_deleted)
|
| 141 |
+
})
|
| 142 |
+
return valid_notes
|
| 143 |
+
except Exception as e:
|
| 144 |
+
print(f"读取笔记失败: {e}")
|
| 145 |
+
return []
|
| 146 |
+
|
| 147 |
+
def write_notes(notes):
|
| 148 |
+
ensure_local_notes()
|
| 149 |
+
Path(LOCAL_NOTES_PATH).write_text(
|
| 150 |
+
json.dumps(notes, ensure_ascii=False, indent=2),
|
| 151 |
+
encoding="utf-8",
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
def now_beijing():
|
| 155 |
+
return datetime.now(BEIJING_TZ)
|
| 156 |
+
|
| 157 |
+
# --- 持久化管理 ---
|
| 158 |
+
class CloudSync:
|
| 159 |
+
def __init__(self):
|
| 160 |
+
self.api = HfApi(token=HF_TOKEN)
|
| 161 |
+
|
| 162 |
+
def pull(self):
|
| 163 |
+
try:
|
| 164 |
+
ensure_local_notes()
|
| 165 |
+
print(f"🔄 正在从 Dataset {DATASET_REPO_ID} 拉取 {REMOTE_NOTES_PATH}...")
|
| 166 |
+
downloaded_path = hf_hub_download(
|
| 167 |
+
repo_id=DATASET_REPO_ID,
|
| 168 |
+
filename=REMOTE_NOTES_PATH,
|
| 169 |
+
repo_type="dataset",
|
| 170 |
+
token=HF_TOKEN,
|
| 171 |
+
force_download=True,
|
| 172 |
+
revision="main",
|
| 173 |
+
)
|
| 174 |
+
shutil.copy(downloaded_path, LOCAL_NOTES_PATH)
|
| 175 |
+
return True, f"✅ 云端拉取同步完成"
|
| 176 |
+
except Exception as e:
|
| 177 |
+
msg = str(e)
|
| 178 |
+
print(f"拉取失败详情: {msg}")
|
| 179 |
+
# 如果是 401/404,通常是 Token 没设或权限问题
|
| 180 |
+
if "401" in msg or "404" in msg:
|
| 181 |
+
return False, f"⚠️ 拉取失败: 请检查 Space 的 HF_TOKEN 是否已正确配置 (Dataset 可能为私有)"
|
| 182 |
+
return False, f"⚠️ 拉取失败: {msg}"
|
| 183 |
+
|
| 184 |
+
def push(self):
|
| 185 |
+
ensure_local_notes()
|
| 186 |
+
if not os.path.exists(LOCAL_NOTES_PATH): return False, "❌ 文件丢失"
|
| 187 |
+
try:
|
| 188 |
+
self.api.upload_file(
|
| 189 |
+
path_or_fileobj=LOCAL_NOTES_PATH,
|
| 190 |
+
path_in_repo=REMOTE_NOTES_PATH,
|
| 191 |
+
repo_id=DATASET_REPO_ID,
|
| 192 |
+
repo_type="dataset",
|
| 193 |
+
commit_message=f"Web Update Pro at {now_beijing().strftime('%Y-%m-%d %H:%M:%S %z')}"
|
| 194 |
+
)
|
| 195 |
+
return True, "✅ 已备份至云端"
|
| 196 |
+
except Exception as e:
|
| 197 |
+
return False, f"❌ 备份失败: {e}"
|
| 198 |
+
|
| 199 |
+
sync_manager = CloudSync()
|
| 200 |
+
|
| 201 |
+
# --- 业务逻辑 ---
|
| 202 |
+
def load_notes_list(filter_type="all", search_query=""):
|
| 203 |
+
notes = read_notes()
|
| 204 |
+
query = search_query.lower() if search_query else ""
|
| 205 |
+
|
| 206 |
+
filtered = []
|
| 207 |
+
for n in notes:
|
| 208 |
+
# Tab 过滤
|
| 209 |
+
is_deleted = n.get("is_deleted", False)
|
| 210 |
+
is_pinned = n.get("is_pinned", False)
|
| 211 |
+
|
| 212 |
+
if filter_type == "trash":
|
| 213 |
+
if not is_deleted: continue
|
| 214 |
+
else:
|
| 215 |
+
if is_deleted: continue
|
| 216 |
+
if filter_type == "pinned" and not is_pinned: continue
|
| 217 |
+
|
| 218 |
+
# 搜索过滤
|
| 219 |
+
if query and query not in n["title"].lower() and query not in n["content"].lower():
|
| 220 |
+
continue
|
| 221 |
+
|
| 222 |
+
filtered.append(n)
|
| 223 |
+
|
| 224 |
+
# 排序:置顶优先,时间倒序
|
| 225 |
+
sorted_notes = sorted(filtered, key=lambda x: (x.get("is_pinned", False), x.get("updated_at", "")), reverse=True)
|
| 226 |
+
|
| 227 |
+
return [
|
| 228 |
+
[n["id"], f"{'📌 ' if n.get('is_pinned') else ''}{n['title'] or '未命名'}", n["updated_at"]]
|
| 229 |
+
for n in sorted_notes
|
| 230 |
+
]
|
| 231 |
+
|
| 232 |
+
def get_note_detail(note_id):
|
| 233 |
+
if not note_id: return "", "", ""
|
| 234 |
+
notes = read_notes()
|
| 235 |
+
for n in notes:
|
| 236 |
+
if n["id"] == note_id:
|
| 237 |
+
return n["title"], n["content"], n["updated_at"]
|
| 238 |
+
return "", "", ""
|
| 239 |
+
|
| 240 |
+
def handle_save(note_id, title, content, push_cloud=False):
|
| 241 |
+
if not title and not content:
|
| 242 |
+
return "无内容可保存", load_notes_list(), note_id
|
| 243 |
+
|
| 244 |
+
notes = read_notes()
|
| 245 |
+
now = now_beijing().isoformat(timespec="seconds")
|
| 246 |
+
|
| 247 |
+
found = False
|
| 248 |
+
for n in notes:
|
| 249 |
+
if n["id"] == note_id:
|
| 250 |
+
n["title"], n["content"], n["updated_at"] = title, content, now
|
| 251 |
+
found = True
|
| 252 |
+
break
|
| 253 |
+
|
| 254 |
+
if not found:
|
| 255 |
+
new_id = uuid4().hex
|
| 256 |
+
new_note = {
|
| 257 |
+
"id": new_id,
|
| 258 |
+
"title": title or "新笔记",
|
| 259 |
+
"content": content,
|
| 260 |
+
"updated_at": now,
|
| 261 |
+
"is_pinned": False,
|
| 262 |
+
"is_deleted": False
|
| 263 |
+
}
|
| 264 |
+
notes.insert(0, new_note)
|
| 265 |
+
note_id = new_id
|
| 266 |
+
|
| 267 |
+
write_notes(notes)
|
| 268 |
+
|
| 269 |
+
if push_cloud:
|
| 270 |
+
_, msg = sync_manager.push()
|
| 271 |
+
status = f"已保存并同步 | {msg}"
|
| 272 |
+
else:
|
| 273 |
+
status = "已自动保存到本地"
|
| 274 |
+
|
| 275 |
+
return status, load_notes_list(), note_id
|
| 276 |
+
|
| 277 |
+
def handle_delete(note_id, current_filter):
|
| 278 |
+
if not note_id: return "未选择笔记", load_notes_list(current_filter), ""
|
| 279 |
+
notes = read_notes()
|
| 280 |
+
for n in notes:
|
| 281 |
+
if n["id"] == note_id:
|
| 282 |
+
if current_filter == "trash":
|
| 283 |
+
notes.remove(n)
|
| 284 |
+
else:
|
| 285 |
+
n["is_deleted"] = True
|
| 286 |
+
n["is_pinned"] = False
|
| 287 |
+
break
|
| 288 |
+
write_notes(notes)
|
| 289 |
+
sync_manager.push()
|
| 290 |
+
return "已移至回收站" if current_filter != "trash" else "已彻底删除", load_notes_list(current_filter), ""
|
| 291 |
+
|
| 292 |
+
def handle_pin(note_id, current_filter):
|
| 293 |
+
if not note_id: return load_notes_list(current_filter)
|
| 294 |
+
notes = read_notes()
|
| 295 |
+
for n in notes:
|
| 296 |
+
if n["id"] == note_id:
|
| 297 |
+
n["is_pinned"] = not n.get("is_pinned", False)
|
| 298 |
+
break
|
| 299 |
+
write_notes(notes)
|
| 300 |
+
sync_manager.push()
|
| 301 |
+
return load_notes_list(current_filter)
|
| 302 |
+
|
| 303 |
+
# --- Gradio UI ---
|
| 304 |
+
with gr.Blocks(theme=gr.themes.Default(), head=PWA_HEAD) as demo:
|
| 305 |
+
current_filter_state = gr.State("all")
|
| 306 |
+
selected_note_id = gr.State("")
|
| 307 |
+
|
| 308 |
+
with gr.Row(equal_height=True):
|
| 309 |
+
# 1. 导航栏 (ClassNote 风格)
|
| 310 |
+
with gr.Column(scale=1, min_width=150):
|
| 311 |
+
gr.HTML("<div style='font-size: 20px; font-weight: bold; margin-bottom: 30px; color: white;'>HF Note</div>")
|
| 312 |
+
btn_all = gr.Button("全部笔记", variant="secondary", elem_classes=["nav-btn", "active-nav"])
|
| 313 |
+
btn_pinned = gr.Button("已置顶", variant="secondary", elem_classes=["nav-btn"])
|
| 314 |
+
btn_trash = gr.Button("回收站", variant="secondary", elem_classes=["nav-btn"])
|
| 315 |
+
|
| 316 |
+
gr.Markdown("---")
|
| 317 |
+
btn_new = gr.Button("➕ 新建笔记", variant="secondary")
|
| 318 |
+
btn_sync_pull = gr.Button("🔄 同步云端", variant="secondary")
|
| 319 |
+
|
| 320 |
+
# 2. 列表栏
|
| 321 |
+
with gr.Column(scale=2, min_width=250):
|
| 322 |
+
search_box = gr.Textbox(placeholder="搜索笔记...", show_label=False, elem_id="search_box")
|
| 323 |
+
note_list = gr.Dataframe(
|
| 324 |
+
headers=["ID", "标题", "时间"],
|
| 325 |
+
datatype=["str", "str", "str"],
|
| 326 |
+
col_count=(3, "fixed"),
|
| 327 |
+
interactive=False,
|
| 328 |
+
label=None,
|
| 329 |
+
)
|
| 330 |
+
status_text = gr.Markdown("就绪")
|
| 331 |
+
|
| 332 |
+
# 3. 编辑器栏
|
| 333 |
+
with gr.Column(scale=4):
|
| 334 |
+
with gr.Row():
|
| 335 |
+
btn_save = gr.Button("💾 保存", variant="primary", size="sm")
|
| 336 |
+
btn_pin = gr.Button("📌 置顶", variant="secondary", size="sm")
|
| 337 |
+
btn_del = gr.Button("🗑️ 删除", variant="stop", size="sm")
|
| 338 |
+
btn_ai = gr.Button("AI 润色", variant="primary", size="sm", elem_id="ai_btn")
|
| 339 |
+
|
| 340 |
+
edit_title = gr.Textbox(placeholder="无标题笔记", show_label=False, elem_id="note_title")
|
| 341 |
+
edit_content = gr.TextArea(placeholder="暂无内容,开始输入...", show_label=False, lines=25, elem_id="note_content")
|
| 342 |
+
edit_date = gr.Markdown("", elem_id="note_date")
|
| 343 |
+
|
| 344 |
+
# --- 交互事件 ---
|
| 345 |
+
|
| 346 |
+
def on_note_select(evt: gr.SelectData, filt):
|
| 347 |
+
curr_list = load_notes_list(filt)
|
| 348 |
+
if not curr_list or not evt.index or evt.index[0] >= len(curr_list):
|
| 349 |
+
return "", "", "", ""
|
| 350 |
+
note_id = curr_list[evt.index[0]][0]
|
| 351 |
+
title, content, date = get_note_detail(note_id)
|
| 352 |
+
return note_id, title, content, f"最后修改: {date}"
|
| 353 |
+
|
| 354 |
+
def handle_autosave(note_id, title, content):
|
| 355 |
+
return handle_save(note_id, title, content, push_cloud=False)
|
| 356 |
+
|
| 357 |
+
def handle_manual_save(note_id, title, content):
|
| 358 |
+
return handle_save(note_id, title, content, push_cloud=True)
|
| 359 |
+
|
| 360 |
+
def switch_filter(filter_type, search_query):
|
| 361 |
+
return (
|
| 362 |
+
filter_type,
|
| 363 |
+
load_notes_list(filter_type, search_query),
|
| 364 |
+
"",
|
| 365 |
+
"",
|
| 366 |
+
"",
|
| 367 |
+
"",
|
| 368 |
+
f"已切换到:{filter_type}"
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
+
def switch_all(search_query):
|
| 372 |
+
return switch_filter("all", search_query)
|
| 373 |
+
|
| 374 |
+
def switch_pinned(search_query):
|
| 375 |
+
return switch_filter("pinned", search_query)
|
| 376 |
+
|
| 377 |
+
def switch_trash(search_query):
|
| 378 |
+
return switch_filter("trash", search_query)
|
| 379 |
+
|
| 380 |
+
note_list.select(on_note_select, [current_filter_state], [selected_note_id, edit_title, edit_content, edit_date])
|
| 381 |
+
|
| 382 |
+
search_box.change(load_notes_list, [current_filter_state, search_box], [note_list])
|
| 383 |
+
|
| 384 |
+
# 离开焦点时保存
|
| 385 |
+
edit_title.blur(handle_autosave, [selected_note_id, edit_title, edit_content], [status_text, note_list, selected_note_id])
|
| 386 |
+
edit_content.blur(handle_autosave, [selected_note_id, edit_title, edit_content], [status_text, note_list, selected_note_id])
|
| 387 |
+
# 显式保存按钮(PWA/移动端更可靠)
|
| 388 |
+
btn_save.click(handle_manual_save, [selected_note_id, edit_title, edit_content], [status_text, note_list, selected_note_id])
|
| 389 |
+
|
| 390 |
+
btn_all.click(switch_all, [search_box], [current_filter_state, note_list, selected_note_id, edit_title, edit_content, edit_date, status_text])
|
| 391 |
+
btn_pinned.click(switch_pinned, [search_box], [current_filter_state, note_list, selected_note_id, edit_title, edit_content, edit_date, status_text])
|
| 392 |
+
btn_trash.click(switch_trash, [search_box], [current_filter_state, note_list, selected_note_id, edit_title, edit_content, edit_date, status_text])
|
| 393 |
+
|
| 394 |
+
btn_new.click(lambda: ("", "新笔记", "", ""), None, [selected_note_id, edit_title, edit_content, edit_date])
|
| 395 |
+
btn_pin.click(handle_pin, [selected_note_id, current_filter_state], [note_list])
|
| 396 |
+
btn_del.click(handle_delete, [selected_note_id, current_filter_state], [status_text, note_list, selected_note_id])
|
| 397 |
+
|
| 398 |
+
btn_sync_pull.click(lambda: (sync_manager.pull()[1], load_notes_list()), None, [status_text, note_list])
|
| 399 |
+
|
| 400 |
+
def ai_polish(content):
|
| 401 |
+
if not content: return content
|
| 402 |
+
return f"✨ [AI 润色已模拟完成]\n\n{content}\n\n(请在本地动作中使用完整的 DeepSeek 润色服务)"
|
| 403 |
+
btn_ai.click(ai_polish, [edit_content], [edit_content])
|
| 404 |
+
|
| 405 |
+
# 启动拉取
|
| 406 |
+
demo.load(lambda: (sync_manager.pull()[1], load_notes_list()), None, [status_text, note_list])
|
| 407 |
+
|
| 408 |
+
if __name__ == "__main__":
|
| 409 |
+
demo.launch()
|