iter(g266): 新增 JSON 树查看组件

This commit is contained in:
HIsheR
2026-10-26 22:50:05 +08:00
parent e2fb02f40e
commit 90bd60e0cf
12 changed files with 293 additions and 11 deletions
+1
View File
@@ -77,3 +77,4 @@ export { kbd } from './kbd.js';
export { highlight } from './highlight.js';
export { inputTag } from './inputTag.js';
export { layout } from './layout.js';
export { jsonViewer } from './jsonViewer.js';
+54
View File
@@ -0,0 +1,54 @@
import { h } from '@ui-arch/frame';
import type { JsonViewerProps } from './types.js';
// 值类型归类:数组 / 对象算分支,其余算叶子(null 单独一类,避免被 typeof 报成 object)
type JsonKind = 'array' | 'object' | 'string' | 'number' | 'boolean' | 'null';
const kindOf = (v: unknown): JsonKind => (v === null ? 'null' : Array.isArray(v) ? 'array' : typeof v === 'object' ? 'object' : (typeof v as JsonKind));
/*** 叶子值文案:字符串补引号,其余按字面;null 用灰字斜体在样式层区分 ***/
function leafText(v: unknown): string {
return typeof v === 'string' ? `"${v}"` : String(v);
}
/*** 分支摘要:收起时也要看得出里面有多少内容,否则无法判断值不值得展开 ***/
function summaryOf(v: unknown): string {
if (Array.isArray(v)) return `${v.length} 项`;
const n = Object.keys(v as Record<string, unknown>).length; // 对象分支:按自有键计数
return `${n} 个键`;
}
/*** JSON 树查看:按层折叠展示任意值,数组 / 对象可展开收起,叶子按类型着色;只读、不解析编辑 ***/
export function jsonViewer(props: JsonViewerProps): HTMLElement {
const rootLabel = props.rootLabel ?? 'root'; // 根标签:贴顶那一行的键名
const openDepth = props.depth ?? 2; // 默认展开层数:2 层能看出一级结构又不至于刷屏
const root = h('div', { class: 'ua-json', role: 'tree', 'aria-label': props.ariaLabel ?? 'JSON 数据' }) as HTMLDivElement;
if (props.maxHeight !== undefined) root.style.maxHeight = `${props.maxHeight}px`; // 只在给了上限时才限高,缺省随内容
/*** 单行渲染:分支行带开关与计数摘要,叶子行只带类型类名 ***/
const row = (key: string, value: unknown, level: number, path: string): HTMLElement => {
const kind = kindOf(value);
const branch = kind === 'array' || kind === 'object';
const label = h('span', { class: 'ua-json-key' }, key); // 键名:数组下标也用 key 位展示
if (!branch) {
return h('div', { class: 'ua-json-row is-leaf', role: 'treeitem' }, label, h('span', { class: 'ua-json-colon' }, ':'), h('span', { class: `ua-json-val ua-json-${kind}` }, leafText(value)));
}
const box = h('div', { class: 'ua-json-children', role: 'group' }); // 子节点容器:收起时整块隐藏,DOM 保留折叠态
const caret = h('span', { class: 'ua-json-caret', 'aria-hidden': 'true' }, '▸'); // 开关图标:方向由 aria-expanded 决定样式
const toggle = h('button', {
type: 'button',
class: 'ua-json-toggle',
'aria-expanded': () => String(!box.hidden),
'aria-label': () => `${box.hidden ? '展开' : '收起'} ${path}`,
onClick: () => { box.hidden = !box.hidden; }, // 只翻 hidden:样式按 :has 或 aria-expanded 跟随
}, caret, label, h('span', { class: 'ua-json-colon' }, ':'), h('span', { class: 'ua-json-sum' }, `${kind === 'array' ? '[' : '{'}${summaryOf(value)}${kind === 'array' ? ']' : '}'}`));
const entries: [string, unknown][] = kind === 'array'
? (value as unknown[]).map((v, i) => [String(i), v]) // 数组下标转字符串,走同一套行渲染
: Object.entries(value as Record<string, unknown>);
for (const [k, v] of entries) box.append(row(k, v, level + 1, `${path}.${k}`)); // 递归:路径用点号拼接,报给读屏时能定位
box.hidden = level >= openDepth; // 超出默认层数的一律先收起,用户按需展开
return h('div', { class: 'ua-json-branch', role: 'treeitem', 'aria-level': String(level + 1) }, toggle, box);
};
root.append(row(rootLabel, props.value, 0, rootLabel));
return root;
}
+9
View File
@@ -1021,3 +1021,12 @@ export interface LayoutProps {
collapsed?: Signal<boolean>; // 折叠态信号;传入则双向绑定(外部可读数),缺省内部自管
sticky?: boolean; // 页头是否吸顶;缺省 false(长内容里建议打开)
}
/*** JSON 树查看入参 ***/
export interface JsonViewerProps {
value: unknown; // 任意可序列化数据:对象 / 数组 / 标量都行
depth?: number; // 默认展开层数;缺省 2(看得到一级结构又不至于刷屏)
rootLabel?: string; // 根节点键名;缺省 root
maxHeight?: number; // 最高高度(px),超出在框内滚动;缺省不限高
ariaLabel?: string; // 可访问名;缺省「JSON 数据」
}
+42 -1
View File
@@ -91,7 +91,8 @@
"kbd(props):键盘按键标记,键帽样式展示单个按键或组合键(多键以 + 连接),用于快捷键提示",
"highlight(props):关键词高亮,命中片段包 mark(大小写不敏感全部命中),text / keyword 支持信号响应式刷新",
"inputTag(props):标签输入,输入后回车 / 逗号成标签、chip 可单个删除、空框退格删末个,value 信号双向绑定,max 满额自动禁输",
"layout(props, ...children):布局骨架,页头 / 侧栏 / 内容 / 页脚四区一次成型,侧栏可折叠(collapsed 信号双向绑定)"
"layout(props, ...children):布局骨架,页头 / 侧栏 / 内容 / 页脚四区一次成型,侧栏可折叠(collapsed 信号双向绑定)",
"jsonViewer(props):JSON 树查看,按层折叠、分支带计数摘要、叶子按类型着色,只读不编辑"
],
"keyTypes": [
"ButtonProps",
@@ -4034,6 +4035,46 @@
"长内容页面打开 sticky,页头吸顶;内容区自己滚动,不会把整页撑高",
"只要内容不要骨架时省略 header / sider / footer:不套多余容器,内容区独占整行"
]
},
{
"id": "jsonViewer",
"title": "JSON 树查看 JsonViewer",
"demo": "sec-jsonviewer",
"props": [
{
"n": "value",
"t": "unknown",
"d": "任意可序列化数据:对象 / 数组 / 标量都行"
},
{
"n": "depth",
"t": "number",
"d": "默认展开层数;缺省 2(看得到一级结构又不至于刷屏)"
},
{
"n": "rootLabel",
"t": "string",
"d": "根节点键名;缺省 root"
},
{
"n": "maxHeight",
"t": "number",
"d": "最高高度(px),超出在框内滚动;缺省不限高"
},
{
"n": "ariaLabel",
"t": "string",
"d": "可访问名;缺省「JSON 数据」"
}
],
"example": "jsonViewer({ value: payload, depth: 2, rootLabel: 'response', maxHeight: 320 })",
"steps": [
"把接口返回的原始对象直接交给 value:不用自己写 JSON.stringify 再塞进 pre,组件会按层折叠",
"默认展开两层;数据层级深时调小 depth(1 层只看顶层键),扁平数据可调大",
"长数据给 maxHeight,超出在框内滚动,滚动条已是统一细条(暗色下不会露出原生亮条)",
"数组下标与对象键都走同一套行渲染,收起时右侧显示「3 项 / 2 个键」,先看规模再决定要不要展开",
"要展示编辑结果时不要用它(只读):输入侧请用 textarea,展示侧再交给 jsonViewer"
]
}
]
}
+93 -1
View File
@@ -4993,7 +4993,8 @@ h5.ua-type-title { font-size: var(--ua-font-size-md); }
.ua-vlist,
.ua-cmdk-list,
.ua-timepicker-col,
.ua-layout-content {
.ua-layout-content,
.ua-json {
scrollbar-width: thin; /* 标准属性:Firefox / Chromium 121+ 生效,细条不挤内容 */
scrollbar-color: var(--ua-color-scrollbar) transparent; /* 拇指走令牌、轨道透明,避免画出方块底 */
}
@@ -5407,3 +5408,94 @@ h5.ua-type-title { font-size: var(--ua-font-size-md); }
color: var(--ua-color-text-muted);
font-size: var(--ua-font-size-sm);
}
/* ---- JSON 树查看:等宽字体 + 按类型着色,缩进靠嵌套 padding,不为每层写死 margin ---- */
.ua-json {
padding: var(--ua-space-2);
overflow: auto;
border: 1px solid var(--ua-border-subtle);
border-radius: var(--ua-radius-md);
background: var(--ua-color-surface-inset);
color: var(--ua-color-text);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; /* 等宽:键名与值对齐才好扫 */
font-size: var(--ua-font-size-sm);
line-height: var(--ua-leading-normal);
}
.ua-json-row,
.ua-json-toggle {
display: flex;
align-items: baseline;
gap: var(--ua-space-1);
width: 100%;
padding: 0;
border: 0;
background: transparent;
color: inherit;
font: inherit;
text-align: left;
}
.ua-json-toggle {
cursor: pointer;
border-radius: var(--ua-radius-sm);
}
.ua-json-toggle:hover {
background: var(--ua-color-tint-accent-weak);
}
.ua-json-toggle:focus-visible {
outline: none;
box-shadow: var(--ua-focus-ring);
}
/* 开关图标:收起时 ▸,展开时旋转成 ▾;旋转在减少动效下由全局规则归零成瞬时切换 */
.ua-json-caret {
flex: 0 0 auto;
width: var(--ua-space-3);
color: var(--ua-color-text-faint);
transition: rotate var(--ua-duration-fast) var(--ua-ease);
}
.ua-json-toggle[aria-expanded='true'] .ua-json-caret {
rotate: 90deg;
}
.ua-json-key {
color: var(--ua-color-text-secondary);
}
.ua-json-colon {
color: var(--ua-color-text-faint);
}
.ua-json-sum {
color: var(--ua-color-text-faint);
font-size: var(--ua-font-size-xs);
}
/* 叶子值按类型着色:字符串 / 数字 / 布尔各一档,null 最弱——扫一眼就能分辨结构 */
.ua-json-string {
color: var(--ua-color-success);
}
.ua-json-number {
color: var(--ua-color-accent);
}
.ua-json-boolean {
color: var(--ua-color-warning);
}
.ua-json-null {
color: var(--ua-color-text-faint);
font-style: italic;
}
/* 缩进:嵌套容器统一左移一档,层级越深缩进越深,不用每层传值 */
.ua-json-children {
padding-left: var(--ua-space-4);
border-left: 1px solid var(--ua-border-subtle);
margin-left: var(--ua-space-2);
}