mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-08-08 06:56:10 +00:00
Merge fa64e8cb67c024bf2252aa655f2323faebc8ffba into 1f59e814f90c3f489f48d68262cb1bf640bf6181
This commit is contained in:
commit
cef211361d
File diff suppressed because it is too large
Load Diff
62
bitchat/Services/MessageClipboard.swift
Normal file
62
bitchat/Services/MessageClipboard.swift
Normal file
@ -0,0 +1,62 @@
|
||||
//
|
||||
// MessageClipboard.swift
|
||||
// bitchat
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import Foundation
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#elseif os(macOS)
|
||||
import AppKit
|
||||
#endif
|
||||
|
||||
/// Clipboard helpers for message actions. Kept out of the view so unit tests
|
||||
/// can assert quote formatting without standing up SwiftUI.
|
||||
enum MessageClipboard {
|
||||
/// Plain message body for the pasteboard.
|
||||
static func copyPlaintext(_ content: String) {
|
||||
#if os(iOS)
|
||||
UIPasteboard.general.string = content
|
||||
#elseif os(macOS)
|
||||
let pasteboard = NSPasteboard.general
|
||||
pasteboard.clearContents()
|
||||
pasteboard.setString(content, forType: .string)
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Builds a composer-ready quote block (`> line` per line, trailing blank).
|
||||
/// Sender header is plain text (`> alice:`), not `@alice`, so quoting does
|
||||
/// not re-fire mention notifications for the quoted person or for names
|
||||
/// inside the quoted body (those stay behind `> ` and are not live tokens).
|
||||
static func quoteForComposer(_ content: String, sender: String?) -> String {
|
||||
let body = content.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !body.isEmpty else { return "" }
|
||||
let header: String
|
||||
if let sender, !sender.isEmpty, sender != "system" {
|
||||
header = "> \(sender):\n"
|
||||
} else {
|
||||
header = ""
|
||||
}
|
||||
let quoted = body
|
||||
.split(separator: "\n", omittingEmptySubsequences: false)
|
||||
.map { "> \($0)" }
|
||||
.joined(separator: "\n")
|
||||
return header + quoted + "\n\n"
|
||||
}
|
||||
|
||||
/// Appends a quote to an existing composer draft without wiping it.
|
||||
/// Only a trailing newline skips inserting another separator — a trailing
|
||||
/// space must still get a newline so the quote marker starts its own line.
|
||||
static func appendQuote(to draft: String, content: String, sender: String?) -> String {
|
||||
let quote = quoteForComposer(content, sender: sender)
|
||||
guard !quote.isEmpty else { return draft }
|
||||
if draft.isEmpty { return quote }
|
||||
if draft.hasSuffix("\n") {
|
||||
return draft + quote
|
||||
}
|
||||
return draft + "\n" + quote
|
||||
}
|
||||
}
|
||||
@ -254,12 +254,17 @@ struct ContentView: View {
|
||||
.frame(minWidth: 600, minHeight: 400)
|
||||
#endif
|
||||
.onChange(of: selectedPrivatePeerID) { newValue in
|
||||
// Shared composer draft must not travel across conversation
|
||||
// targets (quoting a DM then opening public would otherwise
|
||||
// one-tap-broadcast private content).
|
||||
messageText = ""
|
||||
if newValue != nil {
|
||||
showSidebar = true
|
||||
}
|
||||
sharedContentImportModel.updateDestination(sharedContentDestination)
|
||||
}
|
||||
.onChange(of: locationChannelsModel.selectedChannel) { _ in
|
||||
messageText = ""
|
||||
sharedContentImportModel.updateDestination(sharedContentDestination)
|
||||
}
|
||||
.sheet(
|
||||
|
||||
@ -143,13 +143,23 @@ struct MessageListView: View {
|
||||
}
|
||||
}
|
||||
Button("content.message.copy") {
|
||||
#if os(iOS)
|
||||
UIPasteboard.general.string = message.content
|
||||
#else
|
||||
let pb = NSPasteboard.general
|
||||
pb.clearContents()
|
||||
pb.setString(message.content, forType: .string)
|
||||
#endif
|
||||
MessageClipboard.copyPlaintext(message.content)
|
||||
}
|
||||
if !message.content.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
|
||||
message.sender != "system" {
|
||||
Button {
|
||||
messageText = MessageClipboard.appendQuote(
|
||||
to: messageText,
|
||||
content: message.content,
|
||||
sender: message.sender
|
||||
)
|
||||
} label: {
|
||||
Text(String(
|
||||
localized: "content.message.quote",
|
||||
defaultValue: "quote in composer",
|
||||
comment: "Context menu action that inserts a quoted copy of the message into the composer draft"
|
||||
))
|
||||
}
|
||||
}
|
||||
if isResendableFailedMessage(message) {
|
||||
Button("content.actions.resend") {
|
||||
|
||||
46
bitchatTests/Services/MessageClipboardTests.swift
Normal file
46
bitchatTests/Services/MessageClipboardTests.swift
Normal file
@ -0,0 +1,46 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import bitchat
|
||||
|
||||
struct MessageClipboardTests {
|
||||
@Test func quoteWrapsEachLineAndAddsSender() {
|
||||
let quoted = MessageClipboard.quoteForComposer("hello\nworld", sender: "alice")
|
||||
#expect(quoted == "> alice:\n> hello\n> world\n\n")
|
||||
}
|
||||
|
||||
@Test func quoteSkipsSystemSenderHeader() {
|
||||
let quoted = MessageClipboard.quoteForComposer("joined", sender: "system")
|
||||
#expect(quoted == "> joined\n\n")
|
||||
}
|
||||
|
||||
@Test func emptyContentYieldsEmptyQuote() {
|
||||
#expect(MessageClipboard.quoteForComposer(" ", sender: "alice").isEmpty)
|
||||
}
|
||||
|
||||
@Test func appendQuotePreservesExistingDraft() {
|
||||
let result = MessageClipboard.appendQuote(
|
||||
to: "already typing",
|
||||
content: "prior message",
|
||||
sender: "bob"
|
||||
)
|
||||
#expect(result == "already typing\n> bob:\n> prior message\n\n")
|
||||
}
|
||||
|
||||
@Test func appendQuoteOntoEmptyDraft() {
|
||||
let result = MessageClipboard.appendQuote(
|
||||
to: "",
|
||||
content: "solo",
|
||||
sender: "carol"
|
||||
)
|
||||
#expect(result == "> carol:\n> solo\n\n")
|
||||
}
|
||||
|
||||
@Test func appendQuoteAfterTrailingSpaceStartsNewLine() {
|
||||
let result = MessageClipboard.appendQuote(
|
||||
to: "draft with space ",
|
||||
content: "quoted",
|
||||
sender: "dana"
|
||||
)
|
||||
#expect(result == "draft with space \n> dana:\n> quoted\n\n")
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user