From 0baac64db8f516cb1b3fe13dfeb7f7d5b287119c Mon Sep 17 00:00:00 2001 From: "i miss her." <232419715+ettyxne@users.noreply.github.com> Date: Tue, 12 May 2026 20:58:51 +0300 Subject: [PATCH] Fix code/monospace lost on copy-paste Copying formatted text strips the code and monospace attributes from the kInApp pasteboard entry, so pasting back into Telegram loses the formatting. The bug: removeAttribute(.code/.monospace) was called on `modified` before building ChatTextInputState from it. Since NSMutableAttributedString is a reference type, mutating it afterward affects the already-captured attributedText, so the encoded kInApp data has no code/monospace runs. Fix: remove those two removeAttribute calls. refreshTextInputAttributes already sets .font to NSFont.code() on every code/monospace run, so RTF export naturally produces monospace output with no extra work. Also move .string out of the `if let rtf` branch and declare all three pasteboard types upfront, as required by the NSPasteboard API contract. --- Telegram-Mac/InAppLinks.swift | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Telegram-Mac/InAppLinks.swift b/Telegram-Mac/InAppLinks.swift index 74c3937f6a..e847a16817 100644 --- a/Telegram-Mac/InAppLinks.swift +++ b/Telegram-Mac/InAppLinks.swift @@ -347,31 +347,24 @@ var globalLinkExecutor:TextViewInteractions { } //modified.removeAttribute(TextInputAttributes.quote, range: modified.range) - modified.removeAttribute(TextInputAttributes.code, range: modified.range) - modified.removeAttribute(TextInputAttributes.monospace, range: modified.range) let input = ChatTextInputState(attributedText: modified, selectionRange: 0 ..< modified.length) - + if !modified.string.isEmpty { pb.clearContents() let rtf = try? modified.data(from: modified.range, documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.rtf]) - - - pb.declareTypes([.rtf, .kInApp], owner: nil) - + pb.declareTypes([.rtf, .kInApp, .string], owner: nil) let encoder = AdaptedPostboxEncoder() - let encoded = try? encoder.encode(input) - - if let data = encoded { + if let data = try? encoder.encode(input) { pb.setData(data, forType: .kInApp) } if let rtf = rtf { pb.setData(rtf, forType: .rtf) - pb.setString(modified.string, forType: .string) - return true } + pb.setString(modified.string, forType: .string) + return true } - + return false }, translate: { text, window in let language = Translate.detectLanguage(for: text)