Skip to content

Commit fba8a03

Browse files
authored
Improve pasteboard handling (#900)
1 parent 2436600 commit fba8a03

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
if: github.event.pull_request.draft == false
1515
env:
1616
node-version: '20.x'
17-
DEVELOPER_DIR: /Applications/Xcode_16.2.app/Contents/Developer
17+
DEVELOPER_DIR: /Applications/Xcode_16.3.app/Contents/Developer
1818

1919
defaults:
2020
run:

MarkEditMac/Modules/Sources/AppKitExtensions/Foundation/NSPasteboard+Extension.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77
import AppKit
88

99
public extension NSPasteboard {
10-
var canPaste: Bool {
11-
pasteboardItems?.isEmpty == false
10+
var hasText: Bool {
11+
pasteboardItems?.contains { $0.types.contains(.string) } == true
1212
}
1313

1414
var string: String? {
1515
string(forType: .string)
1616
}
1717

18-
var url: String? {
19-
guard let string else {
20-
return string(forType: .URL)
18+
func url() async -> String? {
19+
guard #available(macOS 15.4, *) else {
20+
guard let string else {
21+
return string(forType: .URL)
22+
}
23+
24+
return NSDataDetector.extractURL(from: string)
2125
}
2226

23-
return NSDataDetector.extractURL(from: string)
27+
// This alerts the user only when the pasteboard really contains links
28+
let values = try? await NSPasteboard.general.detectedValues(for: [\.links])
29+
return values?.links.first?.url.absoluteString
2430
}
2531

2632
func overwrite(string: String?) {

MarkEditMac/Sources/Editor/Controllers/EditorViewController+HyperLink.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension EditorViewController {
2222
// Try our best to guess from selection and clipboard
2323
bridge.format.insertHyperLink(
2424
title: prefersURL ? defaultTitle : title,
25-
url: prefersURL ? text : (NSPasteboard.general.url ?? "https://"),
25+
url: prefersURL ? text : (await NSPasteboard.general.url() ?? "https://"),
2626
prefix: prefix
2727
)
2828
}

MarkEditMac/Sources/Editor/Views/EditorWebView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private extension EditorWebView {
134134

135135
// Disable paste for empty pasteboard
136136
if item.tag == WKContextMenuItemTag.paste.rawValue {
137-
item.isEnabled = NSPasteboard.general.canPaste
137+
item.isEnabled = NSPasteboard.general.hasText
138138
}
139139

140140
// Hide native items that require text selection

MarkEditMac/Sources/Main/Application/AppDelegate+Menu.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private extension AppDelegate {
3939
$0?.superMenuItem?.isHidden = document?.fileURL == nil
4040
}
4141

42-
fileFromClipboardItem?.isHidden = !NSPasteboard.general.canPaste
42+
fileFromClipboardItem?.isHidden = !NSPasteboard.general.hasText
4343
}
4444

4545
func reconfigureMainEditMenu(document: EditorDocument?) {
@@ -50,7 +50,7 @@ private extension AppDelegate {
5050

5151
editUndoItem?.isEnabled = await document.canUndo
5252
editRedoItem?.isEnabled = await document.canRedo
53-
editPasteItem?.isEnabled = NSPasteboard.general.canPaste
53+
editPasteItem?.isEnabled = NSPasteboard.general.hasText
5454
}
5555

5656
// [macOS 15] The system one doesn't work for WKWebView

0 commit comments

Comments
 (0)