From 1e787f85d2831184fabd0ac0ecdd12d548ff3781 Mon Sep 17 00:00:00 2001 From: varkrishna Date: Sat, 21 Sep 2024 10:51:37 +0530 Subject: [PATCH] Support paste operation for iOS --- Sources/JSONViewer/JSONNodeView.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/JSONViewer/JSONNodeView.swift b/Sources/JSONViewer/JSONNodeView.swift index b2b9719..b322aa6 100644 --- a/Sources/JSONViewer/JSONNodeView.swift +++ b/Sources/JSONViewer/JSONNodeView.swift @@ -58,13 +58,23 @@ public struct JSONNodeView: View { } .contextMenu(menuItems: { Button { +#if os(macOS) NSPasteboard.general.clearContents() if !node.isExpandable { - NSPasteboard.general.setString("{\"\(node.key)\": \"\(node.value)\"}", forType: .string) + NSPasteboard.general.setString("{\"\(node.key)\": \"\(node.value ?? "")\"}", forType: .string) } else { let jsonString = node.jsonString() NSPasteboard.general.setString(jsonString, forType: .string) } +#elseif os(iOS) + UIPasteboard.general.string = nil + if !node.isExpandable { + UIPasteboard.general.string = "{\"\(node.key)\": \"\(node.value ?? "")\"}" + } else { + let jsonString = node.jsonString() + UIPasteboard.general.string = jsonString + } +#endif } label: { Text("Copy") }