Files
AgentLab/frontend/src/lib/keys.ts
T
ThearchyHelios e10c4f23fc 界面与交互全面翻修(前两波):运行态有了 HUD 和时间轴,终态会收尾,全站令牌、组件、文案统一
来自 2026-09 的 UI/UX 审查(142 条)。本提交是前两波,第三波(记录页接入时间轴、跨页互链、check-ui 更新、全站扫尾)另行提交。

地基
- 设计令牌:全局规则和 .btn/.field/.chip 放进 CSS 层,Tailwind 工具类重新生效,选中态、开启态不再被吞掉;字号阶梯;
  辅助文字在所有底色上 >= 4.5:1;状态色专用,节点类型色与之拉开色相;「跟随系统」真的跟随,首帧不闪深色;新字标与 favicon
- 基础组件:confirmDialog / promptDialog 取代原生对话框;toast 出错不自动消失、可复制、读屏可播报;离线横幅与断线恢复;
  九种剪影的状态徽标;唯一一份状态文案(lib/status)、格式化(lib/format)、术语(lib/terms)、报错人话(lib/errors)
- 运行态内核:事件折成航迹(run/trace.ts),可投影到任意时刻;取消 / 失败 / 服务重启后统一收尾,不再永远转圈;
  run.status 随事件回写;token 事件不再堆进 events
- 后端:协作成员逐个完成即发事件,调度者决策有事件;时长分执行 / 墙钟 / 等人;API 时间带时区;
  中断的运行也发 stream.end;运行列表可筛选翻页;删除运行 / 工作流有保护;运行默认值接通;可测未保存的配置;
  报错说清发生了什么、为什么、怎么办

功能
- 画布:状态四重编码、固定高度遥测槽、按缩放三档、运行胶囊与 HUD、航迹时间轴与回放、执行轨迹边、
  等待闸门、跟随执行、正式运行只读、一次性时刻(开场点亮、回扫、阻断级联、出具印章)
- 编辑:撤销重做、版本历史、问题面板可定位、三栏可收起、模板与表达式字段区分、变量血缘、快捷键、带搜索的选择器
- 回答流 / 问数据:贴底才跟随滚动、工件下钻、出具横幅列出缺口、答案可复制导出、按会话的忙闲、
  答案不再在 2000 字处截断、重启后这一轮会收尾
- 记录页:待审批 / 运行中 / 失败页签、筛选翻页、实时接流、排错路径指到节点、封存凭证常驻
- 管理页:新的「数据」页、写操作失败都有反馈、弹窗里先测连接、统一持久的连接状态
- 外壳:导航改版、⌘K 命令面板、? 快捷键帮助、404、页面标题与 favicon 状态点、后台提醒

验证(沙箱):tsc 干净,pytest 579 passed,14 个检查脚本中 13 个全绿;check-ui 剩 1 项旧断言(按旧日期格式找运行行),第三波更新。
2026-09-26 23:11:31 +08:00

