2018-01-09 06:53:49 +01: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-29 07:18:24 +02:00
// N e t N e w s W i r e
2018-01-09 06:53:49 +01: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-03 07:51:32 +01:00
import AppKit
2018-07-24 03:29:08 +02:00
import Articles
2018-01-14 19:56:49 +01:00
import RSCore
2018-01-09 06:53:49 +01:00
// N o t u n d o a b l e .
2020-04-07 22:25:33 +02:00
final class SendToMicroBlogCommand : ExtensionPoint , SendToCommand {
2018-01-09 07:10:56 +01:00
2020-04-22 21:16:50 +02:00
static var isSinglton = true
static var isDeveloperBuildRestricted = false
2020-04-14 23:47:05 +02:00
static var title : String = NSLocalizedString ( " Micro.blog " , comment : " Micro.blog " )
2020-10-30 21:42:45 +01:00
static var image = AppAssets . extensionPointMicroblog
2020-04-14 23:47:05 +02:00
static var description : NSAttributedString = {
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. " )
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 17:12:06 +02:00
let extensionPointID = ExtensionPointIdentifer . microblog
2020-04-15 05:33:05 +02:00
var title : String {
2020-04-15 06:03:08 +02:00
return extensionPointID . extensionPointType . title
2020-04-15 05:33:05 +02:00
}
2018-01-12 07:18:46 +01:00
var image : NSImage ? {
return microBlogApp . icon
2018-01-09 07:10:56 +01:00
}
2018-01-09 06:53:49 +01:00
2018-01-14 19:56:49 +01:00
private let microBlogApp = UserApp ( bundleID : " blog.micro.mac " )
2018-01-12 07:18:46 +01:00
2018-01-10 07:04:45 +01:00
func canSendObject ( _ object : Any ? , selectedText : String ? ) -> Bool {
2018-01-09 06:53:49 +01:00
2018-01-14 19:56:49 +01:00
microBlogApp . updateStatus ( )
2018-01-12 07:18:46 +01:00
guard microBlogApp . existsOnDisk , let article = ( object as ? ArticlePasteboardWriter ) ? . article , let _ = article . preferredLink else {
2018-01-10 07:04:45 +01:00
return false
}
return true
2018-01-09 06:53:49 +01:00
}
2018-01-10 07:04:45 +01:00
func sendObject ( _ object : Any ? , selectedText : String ? ) {
guard canSendObject ( object , selectedText : selectedText ) else {
return
}
2018-01-12 07:18:46 +01:00
guard let article = ( object as ? ArticlePasteboardWriter ) ? . article else {
return
}
2018-01-14 19:56:49 +01:00
guard microBlogApp . launchIfNeeded ( ) , microBlogApp . bringToFront ( ) else {
2018-01-10 07:04:45 +01: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 21:00:09 +01: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-10 07:04:45 +01:00
2018-01-14 18:36:24 +01:00
let urlQueryDictionary = [ " text " : s ]
2020-01-28 05:58:59 +01:00
guard let urlQueryString = urlQueryDictionary . urlQueryString else {
2018-01-10 07:04:45 +01:00
return
}
2018-01-14 18:36:24 +01:00
guard let url = URL ( string : " microblog://post? " + urlQueryString ) else {
2018-01-10 07:04:45 +01:00
return
}
2018-01-09 06:53:49 +01:00
2020-08-02 20:37:09 +02:00
NSWorkspace . shared . open ( url )
2018-01-09 06:53:49 +01:00
}
}
2018-01-09 07:10:56 +01:00
2018-01-14 21:00:09 +01: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-15 03:11:41 +01:00
if let feedName = webFeed ? . nameForDisplay , let authorName = authors ? . first ? . name {
2018-01-14 21:00:09 +01:00
return feedName + " , " + authorName + " : "
}
2019-11-15 03:11:41 +01:00
if let feedName = webFeed ? . nameForDisplay {
2018-01-14 21:00:09 +01: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-09 07:10:56 +01:00
2018-01-14 21:00:09 +01:00
if let title = title , let link = preferredLink {
return " [ " + title + " ]( " + link + " ) "
}
if let preferredLink = preferredLink {
return preferredLink
}
if let title = title {
return title
}
return " "
}
}