From a195299e91a364384451c62d60cd4b68c4aa78b5 Mon Sep 17 00:00:00 2001 From: zihluwang Date: Tue, 24 Feb 2026 10:58:54 +0800 Subject: [PATCH] feat: add functionality to copy selected raw JSON in JsonViewer and update related state management --- src/init/i18n/locales/BritishEnglish.json | 1 + src/init/i18n/locales/SimplifiedChinese.json | 1 + src/page/json-viewer/index.tsx | 28 +++++++++++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/init/i18n/locales/BritishEnglish.json b/src/init/i18n/locales/BritishEnglish.json index cd42522..a0716ea 100644 --- a/src/init/i18n/locales/BritishEnglish.json +++ b/src/init/i18n/locales/BritishEnglish.json @@ -73,6 +73,7 @@ "visualisedResult": "Visualised Result", "matches": "matches", "copied": "Copied!", + "copyRawJson": "Copy selected JSON", "copyAsCsv": "Copy as CSV", "error": "Error:" }, diff --git a/src/init/i18n/locales/SimplifiedChinese.json b/src/init/i18n/locales/SimplifiedChinese.json index 6607325..79a79ed 100644 --- a/src/init/i18n/locales/SimplifiedChinese.json +++ b/src/init/i18n/locales/SimplifiedChinese.json @@ -73,6 +73,7 @@ "visualisedResult": "可视化结果", "matches": "个匹配", "copied": "已复制!", + "copyRawJson": "复制选中 JSON", "copyAsCsv": "复制为 CSV", "error": "错误:" }, diff --git a/src/page/json-viewer/index.tsx b/src/page/json-viewer/index.tsx index 6a6560e..5299b5a 100644 --- a/src/page/json-viewer/index.tsx +++ b/src/page/json-viewer/index.tsx @@ -26,7 +26,8 @@ export default function JsonViewer() { const [jsonInput, setJsonInput] = useState(JSON.stringify(initialData, null, 2)) const [query, setQuery] = useState("$.staff_members[*].name") - const [copied, setCopied] = useState(false) + const [copiedCsv, setCopiedCsv] = useState(false) + const [copiedRawJson, setCopiedRawJson] = useState(false) const isPlainObject = (value: unknown): value is Record => { return value !== null && typeof value === "object" && !Array.isArray(value) @@ -85,11 +86,23 @@ export default function JsonViewer() { })() navigator.clipboard.writeText(csv).then(() => { - setCopied(true) - setTimeout(() => setCopied(false), 2000) + setCopiedCsv(true) + setTimeout(() => setCopiedCsv(false), 2000) }) }, [query, result.matchedValues]) + const copySelectedRawJson = useCallback(() => { + if (result.matchedValues.length === 0) return + + const payload = result.matchedValues.length === 1 ? result.matchedValues[0] : result.matchedValues + const json = JSON.stringify(payload, null, 2) + + navigator.clipboard.writeText(json).then(() => { + setCopiedRawJson(true) + setTimeout(() => setCopiedRawJson(false), 2000) + }) + }, [result.matchedValues]) + return (
{result.matchedPaths.length} {t("jsonViewer.matches")} +