Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions TablePro/Core/Services/Infrastructure/ClipboardService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import UniformTypeIdentifiers
protocol ClipboardProvider {
func readText() -> String?
func writeText(_ text: String)
func writeTabular(tsv: String, html: String)
func writeRows(tsv: String, html: String?)
var hasText: Bool { get }
var hasGridRows: Bool { get }
}

struct NSPasteboardClipboardProvider: ClipboardProvider {
private static let tsvType = NSPasteboard.PasteboardType("public.utf8-tab-separated-values-text")
private static let gridRowsType = NSPasteboard.PasteboardType("com.TablePro.gridRows")

func readText() -> String? {
NSPasteboard.general.string(forType: .string)
Expand All @@ -30,17 +32,24 @@ struct NSPasteboardClipboardProvider: ClipboardProvider {
pb.setString(text, forType: NSPasteboard.PasteboardType(UTType.utf8PlainText.identifier))
}

func writeTabular(tsv: String, html: String) {
func writeRows(tsv: String, html: String?) {
let pb = NSPasteboard.general
pb.clearContents()
pb.setString(tsv, forType: .string)
pb.setString(tsv, forType: Self.tsvType)
pb.setString(html, forType: .html)
if let html {
pb.setString(html, forType: .html)
}
pb.setString("1", forType: Self.gridRowsType)
}

var hasText: Bool {
NSPasteboard.general.string(forType: .string) != nil
}

var hasGridRows: Bool {
NSPasteboard.general.types?.contains(Self.gridRowsType) == true
}
}

@MainActor
Expand Down
2 changes: 1 addition & 1 deletion TablePro/Core/Services/Query/RowOperationsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ final class RowOperationsManager {
result.append("\n(truncated, showing first \(Self.maxClipboardRows) of \(totalSelected) rows)")
}

ClipboardService.shared.writeText(result)
ClipboardService.shared.writeRows(tsv: result, html: nil)
}

func pasteRowsFromClipboard(
Expand Down
4 changes: 2 additions & 2 deletions TablePro/Views/Results/DataGridView+RowActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension TableViewCoordinator {

let tsv = tsvRows.joined(separator: "\n")
let html = HtmlTableEncoder.encode(rows: htmlRows)
ClipboardService.shared.writeTabular(tsv: tsv, html: html)
ClipboardService.shared.writeRows(tsv: tsv, html: html)
}

func copyRowsWithHeaders(at indices: Set<Int>) {
Expand All @@ -69,7 +69,7 @@ extension TableViewCoordinator {

let tsv = tsvRows.joined(separator: "\n")
let html = HtmlTableEncoder.encode(rows: htmlRows, headers: columns)
ClipboardService.shared.writeTabular(tsv: tsv, html: html)
ClipboardService.shared.writeRows(tsv: tsv, html: html)
}

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ import AppKit
extension TableViewCoordinator {
func pasteCellsFromClipboard(anchorRow: Int, anchorColumn: Int) -> Bool {
guard isEditable else { return false }
if ClipboardService.shared.hasGridRows { return false }
guard let text = ClipboardService.shared.readText(), !text.isEmpty else { return false }

let grid = text.components(separatedBy: "\n")
.filter { !$0.isEmpty }
.map { $0.components(separatedBy: "\t") }
guard !grid.isEmpty, grid[0].count > 1 || grid.count > 1 else { return false }

let dataColumnCount = tableRowsProvider().columns.count
if dataColumnCount > 0, grid.allSatisfy({ $0.count == dataColumnCount }) {
Comment thread
datlechin marked this conversation as resolved.
return false
}

let maxRow = min(anchorRow + grid.count, cachedRowCount)
let maxCol = min(anchorColumn + (grid.first?.count ?? 0), tableRowsProvider().columns.count)
let maxCol = min(anchorColumn + (grid.first?.count ?? 0), dataColumnCount)
guard anchorRow < maxRow, anchorColumn < maxCol else { return false }

let undoManager = tableView?.window?.undoManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import Testing
private final class MockClipboardProvider: ClipboardProvider {
var lastWrittenText: String?
var textToRead: String?
var lastWasGridRows = false

func readText() -> String? { textToRead }

func writeText(_ text: String) {
lastWrittenText = text
lastWasGridRows = false
}

func writeTabular(tsv: String, html: String) {
func writeRows(tsv: String, html: String?) {
lastWrittenText = tsv
lastWasGridRows = true
}

var hasText: Bool { textToRead != nil }
var hasGridRows: Bool { lastWasGridRows }
}

@MainActor
Expand Down
Loading