From 24f497d43a5dba7f1ed54704c62ddebec63704f3 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Mon, 22 Sep 2025 20:49:28 +1200 Subject: [PATCH] Replace UITextInteraction API with UITextItem ones --- .../ActivityFormattableContentView.swift | 24 ++++++++++++++----- .../ViewRelated/Cells/ExpandableCell.swift | 24 ++++++++++++------- .../Views/NoteBlockTextTableViewCell.swift | 19 ++++++++++++--- .../Views/RichTextView/RichTextView.swift | 8 +++---- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Activity/Details/ActivityFormattableContentView.swift b/WordPress/Classes/ViewRelated/Activity/Details/ActivityFormattableContentView.swift index 935176e91e9b..de01a83f381b 100644 --- a/WordPress/Classes/ViewRelated/Activity/Details/ActivityFormattableContentView.swift +++ b/WordPress/Classes/ViewRelated/Activity/Details/ActivityFormattableContentView.swift @@ -57,14 +57,28 @@ struct ActivityFormattableContentView: UIViewRepresentable { super.init() } - func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { - guard interaction == .invokeDefaultAction else { - return false + func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? { + guard case let .link(URL) = textItem.content else { + return nil } + return UIAction { [weak self] _ in + self?.routeTo(URL) + } + } + + func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? { + if case .link = textItem.content { + return nil + } + + return .init(menu: defaultMenu) + } + + private func routeTo(_ URL: URL) { // Get the top view controller to create content coordinator guard let viewController = UIViewController.topViewController else { - return false + return } let contentCoordinator = DefaultContentCoordinator( @@ -78,8 +92,6 @@ struct ActivityFormattableContentView: UIViewRepresentable { ) router.routeTo(URL) - - return false } } } diff --git a/WordPress/Classes/ViewRelated/Cells/ExpandableCell.swift b/WordPress/Classes/ViewRelated/Cells/ExpandableCell.swift index 31401197a2e6..0e7b06141a84 100644 --- a/WordPress/Classes/ViewRelated/Cells/ExpandableCell.swift +++ b/WordPress/Classes/ViewRelated/Cells/ExpandableCell.swift @@ -76,15 +76,21 @@ class ExpandableCell: WPReusableTableViewCell, NibLoadable { } extension ExpandableCell: UITextViewDelegate { - func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { - switch interaction { - case .invokeDefaultAction: - urlCallback?(URL) - return false - case .preview, .presentActions: - return true - @unknown default: - fatalError() + func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? { + guard case let .link(URL) = textItem.content else { + return defaultAction } + + return UIAction { [weak self] _ in + self?.urlCallback?(URL) + } + } + + func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? { + if case .link = textItem.content { + return nil + } + + return .init(menu: defaultMenu) } } diff --git a/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockTextTableViewCell.swift b/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockTextTableViewCell.swift index 7cf0e5787e84..03bebe78fab1 100644 --- a/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockTextTableViewCell.swift +++ b/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockTextTableViewCell.swift @@ -119,9 +119,22 @@ class NoteBlockTextTableViewCell: NoteBlockTableViewCell, RichTextViewDataSource // MARK: - RichTextView Data Source - func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { - onUrlClick?(URL) - return false + func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? { + guard case let .link(URL) = textItem.content else { + return nil + } + + return UIAction { [weak self] _ in + self?.onUrlClick?(URL) + } + } + + func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? { + if case .link = textItem.content { + return nil + } + + return .init(menu: defaultMenu) } func textView(_ textView: UITextView, didPressLink link: URL) { diff --git a/WordPress/Classes/ViewRelated/Views/RichTextView/RichTextView.swift b/WordPress/Classes/ViewRelated/Views/RichTextView/RichTextView.swift index e725380cfb61..4c0410adda79 100644 --- a/WordPress/Classes/ViewRelated/Views/RichTextView/RichTextView.swift +++ b/WordPress/Classes/ViewRelated/Views/RichTextView/RichTextView.swift @@ -259,12 +259,12 @@ import UniformTypeIdentifiers delegate?.textViewDidChangeSelection?(textView) } - open func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { - return delegate?.textView?(textView, shouldInteractWith: URL, in: characterRange, interaction: interaction) ?? true + open func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? { + delegate?.textView?(textView, primaryActionFor: textItem, defaultAction: defaultAction) } - open func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { - return delegate?.textView?(textView, shouldInteractWith: textAttachment, in: characterRange, interaction: interaction) ?? true + public func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? { + delegate?.textView?(textView, menuConfigurationFor: textItem, defaultMenu: defaultMenu) } // MARK: - Private Properites