updates plak menu item
This commit is contained in:
@@ -10,6 +10,8 @@ public protocol SystemActing: AnyObject {
|
||||
func copy(_ text: String)
|
||||
func pasteboardText() -> String?
|
||||
func revealInFinder(_ url: URL)
|
||||
/// Adds the text as a new block in Plak, which takes focus.
|
||||
func sendToPlak(_ text: String)
|
||||
/// Asked before a file with unsaved edits closes.
|
||||
func confirmUnsavedClose(_ path: String) -> UnsavedCloseChoice
|
||||
func openLog()
|
||||
@@ -45,6 +47,39 @@ public final class LiveSystemActions: SystemActing {
|
||||
NSWorkspace.shared.activateFileViewerSelecting([url])
|
||||
}
|
||||
|
||||
public func sendToPlak(_ text: String) {
|
||||
switch Plak.delivery(for: text) {
|
||||
case let .append(url)?:
|
||||
NSWorkspace.shared.open(url)
|
||||
case .paste?:
|
||||
pasteToPlak(text)
|
||||
case nil:
|
||||
Log.error("plak", "could not build the append URL")
|
||||
}
|
||||
}
|
||||
|
||||
/// Long text travels on the pasteboard; the user's own items return a second later,
|
||||
/// unless something else was copied in that second.
|
||||
private func pasteToPlak(_ text: String) {
|
||||
let pasteboard = NSPasteboard.general
|
||||
let saved = (pasteboard.pasteboardItems ?? []).map { item in
|
||||
item.types.compactMap { type in item.data(forType: type).map { (type, $0) } }
|
||||
}
|
||||
copy(text)
|
||||
let ours = pasteboard.changeCount
|
||||
NSWorkspace.shared.open(Plak.pasteURL)
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
||||
guard pasteboard.changeCount == ours else { return }
|
||||
pasteboard.clearContents()
|
||||
guard !saved.isEmpty else { return }
|
||||
pasteboard.writeObjects(saved.map { pairs in
|
||||
let item = NSPasteboardItem()
|
||||
for (type, data) in pairs { item.setData(data, forType: type) }
|
||||
return item
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public func confirmUnsavedClose(_ path: String) -> UnsavedCloseChoice {
|
||||
let alert = NSAlert()
|
||||
alert.messageText = "Save changes to \(PathUtils.basename(path))?"
|
||||
|
||||
Reference in New Issue
Block a user