124 lines
5.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 快捷键:平台判断、显示格式、事件匹配。
*
* 提示文字和处理器必须出自同一份描述——之前界面上写着 ⌥V,全仓却没有一个
* 监听处理 V;RunControl 的处理器接受 Ctrl+Enter,提示却只写 ⌘⏎,Windows
* 用户照着按不出来。组合键一律写成 'Mod+Enter' 这样的串,显示走
* formatShortcut,判定走 matchShortcut。
*
* Mod 在 Mac 上是 ⌘,其他平台是 Ctrl。
*/
export const isMac: boolean = (() => {
if (typeof navigator === 'undefined') return false
const nav = navigator as Navigator & { userAgentData?: { platform?: string } }
const platform = nav.userAgentData?.platform || navigator.platform || navigator.userAgent
return /mac|iphone|ipad|ipod/i.test(platform)
})()
/** 这个平台上"主修饰键"对应事件上的哪个字段:e[modKey] */
export const modKey: 'metaKey' | 'ctrlKey' = isMac ? 'metaKey' : 'ctrlKey'
/** 主修饰键的显示名 */
export const modLabel: string = isMac ? '⌘' : 'Ctrl'
const MAC: Record<string, string> = {
mod: '⌘', meta: '⌘', cmd: '⌘', ctrl: '⌃', alt: '⌥', option: '⌥', shift: '⇧',
enter: '⏎', return: '⏎', backspace: '⌫', delete: '⌦', tab: '⇥', esc: 'Esc', escape: 'Esc',
up: '↑', down: '↓', left: '←', right: '→', space: '空格',
}
const OTHER: Record<string, string> = {
mod: 'Ctrl', meta: 'Win', cmd: 'Ctrl', ctrl: 'Ctrl', alt: 'Alt', option: 'Alt', shift: 'Shift',
enter: 'Enter', return: 'Enter', backspace: 'Backspace', delete: 'Del', tab: 'Tab',
esc: 'Esc', escape: 'Esc', up: '↑', down: '↓', left: '←', right: '→', space: '空格',
}
function split(combo: string): string[] {
// 'Mod++' 这种以加号本身为键的写法:最后一段是空串时补回 '+'
const parts = combo.split('+').map((p) => p.trim())
if (parts.length > 1 && parts[parts.length - 1] === '') parts[parts.length - 1] = '+'
return parts.filter(Boolean)
}
/** 组合键拆成一个个键帽的文字:'Mod+Shift+K' → ['⌘','⇧','K'] 或 ['Ctrl','Shift','K'] */
export function shortcutParts(combo: string): string[] {
const table = isMac ? MAC : OTHER
return split(combo).map((p) => table[p.toLowerCase()] ?? (p.length === 1 ? p.toUpperCase() : p))
}
/**
* 给人看的组合键。Mac 上符号连写(⌘⏎、⌥V),其他平台用加号(Ctrl+Enter、Alt+V),
* 这是两边各自系统菜单的写法。
*/
export function formatShortcut(combo: string): string {
return shortcutParts(combo).join(isMac ? '' : '+')
}
/** aria-keyshortcuts 的键名:修饰键和功能键按 UI Events 的 key 值写,不是给人看的简写 */
const ARIA: Record<string, string> = {
meta: 'Meta', cmd: 'Meta', ctrl: 'Control', alt: 'Alt', option: 'Alt', shift: 'Shift',
enter: 'Enter', return: 'Enter', esc: 'Escape', escape: 'Escape', up: 'ArrowUp', down: 'ArrowDown',
left: 'ArrowLeft', right: 'ArrowRight', space: 'Space', tab: 'Tab', backspace: 'Backspace', delete: 'Delete',
}
/**
* 写进 aria-keyshortcuts 的串:'Mod+Enter' 在 Mac 上是 'Meta+Enter',其他平台是
* 'Control+Enter'。读屏照这个念,Windows 上念成 Meta 就是在教人按错键。
*/
export function ariaShortcut(combo: string): string {
return split(combo).map((raw) => {
const p = raw.toLowerCase()
if (p === 'mod') return isMac ? 'Meta' : 'Control'
return ARIA[p] ?? (raw.length === 1 ? raw.toUpperCase() : raw)
}).join('+')
}
/**
* 键盘事件是否就是这个组合键。
*
* 字母和数字按 e.code 比:Mac 上 Option+V 产生的 e.key 是「√」,按 e.key 比
* ⌥V 永远按不中。修饰键要求完全一致,免得 ⌘⇧S 也触发 ⌘S。
* Mod 在 Mac 上只认 ⌘:Mac 的 Ctrl+字母另有系统含义(比如 Ctrl+A 回行首)。
*/
export function matchShortcut(e: KeyboardEvent | { key: string; code?: string; metaKey: boolean; ctrlKey: boolean; altKey: boolean; shiftKey: boolean }, combo: string): boolean {
const want = { meta: false, ctrl: false, alt: false, shift: false }
let key = ''
for (const raw of split(combo)) {
const p = raw.toLowerCase()
if (p === 'mod') want[isMac ? 'meta' : 'ctrl'] = true
else if (p === 'meta' || p === 'cmd') want.meta = true
else if (p === 'ctrl') want.ctrl = true
else if (p === 'alt' || p === 'option') want.alt = true
else if (p === 'shift') want.shift = true
else key = p
}
if (e.metaKey !== want.meta || e.ctrlKey !== want.ctrl || e.altKey !== want.alt) return false
// '?' 这类本身就要按 Shift 才打得出的键,不强求 shift 一致
const shiftNeutral = key.length === 1 && !/[a-z0-9]/.test(key)
if (!shiftNeutral && e.shiftKey !== want.shift) return false
if (/^[a-z]$/.test(key)) return e.code === `Key${key.toUpperCase()}` || e.key.toLowerCase() === key
if (/^[0-9]$/.test(key)) return e.code === `Digit${key}` || e.key === key
const alias: Record<string, string> = {
enter: 'Enter', return: 'Enter', esc: 'Escape', escape: 'Escape', up: 'ArrowUp',
down: 'ArrowDown', left: 'ArrowLeft', right: 'ArrowRight', space: ' ', tab: 'Tab',
backspace: 'Backspace', delete: 'Delete',
}
return e.key === (alias[key] ?? key)
}
/**
* 焦点是不是在一个能打字的地方。全局快捷键遇到它要让路:用户在输入框里按
* Esc 是给输入法的,按 ⌥V 是要打字符的。
*/
export function isTypingTarget(target: EventTarget | null = typeof document !== 'undefined' ? document.activeElement : null): boolean {
if (!(target instanceof HTMLElement)) return false
if (target.isContentEditable) return true
const tag = target.tagName
if (tag === 'TEXTAREA' || tag === 'SELECT') return true
if (tag !== 'INPUT') return false
const type = (target as HTMLInputElement).type
// 复选框、按钮之类的 input 不接收文字
return !['checkbox', 'radio', 'button', 'submit', 'reset', 'range', 'color', 'file'].includes(type)
}