Add Copy Value as JSON

This commit is contained in:
Shadowfacts 2020-08-12 19:37:11 -04:00
parent c96e093c72
commit 8e71077070
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 17 additions and 1 deletions

View File

@ -195,6 +195,16 @@ class QueryViewController: NSViewController {
NSPasteboard.general.setString(node.valueString, forType: .string) NSPasteboard.general.setString(node.valueString, forType: .string)
// todo: support copying more specific types? // todo: support copying more specific types?
} }
@IBAction func copyAsJSON(_ sender: Any) {
guard let node = nodeForCopying() else { return }
let doc: BSONDocument = ["value": node.value]
let ext = doc.toExtendedJSONString()
// toExtendedJSON returns `{ "value": <whatever> }`, drop the object wrapper
let str = String(ext.dropFirst(12).dropLast(2))
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(str, forType: .string)
}
} }
extension QueryViewController: NSMenuItemValidation { extension QueryViewController: NSMenuItemValidation {
@ -205,7 +215,7 @@ extension QueryViewController: NSMenuItemValidation {
} else { } else {
return false return false
} }
} else if menuItem.action == #selector(copy(_:)) { } else if menuItem.action == #selector(copy(_:)) || menuItem.action == #selector(copyAsJSON(_:)) {
let node = nodeForCopying() let node = nodeForCopying()
return node != nil && node!.isValueCopyable return node != nil && node!.isValueCopyable
} }

View File

@ -1158,6 +1158,12 @@
<action selector="copy:" target="-2" id="HsR-MZ-ayB"/> <action selector="copy:" target="-2" id="HsR-MZ-ayB"/>
</connections> </connections>
</menuItem> </menuItem>
<menuItem title="Copy Value as JSON" id="5lo-Mc-kwv">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="copyAsJSON:" target="-2" id="rb1-6U-LO5"/>
</connections>
</menuItem>
</items> </items>
<point key="canvasLocation" x="887" y="210"/> <point key="canvasLocation" x="887" y="210"/>
</menu> </menu>