@tobyt/expo-pdf-markup 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +7 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +85 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +1 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +83 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +2 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +203 -37
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +36 -8
- package/build/ExpoPdfMarkup.types.d.ts +25 -3
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.js +2 -2
- package/build/ExpoPdfMarkupView.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +127 -43
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/web/annotationUtils.d.ts +9 -1
- package/build/web/annotationUtils.d.ts.map +1 -1
- package/build/web/annotationUtils.js +88 -5
- package/build/web/annotationUtils.js.map +1 -1
- package/ios/AnnotationModels.swift +185 -0
- package/ios/AnnotationSerializer.swift +63 -33
- package/ios/ExpoPdfMarkupModule.swift +4 -0
- package/ios/ExpoPdfMarkupView.swift +279 -86
- package/ios/Tests/AnnotationSerializerTests.swift +18 -0
- package/package.json +1 -1
- package/src/ExpoPdfMarkup.types.ts +33 -3
- package/src/ExpoPdfMarkupView.tsx +12 -9
- package/src/ExpoPdfMarkupView.web.tsx +165 -41
- package/src/web/annotationUtils.ts +121 -4
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import Foundation
|
|
2
|
+
import PDFKit
|
|
3
|
+
import UIKit
|
|
2
4
|
|
|
3
5
|
struct AnnotationPoint: Codable, Equatable {
|
|
4
6
|
let x: CGFloat
|
|
@@ -41,6 +43,7 @@ struct AnnotationModel: Codable, Equatable {
|
|
|
41
43
|
var bounds: AnnotationBounds?
|
|
42
44
|
var contents: String?
|
|
43
45
|
var fontSize: CGFloat?
|
|
46
|
+
var fontFamily: String?
|
|
44
47
|
var createdAt: Double?
|
|
45
48
|
|
|
46
49
|
var decodedPaths: [[AnnotationPoint]] {
|
|
@@ -71,3 +74,185 @@ struct AnnotationsData: Codable, Equatable {
|
|
|
71
74
|
self.annotations = annotations
|
|
72
75
|
}
|
|
73
76
|
}
|
|
77
|
+
|
|
78
|
+
extension ExpoPdfMarkupView {
|
|
79
|
+
func setupTextGesture() {
|
|
80
|
+
guard textTapGesture == nil else { return }
|
|
81
|
+
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTextTap(_:)))
|
|
82
|
+
tap.delegate = self
|
|
83
|
+
pdfView.addGestureRecognizer(tap)
|
|
84
|
+
textTapGesture = tap
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func removeTextGesture() {
|
|
88
|
+
if let gesture = textTapGesture {
|
|
89
|
+
pdfView.removeGestureRecognizer(gesture)
|
|
90
|
+
textTapGesture = nil
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@objc func handleTextTap(_ gesture: UITapGestureRecognizer) {
|
|
95
|
+
let location = gesture.location(in: pdfView)
|
|
96
|
+
guard let page = pdfView.page(for: location, nearest: true) else { return }
|
|
97
|
+
|
|
98
|
+
let pdfPoint = pdfView.convert(location, to: page)
|
|
99
|
+
let existingAnnotation = editableTextAnnotation(at: pdfPoint, on: page)
|
|
100
|
+
|
|
101
|
+
if useJsTextDialog {
|
|
102
|
+
pendingTextPage = page
|
|
103
|
+
pendingTextPoint = pdfPoint
|
|
104
|
+
pendingTextAnnotation = existingAnnotation
|
|
105
|
+
onTextInputRequested(textInputRequestPayload(page: page, point: pdfPoint, annotation: existingAnnotation))
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
presentTextAlert(for: page, at: pdfPoint, existingAnnotation: existingAnnotation)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
func provideTextInput(text: String?) {
|
|
113
|
+
guard let page = pendingTextPage, let point = pendingTextPoint else { return }
|
|
114
|
+
let annotation = pendingTextAnnotation
|
|
115
|
+
pendingTextPage = nil
|
|
116
|
+
pendingTextPoint = nil
|
|
117
|
+
pendingTextAnnotation = nil
|
|
118
|
+
guard let text, !text.isEmpty else { return }
|
|
119
|
+
upsertFreeTextAnnotation(text: text, at: point, on: page, replacing: annotation)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private func presentTextAlert(for page: PDFPage, at point: CGPoint, existingAnnotation: PDFAnnotation?) {
|
|
123
|
+
guard let viewController = findViewController() else { return }
|
|
124
|
+
let isEditing = existingAnnotation != nil
|
|
125
|
+
let alert = UIAlertController(
|
|
126
|
+
title: isEditing ? "Edit Text" : "Add Text",
|
|
127
|
+
message: nil,
|
|
128
|
+
preferredStyle: .alert
|
|
129
|
+
)
|
|
130
|
+
alert.addTextField { textField in
|
|
131
|
+
textField.placeholder = "Enter text"
|
|
132
|
+
textField.text = existingAnnotation?.contents
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
136
|
+
alert.addAction(UIAlertAction(title: isEditing ? "Update" : "Add", style: .default) { [weak self] _ in
|
|
137
|
+
guard let self, let text = alert.textFields?.first?.text, !text.isEmpty else { return }
|
|
138
|
+
upsertFreeTextAnnotation(text: text, at: point, on: page, replacing: existingAnnotation)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
viewController.present(alert, animated: true)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private func editableTextAnnotation(at point: CGPoint, on page: PDFPage) -> PDFAnnotation? {
|
|
145
|
+
page.annotations.reversed().first { annotation in
|
|
146
|
+
AnnotationSerializer.isModuleManaged(annotation) &&
|
|
147
|
+
(annotation.type == "FreeText" || annotation.type == "Text") &&
|
|
148
|
+
annotation.bounds.contains(point)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private func textInputRequestPayload(
|
|
153
|
+
page: PDFPage,
|
|
154
|
+
point: CGPoint,
|
|
155
|
+
annotation: PDFAnnotation?
|
|
156
|
+
) -> [String: Any] {
|
|
157
|
+
let pageIndex = pdfView.document?.index(for: page) ?? 0
|
|
158
|
+
var payload: [String: Any] = [
|
|
159
|
+
"mode": annotation == nil ? "create" : "edit",
|
|
160
|
+
"page": pageIndex,
|
|
161
|
+
"point": [
|
|
162
|
+
"x": point.x,
|
|
163
|
+
"y": point.y,
|
|
164
|
+
],
|
|
165
|
+
]
|
|
166
|
+
if let contents = annotation?.contents {
|
|
167
|
+
payload["currentText"] = contents
|
|
168
|
+
}
|
|
169
|
+
return payload
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private func upsertFreeTextAnnotation(
|
|
173
|
+
text: String,
|
|
174
|
+
at point: CGPoint,
|
|
175
|
+
on page: PDFPage,
|
|
176
|
+
replacing annotation: PDFAnnotation?
|
|
177
|
+
) {
|
|
178
|
+
if let annotation {
|
|
179
|
+
updateFreeTextAnnotation(annotation, text: text)
|
|
180
|
+
} else {
|
|
181
|
+
addFreeTextAnnotation(text: text, at: point, on: page)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private func addFreeTextAnnotation(text: String, at point: CGPoint, on page: PDFPage) {
|
|
186
|
+
let fontSize: CGFloat = 16.0
|
|
187
|
+
let font = UIFont(name: annotationFontFamily ?? "", size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)
|
|
188
|
+
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
189
|
+
let bounds = measuredTextBounds(text: text, font: font, at: point)
|
|
190
|
+
|
|
191
|
+
let annotation = PDFAnnotation(bounds: bounds, forType: .freeText, withProperties: nil)
|
|
192
|
+
annotation.font = font
|
|
193
|
+
annotation.fontColor = color
|
|
194
|
+
annotation.color = .clear
|
|
195
|
+
annotation.contents = text
|
|
196
|
+
|
|
197
|
+
let id = UUID().uuidString
|
|
198
|
+
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
199
|
+
page.addAnnotation(annotation)
|
|
200
|
+
emitAnnotationsChanged()
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private func updateFreeTextAnnotation(_ annotation: PDFAnnotation, text: String) {
|
|
204
|
+
let fontSize = annotation.font?.pointSize ?? 16.0
|
|
205
|
+
let font = annotation.font ??
|
|
206
|
+
UIFont(name: annotationFontFamily ?? "", size: fontSize) ??
|
|
207
|
+
UIFont.systemFont(ofSize: fontSize)
|
|
208
|
+
let bounds = measuredTextBounds(text: text, font: font, replacing: annotation.bounds)
|
|
209
|
+
|
|
210
|
+
annotation.font = font
|
|
211
|
+
annotation.bounds = bounds
|
|
212
|
+
annotation.contents = text
|
|
213
|
+
annotation.color = .clear
|
|
214
|
+
if annotation.fontColor == nil {
|
|
215
|
+
annotation.fontColor = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
emitAnnotationsChanged()
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private func measuredTextBounds(text: String, font: UIFont, at point: CGPoint) -> CGRect {
|
|
222
|
+
let textSize = measuredTextSize(text: text, font: font)
|
|
223
|
+
let padding: CGFloat = 4.0
|
|
224
|
+
return CGRect(
|
|
225
|
+
x: point.x,
|
|
226
|
+
y: point.y - textSize.height - padding,
|
|
227
|
+
width: textSize.width + padding * 2,
|
|
228
|
+
height: textSize.height + padding * 2
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private func measuredTextBounds(text: String, font: UIFont, replacing bounds: CGRect) -> CGRect {
|
|
233
|
+
let textSize = measuredTextSize(text: text, font: font)
|
|
234
|
+
let padding: CGFloat = 4.0
|
|
235
|
+
let height = textSize.height + padding * 2
|
|
236
|
+
return CGRect(
|
|
237
|
+
x: bounds.minX,
|
|
238
|
+
y: bounds.maxY - height,
|
|
239
|
+
width: textSize.width + padding * 2,
|
|
240
|
+
height: height
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private func measuredTextSize(text: String, font: UIFont) -> CGSize {
|
|
245
|
+
(text as NSString).size(withAttributes: [.font: font])
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private func findViewController() -> UIViewController? {
|
|
249
|
+
var responder: UIResponder? = self
|
|
250
|
+
while let next = responder?.next {
|
|
251
|
+
if let vc = next as? UIViewController {
|
|
252
|
+
return vc
|
|
253
|
+
}
|
|
254
|
+
responder = next
|
|
255
|
+
}
|
|
256
|
+
return nil
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -44,44 +44,53 @@ enum AnnotationSerializer {
|
|
|
44
44
|
forAnnotationKey: PDFAnnotationKey(rawValue: "_createdAt")
|
|
45
45
|
) as? Double ?? Date().timeIntervalSince1970
|
|
46
46
|
|
|
47
|
-
let type:
|
|
48
|
-
|
|
47
|
+
guard let type = modelType(for: annotation.type) else { return nil }
|
|
48
|
+
var model = AnnotationModel(
|
|
49
|
+
id: id,
|
|
50
|
+
type: type,
|
|
51
|
+
page: page,
|
|
52
|
+
color: colorHex(for: annotation, type: type),
|
|
53
|
+
createdAt: createdAt
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
populateModelDetails(&model, from: annotation)
|
|
57
|
+
|
|
58
|
+
return model
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private static func modelType(for annotationType: String?) -> String? {
|
|
62
|
+
switch annotationType {
|
|
49
63
|
case "Ink":
|
|
50
|
-
|
|
64
|
+
"ink"
|
|
51
65
|
case "Highlight":
|
|
52
|
-
|
|
66
|
+
"highlight"
|
|
53
67
|
case "Underline":
|
|
54
|
-
|
|
68
|
+
"underline"
|
|
55
69
|
case "Text":
|
|
56
|
-
|
|
70
|
+
"text"
|
|
57
71
|
case "FreeText":
|
|
58
|
-
|
|
72
|
+
"freeText"
|
|
59
73
|
default:
|
|
60
|
-
|
|
74
|
+
nil
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private static func colorHex(for annotation: PDFAnnotation, type: String) -> String {
|
|
79
|
+
if let storedHex = annotation.value(forAnnotationKey: PDFAnnotationKey(rawValue: "_color")) as? String {
|
|
80
|
+
return storedHex
|
|
61
81
|
}
|
|
62
82
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
let isFreeText = type == "text" || type == "freeText"
|
|
66
|
-
let storedHex = annotation.value(forAnnotationKey: PDFAnnotationKey(rawValue: "_color")) as? String
|
|
67
|
-
let colorHex: String
|
|
68
|
-
if let storedHex {
|
|
69
|
-
colorHex = storedHex
|
|
83
|
+
let effectiveColor: UIColor = if type == "text" || type == "freeText" {
|
|
84
|
+
annotation.fontColor ?? annotation.color ?? .black
|
|
70
85
|
} else {
|
|
71
|
-
|
|
72
|
-
? (annotation.fontColor ?? annotation.color ?? .black)
|
|
73
|
-
: (annotation.color ?? .red)
|
|
74
|
-
colorHex = hexFromColor(effectiveColor)
|
|
86
|
+
annotation.color ?? .red
|
|
75
87
|
}
|
|
76
|
-
var model = AnnotationModel(
|
|
77
|
-
id: id,
|
|
78
|
-
type: type,
|
|
79
|
-
page: page,
|
|
80
|
-
color: colorHex,
|
|
81
|
-
createdAt: createdAt
|
|
82
|
-
)
|
|
83
88
|
|
|
84
|
-
|
|
89
|
+
return hexFromColor(effectiveColor)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private static func populateModelDetails(_ model: inout AnnotationModel, from annotation: PDFAnnotation) {
|
|
93
|
+
switch model.type {
|
|
85
94
|
case "ink":
|
|
86
95
|
model.lineWidth = annotation.border?.lineWidth ?? 2.0
|
|
87
96
|
if let bezierPaths = annotation.paths {
|
|
@@ -91,7 +100,7 @@ enum AnnotationSerializer {
|
|
|
91
100
|
}
|
|
92
101
|
case "highlight", "underline":
|
|
93
102
|
model.bounds = AnnotationBounds(annotation.bounds)
|
|
94
|
-
if type == "highlight" {
|
|
103
|
+
if model.type == "highlight" {
|
|
95
104
|
model.alpha = 0.5
|
|
96
105
|
}
|
|
97
106
|
case "text", "freeText":
|
|
@@ -99,12 +108,11 @@ enum AnnotationSerializer {
|
|
|
99
108
|
model.contents = annotation.contents
|
|
100
109
|
if let font = annotation.font {
|
|
101
110
|
model.fontSize = font.pointSize
|
|
111
|
+
model.fontFamily = font.fontName
|
|
102
112
|
}
|
|
103
113
|
default:
|
|
104
114
|
break
|
|
105
115
|
}
|
|
106
|
-
|
|
107
|
-
return model
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
// MARK: - Ownership
|
|
@@ -233,18 +241,40 @@ enum AnnotationSerializer {
|
|
|
233
241
|
|
|
234
242
|
private static func createFreeTextAnnotation(model: AnnotationModel, color: UIColor) -> PDFAnnotation? {
|
|
235
243
|
guard let bounds = model.bounds else { return nil }
|
|
236
|
-
let annotation = PDFAnnotation(bounds: bounds.cgRect, forType: .freeText, withProperties: nil)
|
|
237
244
|
let fontSize = model.fontSize ?? 16.0
|
|
238
|
-
|
|
245
|
+
let font = UIFont(name: model.fontFamily ?? "", size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)
|
|
246
|
+
let contents = model.contents ?? ""
|
|
247
|
+
let annotation = PDFAnnotation(
|
|
248
|
+
bounds: normalizedFreeTextBounds(bounds: bounds.cgRect, text: contents, font: font),
|
|
249
|
+
forType: .freeText,
|
|
250
|
+
withProperties: nil
|
|
251
|
+
)
|
|
252
|
+
annotation.font = font
|
|
239
253
|
annotation.fontColor = color
|
|
240
254
|
annotation.color = .clear
|
|
241
|
-
annotation.contents =
|
|
255
|
+
annotation.contents = contents
|
|
242
256
|
|
|
243
257
|
let createdAt = model.createdAt ?? Date().timeIntervalSince1970
|
|
244
258
|
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt, colorHex: model.color)
|
|
245
259
|
return annotation
|
|
246
260
|
}
|
|
247
261
|
|
|
262
|
+
private static func normalizedFreeTextBounds(bounds: CGRect, text: String, font: UIFont) -> CGRect {
|
|
263
|
+
guard !text.isEmpty else { return bounds }
|
|
264
|
+
|
|
265
|
+
let padding: CGFloat = 4.0
|
|
266
|
+
let textSize = (text as NSString).size(withAttributes: [.font: font])
|
|
267
|
+
let width = max(bounds.width, ceil(textSize.width + padding * 2))
|
|
268
|
+
let height = max(bounds.height, ceil(textSize.height + padding * 2))
|
|
269
|
+
|
|
270
|
+
return CGRect(
|
|
271
|
+
x: bounds.minX,
|
|
272
|
+
y: bounds.maxY - height,
|
|
273
|
+
width: width,
|
|
274
|
+
height: height
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
248
278
|
private static func pointsFromBezierPath(_ path: UIBezierPath) -> [CGPoint] {
|
|
249
279
|
var points: [CGPoint] = []
|
|
250
280
|
let cgPath = path.cgPath
|
|
@@ -40,6 +40,10 @@ public class ExpoPdfMarkupModule: Module {
|
|
|
40
40
|
view.annotationLineWidth = CGFloat(width ?? 2.0)
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
Prop("annotationFontFamily") { (view: ExpoPdfMarkupView, font: String?) in
|
|
44
|
+
view.annotationFontFamily = font
|
|
45
|
+
}
|
|
46
|
+
|
|
43
47
|
Prop("useJsTextDialog") { (view: ExpoPdfMarkupView, use: Bool?) in
|
|
44
48
|
view.useJsTextDialog = use ?? false
|
|
45
49
|
}
|