Skip to content

Commit

Permalink
get link text to copy
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Dec 17, 2024
1 parent 4889e0a commit f8e994c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
26 changes: 16 additions & 10 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2021,22 +2021,22 @@ extension ChatViewController {
menuElements.append(action)
}

private func isLinkTapped(indexPath: IndexPath, point: CGPoint) -> String? {
if let cell = tableView.cellForRow(at: indexPath) as? BaseMessageCell {
let label = cell.messageLabel.label
let localTouchLocation = tableView.convert(point, to: label)
return label.getCopyableLinkText(localTouchLocation)
}
return nil
}

// context menu for iOS 13+
override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let messageId = messageIds[indexPath.row]
if tableView.isEditing || messageId == DC_MSG_ID_MARKER1 || messageId == DC_MSG_ID_DAYMARKER {
return nil
}

// Check if the long tap is on a link (or other message text element with custom long tap behavior)
if let msgcell = tableView.cellForRow(at: indexPath) as? BaseMessageCell {
let label = msgcell.messageLabel.label
let localTouchLocation = tableView.convert(point, to: label)
if let (detectorType, value) = label.detectGesture(localTouchLocation) {
print("url: \(detectorType) -- \(value) -- ")
}
}

return UIContextMenuConfiguration(
identifier: NSString(string: "\(messageId)"),
previewProvider: nil,
Expand Down Expand Up @@ -2076,7 +2076,13 @@ extension ChatViewController {
UIAction.menuAction(localizationKey: "forward", image: image, indexPath: indexPath, action: { self.forward(at: $0 ) })
)

if let text = message.text, !text.isEmpty {
if let link = isLinkTapped(indexPath: indexPath, point: point) {
children.append(
UIAction.menuAction(localizationKey: "menu_copy_link_to_clipboard", systemImageName: "link", indexPath: indexPath, action: { _ in
UIPasteboard.general.string = link
})
)
} else if let text = message.text, !text.isEmpty {
let copyTitle = message.file == nil ? "global_menu_edit_copy_desktop" : "menu_copy_text_to_clipboard"
children.append(
UIAction.menuAction(localizationKey: copyTitle, systemImageName: "doc.on.doc", indexPath: indexPath, action: { self.copyToClipboard(at: $0 ) })
Expand Down
64 changes: 38 additions & 26 deletions deltachat-ios/Chat/Views/MessageLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,55 +452,65 @@ open class MessageLabel: UILabel {

}

internal func detectGesture(_ touchLocation: CGPoint) -> (DetectorType, NewMessageTextCheckingType)? {
guard let index = stringIndex(at: touchLocation) else { return nil }

for (detectorType, ranges) in rangesForDetectors {
for (range, value) in ranges {
if range.contains(index) {
return (detectorType, value)
}
}
}
return nil
}

open func handleGesture(_ touchLocation: CGPoint) -> Bool {
if let (detectorType, value) = detectGesture(touchLocation) {
handleGesture(for: detectorType, value: value)
return true
internal func detectLink(_ touchLocation: CGPoint) -> (DetectorType, NewMessageTextCheckingType)? {
guard let index = stringIndex(at: touchLocation) else { return nil }

for (detectorType, ranges) in rangesForDetectors {
for (range, value) in ranges {
if range.contains(index) {
return (detectorType, value)
}
}
}

return nil
}

internal func getCopyableLinkText(_ touchLocation: CGPoint) -> String? {
guard let (detectorType, value) = detectLink(touchLocation) else { return nil }

Check warning on line 470 in deltachat-ios/Chat/Views/MessageLabel.swift

View workflow job for this annotation

GitHub Actions / build

immutable value 'detectorType' was never used; consider replacing with '_' or removing it

Check warning on line 470 in deltachat-ios/Chat/Views/MessageLabel.swift

View workflow job for this annotation

GitHub Actions / build

immutable value 'detectorType' was never used; consider replacing with '_' or removing it

switch value {
case let .link(url):
guard let url = url else { return nil }
return url.absoluteString
case let .phoneNumber(phoneNumber):
return phoneNumber
case let .custom(pattern, match):

Check warning on line 478 in deltachat-ios/Chat/Views/MessageLabel.swift

View workflow job for this annotation

GitHub Actions / build

immutable value 'pattern' was never used; consider replacing with '_' or removing it

Check warning on line 478 in deltachat-ios/Chat/Views/MessageLabel.swift

View workflow job for this annotation

GitHub Actions / build

immutable value 'pattern' was never used; consider replacing with '_' or removing it
return match
case .addressComponents, .date, .transitInfoComponents:
return nil
}
return false
}

/// swiftlint:disable cyclomatic_complexity
private func handleGesture(for detectorType: DetectorType, value: NewMessageTextCheckingType) {
internal func handleGesture(_ touchLocation: CGPoint) -> Bool {
guard let (detectorType, value) = detectLink(touchLocation) else { return false }

switch value {
case let .addressComponents(addressComponents):
var transformedAddressComponents = [String: String]()
guard let addressComponents = addressComponents else { return }
guard let addressComponents = addressComponents else { return false }
addressComponents.forEach { (key, value) in
transformedAddressComponents[key.rawValue] = value
}
handleAddress(transformedAddressComponents)
case let .phoneNumber(phoneNumber):
guard let phoneNumber = phoneNumber else { return }
guard let phoneNumber = phoneNumber else { return false }
handlePhoneNumber(phoneNumber)
case let .date(date):
guard let date = date else { return }
guard let date = date else { return false }
handleDate(date)
case let .link(url):
guard let url = url else { return }
guard let url = url else { return false }
handleURL(url)
case let .transitInfoComponents(transitInformation):
var transformedTransitInformation = [String: String]()
guard let transitInformation = transitInformation else { return }
guard let transitInformation = transitInformation else { return false }
transitInformation.forEach { (key, value) in
transformedTransitInformation[key.rawValue] = value
}
handleTransitInformation(transformedTransitInformation)
case let .custom(pattern, match):
guard let match = match else { return }
guard let match = match else { return false }
switch detectorType {
case .hashtag:
handleHashtag(match)
Expand All @@ -512,6 +522,8 @@ open class MessageLabel: UILabel {
handleCustom(pattern, match: match)
}
}

return true
}

private func handleAddress(_ addressComponents: [String: String]) {
Expand Down

0 comments on commit f8e994c

Please sign in to comment.