|
| 1 | +/** |
| 2 | + * NoteMoveToGroupMenu — 阅读头「移动到组」下拉(v0.16.1 手动分组入口)。 |
| 3 | + * |
| 4 | + * @ai-context: 手动分组此前仅两条路径——拖拽笔记行到组行(HTML5 DnD,WebView2 |
| 5 | + * 下不稳健且难发现)与组行 ⓘ 弹层「移入选中笔记」(需先选中笔记, |
| 6 | + * 入口隐蔽)。本组件在笔记阅读头提供显式「移动到组」:当前组 ✓ + |
| 7 | + * 组色点 + 全部组列表 + 移出组(回「全部」);复用 move_note_to_group |
| 8 | + * 命令;变更经 onChanged(NotesPage.handleNoteChanged)刷新列表与 |
| 9 | + * 右栏(选中笔记 get_note 回填 group_id)。 |
| 10 | + */ |
| 11 | +import { useMemo, useState } from "react"; |
| 12 | +import { invoke } from "@tauri-apps/api/core"; |
| 13 | +import type { Note, NoteGroup } from "../types"; |
| 14 | +import { paletteHex } from "../utils/colorPalette"; |
| 15 | +import type { ThemeMode } from "../utils/colorPalette"; |
| 16 | + |
| 17 | +interface Props { |
| 18 | + note: Note; |
| 19 | + groups: NoteGroup[]; |
| 20 | + /** 变更后的刷新回调(父层重载列表 + 重取选中笔记) */ |
| 21 | + onChanged: () => void; |
| 22 | +} |
| 23 | + |
| 24 | +const DROPDOWN_ITEM: React.CSSProperties = { |
| 25 | + display: "flex", |
| 26 | + alignItems: "center", |
| 27 | + gap: 6, |
| 28 | + width: "100%", |
| 29 | + padding: "5px 8px", |
| 30 | + border: "none", |
| 31 | + background: "none", |
| 32 | + borderRadius: 4, |
| 33 | + fontSize: 12, |
| 34 | + color: "#374151", |
| 35 | + cursor: "pointer", |
| 36 | + textAlign: "left", |
| 37 | + whiteSpace: "nowrap", |
| 38 | + overflow: "hidden", |
| 39 | + textOverflow: "ellipsis", |
| 40 | +}; |
| 41 | + |
| 42 | +function GroupDot({ colorId }: { colorId: string | null | undefined }) { |
| 43 | + const theme: ThemeMode = useMemo( |
| 44 | + () => (typeof window !== "undefined" && window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light"), |
| 45 | + [], |
| 46 | + ); |
| 47 | + return ( |
| 48 | + <span |
| 49 | + style={{ |
| 50 | + width: 9, |
| 51 | + height: 9, |
| 52 | + borderRadius: 3, |
| 53 | + flexShrink: 0, |
| 54 | + display: "inline-block", |
| 55 | + background: paletteHex(colorId, theme), |
| 56 | + }} |
| 57 | + /> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export default function NoteMoveToGroupMenu({ note, groups, onChanged }: Props) { |
| 62 | + const [open, setOpen] = useState(false); |
| 63 | + const [busy, setBusy] = useState(false); |
| 64 | + const [status, setStatus] = useState(""); |
| 65 | + const currentId = note.group_id ?? null; |
| 66 | + |
| 67 | + const move = async (groupId: number | null) => { |
| 68 | + if (busy || groupId === currentId) return; |
| 69 | + setBusy(true); |
| 70 | + try { |
| 71 | + await invoke<boolean>("move_note_to_group", { noteId: note.id, groupId }); |
| 72 | + setStatus(""); |
| 73 | + setOpen(false); |
| 74 | + onChanged(); |
| 75 | + } catch (e) { |
| 76 | + setStatus(`移动失败: ${e}`); |
| 77 | + } finally { |
| 78 | + setBusy(false); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <div style={{ position: "relative" }}> |
| 84 | + <button |
| 85 | + data-testid="move-to-group-open" |
| 86 | + onClick={() => setOpen((v) => !v)} |
| 87 | + title="手动归组:把这条笔记移入/移出笔记组" |
| 88 | + style={{ |
| 89 | + fontSize: 12, |
| 90 | + cursor: "pointer", |
| 91 | + padding: "4px 8px", |
| 92 | + borderRadius: 4, |
| 93 | + border: "1px solid #d1d5db", |
| 94 | + background: "#fff", |
| 95 | + color: "#374151", |
| 96 | + }} |
| 97 | + > |
| 98 | + 📁 分组 |
| 99 | + </button> |
| 100 | + {open && ( |
| 101 | + <> |
| 102 | + {/* 透明背板:点击外部收起 */} |
| 103 | + <div |
| 104 | + onClick={() => setOpen(false)} |
| 105 | + style={{ position: "fixed", inset: 0, zIndex: 30, background: "transparent" }} |
| 106 | + /> |
| 107 | + <div |
| 108 | + data-testid="move-to-group-pop" |
| 109 | + style={{ |
| 110 | + position: "absolute", |
| 111 | + top: "100%", |
| 112 | + right: 0, |
| 113 | + zIndex: 31, |
| 114 | + minWidth: 220, |
| 115 | + maxHeight: 320, |
| 116 | + overflowY: "auto", |
| 117 | + background: "#fff", |
| 118 | + border: "1px solid #e5e7eb", |
| 119 | + borderRadius: 6, |
| 120 | + padding: 6, |
| 121 | + boxShadow: "0 4px 12px rgba(0,0,0,0.12)", |
| 122 | + }} |
| 123 | + > |
| 124 | + <div style={{ fontSize: 11, fontWeight: 600, color: "#6b7280", padding: "2px 8px 4px" }}> |
| 125 | + 移动到组(当前:{groups.find((g) => g.id === currentId)?.name ?? "未分组"}) |
| 126 | + </div> |
| 127 | + {groups.length === 0 && ( |
| 128 | + <div style={{ fontSize: 12, color: "#9ca3af", padding: "2px 8px 6px" }}> |
| 129 | + 暂无组——左侧「+ 新建组」创建后即可归组 |
| 130 | + </div> |
| 131 | + )} |
| 132 | + {groups.map((g) => { |
| 133 | + const active = g.id === currentId; |
| 134 | + return ( |
| 135 | + <button |
| 136 | + key={g.id} |
| 137 | + data-testid={`move-to-group-${g.id}`} |
| 138 | + onClick={() => void move(g.id)} |
| 139 | + disabled={busy || active} |
| 140 | + style={{ |
| 141 | + ...DROPDOWN_ITEM, |
| 142 | + background: active ? "#f0fdfa" : undefined, |
| 143 | + color: active ? "#0f766e" : "#374151", |
| 144 | + cursor: active ? "default" : "pointer", |
| 145 | + }} |
| 146 | + title={g.name} |
| 147 | + > |
| 148 | + <GroupDot colorId={g.color} /> |
| 149 | + <span style={{ flex: 1, overflow: "hidden", textOverflow: "ellipsis" }}>{g.name}</span> |
| 150 | + {active && <span>✓</span>} |
| 151 | + </button> |
| 152 | + ); |
| 153 | + })} |
| 154 | + {currentId != null && ( |
| 155 | + <button |
| 156 | + data-testid="move-to-group-none" |
| 157 | + onClick={() => void move(null)} |
| 158 | + disabled={busy} |
| 159 | + style={{ ...DROPDOWN_ITEM, color: "#b91c1c", borderTop: "1px solid #f3f4f6", marginTop: 4 }} |
| 160 | + > |
| 161 | + 移出分组(回「全部笔记」) |
| 162 | + </button> |
| 163 | + )} |
| 164 | + {status && ( |
| 165 | + <div style={{ fontSize: 11, color: "#dc2626", padding: "4px 8px" }}>{status}</div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + </> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments