将 PDF 划线/注释交互完整下沉到 SDK 库
- RDPDFReaderPageView:完整页面视图移入 SDK(缩放画布、文字选择层、 选区放大镜、无障碍视口),并内置与 EPUB 一致的两套系统编辑菜单: 选区菜单(复制/划线/注释)、已有划线菜单(复制/注释|删除注释/删除划线) - RDPDFReaderPageViewDelegate:宿主只通过代理做数据持久化与打开业务界面 - 修复划线命中判定:点击点先归一化再与标注比例坐标比较 - 修复 tappedHighlight 残留导致选区菜单项被错误过滤的问题 - 主题应用改为纯颜色入参,SDK 不依赖 EPUB 主题类型 - RDPDFReaderHighlightOverlayView 恢复为纯绘制层,点击交互统一由页面视图处理 - Demo 删除 PDFDemoPageView(约 290 行),改为实现代理回调 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
5a19cfde14
commit
1bde945d36
@@ -164,9 +164,9 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
func pageCountOfReaderView(readerView: RDPDFReaderView) -> Int { document.pageCount }
|
||||
|
||||
func pageContentView(readerView: RDPDFReaderView, pageNum: Int) -> UIView {
|
||||
let page = PDFDemoPageView()
|
||||
let page = RDPDFReaderPageView()
|
||||
page.image = renderedImage(pageNum)
|
||||
page.apply(theme: currentThemePreset.theme)
|
||||
applyTheme(to: page)
|
||||
configure(page, for: pageNum)
|
||||
return page
|
||||
}
|
||||
@@ -204,34 +204,22 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
refreshDemoState()
|
||||
}
|
||||
|
||||
private func configure(_ page: PDFDemoPageView, for pageIndex: Int) {
|
||||
private func configure(_ page: RDPDFReaderPageView, for pageIndex: Int) {
|
||||
page.configureTextLayer(
|
||||
pageIndex: pageIndex,
|
||||
textRuns: textRuns(for: pageIndex),
|
||||
textSource: imageTextSource.annotationSource,
|
||||
annotations: annotations(for: pageIndex)
|
||||
)
|
||||
page.onHighlight = { [weak self] selection, color in
|
||||
self?.addAnnotation(from: selection, pageIndex: pageIndex, color: color, note: nil)
|
||||
}
|
||||
page.onRequestNote = { [weak self] selection in
|
||||
self?.showAnnotationEditor(for: selection, pageIndex: pageIndex)
|
||||
}
|
||||
page.onOpenAnnotation = { [weak self] annotation in
|
||||
self?.showAnnotationEditor(editing: annotation)
|
||||
}
|
||||
page.onCopyText = { [weak self] text in
|
||||
self?.copiedText = text
|
||||
self?.refreshDemoState()
|
||||
}
|
||||
page.onSelectionChanged = { [weak self] selection in
|
||||
guard let self, self.readerView.currentPage == pageIndex else { return }
|
||||
self.activeSelection = selection
|
||||
self.refreshDemoState()
|
||||
}
|
||||
page.onExistingHighlightMenuAction = { [weak self] action, annotation in
|
||||
self?.handleExistingHighlightMenuAction(action, annotation: annotation)
|
||||
}
|
||||
page.delegate = self
|
||||
}
|
||||
|
||||
private func applyTheme(to page: RDPDFReaderPageView) {
|
||||
let theme = currentThemePreset.theme
|
||||
page.applyTheme(
|
||||
contentBackgroundColor: theme.contentBackgroundColor,
|
||||
surroundingBackgroundColor: theme.toolBackgroundColor
|
||||
)
|
||||
}
|
||||
|
||||
private func textRuns(for pageIndex: Int) -> [RDPDFReaderTextRun] {
|
||||
@@ -290,7 +278,7 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
}
|
||||
|
||||
private func refreshVisiblePageContent(for pageIndex: Int) {
|
||||
guard let page = readerView.pageContentView(pageNum: pageIndex) as? PDFDemoPageView else { return }
|
||||
guard let page = readerView.pageContentView(pageNum: pageIndex) as? RDPDFReaderPageView else { return }
|
||||
page.configureTextLayer(
|
||||
pageIndex: pageIndex,
|
||||
textRuns: textRuns(for: pageIndex),
|
||||
@@ -365,7 +353,7 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
break
|
||||
case .deleteUnderline:
|
||||
do {
|
||||
try annotationStore.deleteAnnotation(withID: annotation.id)
|
||||
_ = try annotationStore.deleteAnnotation(id: annotation.id)
|
||||
annotationPersistenceError = nil
|
||||
refreshVisiblePageContent(for: annotation.pageIndex)
|
||||
refreshDemoState()
|
||||
@@ -378,7 +366,7 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
do {
|
||||
var updated = annotation
|
||||
updated.note = nil
|
||||
try annotationStore.updateAnnotation(updated)
|
||||
_ = try annotationStore.updateAnnotation(updated)
|
||||
annotationPersistenceError = nil
|
||||
refreshVisiblePageContent(for: annotation.pageIndex)
|
||||
refreshDemoState()
|
||||
@@ -672,8 +660,8 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
themedImageCache.removeAll()
|
||||
|
||||
if refreshCurrentPage, readerView.currentPage >= 0 {
|
||||
if let pageView = readerView.pageContentView(pageNum: readerView.currentPage) as? PDFDemoPageView {
|
||||
pageView.apply(theme: theme)
|
||||
if let pageView = readerView.pageContentView(pageNum: readerView.currentPage) as? RDPDFReaderPageView {
|
||||
applyTheme(to: pageView)
|
||||
pageView.image = renderedImage(readerView.currentPage)
|
||||
}
|
||||
refreshVisiblePageContent(for: readerView.currentPage)
|
||||
@@ -787,6 +775,42 @@ final class PDFDemoReaderViewController: UIViewController, RDPDFReaderDataSource
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - RDPDFReaderPageViewDelegate
|
||||
// 选区/划线/注释的交互与菜单由 SDK 处理,这里只做数据持久化和打开业务界面。
|
||||
extension PDFDemoReaderViewController: RDPDFReaderPageViewDelegate {
|
||||
|
||||
func pageView(_ pageView: RDPDFReaderPageView, didChangeSelection selection: RDPDFReaderImageTextSelection?) {
|
||||
guard readerView.currentPage == pageView.pageIndex else { return }
|
||||
activeSelection = selection
|
||||
refreshDemoState()
|
||||
}
|
||||
|
||||
func pageView(_ pageView: RDPDFReaderPageView, didCopyText text: String) {
|
||||
copiedText = text
|
||||
refreshDemoState()
|
||||
}
|
||||
|
||||
func pageView(_ pageView: RDPDFReaderPageView, didRequestHighlight selection: RDPDFReaderImageTextSelection, color: String) {
|
||||
addAnnotation(from: selection, pageIndex: pageView.pageIndex, color: color, note: nil)
|
||||
}
|
||||
|
||||
func pageView(_ pageView: RDPDFReaderPageView, didRequestAnnotation selection: RDPDFReaderImageTextSelection) {
|
||||
showAnnotationEditor(for: selection, pageIndex: pageView.pageIndex)
|
||||
}
|
||||
|
||||
func pageView(_ pageView: RDPDFReaderPageView, didOpenAnnotation annotation: RDPDFReaderAnnotation) {
|
||||
showAnnotationEditor(editing: annotation)
|
||||
}
|
||||
|
||||
func pageView(
|
||||
_ pageView: RDPDFReaderPageView,
|
||||
didRequestHighlightMenuAction action: RDPDFReaderExistingHighlightMenuAction,
|
||||
highlight: RDPDFReaderAnnotation
|
||||
) {
|
||||
handleExistingHighlightMenuAction(action, annotation: highlight)
|
||||
}
|
||||
}
|
||||
|
||||
private final class PDFDemoNavigationViewController: UIViewController {
|
||||
private let handleView = UIView()
|
||||
private let closeButton = UIButton(type: .system)
|
||||
@@ -864,296 +888,6 @@ private final class PDFDemoNavigationViewController: UIViewController {
|
||||
}
|
||||
}
|
||||
|
||||
private final class PDFDemoPageView: UIView, RDPDFReaderPageInteractable, UIGestureRecognizerDelegate {
|
||||
private let zoomView = RDPDFZoomablePageView()
|
||||
private let textLayer = RDPDFReaderImageTextLayerView()
|
||||
private let contentAccessibilityView = UIView()
|
||||
private let selectionLoupe = RDPDFReaderSelectionLoupeView()
|
||||
|
||||
var image: UIImage? { didSet { zoomView.image = image } }
|
||||
var readerContentTapHandler: ((CGPoint) -> Void)?
|
||||
var readerZoomStateChangedHandler: ((Bool) -> Void)?
|
||||
var readerSelectionStateChangedHandler: ((Bool) -> Void)?
|
||||
var onSelectionChanged: ((RDPDFReaderImageTextSelection?) -> Void)?
|
||||
var onCopyText: ((String) -> Void)?
|
||||
var onHighlight: ((RDPDFReaderImageTextSelection, String) -> Void)?
|
||||
var onRequestNote: ((RDPDFReaderImageTextSelection) -> Void)?
|
||||
var onOpenAnnotation: ((RDPDFReaderAnnotation) -> Void)?
|
||||
var onExistingHighlightMenuAction: ((RDPDFReaderExistingHighlightMenuAction, RDPDFReaderAnnotation) -> Void)?
|
||||
|
||||
var readerIsZoomed: Bool { zoomView.isZoomed }
|
||||
var readerHasActiveTextSelection: Bool { textLayer.selectedSelection != nil }
|
||||
|
||||
func configureTextLayer(
|
||||
pageIndex: Int,
|
||||
textRuns: [RDPDFReaderTextRun],
|
||||
textSource: RDPDFReaderAnnotationSource,
|
||||
annotations: [RDPDFReaderAnnotation]
|
||||
) {
|
||||
textLayer.pageIndex = pageIndex
|
||||
textLayer.textSource = textSource
|
||||
textLayer.textRuns = textRuns
|
||||
textLayer.annotations = annotations
|
||||
updateAccessibilityViewport()
|
||||
}
|
||||
|
||||
func readerSetInternalGesturesEnabled(_ enabled: Bool) { zoomView.setInternalGesturesEnabled(enabled) }
|
||||
func readerClearTextSelection() { textLayer.clearSelection() }
|
||||
func readerBeginExternalPinch(at point: CGPoint) { zoomView.beginExternalPinch(at: convert(point, to: zoomView)) }
|
||||
func readerUpdateExternalPinch(scale: CGFloat, at point: CGPoint) {
|
||||
zoomView.updateExternalPinch(scale: scale, at: convert(point, to: zoomView))
|
||||
}
|
||||
func readerEndExternalPinch() { zoomView.endExternalPinch() }
|
||||
func readerBeginExternalPan() { zoomView.beginExternalPan() }
|
||||
func readerUpdateExternalPan(translation: CGPoint) { zoomView.updateExternalPan(translation: translation) }
|
||||
func readerEndExternalPan() { zoomView.endExternalPan() }
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
isAccessibilityElement = false
|
||||
|
||||
zoomView.frame = bounds
|
||||
zoomView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
zoomView.zoomStateChanged = { [weak self] zoomed in
|
||||
self?.readerZoomStateChangedHandler?(zoomed)
|
||||
self?.updateAccessibilityViewport()
|
||||
}
|
||||
zoomView.viewportChanged = { [weak self] _, _ in self?.updateAccessibilityViewport() }
|
||||
addSubview(zoomView)
|
||||
|
||||
textLayer.frame = zoomView.contentView.bounds
|
||||
textLayer.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
textLayer.isAccessibilityElement = true
|
||||
textLayer.onSelectionChanged = { [weak self] selection in
|
||||
self?.handleSelectionChange(selection)
|
||||
}
|
||||
textLayer.onInteractionStateChanged = { [weak self] state in
|
||||
self?.handleSelectionInteractionStateChange(state)
|
||||
}
|
||||
textLayer.onSelectionFocusChanged = { [weak self] focusPoint in
|
||||
self?.updateSelectionLoupe(focusPoint)
|
||||
}
|
||||
zoomView.contentView.addSubview(textLayer)
|
||||
|
||||
contentAccessibilityView.frame = bounds
|
||||
contentAccessibilityView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
contentAccessibilityView.backgroundColor = .clear
|
||||
contentAccessibilityView.isUserInteractionEnabled = false
|
||||
contentAccessibilityView.isAccessibilityElement = true
|
||||
contentAccessibilityView.accessibilityIdentifier = "epub.reader.content"
|
||||
contentAccessibilityView.accessibilityLabel = "PDF 阅读页面"
|
||||
addSubview(contentAccessibilityView)
|
||||
|
||||
addSubview(selectionLoupe)
|
||||
|
||||
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
|
||||
tap.delegate = self
|
||||
tap.require(toFail: zoomView.doubleTapZoomGestureRecognizer)
|
||||
tap.require(toFail: textLayer.longPressGestureRecognizer)
|
||||
tap.require(toFail: textLayer.selectionHandlePanGestureRecognizer)
|
||||
tap.cancelsTouchesInView = false
|
||||
addGestureRecognizer(tap)
|
||||
updateAccessibilityViewport()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
zoomView.layoutIfNeeded()
|
||||
textLayer.frame = zoomView.contentView.bounds
|
||||
}
|
||||
|
||||
func apply(theme: RDEPUBReaderTheme) {
|
||||
backgroundColor = theme.contentBackgroundColor
|
||||
zoomView.backgroundColor = theme.toolBackgroundColor
|
||||
zoomView.contentView.backgroundColor = theme.contentBackgroundColor
|
||||
}
|
||||
|
||||
@objc private func handleTap(_ gesture: UITapGestureRecognizer) {
|
||||
if textLayer.selectedSelection != nil {
|
||||
textLayer.clearSelection()
|
||||
return
|
||||
}
|
||||
let textLayerPoint = gesture.location(in: textLayer)
|
||||
if let annotation = textLayer.note(at: textLayerPoint) {
|
||||
onOpenAnnotation?(annotation)
|
||||
return
|
||||
}
|
||||
if let highlight = highlightAt(textLayerPoint) {
|
||||
showExistingHighlightMenu(for: highlight, at: gesture.location(in: self))
|
||||
return
|
||||
}
|
||||
readerContentTapHandler?(gesture.location(in: self))
|
||||
}
|
||||
|
||||
private func highlightAt(_ point: CGPoint) -> RDPDFReaderAnnotation? {
|
||||
let currentPageAnnotations = textLayer.annotations.filter { $0.pageIndex == textLayer.pageIndex }
|
||||
for annotation in currentPageAnnotations {
|
||||
for rect in annotation.normalizedRects {
|
||||
if rect.contains(point) {
|
||||
return annotation
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MARK: - 选区菜单(与 EPUB 一致:锚定在选区旁的系统编辑菜单)
|
||||
|
||||
override var canBecomeFirstResponder: Bool { true }
|
||||
|
||||
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
||||
if tappedHighlight != nil {
|
||||
return action == #selector(rd_copy_highlight(_:))
|
||||
|| action == #selector(rd_delete_highlight(_:))
|
||||
|| action == #selector(rd_annotate_highlight(_:))
|
||||
|| action == #selector(rd_delete_annotation(_:))
|
||||
}
|
||||
guard let selection = textLayer.selectedSelection else { return false }
|
||||
switch action {
|
||||
case #selector(rd_copy(_:)):
|
||||
return selection.canCopyText
|
||||
case #selector(rd_highlight(_:)), #selector(rd_annotate(_:)):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func handleSelectionChange(_ selection: RDPDFReaderImageTextSelection?) {
|
||||
if selection == nil {
|
||||
hideSelectionMenu()
|
||||
}
|
||||
readerSelectionStateChangedHandler?(selection != nil)
|
||||
onSelectionChanged?(selection)
|
||||
}
|
||||
|
||||
private func handleSelectionInteractionStateChange(_ state: RDPDFReaderSelectionInteractionState) {
|
||||
switch state {
|
||||
case .selectionActive:
|
||||
showSelectionMenu()
|
||||
case .idle, .selecting, .adjustingHandle:
|
||||
hideSelectionMenu()
|
||||
}
|
||||
}
|
||||
|
||||
private func showSelectionMenu() {
|
||||
guard textLayer.selectedSelection != nil,
|
||||
let anchorRect = textLayer.menuAnchorRect() else {
|
||||
return
|
||||
}
|
||||
becomeFirstResponder()
|
||||
let menuController = UIMenuController.shared
|
||||
menuController.menuItems = [
|
||||
UIMenuItem(title: "复制", action: #selector(rd_copy(_:))),
|
||||
UIMenuItem(title: "高亮", action: #selector(rd_highlight(_:))),
|
||||
UIMenuItem(title: "注释", action: #selector(rd_annotate(_:)))
|
||||
]
|
||||
menuController.setTargetRect(convert(anchorRect, from: textLayer), in: self)
|
||||
menuController.setMenuVisible(true, animated: true)
|
||||
}
|
||||
|
||||
private func hideSelectionMenu() {
|
||||
UIMenuController.shared.setMenuVisible(false, animated: true)
|
||||
}
|
||||
|
||||
private func updateSelectionLoupe(_ focusPoint: CGPoint?) {
|
||||
guard let focusPoint else {
|
||||
selectionLoupe.dismiss()
|
||||
return
|
||||
}
|
||||
selectionLoupe.present(
|
||||
sourceView: zoomView.contentView,
|
||||
focusPoint: textLayer.convert(focusPoint, to: zoomView.contentView),
|
||||
hostBounds: bounds,
|
||||
targetPoint: convert(focusPoint, from: textLayer)
|
||||
)
|
||||
}
|
||||
|
||||
@objc private func rd_copy(_ sender: Any?) {
|
||||
guard let selection = textLayer.selectedSelection,
|
||||
selection.canCopyText,
|
||||
let text = selection.text?.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
!text.isEmpty else { return }
|
||||
UIPasteboard.general.string = text
|
||||
onCopyText?(text)
|
||||
textLayer.clearSelection()
|
||||
}
|
||||
|
||||
@objc private func rd_highlight(_ sender: Any?) {
|
||||
guard let selection = textLayer.selectedSelection else { return }
|
||||
// 与 EPUB 相同的“一步高亮”:直接采用默认黄色。
|
||||
onHighlight?(selection, "#F8E16C")
|
||||
textLayer.clearSelection()
|
||||
}
|
||||
|
||||
@objc private func rd_annotate(_ sender: Any?) {
|
||||
guard let selection = textLayer.selectedSelection else { return }
|
||||
onRequestNote?(selection)
|
||||
textLayer.clearSelection()
|
||||
}
|
||||
|
||||
// MARK: - 已有高亮菜单(与 EPUB 一致)
|
||||
|
||||
private var tappedHighlight: RDPDFReaderAnnotation?
|
||||
|
||||
private func showExistingHighlightMenu(for annotation: RDPDFReaderAnnotation, at point: CGPoint) {
|
||||
tappedHighlight = annotation
|
||||
becomeFirstResponder()
|
||||
|
||||
var menuItems: [UIMenuItem] = [
|
||||
UIMenuItem(title: "复制", action: #selector(rd_copy_highlight(_:)))
|
||||
]
|
||||
|
||||
if annotation.note?.isEmpty == false {
|
||||
menuItems.append(UIMenuItem(title: "删除注释", action: #selector(rd_delete_annotation(_:))))
|
||||
} else {
|
||||
menuItems.append(UIMenuItem(title: "注释", action: #selector(rd_annotate_highlight(_:))))
|
||||
}
|
||||
|
||||
menuItems.append(UIMenuItem(title: "删除划线", action: #selector(rd_delete_highlight(_:))))
|
||||
|
||||
let menuController = UIMenuController.shared
|
||||
menuController.menuItems = menuItems
|
||||
menuController.setTargetRect(CGRect(x: point.x, y: point.y, width: 1, height: 1), in: self)
|
||||
menuController.setMenuVisible(true, animated: true)
|
||||
}
|
||||
|
||||
@objc private func rd_copy_highlight(_ sender: Any?) {
|
||||
guard let highlight = tappedHighlight,
|
||||
let text = highlight.selectedText?.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
!text.isEmpty else { return }
|
||||
UIPasteboard.general.string = text
|
||||
onExistingHighlightMenuAction?(.copy, highlight)
|
||||
}
|
||||
|
||||
@objc private func rd_annotate_highlight(_ sender: Any?) {
|
||||
guard let highlight = tappedHighlight else { return }
|
||||
onExistingHighlightMenuAction?(.annotate, highlight)
|
||||
}
|
||||
|
||||
@objc private func rd_delete_annotation(_ sender: Any?) {
|
||||
guard let highlight = tappedHighlight else { return }
|
||||
onExistingHighlightMenuAction?(.deleteAnnotation, highlight)
|
||||
}
|
||||
|
||||
@objc private func rd_delete_highlight(_ sender: Any?) {
|
||||
guard let highlight = tappedHighlight else { return }
|
||||
onExistingHighlightMenuAction?(.deleteUnderline, highlight)
|
||||
}
|
||||
|
||||
private func updateAccessibilityViewport() {
|
||||
let offset = zoomView.contentOffset
|
||||
contentAccessibilityView.accessibilityValue = String(
|
||||
format: "zoom=%.2f offset=%.0f,%.0f",
|
||||
zoomView.zoomScale,
|
||||
offset.x,
|
||||
offset.y
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private final class PDFDemoSettingsViewController: UIViewController {
|
||||
var onBrightnessChange: ((CGFloat) -> Void)?
|
||||
var onDisplayTypeChange: ((RDPDFReaderView.DisplayType) -> Void)?
|
||||
|
||||
Reference in New Issue
Block a user