@@ -30,11 +30,32 @@ const entries = computed<[string | number, unknown][]>(() => {
3030const count = computed (() => entries .value .length )
3131const bracket = computed (() => (type .value === ' array' ? [' [' , ' ]' ] : [' {' , ' }' ]))
3232
33+ // 长字符串默认截断为单行,点击「展开」查看完整内容
34+ const MAX_STR = 160
35+ const rawString = computed (() => (type .value === ' string' ? (props .value as string ) : ' ' ))
36+ const stringTooLong = computed (() => rawString .value .length > MAX_STR )
37+ const expandedStr = ref (false )
38+
3339const displayValue = computed (() => {
34- if (type .value === ' string' ) return JSON .stringify (props .value )
40+ if (type .value === ' string' ) {
41+ if (stringTooLong .value && ! expandedStr .value ) {
42+ return JSON .stringify (rawString .value .slice (0 , MAX_STR )) + ` … (+${rawString .value .length - MAX_STR } 字符) `
43+ }
44+ return JSON .stringify (rawString .value )
45+ }
3546 return String (props .value )
3647})
3748
49+ // 字符串本身是 JSON(对象/数组)时,可内联解析成子树
50+ const subJson = computed <unknown >(() => {
51+ if (type .value !== ' string' ) return null
52+ const s = rawString .value .trim ()
53+ if (s .length > 200_000 ) return null
54+ if (! (s .startsWith (' {' ) && s .endsWith (' }' )) && ! (s .startsWith (' [' ) && s .endsWith (' ]' ))) return null
55+ try { return JSON .parse (s ) } catch { return null }
56+ })
57+ const showSubJson = ref (false )
58+
3859function childPath(key : string | number ): string {
3960 if (typeof key === ' number' ) return ` ${props .path }[${key }] `
4061 return / ^ [A-Za-z _$][\w $] * $ / .test (key ) ? ` ${props .path }.${key } ` : ` ${props .path }[${JSON .stringify (key )}] `
@@ -69,8 +90,18 @@ async function copy(what: 'value' | 'path') {
6990 </span >
7091 <span v-if =" !collapsed" class =" muted count" >{{ count }} 项</span >
7192 </template >
93+ <span v-else-if =" type === 'string'" class =" val string" :class =" { expanded: expandedStr }" >{{ displayValue }}</span >
7294 <span v-else class =" val" :class =" type" >{{ displayValue }}</span >
7395
96+ <template v-if =" type === ' string' " >
97+ <button v-if =" stringTooLong" class =" mini inline" @click =" expandedStr = !expandedStr" >
98+ {{ expandedStr ? '收起' : `展开 (共 ${rawString.length} 字符)` }}
99+ </button >
100+ <button v-if =" subJson !== null" class =" mini inline" @click =" showSubJson = !showSubJson" >
101+ {{ showSubJson ? '收起解析' : '解析为 JSON' }}
102+ </button >
103+ </template >
104+
74105 <span class =" actions" >
75106 <button class =" mini" @click =" copy('value')" title =" 复制该节点的值(JSON)" >{{ copiedWhat === 'value' ? '✓' : '复制值' }}</button >
76107 <button class =" mini" @click =" copy('path')" title =" 复制访问路径" >{{ copiedWhat === 'path' ? '✓' : '路径' }}</button >
@@ -88,6 +119,11 @@ async function copy(what: 'value' | 'path') {
88119 />
89120 <div class =" line closing" ><span class =" toggle spacer" /><span class =" brace" >{{ bracket[1] }}</span ></div >
90121 </div >
122+
123+ <!-- 字符串值本身是 JSON 时,内联展示解析后的子树 -->
124+ <div v-if =" !isBranch && showSubJson && subJson !== null" class =" children sub-json" >
125+ <JsonNode :k =" null " :value =" subJson " :path =" path " :depth =" 0 " />
126+ </div >
91127 </div >
92128</template >
93129
@@ -107,17 +143,23 @@ async function copy(what: 'value' | 'path') {
107143.muted { color : var (--fg-muted ); }
108144.count { font-size : 11px ; margin-left : 4px ; }
109145
146+ .val { min-width : 0 ; }
110147.val.string { color : var (--success ); }
148+ .val.string :not (.expanded ) { overflow : hidden ; text-overflow : ellipsis ; white-space : nowrap ; flex : 0 1 auto ; }
149+ .val.string.expanded { white-space : pre-wrap ; overflow-wrap : anywhere; flex : 1 1 auto ; }
111150.val.number { color : #b8860b ; }
112151.val.boolean { color : #8250df ; }
113152.val.null { color : var (--fg-muted ); font-style : italic ; }
114153
115154.children { padding-left : 16px ; border-left : 1px solid var (--border ); margin-left : 6px ; }
155+ .children.sub-json { margin-top : 2px ; background : var (--bg ); border-radius : 4px ; padding : 6px 8px 6px 16px ; }
116156
117- .actions { opacity : 0 ; display : inline-flex ; gap : 4px ; margin-left : 8px ; transition : opacity 0.1s ; }
157+ .actions { opacity : 0 ; display : inline-flex ; gap : 4px ; margin-left : 8px ; flex : 0 0 auto ; transition : opacity 0.1s ; }
118158.mini {
119159 background : none ; border : 1px solid var (--border ); border-radius : 3px ;
120160 font-size : 10px ; padding : 0 6px ; line-height : 16px ; color : var (--fg-muted ); cursor : pointer ;
161+ white-space : nowrap ; flex : 0 0 auto ;
121162}
163+ .mini.inline { flex : 0 0 auto ; opacity : 0.8 ; }
122164.mini :hover { border-color : var (--primary ); color : var (--primary ); }
123165 </style >
0 commit comments