feat: auto-format JSON display everywhere
- Shared prettyJson/truncate/isJson utility in src/utils/format.ts - MessageStream: collapsed preview + expanded view both auto-format JSON - LogPanel: log details with JSON content auto-formatted - PresetList: payload preview auto-formatted - Deduplicated isJson/formatPayload logic
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Pretty-print a JSON string if it's valid JSON.
|
||||
* Returns the original string if it's not valid JSON.
|
||||
*/
|
||||
export function prettyJson(str: string): string {
|
||||
if (!str || str.trim() === '') return str
|
||||
try {
|
||||
const parsed = JSON.parse(str)
|
||||
return JSON.stringify(parsed, null, 2)
|
||||
} catch {
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is valid JSON.
|
||||
*/
|
||||
export function isJson(str: string): boolean {
|
||||
try {
|
||||
JSON.parse(str)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate a string to a maximum length, adding an ellipsis if truncated.
|
||||
*/
|
||||
export function truncate(str: string, maxLen: number): string {
|
||||
if (str.length <= maxLen) return str
|
||||
return str.substring(0, maxLen) + '...'
|
||||
}
|
||||
Reference in New Issue
Block a user