2018-01-08 21:53:49 -08:00
//
// S e n d T o M i c r o B l o g C o m m a n d . s w i f t
2018-08-28 22:18:24 -07:00
// N e t N e w s W i r e
2018-01-08 21:53:49 -08:00
//
// C r e a t e d b y B r e n t S i m m o n s o n 1 / 8 / 1 8 .
// C o p y r i g h t © 2 0 1 8 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
2018-02-02 22:51:32 -08:00
import AppKit
2018-07-23 18:29:08 -07:00
import Articles
2018-01-14 10:56:49 -08:00
import RSCore
2018-01-08 21:53:49 -08:00
// N o t u n d o a b l e .
2020-04-07 15:25:33 -05:00
final class SendToMicroBlogCommand : ExtensionPoint , SendToCommand {
2018-01-08 22:10:56 -08:00
2020-04-22 14:16:50 -05:00
static var isSinglton = true
static var isDeveloperBuildRestricted = false
2020-04-14 16:47:05 -05:00
static var title : String = NSLocalizedString ( " Micro.blog " , comment : " Micro.blog " )
2020-10-30 15:42:45 -05:00
static var image = AppAssets . extensionPointMicroblog
2020-04-14 16:47:05 -05:00
static var description : NSAttributedString = {
2020-11-01 18:29:13 -05:00
let attrString = SendToMicroBlogCommand . makeAttrString ( " This extension enables share menu functionality to send selected article text to Micro.blog. You need the Micro.blog application for this to work. " )
2020-04-14 16:47:05 -05:00
let range = NSRange ( location : 81 , length : 10 )
attrString . beginEditing ( )
attrString . addAttribute ( NSAttributedString . Key . link , value : " https://micro.blog " , range : range )
attrString . addAttribute ( NSAttributedString . Key . foregroundColor , value : NSColor . systemBlue , range : range )
attrString . endEditing ( )
return attrString
} ( )
2020-04-08 10:12:06 -05:00
let extensionPointID = ExtensionPointIdentifer . microblog
2020-04-14 22:33:05 -05:00
var title : String {
2020-04-14 23:03:08 -05:00
return extensionPointID . extensionPointType . title
2020-04-14 22:33:05 -05:00
}
2018-01-11 22:18:46 -08:00
var image : NSImage ? {
return microBlogApp . icon
2018-01-08 22:10:56 -08:00
}
2018-01-08 21:53:49 -08:00
2018-01-14 10:56:49 -08:00
private let microBlogApp = UserApp ( bundleID : " blog.micro.mac " )
2018-01-11 22:18:46 -08:00
2018-01-09 22:04:45 -08:00
func canSendObject ( _ object : Any ? , selectedText : String ? ) -> Bool {
2018-01-08 21:53:49 -08:00
2018-01-14 10:56:49 -08:00
microBlogApp . updateStatus ( )
2018-01-11 22:18:46 -08:00
guard microBlogApp . existsOnDisk , let article = ( object as ? ArticlePasteboardWriter ) ? . article , let _ = article . preferredLink else {
2018-01-09 22:04:45 -08:00
return false
}
return true
2018-01-08 21:53:49 -08:00
}
2018-01-09 22:04:45 -08:00
func sendObject ( _ object : Any ? , selectedText : String ? ) {
guard canSendObject ( object , selectedText : selectedText ) else {
return
}
2018-01-11 22:18:46 -08:00
guard let article = ( object as ? ArticlePasteboardWriter ) ? . article else {
return
}
2018-01-14 10:56:49 -08:00
guard microBlogApp . launchIfNeeded ( ) , microBlogApp . bringToFront ( ) else {
2018-01-09 22:04:45 -08:00
return
}
// TODO: g e t t e x t f r o m c o n t e n t H T M L o r c o n t e n t T e x t i f n o t i t l e a n d n o s e l e c t e d T e x t .
2018-01-14 12:00:09 -08:00
// TODO: c o n s i d e r s e l e c t e d T e x t .
let s = article . attributionString + article . linkString
2018-01-09 22:04:45 -08:00
2018-01-14 09:36:24 -08:00
let urlQueryDictionary = [ " text " : s ]
2020-01-27 22:58:59 -06:00
guard let urlQueryString = urlQueryDictionary . urlQueryString else {
2018-01-09 22:04:45 -08:00
return
}
2018-01-14 09:36:24 -08:00
guard let url = URL ( string : " microblog://post? " + urlQueryString ) else {
2018-01-09 22:04:45 -08:00
return
}
2018-01-08 21:53:49 -08:00
2020-08-02 13:37:09 -05:00
NSWorkspace . shared . open ( url )
2018-01-08 21:53:49 -08:00
}
}
2018-01-08 22:10:56 -08:00
2018-01-14 12:00:09 -08:00
private extension Article {
var attributionString : String {
// F e e d n a m e , o r f e e d n a m e + a u t h o r n a m e ( i f a u t h o r i s s p e c i f i e d p e r - a r t i c l e ) .
// I n c l u d e s t r a i l i n g s p a c e .
2019-11-14 20:11:41 -06:00
if let feedName = webFeed ? . nameForDisplay , let authorName = authors ? . first ? . name {
2018-01-14 12:00:09 -08:00
return feedName + " , " + authorName + " : "
}
2019-11-14 20:11:41 -06:00
if let feedName = webFeed ? . nameForDisplay {
2018-01-14 12:00:09 -08:00
return feedName + " : "
}
return " "
}
var linkString : String {
// T i t l e + l i n k o r j u s t t i t l e ( i f n o l i n k ) o r j u s t l i n k i f n o t i t l e
2018-01-08 22:10:56 -08:00
2018-01-14 12:00:09 -08:00
if let title = title , let link = preferredLink {
return " [ " + title + " ]( " + link + " ) "
}
if let preferredLink = preferredLink {
return preferredLink
}
if let title = title {
return title
}
return " "
}
}