Skip to content

Commit

Permalink
Preserve file URLs when pasting without formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Nov 28, 2024
1 parent 23a2f41 commit e09fb0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Binary file modified Designs/App-Store/01-Main.pxd
Binary file not shown.
30 changes: 21 additions & 9 deletions Maccy/Clipboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ class Clipboard {
var contents = item.contents

if removeFormatting {
let stringContents = contents.filter({
NSPasteboard.PasteboardType($0.type) == .string
})

// If there is no string representation of data,
// behave like we didn't have to remove formatting.
if !stringContents.isEmpty {
contents = stringContents
}
contents = clearFormatting(contents)
}

for content in contents {
Expand Down Expand Up @@ -296,4 +288,24 @@ class Clipboard {
NSApp.activate(ignoringOtherApps: true)
NSApp.hide(self)
}

private func clearFormatting(_ contents: [HistoryItemContent]) -> [HistoryItemContent] {
var newContents: [HistoryItemContent] = contents
let stringContents = contents.filter { NSPasteboard.PasteboardType($0.type) == .string }

// If there is no string representation of data,
// behave like we didn't have to remove formatting.
if !stringContents.isEmpty {
newContents = stringContents

// Preserve file URLs.
// https://github.com/p0deje/Maccy/issues/962
let fileURLContents = contents.filter { NSPasteboard.PasteboardType($0.type) == .fileURL }
if !fileURLContents.isEmpty {
newContents += fileURLContents
}
}

return newContents
}
}
2 changes: 2 additions & 0 deletions MaccyTests/ClipboardTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class ClipboardTests: XCTestCase {
func testCopyWithoutFormatting() {
let contents = [
HistoryItemContent(type: stringType.rawValue, value: "foo".data(using: .utf8)!),
HistoryItemContent(type: fileURLType.rawValue, value: "file://foo.bar".data(using: .utf8)!),
HistoryItemContent(type: rtfType.rawValue,
value: coloredString.rtf(from: NSRange(location: 0, length: coloredString.length),
documentAttributes: [:]))
Expand All @@ -234,6 +235,7 @@ class ClipboardTests: XCTestCase {
clipboard.copy(item, removeFormatting: true)
XCTAssertEqual(pasteboard.string(forType: .string), "foo")
XCTAssertEqual(pasteboard.string(forType: .fromMaccy), "")
XCTAssertEqual(pasteboard.string(forType: .fileURL), "file://foo.bar")
XCTAssertNil(pasteboard.data(forType: .rtf))
}

Expand Down

0 comments on commit e09fb0d

Please sign in to comment.