From f324e65f16164a36d1efaa6e1b91a96f5dc32e37 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 9 Jan 2018 22:04:45 -0800 Subject: [PATCH] Make further progress on sending to MarsEdit and Micro.blog. --- Commands/SendToCommand.swift | 6 ++-- Commands/SendToMarsEditCommand.swift | 4 +-- Commands/SendToMicroBlogCommand.swift | 46 ++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Commands/SendToCommand.swift b/Commands/SendToCommand.swift index ec56f3338..413ddb8c1 100644 --- a/Commands/SendToCommand.swift +++ b/Commands/SendToCommand.swift @@ -8,10 +8,12 @@ import Cocoa +// Unlike UndoableCommand commands, you instantiate one of each of these and reuse them. + protocol SendToCommand { - func canSendObject(_ object: Any?) -> Bool - func sendObject(_ object: Any?) + func canSendObject(_ object: Any?, selectedText: String?) -> Bool + func sendObject(_ object: Any?, selectedText: String?) } extension SendToCommand { diff --git a/Commands/SendToMarsEditCommand.swift b/Commands/SendToMarsEditCommand.swift index ca1e29d81..5e221e843 100644 --- a/Commands/SendToMarsEditCommand.swift +++ b/Commands/SendToMarsEditCommand.swift @@ -10,12 +10,12 @@ import Foundation final class SendToMarsEditCommand: SendToCommand { - func canSendObject(_ object: Any?) -> Bool { + func canSendObject(_ object: Any?, selectedText: String?) -> Bool { return false } - func sendObject(_ object: Any?) { + func sendObject(_ object: Any?, selectedText: String?) { } } diff --git a/Commands/SendToMicroBlogCommand.swift b/Commands/SendToMicroBlogCommand.swift index 8260799dc..f0f401f52 100644 --- a/Commands/SendToMicroBlogCommand.swift +++ b/Commands/SendToMicroBlogCommand.swift @@ -7,6 +7,7 @@ // import Cocoa +import Data // Not undoable. @@ -21,16 +22,53 @@ final class SendToMicroBlogCommand: SendToCommand { NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive(_:)), name: NSApplication.didBecomeActiveNotification, object: nil) } - func canSendObject(_ object: Any?) -> Bool { + func canSendObject(_ object: Any?, selectedText: String?) -> Bool { - if !appExists { + guard appExists else { return false } - return false + guard let article = object as? Article else { + return false + } + guard let _ = article.preferredLink else { + return false + } + + return true } - func sendObject(_ object: Any?) { + func sendObject(_ object: Any?, selectedText: String?) { + guard canSendObject(object, selectedText: selectedText) else { + return + } + guard let article = object as? Article else { + return + } + + // TODO: get text from contentHTML or contentText if no title and no selectedText. + var s = "" + if let selectedText = selectedText { + s += selectedText + if let link = article.preferredLink { + s += "\n\n\(link)" + } + } + else if let title = article.title { + s += title + if let link = article.preferredLink { + s = "[" + s + "](" + link + ")" + } + } + + guard let encodedString = s.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { + return + } + guard let url = URL(string: "microblog://post?text=" + encodedString) else { + return + } + + let _ = try? NSWorkspace.shared.open(url, options: [], configuration: [:]) } @objc func appDidBecomeActive(_ note: Notification) {