updates plak menu item

This commit is contained in:
2026-10-28 16:18:03 +01:00
parent 92b90c0e13
commit 9bdda896af
13 changed files with 339 additions and 8 deletions
+15 -2
View File
@@ -41,6 +41,8 @@ public enum ContextAction: Sendable, Equatable {
case delete(String, isDirectory: Bool)
case copyTerminalSelection(TerminalKind)
case pasteIntoTerminal(TerminalKind)
/// Reads the pane's selection aloud; the text is fetched when the item runs.
case readAloudTerminalSelection(TerminalKind)
/// Inserts `/wait-what` with the pane's selection into the agent composer.
case waitWhat(TerminalKind)
/// Inserts `/bouwen <name>` into the agent composer for an ontwikkeling folder.
@@ -48,6 +50,10 @@ public enum ContextAction: Sendable, Equatable {
/// Reads a Markdown file's prose aloud.
case readAloudFile(String)
case readAloudText(String)
/// Sends the editor selection to Plak as it is, code and all.
case plak(String)
/// Sends the pane's selection to Plak; the text is fetched when the item runs.
case plakTerminalSelection(TerminalKind)
/// Destructive items read in amber-deep.
public var isDestructive: Bool {
@@ -86,7 +92,7 @@ public enum ContextActions {
return [
.action(.passOnReference(ref, code: hasCode ? code : nil), label: hasCode ? "Pass on selection" : "Pass on reference"),
.action(.copyReference(ref), label: "Copy reference"),
] + (hasCode ? readAloudSelection(code ?? "", path: reference.path) : []) + showDiff
] + (hasCode ? readAloudSelection(code ?? "", path: reference.path) + plak(code ?? "") : []) + showDiff
case .projectRoot:
return [
.action(.newFile(inDirectory: ""), label: "New file"),
@@ -114,10 +120,13 @@ public enum ContextActions {
.action(.delete(path, isDirectory: true), label: "Delete folder"),
]
case let .terminal(kind, hasSelection):
let readAloud: [ContextMenuEntry] = hasSelection
? [.separator, .action(.readAloudTerminalSelection(kind), label: "Read aloud selection"),
.separator, .action(.plakTerminalSelection(kind), label: "Plak")] : []
let waitWhat: [ContextMenuEntry] = hasSelection && kind == .agent
? [.separator, .action(.waitWhat(kind), label: "Wait what?")] : []
return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : [])
+ [.action(.pasteIntoTerminal(kind), label: "Paste")] + waitWhat
+ [.action(.pasteIntoTerminal(kind), label: "Paste")] + readAloud + waitWhat
case let .gitRow(path, staged):
return fileHead(path) + readAloudFile(path) + [
.separator,
@@ -145,6 +154,10 @@ public enum ContextActions {
return [.separator, .action(.readAloudText(prose.isEmpty ? code : prose), label: "Read aloud selection")]
}
private static func plak(_ code: String) -> [ContextMenuEntry] {
[.separator, .action(.plak(code), label: "Plak")]
}
private static func fileHead(_ path: String) -> [ContextMenuEntry] {
let name = PathUtils.basename(path)
return [
+30
View File
@@ -0,0 +1,30 @@
import Foundation
/// The URL scheme of Plak, the scratchpad app: each send adds one block at the end of its page.
public enum Plak {
/// Text above this many UTF-8 bytes goes through the pasteboard, since a long URL can fail to open.
public static let appendLimit = 100_000
public static let pasteURL = URL(string: "plak://paste")!
public enum Delivery: Sendable, Equatable {
case append(URL)
case paste
}
/// URLComponents leaves `+` bare in a query, and Plak's contract asks for `%2B`.
public static func appendURL(_ text: String) -> URL? {
var components = URLComponents()
components.scheme = "plak"
components.host = "append"
components.queryItems = [URLQueryItem(name: "text", value: text)]
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
return components.url
}
/// Nil when short text yields no URL.
public static func delivery(for text: String) -> Delivery? {
guard text.utf8.count <= appendLimit else { return .paste }
return appendURL(text).map(Delivery.append)
}
}
@@ -108,7 +108,8 @@ struct ContextMenuOverlay: View {
nonisolated static func tint(for action: ContextAction) -> MenuIconTint {
switch action {
case .copyReference, .copyTerminalSelection, .pasteIntoTerminal, .readAloudFile, .readAloudText: .plain
case .copyReference, .copyTerminalSelection, .pasteIntoTerminal, .readAloudFile, .readAloudText, .readAloudTerminalSelection,
.plak, .plakTerminalSelection: .plain
case .newFile, .newFolder, .stage: .add
case .copyFileName, .unstage, .revealInFinder: .modify
case .passOnReference, .passOnFileName, .bouwen, .waitWhat, .openDiff, .showDiff, .rename: .rename
@@ -121,7 +122,8 @@ struct ContextMenuOverlay: View {
case .passOnReference, .passOnFileName: ContextMenuRow.spark
case .bouwen: "hammer"
case .waitWhat: "questionmark.bubble"
case .readAloudFile, .readAloudText: "speaker.wave.2"
case .readAloudFile, .readAloudText, .readAloudTerminalSelection: "speaker.wave.2"
case .plak, .plakTerminalSelection: "note.text.badge.plus"
case .copyReference, .copyFileName, .copyTerminalSelection: "doc.on.doc"
case .pasteIntoTerminal: "doc.on.clipboard"
case .newFile: "doc.badge.plus"
@@ -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))?"
@@ -103,6 +103,14 @@ extension WorkspaceModel {
readAloud.read(text)
case let .readAloudFile(path):
readAloudFile(path)
case let .readAloudTerminalSelection(kind):
let text = terminal.selection(kind)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
let prose = MarkdownSpeech.plainText(MarkdownParser.parse(text))
if !readAloud.read(prose.isEmpty ? text : prose) { toasts.show("Nothing to read") }
case let .plak(text):
sendToPlak(text)
case let .plakTerminalSelection(kind):
sendToPlak(terminal.selection(kind) ?? "")
case let .newFile(directory):
overlay.present(.namePrompt(NamePromptRequest(purpose: .newFile(inDirectory: directory), anchor: anchor)))
case let .newFolder(directory):
@@ -137,6 +145,11 @@ extension WorkspaceModel {
}
}
private func sendToPlak(_ text: String) {
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.isEmpty { toasts.show("Nothing to send") } else { app.system.sendToPlak(trimmed) }
}
/// The open file is read from its buffer, unsaved edits included; any other file from disk.
private func readAloudFile(_ path: String) {
let live = editor.flatMap { $0.activePath == path && $0.isLoaded(path) ? $0.text(for: path) : nil }