diff --git a/MongoView/Node.swift b/MongoView/Node.swift index f78414d..cba7a80 100644 --- a/MongoView/Node.swift +++ b/MongoView/Node.swift @@ -230,4 +230,13 @@ extension Node { return "MaxKey" } } + + var isValueCopyable: Bool { + switch value { + case .document(_), .array(_), .binary(_), .minKey, .maxKey: + return false + default: + return true + } + } } diff --git a/MongoView/View Controllers/QueryViewController.swift b/MongoView/View Controllers/QueryViewController.swift index 44d4f96..375219f 100644 --- a/MongoView/View Controllers/QueryViewController.swift +++ b/MongoView/View Controllers/QueryViewController.swift @@ -138,6 +138,14 @@ class QueryViewController: NSViewController { } } + private func nodeForCopying() -> Node? { + if outlineView.clickedRow >= 0 { + return outlineView.item(atRow: outlineView.clickedRow) as? Node + } else { + return outlineView.item(atRow: outlineView.selectedRow) as? Node + } + } + @objc func outlineCellDoubleClicked() { if let item = outlineView.item(atRow: outlineView.clickedRow) { if outlineView.isItemExpanded(item) { @@ -171,6 +179,13 @@ class QueryViewController: NSViewController { } wc.showWindow(nil) } + + @IBAction func copy(_ sender: Any) { + guard let node = nodeForCopying() else { return } + NSPasteboard.general.clearContents() + NSPasteboard.general.setString(node.valueString, forType: .string) + // todo: support copying more specific types? + } } extension QueryViewController: NSMenuItemValidation { @@ -181,6 +196,9 @@ extension QueryViewController: NSMenuItemValidation { } else { return false } + } else if menuItem.action == #selector(copy(_:)) { + let node = nodeForCopying() + return node != nil && node!.isValueCopyable } return true } diff --git a/MongoView/View Controllers/QueryViewController.xib b/MongoView/View Controllers/QueryViewController.xib index 28b4b11..a675fbe 100644 --- a/MongoView/View Controllers/QueryViewController.xib +++ b/MongoView/View Controllers/QueryViewController.xib @@ -1,7 +1,7 @@ - + - + @@ -1139,18 +1139,25 @@ - + - + + + + + + + +