mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Add basic ExtensionPoint support.
This commit is contained in:
97
Shared/ExtensionPoints/SendToMicroBlogCommand.swift
Normal file
97
Shared/ExtensionPoints/SendToMicroBlogCommand.swift
Normal file
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// SendToMicroBlogCommand.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 1/8/18.
|
||||
// Copyright © 2018 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import Articles
|
||||
import RSCore
|
||||
|
||||
// Not undoable.
|
||||
|
||||
final class SendToMicroBlogCommand: ExtensionPoint, SendToCommand {
|
||||
|
||||
let title = NSLocalizedString("Micro.blog", comment: "Micro.blog")
|
||||
let templateImage = AppAssets.extensionPointMicroblog
|
||||
|
||||
var image: NSImage? {
|
||||
return microBlogApp.icon
|
||||
}
|
||||
|
||||
private let microBlogApp = UserApp(bundleID: "blog.micro.mac")
|
||||
|
||||
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
|
||||
|
||||
microBlogApp.updateStatus()
|
||||
guard microBlogApp.existsOnDisk, let article = (object as? ArticlePasteboardWriter)?.article, let _ = article.preferredLink else {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func sendObject(_ object: Any?, selectedText: String?) {
|
||||
|
||||
guard canSendObject(object, selectedText: selectedText) else {
|
||||
return
|
||||
}
|
||||
guard let article = (object as? ArticlePasteboardWriter)?.article else {
|
||||
return
|
||||
}
|
||||
guard microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: get text from contentHTML or contentText if no title and no selectedText.
|
||||
// TODO: consider selectedText.
|
||||
|
||||
let s = article.attributionString + article.linkString
|
||||
|
||||
let urlQueryDictionary = ["text": s]
|
||||
guard let urlQueryString = urlQueryDictionary.urlQueryString else {
|
||||
return
|
||||
}
|
||||
guard let url = URL(string: "microblog://post?" + urlQueryString) else {
|
||||
return
|
||||
}
|
||||
|
||||
let _ = try? NSWorkspace.shared.open(url, options: [], configuration: [:])
|
||||
}
|
||||
}
|
||||
|
||||
private extension Article {
|
||||
|
||||
var attributionString: String {
|
||||
|
||||
// Feed name, or feed name + author name (if author is specified per-article).
|
||||
// Includes trailing space.
|
||||
|
||||
if let feedName = webFeed?.nameForDisplay, let authorName = authors?.first?.name {
|
||||
return feedName + ", " + authorName + ": "
|
||||
}
|
||||
if let feedName = webFeed?.nameForDisplay {
|
||||
return feedName + ": "
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var linkString: String {
|
||||
|
||||
// Title + link or just title (if no link) or just link if no title
|
||||
|
||||
if let title = title, let link = preferredLink {
|
||||
return "[" + title + "](" + link + ")"
|
||||
}
|
||||
if let preferredLink = preferredLink {
|
||||
return preferredLink
|
||||
}
|
||||
if let title = title {
|
||||
return title
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user