| 1 | /* Utopia UI 组件库 — 页面只用这里的组件与 styles.css 语义类,不写颜色字面量。 */ |
| 2 | import { useEffect, useRef, useState } from "react"; |
| 3 | import type { |
| 4 | ButtonHTMLAttributes, |
| 5 | InputHTMLAttributes, |
| 6 | ReactNode, |
| 7 | } from "react"; |
| 8 | import { |
| 9 | ArrowUpRight, |
| 10 | Check, |
| 11 | ChevronDown, |
| 12 | ChevronLeft, |
| 13 | ChevronRight, |
| 14 | Search as SearchIcon, |
| 15 | } from "lucide-react"; |
| 16 | import { S } from "../i18n"; |
| 17 | |
| 18 | /** 应用内左栏统一底座:宽度 + 玻璃面(各页在此之上加 flex/padding)。 |
| 19 | 以最宽的 Ontology(w-64)为基准——rail 装的是名字,宽一档少截断。 */ |
| 20 | export const RAIL_CLS = "w-64 shrink-0 glass-strong border-y-0 border-l-0"; |
| 21 | |
| 22 | /** 品牌字标:Marcellus 衬线,逐字母从左到右淡入;hover 浮出 ↗,点击去官网。 |
| 23 | 箭头/偏移全部用 em,跟随使用处的字号缩放(登录大标题与顶栏共用)。 */ |
| 24 | export function Wordmark({ className }: { className?: string }) { |
| 25 | return ( |
| 26 | <a |
| 27 | href={S.app.siteUrl} |
| 28 | target="_blank" |
| 29 | rel="noreferrer" |
| 30 | title="utopia.bi" |
| 31 | className={cn("relative inline-flex text-white", className)} |
| 32 | style={{ fontFamily: "var(--font-brand)", letterSpacing: "0.06em" }} |
| 33 | > |
| 34 | {[...S.app.name].map((ch, i) => ( |
| 35 | <span |
| 36 | key={i} |
| 37 | className="u-letter" |
| 38 | style={{ animationDelay: `${80 + i * 65}ms` }} |
| 39 | > |
| 40 | {ch} |
| 41 | </span> |
| 42 | ))} |
| 43 | <ArrowUpRight className="u-mark-arrow" aria-hidden /> |
| 44 | </a> |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | export function cn(...parts: (string | false | null | undefined)[]): string { |
| 49 | return parts.filter(Boolean).join(" "); |
| 50 | } |
| 51 | |
| 52 | /* ---------- Button ---------- */ |
| 53 | type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & { |
| 54 | variant?: "primary" | "ghost"; |
| 55 | size?: "sm" | "md"; |
| 56 | }; |
| 57 | |
| 58 | export function Button({ |
| 59 | variant = "primary", |
| 60 | size = "md", |
| 61 | className, |
| 62 | ...props |
| 63 | }: ButtonProps) { |
| 64 | return ( |
| 65 | <button |
| 66 | className={cn( |
| 67 | "u-btn", |
| 68 | variant === "primary" ? "u-btn-primary" : "u-btn-ghost", |
| 69 | size === "sm" ? "px-3 py-1.5 text-xs" : "px-4 py-2 text-sm", |
| 70 | className, |
| 71 | )} |
| 72 | {...props} |
| 73 | /> |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /* ---------- Input / Select ---------- */ |
| 78 | export function Input({ |
| 79 | className, |
| 80 | ...props |
| 81 | }: InputHTMLAttributes<HTMLInputElement>) { |
| 82 | return ( |
| 83 | <input |
| 84 | className={cn("input-dark px-3 py-2 text-sm", className)} |
| 85 | {...props} |
| 86 | /> |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /* ---------- Dropdown(自制下拉,替代原生 select:原生弹层无法主题化) ---------- */ |
| 91 | export interface DropdownOption { |
| 92 | value: string; |
| 93 | label: ReactNode; |
| 94 | } |
| 95 | |
| 96 | export function Dropdown({ |
| 97 | value, |
| 98 | options, |
| 99 | onChange, |
| 100 | placeholder, |
| 101 | className, |
| 102 | size = "md", |
| 103 | icon, |
| 104 | menuLabel, |
| 105 | footer, |
| 106 | }: { |
| 107 | value: string; |
| 108 | options: DropdownOption[]; |
| 109 | onChange: (v: string) => void; |
| 110 | placeholder?: string; |
| 111 | className?: string; |
| 112 | size?: "sm" | "md"; |
| 113 | /** 触发器左侧的语义图标(说明"这一级是什么") */ |
| 114 | icon?: ReactNode; |
| 115 | /** 弹层顶部的小标题(同时作为触发器 title 提示) */ |
| 116 | menuLabel?: string; |
| 117 | /** 弹层底部固定操作区(点击后弹层关闭) */ |
| 118 | footer?: ReactNode; |
| 119 | }) { |
| 120 | const [open, setOpen] = useState(false); |
| 121 | const rootRef = useRef<HTMLDivElement>(null); |
| 122 | |
| 123 | useEffect(() => { |
| 124 | if (!open) return; |
| 125 | const onDoc = (e: MouseEvent) => { |
| 126 | if (!rootRef.current?.contains(e.target as Node)) setOpen(false); |
| 127 | }; |
| 128 | const onKey = (e: KeyboardEvent) => { |
| 129 | if (e.key === "Escape") setOpen(false); |
| 130 | }; |
| 131 | document.addEventListener("mousedown", onDoc); |
| 132 | document.addEventListener("keydown", onKey); |
| 133 | return () => { |
| 134 | document.removeEventListener("mousedown", onDoc); |
| 135 | document.removeEventListener("keydown", onKey); |
| 136 | }; |
| 137 | }, [open]); |
| 138 | |
| 139 | const current = options.find((o) => o.value === value); |
| 140 | const pad = size === "sm" ? "px-2.5 py-1 text-xs" : "px-3 py-1.5 text-sm"; |
| 141 | |
| 142 | return ( |
| 143 | <div ref={rootRef} className={cn("relative", className)}> |
| 144 | <button |
| 145 | type="button" |
| 146 | onClick={() => setOpen(!open)} |
| 147 | title={menuLabel} |
| 148 | className={cn( |
| 149 | "input-dark w-full flex items-center gap-2 text-left", |
| 150 | pad, |
| 151 | )} |
| 152 | > |
| 153 | {icon && <span className="shrink-0 text-neutral-500">{icon}</span>} |
| 154 | <span className="flex-1 min-w-0 truncate"> |
| 155 | {current?.label ?? ( |
| 156 | <span className="text-neutral-600">{placeholder ?? ""}</span> |
| 157 | )} |
| 158 | </span> |
| 159 | <ChevronDown |
| 160 | size={12} |
| 161 | className={cn( |
| 162 | "shrink-0 text-neutral-500 transition-transform", |
| 163 | open && "rotate-180", |
| 164 | )} |
| 165 | /> |
| 166 | </button> |
| 167 | {open && ( |
| 168 | <div className="u-pop u-pop-in u-pop-in-tl absolute z-50 mt-1 w-full rounded-lg shadow-xl overflow-hidden"> |
| 169 | {menuLabel && ( |
| 170 | <div className="px-2.5 pt-2 pb-1 text-[9.5px] font-medium uppercase tracking-[0.1em] text-neutral-600 border-b border-white/5"> |
| 171 | {menuLabel} |
| 172 | </div> |
| 173 | )} |
| 174 | {/* 选项行顶满面板边缘(无内衬):单选项时整个菜单被这一项填满 */} |
| 175 | <div className="u-scroll max-h-60 overflow-y-auto"> |
| 176 | {options.map((o) => ( |
| 177 | <button |
| 178 | key={o.value} |
| 179 | type="button" |
| 180 | onClick={() => { |
| 181 | onChange(o.value); |
| 182 | setOpen(false); |
| 183 | }} |
| 184 | className={cn( |
| 185 | "w-full flex items-center gap-2 text-left", |
| 186 | pad, |
| 187 | o.value === value |
| 188 | ? "bg-white/[0.12] text-white" |
| 189 | : "text-neutral-300 hover:bg-white/[0.06] hover:text-white", |
| 190 | )} |
| 191 | > |
| 192 | <span className="flex-1 min-w-0 truncate">{o.label}</span> |
| 193 | {o.value === value && ( |
| 194 | <Check size={12} className="shrink-0 text-neutral-400" /> |
| 195 | )} |
| 196 | </button> |
| 197 | ))} |
| 198 | </div> |
| 199 | {footer && ( |
| 200 | <div |
| 201 | className="border-t border-white/10" |
| 202 | onClick={() => setOpen(false)} |
| 203 | > |
| 204 | {footer} |
| 205 | </div> |
| 206 | )} |
| 207 | </div> |
| 208 | )} |
| 209 | </div> |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | /* ---------- SearchSelect(可搜索选择器:无界对象列表专用——成员、父类、数据源…) |
| 214 | 触发器本身是输入框:聚焦即开、键入即过滤;渲染上限 maxVisible,超出提示继续 |
| 215 | 输入收窄。小而有界的枚举(角色/数据类型…)仍用 Dropdown,两击即达不必打字。 ---------- */ |
| 216 | export interface SearchSelectOption { |
| 217 | value: string; |
| 218 | /** 主文案:过滤与选中回显的依据(纯字符串,不能是节点) */ |
| 219 | label: string; |
| 220 | /** 次要文案(邮箱、连接摘要…),一并参与过滤,弱化显示 */ |
| 221 | hint?: string; |
| 222 | /** 层级缩进(浏览态展示树形;键入过滤后拉平对齐) */ |
| 223 | indent?: number; |
| 224 | } |
| 225 | |
| 226 | export function SearchSelect({ |
| 227 | value, |
| 228 | options, |
| 229 | onChange, |
| 230 | placeholder, |
| 231 | className, |
| 232 | size = "md", |
| 233 | maxVisible = 8, |
| 234 | }: { |
| 235 | value: string; |
| 236 | options: SearchSelectOption[]; |
| 237 | onChange: (v: string) => void; |
| 238 | placeholder?: string; |
| 239 | className?: string; |
| 240 | size?: "sm" | "md"; |
| 241 | maxVisible?: number; |
| 242 | }) { |
| 243 | const [open, setOpen] = useState(false); |
| 244 | const [query, setQuery] = useState(""); |
| 245 | const [active, setActive] = useState(0); |
| 246 | const inputRef = useRef<HTMLInputElement>(null); |
| 247 | |
| 248 | const current = options.find((o) => o.value === value); |
| 249 | const q = query.trim().toLowerCase(); |
| 250 | const matches = q |
| 251 | ? options.filter((o) => |
| 252 | `${o.label} ${o.hint ?? ""}`.toLowerCase().includes(q), |
| 253 | ) |
| 254 | : options; |
| 255 | const visible = matches.slice(0, maxVisible); |
| 256 | const hidden = matches.length - visible.length; |
| 257 | |
| 258 | const pick = (v: string) => { |
| 259 | onChange(v); |
| 260 | setOpen(false); |
| 261 | setQuery(""); |
| 262 | inputRef.current?.blur(); |
| 263 | }; |
| 264 | |
| 265 | const pad = |
| 266 | size === "sm" ? "pl-7 pr-2.5 py-1 text-xs" : "pl-8 pr-3 py-1.5 text-sm"; |
| 267 | const rowPad = size === "sm" ? "px-2.5 py-1 text-xs" : "px-3 py-1.5 text-sm"; |
| 268 | |
| 269 | return ( |
| 270 | <div className={cn("relative", className)}> |
| 271 | <SearchIcon |
| 272 | size={size === "sm" ? 11 : 13} |
| 273 | className="absolute left-2.5 top-1/2 -translate-y-1/2 text-neutral-600 pointer-events-none" |
| 274 | /> |
| 275 | <input |
| 276 | ref={inputRef} |
| 277 | className={cn("input-dark w-full", pad)} |
| 278 | value={open ? query : (current?.label ?? "")} |
| 279 | /* 打开后把当前选中项挪进 placeholder:边打字边能看到现值 */ |
| 280 | placeholder={open ? current?.label || placeholder : placeholder} |
| 281 | onFocus={() => { |
| 282 | setOpen(true); |
| 283 | setQuery(""); |
| 284 | setActive(0); |
| 285 | }} |
| 286 | /* 选项行 mousedown 已 preventDefault(不夺焦点),走到这里的失焦都是真离开 */ |
| 287 | onBlur={() => setOpen(false)} |
| 288 | onChange={(e) => { |
| 289 | setQuery(e.target.value); |
| 290 | setActive(0); |
| 291 | }} |
| 292 | onKeyDown={(e) => { |
| 293 | if (e.key === "Escape") { |
| 294 | setOpen(false); |
| 295 | inputRef.current?.blur(); |
| 296 | } else if (e.key === "ArrowDown") { |
| 297 | e.preventDefault(); |
| 298 | setActive((a) => Math.min(a + 1, visible.length - 1)); |
| 299 | } else if (e.key === "ArrowUp") { |
| 300 | e.preventDefault(); |
| 301 | setActive((a) => Math.max(a - 1, 0)); |
| 302 | } else if (e.key === "Enter" && visible[active]) { |
| 303 | e.preventDefault(); |
| 304 | pick(visible[active].value); |
| 305 | } |
| 306 | }} |
| 307 | /> |
| 308 | {open && ( |
| 309 | <div className="u-pop u-pop-in u-pop-in-tl absolute z-50 mt-1 w-full rounded-lg shadow-xl overflow-hidden"> |
| 310 | {visible.map((o, i) => ( |
| 311 | <button |
| 312 | key={o.value} |
| 313 | type="button" |
| 314 | onMouseDown={(e) => e.preventDefault()} |
| 315 | onClick={() => pick(o.value)} |
| 316 | onMouseEnter={() => setActive(i)} |
| 317 | className={cn( |
| 318 | "w-full flex items-center gap-2 text-left", |
| 319 | rowPad, |
| 320 | i === active |
| 321 | ? "bg-white/[0.08] text-white" |
| 322 | : "text-neutral-300", |
| 323 | )} |
| 324 | > |
| 325 | {!q && !!o.indent && ( |
| 326 | <span className="shrink-0" style={{ width: o.indent * 14 }} /> |
| 327 | )} |
| 328 | <span className="min-w-0 flex-1 truncate"> |
| 329 | {o.label} |
| 330 | {o.hint && ( |
| 331 | <span className="ml-2 text-neutral-500">{o.hint}</span> |
| 332 | )} |
| 333 | </span> |
| 334 | {o.value === value && ( |
| 335 | <Check size={12} className="shrink-0 text-neutral-400" /> |
| 336 | )} |
| 337 | </button> |
| 338 | ))} |
| 339 | {visible.length === 0 && ( |
| 340 | <p className={cn(rowPad, "text-neutral-600")}>{S.ui.noMatches}</p> |
| 341 | )} |
| 342 | {hidden > 0 && ( |
| 343 | <div |
| 344 | className={cn( |
| 345 | rowPad, |
| 346 | "border-t border-white/5 text-[11px] text-neutral-600", |
| 347 | )} |
| 348 | > |
| 349 | {S.ui.keepTyping(hidden)} |
| 350 | </div> |
| 351 | )} |
| 352 | </div> |
| 353 | )} |
| 354 | </div> |
| 355 | ); |
| 356 | } |
| 357 | |
| 358 | /* ---------- MultiSearchSelect(多选版 SearchSelect) ---------- */ |
| 359 | |
| 360 | /** |
| 361 | * 多选 + 搜索。与 [`SearchSelect`] 同一套语汇与键盘操作,三处不同: |
| 362 | * |
| 363 | * - **已选的显示在输入框上方**,各带一个移除按钮。不显示在下拉里的原因是 |
| 364 | * 下拉一关就看不见了,而"我到底选了哪些"是随时要看的 |
| 365 | * - **选完不关**:多选多半要连点几个,每次都重新聚焦是折磨 |
| 366 | * - 已选项在列表里带勾,再点一次是取消 |
| 367 | * |
| 368 | * 选项多到几百个时(大本体就是这个量级)它仍然可用——这正是它取代芯片墙的理由: |
| 369 | * 芯片墙的高度随类数线性增长,搜索框不随。 |
| 370 | */ |
| 371 | export function MultiSearchSelect({ |
| 372 | values, |
| 373 | options, |
| 374 | onToggle, |
| 375 | placeholder, |
| 376 | emptyHint, |
| 377 | className, |
| 378 | maxVisible = 8, |
| 379 | }: { |
| 380 | values: string[]; |
| 381 | options: SearchSelectOption[]; |
| 382 | onToggle: (v: string) => void; |
| 383 | placeholder?: string; |
| 384 | /** 一个都没选时显示的话。多选留空往往是有意义的("不限"),不是没填 */ |
| 385 | emptyHint?: string; |
| 386 | className?: string; |
| 387 | maxVisible?: number; |
| 388 | }) { |
| 389 | const [open, setOpen] = useState(false); |
| 390 | const [query, setQuery] = useState(""); |
| 391 | const [active, setActive] = useState(0); |
| 392 | const inputRef = useRef<HTMLInputElement>(null); |
| 393 | |
| 394 | const q = query.trim().toLowerCase(); |
| 395 | const matches = q |
| 396 | ? options.filter((o) => |
| 397 | `${o.label} ${o.hint ?? ""}`.toLowerCase().includes(q), |
| 398 | ) |
| 399 | : options; |
| 400 | const visible = matches.slice(0, maxVisible); |
| 401 | const hidden = matches.length - visible.length; |
| 402 | const picked = values |
| 403 | .map((v) => options.find((o) => o.value === v)) |
| 404 | .filter((o): o is SearchSelectOption => !!o); |
| 405 | |
| 406 | const toggle = (v: string) => { |
| 407 | onToggle(v); |
| 408 | setQuery(""); |
| 409 | setActive(0); |
| 410 | inputRef.current?.focus(); |
| 411 | }; |
| 412 | |
| 413 | return ( |
| 414 | <div className={cn("relative", className)}> |
| 415 | {picked.length > 0 && ( |
| 416 | <div className="mb-1 flex flex-wrap gap-1"> |
| 417 | {picked.map((o) => ( |
| 418 | <button |
| 419 | key={o.value} |
| 420 | type="button" |
| 421 | onClick={() => onToggle(o.value)} |
| 422 | className="group flex items-center gap-1 rounded-full bg-white/[0.10] px-2 py-0.5 text-[11px] text-neutral-200 hover:bg-white/[0.16] transition-colors" |
| 423 | title={o.hint ?? o.label} |
| 424 | > |
| 425 | {o.label} |
| 426 | <span className="text-neutral-500 group-hover:text-neutral-200"> |
| 427 | ✕ |
| 428 | </span> |
| 429 | </button> |
| 430 | ))} |
| 431 | </div> |
| 432 | )} |
| 433 | {picked.length === 0 && emptyHint && ( |
| 434 | <p className="mb-1 text-[11px] text-neutral-600">{emptyHint}</p> |
| 435 | )} |
| 436 | <SearchIcon |
| 437 | size={11} |
| 438 | className="absolute left-2.5 top-1/2 -translate-y-1/2 text-neutral-600 pointer-events-none" |
| 439 | style={{ top: undefined }} |
| 440 | /> |
| 441 | <input |
| 442 | ref={inputRef} |
| 443 | className="input-dark w-full pl-7 pr-2.5 py-1 text-xs" |
| 444 | value={query} |
| 445 | placeholder={placeholder} |
| 446 | onFocus={() => { |
| 447 | setOpen(true); |
| 448 | setActive(0); |
| 449 | }} |
| 450 | onBlur={() => setOpen(false)} |
| 451 | onChange={(e) => { |
| 452 | setQuery(e.target.value); |
| 453 | setActive(0); |
| 454 | }} |
| 455 | onKeyDown={(e) => { |
| 456 | if (e.key === "Escape") { |
| 457 | setOpen(false); |
| 458 | inputRef.current?.blur(); |
| 459 | } else if (e.key === "ArrowDown") { |
| 460 | e.preventDefault(); |
| 461 | setActive((a) => Math.min(a + 1, visible.length - 1)); |
| 462 | } else if (e.key === "ArrowUp") { |
| 463 | e.preventDefault(); |
| 464 | setActive((a) => Math.max(a - 1, 0)); |
| 465 | } else if (e.key === "Enter" && visible[active]) { |
| 466 | e.preventDefault(); |
| 467 | toggle(visible[active].value); |
| 468 | } else if (e.key === "Backspace" && !query && picked.length) { |
| 469 | // 空输入时退格删掉最后一个 —— 与各家 token 输入框一致 |
| 470 | onToggle(picked[picked.length - 1].value); |
| 471 | } |
| 472 | }} |
| 473 | /> |
| 474 | {open && ( |
| 475 | <div className="u-pop u-pop-in u-pop-in-tl absolute z-50 mt-1 w-full rounded-lg shadow-xl overflow-hidden"> |
| 476 | {visible.map((o, i) => ( |
| 477 | <button |
| 478 | key={o.value} |
| 479 | type="button" |
| 480 | onMouseDown={(e) => e.preventDefault()} |
| 481 | onClick={() => toggle(o.value)} |
| 482 | onMouseEnter={() => setActive(i)} |
| 483 | className={cn( |
| 484 | "w-full flex items-center gap-2 text-left px-2.5 py-1 text-xs", |
| 485 | i === active |
| 486 | ? "bg-white/[0.08] text-white" |
| 487 | : "text-neutral-300", |
| 488 | )} |
| 489 | > |
| 490 | {!q && !!o.indent && ( |
| 491 | <span className="shrink-0" style={{ width: o.indent * 14 }} /> |
| 492 | )} |
| 493 | <span className="min-w-0 flex-1 truncate"> |
| 494 | {o.label} |
| 495 | {o.hint && ( |
| 496 | <span className="ml-2 text-neutral-500">{o.hint}</span> |
| 497 | )} |
| 498 | </span> |
| 499 | {values.includes(o.value) && ( |
| 500 | <Check size={12} className="shrink-0 text-neutral-400" /> |
| 501 | )} |
| 502 | </button> |
| 503 | ))} |
| 504 | {visible.length === 0 && ( |
| 505 | <p className="px-2.5 py-1 text-xs text-neutral-600"> |
| 506 | {S.ui.noMatches} |
| 507 | </p> |
| 508 | )} |
| 509 | {hidden > 0 && ( |
| 510 | <div className="px-2.5 py-1 text-[11px] text-neutral-600 border-t border-white/5"> |
| 511 | {S.ui.keepTyping(hidden)} |
| 512 | </div> |
| 513 | )} |
| 514 | </div> |
| 515 | )} |
| 516 | </div> |
| 517 | ); |
| 518 | } |
| 519 | |
| 520 | /* ---------- ColorPicker(精选色板 + hex 兜底;实体色刻意不开放全色域) ---------- |
| 521 | **改这里就得改 `crates/utopia-store/src/palette.rs`**:手动挑的色与自动按 key |
| 522 | 取的色必须来自同一组,否则一张图里会出现两套配色。那边有测试盯着,改漏了会红。 */ |
| 523 | export const ENTITY_PALETTE = [ |
| 524 | "#7fd0ff", |
| 525 | "#5fa8ff", |
| 526 | "#5fd4d0", |
| 527 | "#63e2b7", |
| 528 | "#4cc38a", |
| 529 | "#a8d878", |
| 530 | "#ffd479", |
| 531 | "#f2b66d", |
| 532 | "#ff9d76", |
| 533 | "#ff8a9e", |
| 534 | "#ff9daf", |
| 535 | "#e797d8", |
| 536 | "#c4a5ff", |
| 537 | "#9fa8ff", |
| 538 | "#8ea5bd", |
| 539 | "#b3b9c4", |
| 540 | ]; |
| 541 | |
| 542 | /** |
| 543 | * 类的 key → 颜色。**必须与 `crates/utopia-store/src/palette.rs` 的 |
| 544 | * `color_for_key` 逐位一致**:新建类时前端先按 key 挑一个显示出来, |
| 545 | * 用户不改就这么存下去;而导入/消解那条路是后端算的。两边算得不一样, |
| 546 | * 同一个 key 就会因为「谁建的」而拿到不同颜色。 |
| 547 | * |
| 548 | * FNV-1a + 雪崩混合。用 BigInt 是因为 JS 的位运算是 32 位的, |
| 549 | * 而这里要的是 64 位乘法——用 Number 做会静默丢高位, |
| 550 | * 算出来跟 Rust 对不上,且不会有任何报错。 |
| 551 | */ |
| 552 | export function colorForKey(key: string): string { |
| 553 | let h = 0xcbf29ce484222325n; |
| 554 | const M = (1n << 64n) - 1n; |
| 555 | for (const b of new TextEncoder().encode(key)) { |
| 556 | h = (h ^ BigInt(b)) & M; |
| 557 | h = (h * 0x100000001b3n) & M; |
| 558 | } |
| 559 | h = (h ^ (h >> 33n)) & M; |
| 560 | h = (h * 0xff51afd7ed558ccdn) & M; |
| 561 | h = (h ^ (h >> 33n)) & M; |
| 562 | return ENTITY_PALETTE[Number(h % BigInt(ENTITY_PALETTE.length))]; |
| 563 | } |
| 564 | |
| 565 | export function ColorPicker({ |
| 566 | value, |
| 567 | onChange, |
| 568 | shape, |
| 569 | }: { |
| 570 | value: string; |
| 571 | onChange: (v: string) => void; |
| 572 | /** 给定时,色井渲染"形状 + 颜色"而不是整块填充(方形是直角,与图谱节点一致) */ |
| 573 | shape?: "circle" | "square"; |
| 574 | }) { |
| 575 | const [open, setOpen] = useState(false); |
| 576 | const rootRef = useRef<HTMLDivElement>(null); |
| 577 | const valid = /^#[0-9a-fA-F]{6}$/.test(value); |
| 578 | |
| 579 | useEffect(() => { |
| 580 | if (!open) return; |
| 581 | const onDoc = (e: MouseEvent) => { |
| 582 | if (!rootRef.current?.contains(e.target as Node)) setOpen(false); |
| 583 | }; |
| 584 | const onKey = (e: KeyboardEvent) => { |
| 585 | if (e.key === "Escape") setOpen(false); |
| 586 | }; |
| 587 | document.addEventListener("mousedown", onDoc); |
| 588 | document.addEventListener("keydown", onKey); |
| 589 | return () => { |
| 590 | document.removeEventListener("mousedown", onDoc); |
| 591 | document.removeEventListener("keydown", onKey); |
| 592 | }; |
| 593 | }, [open]); |
| 594 | |
| 595 | return ( |
| 596 | <div ref={rootRef} className="relative inline-block"> |
| 597 | {/* 触发器:当前颜色色块(Figma 式 color well);带 shape 时渲染形状 + 颜色 */} |
| 598 | {shape ? ( |
| 599 | <button |
| 600 | type="button" |
| 601 | title={value} |
| 602 | onClick={() => setOpen(!open)} |
| 603 | className="h-8 w-14 rounded-lg border border-white/15 hover:border-white/35 transition-colors bg-white/[0.04] grid place-items-center" |
| 604 | > |
| 605 | <span |
| 606 | className={cn("h-3.5 w-3.5", shape === "circle" && "rounded-full")} |
| 607 | style={{ background: valid ? value : ENTITY_PALETTE[0] }} |
| 608 | /> |
| 609 | </button> |
| 610 | ) : ( |
| 611 | <button |
| 612 | type="button" |
| 613 | title={value} |
| 614 | onClick={() => setOpen(!open)} |
| 615 | className="h-8 w-14 rounded-lg border border-white/15 hover:border-white/35 transition-colors" |
| 616 | style={{ background: valid ? value : ENTITY_PALETTE[0] }} |
| 617 | /> |
| 618 | )} |
| 619 | {open && ( |
| 620 | // 显式宽度:绝对定位的收缩宽度会被 inline-block 触发器的 56px 容器块钳死 |
| 621 | <div className="u-pop u-pop-in u-pop-in-tl absolute z-50 left-0 top-full mt-2 w-56 rounded-xl p-3 shadow-xl"> |
| 622 | <div className="grid grid-cols-8 gap-1.5 mb-2.5"> |
| 623 | {ENTITY_PALETTE.map((c) => ( |
| 624 | <button |
| 625 | key={c} |
| 626 | type="button" |
| 627 | title={c} |
| 628 | onClick={() => { |
| 629 | onChange(c); |
| 630 | setOpen(false); |
| 631 | }} |
| 632 | className={cn( |
| 633 | "h-5 w-5 rounded-full transition-transform hover:scale-110", |
| 634 | value.toLowerCase() === c && |
| 635 | "outline outline-2 outline-white/80 outline-offset-1", |
| 636 | )} |
| 637 | style={{ background: c }} |
| 638 | /> |
| 639 | ))} |
| 640 | </div> |
| 641 | <input |
| 642 | value={value} |
| 643 | onChange={(e) => onChange(e.target.value)} |
| 644 | placeholder={ENTITY_PALETTE[0]} |
| 645 | className={cn( |
| 646 | "input-dark w-full px-2 py-1 text-xs font-mono", |
| 647 | !valid && "!border-[var(--u-danger)]", |
| 648 | )} |
| 649 | /> |
| 650 | </div> |
| 651 | )} |
| 652 | </div> |
| 653 | ); |
| 654 | } |
| 655 | |
| 656 | /* ---------- Pager(列表分页条:不足一页时自动隐藏) ---------- */ |
| 657 | export function Pager({ |
| 658 | total, |
| 659 | pageSize, |
| 660 | page, |
| 661 | onPage, |
| 662 | /** 覆盖默认的上边距。默认 `mt-3` 适合跟在列表后面; |
| 663 | 放进一个已经有内边距的底栏时传 `""` 去掉它 */ |
| 664 | className = "mt-3", |
| 665 | }: { |
| 666 | total: number; |
| 667 | pageSize: number; |
| 668 | page: number; |
| 669 | onPage: (p: number) => void; |
| 670 | className?: string; |
| 671 | }) { |
| 672 | const pageCount = Math.max(1, Math.ceil(total / pageSize)); |
| 673 | const safe = Math.min(page, pageCount - 1); |
| 674 | if (total <= pageSize) return null; |
| 675 | return ( |
| 676 | <div className={cn("flex items-center justify-end gap-2 text-xs text-neutral-500", className)}> |
| 677 | <span className="u-num"> |
| 678 | {S.library.pageOf( |
| 679 | safe * pageSize + 1, |
| 680 | Math.min((safe + 1) * pageSize, total), |
| 681 | total, |
| 682 | )} |
| 683 | </span> |
| 684 | <button |
| 685 | onClick={() => onPage(safe - 1)} |
| 686 | disabled={safe === 0} |
| 687 | className="u-btn u-btn-ghost h-7 w-7 grid place-items-center rounded-lg" |
| 688 | > |
| 689 | <ChevronLeft size={13} /> |
| 690 | </button> |
| 691 | <button |
| 692 | onClick={() => onPage(safe + 1)} |
| 693 | disabled={safe >= pageCount - 1} |
| 694 | className="u-btn u-btn-ghost h-7 w-7 grid place-items-center rounded-lg" |
| 695 | > |
| 696 | <ChevronRight size={13} /> |
| 697 | </button> |
| 698 | </div> |
| 699 | ); |
| 700 | } |
| 701 | |
| 702 | /** 分页切片辅助:返回当前页数据与安全页号。 */ |
| 703 | export function pageSlice<T>( |
| 704 | items: T[], |
| 705 | page: number, |
| 706 | pageSize: number, |
| 707 | ): { rows: T[]; safe: number } { |
| 708 | const pageCount = Math.max(1, Math.ceil(items.length / pageSize)); |
| 709 | const safe = Math.min(page, pageCount - 1); |
| 710 | return { rows: items.slice(safe * pageSize, (safe + 1) * pageSize), safe }; |
| 711 | } |
| 712 | |
| 713 | /* ---------- DangerConfirm(危险操作确认弹窗:可要求输入指定文本解锁) ---------- */ |
| 714 | export function DangerConfirm({ |
| 715 | title, |
| 716 | hint, |
| 717 | requireText, |
| 718 | confirmLabel, |
| 719 | cancelLabel, |
| 720 | busy, |
| 721 | onConfirm, |
| 722 | onCancel, |
| 723 | }: { |
| 724 | title: string; |
| 725 | hint: string; |
| 726 | /** 要求逐字输入的解锁文本(如资源名称);缺省则直接可确认 */ |
| 727 | requireText?: string; |
| 728 | confirmLabel: string; |
| 729 | cancelLabel: string; |
| 730 | busy?: boolean; |
| 731 | onConfirm: () => void; |
| 732 | onCancel: () => void; |
| 733 | }) { |
| 734 | const [text, setText] = useState(""); |
| 735 | const unlocked = !requireText || text === requireText; |
| 736 | |
| 737 | useEffect(() => { |
| 738 | const onKey = (e: KeyboardEvent) => { |
| 739 | if (e.key === "Escape") onCancel(); |
| 740 | }; |
| 741 | document.addEventListener("keydown", onKey); |
| 742 | return () => document.removeEventListener("keydown", onKey); |
| 743 | }, [onCancel]); |
| 744 | |
| 745 | return ( |
| 746 | <div |
| 747 | className="u-modal-scrim fixed inset-0 z-50 grid place-items-center bg-black/80 backdrop-blur-sm" |
| 748 | onMouseDown={(e) => { |
| 749 | if (e.target === e.currentTarget) onCancel(); |
| 750 | }} |
| 751 | > |
| 752 | <div className="u-modal-panel u-modal-in w-[24rem] max-w-[calc(100vw-2rem)] rounded-2xl shadow-2xl p-5"> |
| 753 | <h2 className="text-[15px] font-semibold text-[var(--u-danger)] mb-2"> |
| 754 | {title} |
| 755 | </h2> |
| 756 | <p className="text-xs text-neutral-400 leading-relaxed mb-4">{hint}</p> |
| 757 | {requireText && ( |
| 758 | <input |
| 759 | autoFocus |
| 760 | className="input-dark w-full px-3 py-2 text-sm mb-4" |
| 761 | placeholder={requireText} |
| 762 | value={text} |
| 763 | onChange={(e) => setText(e.target.value)} |
| 764 | /> |
| 765 | )} |
| 766 | <div className="flex justify-end gap-2"> |
| 767 | <button |
| 768 | className="u-btn u-btn-ghost px-3.5 py-1.5 text-xs" |
| 769 | onClick={onCancel} |
| 770 | > |
| 771 | {cancelLabel} |
| 772 | </button> |
| 773 | <button |
| 774 | className="u-btn px-3.5 py-1.5 text-xs font-semibold disabled:opacity-40" |
| 775 | style={{ background: "var(--u-danger-solid)", color: "#ffffff" }} |
| 776 | disabled={!unlocked || busy} |
| 777 | onClick={onConfirm} |
| 778 | > |
| 779 | {confirmLabel} |
| 780 | </button> |
| 781 | </div> |
| 782 | </div> |
| 783 | </div> |
| 784 | ); |
| 785 | } |
| 786 | |
| 787 | /* ---------- Panel(玻璃面板) ---------- */ |
| 788 | export function Panel({ |
| 789 | strong = false, |
| 790 | className, |
| 791 | children, |
| 792 | }: { |
| 793 | strong?: boolean; |
| 794 | className?: string; |
| 795 | children: ReactNode; |
| 796 | }) { |
| 797 | return ( |
| 798 | <div |
| 799 | className={cn(strong ? "glass-strong" : "glass", "rounded-xl", className)} |
| 800 | > |
| 801 | {children} |
| 802 | </div> |
| 803 | ); |
| 804 | } |
| 805 | |
| 806 | /* ---------- Chip(状态胶囊) ---------- */ |
| 807 | export type ChipTone = |
| 808 | "neutral" | "info" | "success" | "warn" | "danger" | "violet"; |
| 809 | |
| 810 | export function Chip({ |
| 811 | tone = "neutral", |
| 812 | className, |
| 813 | title, |
| 814 | children, |
| 815 | }: { |
| 816 | tone?: ChipTone; |
| 817 | className?: string; |
| 818 | title?: string; |
| 819 | children: ReactNode; |
| 820 | }) { |
| 821 | return ( |
| 822 | <span className={cn("u-chip", `u-chip-${tone}`, className)} title={title}> |
| 823 | {children} |
| 824 | </span> |
| 825 | ); |
| 826 | } |
| 827 | |
| 828 | /* ---------- PageTitle ---------- */ |
| 829 | export function PageTitle({ |
| 830 | className, |
| 831 | children, |
| 832 | }: { |
| 833 | className?: string; |
| 834 | children: ReactNode; |
| 835 | }) { |
| 836 | return <h2 className={cn("u-title text-lg", className)}>{children}</h2>; |
| 837 | } |
| 838 | |
| 839 | /* ---------- EmptyState ---------- */ |
| 840 | export function EmptyState({ |
| 841 | icon, |
| 842 | children, |
| 843 | }: { |
| 844 | icon: ReactNode; |
| 845 | children: ReactNode; |
| 846 | }) { |
| 847 | return ( |
| 848 | <div className="text-center"> |
| 849 | <div className="glass mx-auto mb-4 h-14 w-14 rounded-2xl grid place-items-center text-xl font-bold text-neutral-300"> |
| 850 | {icon} |
| 851 | </div> |
| 852 | <div className="text-sm text-neutral-500 whitespace-pre-line"> |
| 853 | {children} |
| 854 | </div> |
| 855 | </div> |
| 856 | ); |
| 857 | } |
| 858 | |
| 859 | /* ---------- Loading / ErrorText ---------- */ |
| 860 | export function Loading({ children }: { children: ReactNode }) { |
| 861 | return <div className="p-8 text-sm text-neutral-500">{children}</div>; |
| 862 | } |
| 863 | |
| 864 | export function ErrorText({ children }: { children: ReactNode }) { |
| 865 | return <p className="text-sm text-rose-400">{children}</p>; |
| 866 | } |
| 867 | |
| 868 | /* ---------- GithubMark(lucide 无品牌图标,官方 mark 内联) ---------- */ |
| 869 | export function GithubMark({ size = 16 }: { size?: number }) { |
| 870 | return ( |
| 871 | <svg |
| 872 | width={size} |
| 873 | height={size} |
| 874 | viewBox="0 0 24 24" |
| 875 | fill="currentColor" |
| 876 | aria-hidden |
| 877 | > |
| 878 | <path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" /> |
| 879 | </svg> |
| 880 | ); |
| 881 | } |
| 882 | |
| 883 | /* ---------- SectionMark(分区字标:Docs/账户层等,逐字母入场,点击回应用) ---------- */ |
| 884 | import { Link as RouterLink } from "@tanstack/react-router"; |
| 885 | export function SectionMark({ text, title }: { text: string; title: string }) { |
| 886 | return ( |
| 887 | <RouterLink |
| 888 | to="/" |
| 889 | title={title} |
| 890 | className="relative inline-flex text-white text-[17px]" |
| 891 | style={{ fontFamily: "var(--font-brand)", letterSpacing: "0.06em" }} |
| 892 | > |
| 893 | {[...text].map((ch, i) => ( |
| 894 | <span |
| 895 | key={i} |
| 896 | className="u-letter" |
| 897 | style={{ animationDelay: `${80 + i * 45}ms` }} |
| 898 | > |
| 899 | {/* inline-flex 会折叠纯空格 span——换不折叠空格 */} |
| 900 | {ch === " " ? " " : ch} |
| 901 | </span> |
| 902 | ))} |
| 903 | </RouterLink> |
| 904 | ); |
| 905 | } |