@tobyt/expo-pdf-markup 0.1.0 → 0.2.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 +21 -0
- package/README.md +16 -3
- package/ios/AnnotationSerializer.swift +20 -5
- package/ios/Tests/AnnotationModelTests.swift +178 -0
- package/ios/Tests/AnnotationSerializerTests.swift +202 -0
- package/ios/Tests/ExpoPdfMarkupViewTests.swift +127 -0
- package/ios/Tests/test.pdf +0 -0
- package/package.json +7 -2
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @tobyt/expo-pdf-markup
|
|
2
|
+
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fix freeText annotation colour round-trip by storing the original hex string as a custom PDF annotation key, preventing loss when PDFKit's fontColor returns nil outside a document context
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 78be89c: Fix freeText annotation colour lost after serialisation on iOS.
|
|
14
|
+
|
|
15
|
+
PDFKit stores freeText annotation colours in `fontColor` (not `annotation.color`, which is always `.clear` for text annotations). The `toModel` serialiser was unconditionally reading `annotation.color`, causing all freeText colours to round-trip as black. The fix reads `fontColor` for `text`/`freeText` annotation types and falls back to `annotation.color` for all others.
|
|
16
|
+
|
|
17
|
+
## 0.1.0
|
|
18
|
+
|
|
19
|
+
### Minor Changes
|
|
20
|
+
|
|
21
|
+
- Initial release.
|
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# @tobyt/expo-pdf-markup
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@tobyt/expo-pdf-markup)
|
|
4
|
+
[](https://www.npmjs.com/package/@tobyt/expo-pdf-markup)
|
|
5
|
+
[](https://github.com/tobyt42/expo-pdf-markup/blob/main/LICENSE)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
3
8
|
An Expo module for displaying and annotating PDFs, with support for iOS, Android, and Web.
|
|
4
9
|
|
|
5
10
|
> **Status:** Early development — actively being tested. Web support is planned and will be added soon.
|
|
@@ -8,9 +13,9 @@ An Expo module for displaying and annotating PDFs, with support for iOS, Android
|
|
|
8
13
|
|
|
9
14
|
| Platform | PDF rendering | Annotations |
|
|
10
15
|
| -------- | ------------- | ----------- |
|
|
11
|
-
| iOS | ✅
|
|
12
|
-
| Android | ✅
|
|
13
|
-
| Web | 🔜
|
|
16
|
+
| iOS | ✅ | ✅ |
|
|
17
|
+
| Android | ✅ | ✅ |
|
|
18
|
+
| Web | 🔜 | 🔜 |
|
|
14
19
|
|
|
15
20
|
## Installation
|
|
16
21
|
|
|
@@ -49,6 +54,14 @@ cd example
|
|
|
49
54
|
npx expo start
|
|
50
55
|
```
|
|
51
56
|
|
|
57
|
+
## Core Team ✨
|
|
58
|
+
|
|
59
|
+
<table>
|
|
60
|
+
<tr>
|
|
61
|
+
<td align="center"><a href="https://github.com/tobyt42"><img src="https://avatars.githubusercontent.com/u/1678364?v=4" width="100px;" alt=""/><br /><sub><b>Toby Terhoeven</b></sub></a><br /></td>
|
|
62
|
+
</tr>
|
|
63
|
+
</table>
|
|
64
|
+
|
|
52
65
|
## License
|
|
53
66
|
|
|
54
67
|
MIT
|
|
@@ -60,7 +60,19 @@ enum AnnotationSerializer {
|
|
|
60
60
|
return nil
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
// Prefer the stored hex colour (set by this module) to avoid lossy UIColor round-trips.
|
|
64
|
+
// freeText annotations otherwise use fontColor; other types use annotation.color.
|
|
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
|
|
70
|
+
} else {
|
|
71
|
+
let effectiveColor = isFreeText
|
|
72
|
+
? (annotation.fontColor ?? annotation.color ?? .black)
|
|
73
|
+
: (annotation.color ?? .red)
|
|
74
|
+
colorHex = hexFromColor(effectiveColor)
|
|
75
|
+
}
|
|
64
76
|
var model = AnnotationModel(
|
|
65
77
|
id: id,
|
|
66
78
|
type: type,
|
|
@@ -101,10 +113,13 @@ enum AnnotationSerializer {
|
|
|
101
113
|
annotation.value(forAnnotationKey: PDFAnnotationKey(rawValue: ownershipKey)) as? Bool == true
|
|
102
114
|
}
|
|
103
115
|
|
|
104
|
-
static func tagAsModuleManaged(_ annotation: PDFAnnotation, id: String, createdAt: Double) {
|
|
116
|
+
static func tagAsModuleManaged(_ annotation: PDFAnnotation, id: String, createdAt: Double, colorHex: String? = nil) {
|
|
105
117
|
annotation.setValue(true, forAnnotationKey: PDFAnnotationKey(rawValue: ownershipKey))
|
|
106
118
|
annotation.setValue(id, forAnnotationKey: PDFAnnotationKey(rawValue: "_id"))
|
|
107
119
|
annotation.setValue(createdAt, forAnnotationKey: PDFAnnotationKey(rawValue: "_createdAt"))
|
|
120
|
+
if let colorHex {
|
|
121
|
+
annotation.setValue(colorHex, forAnnotationKey: PDFAnnotationKey(rawValue: "_color"))
|
|
122
|
+
}
|
|
108
123
|
}
|
|
109
124
|
|
|
110
125
|
// MARK: - Color helpers
|
|
@@ -197,7 +212,7 @@ enum AnnotationSerializer {
|
|
|
197
212
|
}
|
|
198
213
|
|
|
199
214
|
let createdAt = model.createdAt ?? Date().timeIntervalSince1970
|
|
200
|
-
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt)
|
|
215
|
+
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt, colorHex: model.color)
|
|
201
216
|
return annotation
|
|
202
217
|
}
|
|
203
218
|
|
|
@@ -212,7 +227,7 @@ enum AnnotationSerializer {
|
|
|
212
227
|
annotation.color = color.withAlphaComponent(alpha)
|
|
213
228
|
|
|
214
229
|
let createdAt = model.createdAt ?? Date().timeIntervalSince1970
|
|
215
|
-
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt)
|
|
230
|
+
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt, colorHex: model.color)
|
|
216
231
|
return annotation
|
|
217
232
|
}
|
|
218
233
|
|
|
@@ -226,7 +241,7 @@ enum AnnotationSerializer {
|
|
|
226
241
|
annotation.contents = model.contents ?? ""
|
|
227
242
|
|
|
228
243
|
let createdAt = model.createdAt ?? Date().timeIntervalSince1970
|
|
229
|
-
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt)
|
|
244
|
+
tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt, colorHex: model.color)
|
|
230
245
|
return annotation
|
|
231
246
|
}
|
|
232
247
|
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
@testable internal import ExpoPdfMarkup
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
final class AnnotationModelTests: XCTestCase {
|
|
5
|
+
// MARK: - Decode
|
|
6
|
+
|
|
7
|
+
func testDecodeInkAnnotation() throws {
|
|
8
|
+
let json = """
|
|
9
|
+
{
|
|
10
|
+
"version": 1,
|
|
11
|
+
"annotations": [{
|
|
12
|
+
"id": "abc-123",
|
|
13
|
+
"type": "ink",
|
|
14
|
+
"page": 0,
|
|
15
|
+
"color": "#FF0000",
|
|
16
|
+
"lineWidth": 3.0,
|
|
17
|
+
"paths": [[{"x": 10, "y": 20}, {"x": 30, "y": 40}]],
|
|
18
|
+
"createdAt": 1741340000
|
|
19
|
+
}]
|
|
20
|
+
}
|
|
21
|
+
"""
|
|
22
|
+
let data = AnnotationSerializer.deserialize(from: json)
|
|
23
|
+
XCTAssertNotNil(data)
|
|
24
|
+
XCTAssertEqual(data?.version, 1)
|
|
25
|
+
XCTAssertEqual(data?.annotations.count, 1)
|
|
26
|
+
|
|
27
|
+
let annotation = try XCTUnwrap(data?.annotations[0])
|
|
28
|
+
XCTAssertEqual(annotation.id, "abc-123")
|
|
29
|
+
XCTAssertEqual(annotation.type, "ink")
|
|
30
|
+
XCTAssertEqual(annotation.page, 0)
|
|
31
|
+
XCTAssertEqual(annotation.color, "#FF0000")
|
|
32
|
+
XCTAssertEqual(annotation.lineWidth, 3.0)
|
|
33
|
+
XCTAssertEqual(annotation.decodedPaths.count, 1)
|
|
34
|
+
XCTAssertEqual(annotation.decodedPaths[0].count, 2)
|
|
35
|
+
XCTAssertEqual(annotation.decodedPaths[0][0], AnnotationPoint(x: 10, y: 20))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func testDecodeHighlightAnnotation() throws {
|
|
39
|
+
let json = """
|
|
40
|
+
{
|
|
41
|
+
"version": 1,
|
|
42
|
+
"annotations": [{
|
|
43
|
+
"id": "def-456",
|
|
44
|
+
"type": "highlight",
|
|
45
|
+
"page": 1,
|
|
46
|
+
"color": "#FFFF00",
|
|
47
|
+
"alpha": 0.5,
|
|
48
|
+
"bounds": {"x": 72, "y": 340, "width": 200, "height": 14},
|
|
49
|
+
"createdAt": 1741340000
|
|
50
|
+
}]
|
|
51
|
+
}
|
|
52
|
+
"""
|
|
53
|
+
let data = AnnotationSerializer.deserialize(from: json)
|
|
54
|
+
XCTAssertNotNil(data)
|
|
55
|
+
let annotation = try XCTUnwrap(data?.annotations[0])
|
|
56
|
+
XCTAssertEqual(annotation.type, "highlight")
|
|
57
|
+
XCTAssertEqual(annotation.alpha, 0.5)
|
|
58
|
+
XCTAssertEqual(annotation.bounds, AnnotationBounds(x: 72, y: 340, width: 200, height: 14))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
func testDecodeUnderlineAnnotation() {
|
|
62
|
+
let json = """
|
|
63
|
+
{
|
|
64
|
+
"version": 1,
|
|
65
|
+
"annotations": [{
|
|
66
|
+
"id": "ghi-789",
|
|
67
|
+
"type": "underline",
|
|
68
|
+
"page": 0,
|
|
69
|
+
"color": "#0000FF",
|
|
70
|
+
"bounds": {"x": 50, "y": 100, "width": 150, "height": 12}
|
|
71
|
+
}]
|
|
72
|
+
}
|
|
73
|
+
"""
|
|
74
|
+
let data = AnnotationSerializer.deserialize(from: json)
|
|
75
|
+
XCTAssertNotNil(data)
|
|
76
|
+
XCTAssertEqual(data?.annotations[0].type, "underline")
|
|
77
|
+
XCTAssertNil(data?.annotations[0].alpha)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func testDecodeTextAnnotation() {
|
|
81
|
+
let json = """
|
|
82
|
+
{
|
|
83
|
+
"version": 1,
|
|
84
|
+
"annotations": [{
|
|
85
|
+
"id": "txt-001",
|
|
86
|
+
"type": "text",
|
|
87
|
+
"page": 2,
|
|
88
|
+
"color": "#00FF00",
|
|
89
|
+
"bounds": {"x": 100, "y": 200, "width": 24, "height": 24},
|
|
90
|
+
"contents": "My note"
|
|
91
|
+
}]
|
|
92
|
+
}
|
|
93
|
+
"""
|
|
94
|
+
let data = AnnotationSerializer.deserialize(from: json)
|
|
95
|
+
XCTAssertNotNil(data)
|
|
96
|
+
XCTAssertEqual(data?.annotations[0].type, "text")
|
|
97
|
+
XCTAssertEqual(data?.annotations[0].contents, "My note")
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
func testDecodeMixedAnnotations() {
|
|
101
|
+
let json = """
|
|
102
|
+
{
|
|
103
|
+
"version": 1,
|
|
104
|
+
"annotations": [
|
|
105
|
+
{"id": "1", "type": "ink", "page": 0, "color": "#FF0000", "lineWidth": 2,
|
|
106
|
+
"paths": [[{"x": 0, "y": 0}]]},
|
|
107
|
+
{"id": "2", "type": "highlight", "page": 0, "color": "#FFFF00",
|
|
108
|
+
"bounds": {"x": 0, "y": 0, "width": 100, "height": 10}},
|
|
109
|
+
{"id": "3", "type": "text", "page": 1, "color": "#00FF00",
|
|
110
|
+
"bounds": {"x": 50, "y": 50, "width": 24, "height": 24}, "contents": "Hi"}
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
"""
|
|
114
|
+
let data = AnnotationSerializer.deserialize(from: json)
|
|
115
|
+
XCTAssertNotNil(data)
|
|
116
|
+
XCTAssertEqual(data?.annotations.count, 3)
|
|
117
|
+
XCTAssertEqual(data?.annotations[0].type, "ink")
|
|
118
|
+
XCTAssertEqual(data?.annotations[1].type, "highlight")
|
|
119
|
+
XCTAssertEqual(data?.annotations[2].type, "text")
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
func testDecodeInvalidJSON() {
|
|
123
|
+
let data = AnnotationSerializer.deserialize(from: "not json at all")
|
|
124
|
+
XCTAssertNil(data)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
func testDecodeEmptyAnnotations() {
|
|
128
|
+
let json = """
|
|
129
|
+
{"version": 1, "annotations": []}
|
|
130
|
+
"""
|
|
131
|
+
let data = AnnotationSerializer.deserialize(from: json)
|
|
132
|
+
XCTAssertNotNil(data)
|
|
133
|
+
XCTAssertEqual(data?.annotations.count, 0)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// MARK: - Round-trip
|
|
137
|
+
|
|
138
|
+
func testEncodeDecodeRoundTrip() {
|
|
139
|
+
let original = AnnotationsData(
|
|
140
|
+
version: 1,
|
|
141
|
+
annotations: [
|
|
142
|
+
AnnotationModel(
|
|
143
|
+
id: "rt-1",
|
|
144
|
+
type: "ink",
|
|
145
|
+
page: 0,
|
|
146
|
+
color: "#FF0000",
|
|
147
|
+
lineWidth: 2.5,
|
|
148
|
+
paths: [[["x": 10, "y": 20], ["x": 30, "y": 40]]],
|
|
149
|
+
createdAt: 1_741_340_000
|
|
150
|
+
),
|
|
151
|
+
AnnotationModel(
|
|
152
|
+
id: "rt-2",
|
|
153
|
+
type: "highlight",
|
|
154
|
+
page: 1,
|
|
155
|
+
color: "#FFFF00",
|
|
156
|
+
alpha: 0.5,
|
|
157
|
+
bounds: AnnotationBounds(x: 72, y: 340, width: 200, height: 14),
|
|
158
|
+
createdAt: 1_741_340_000
|
|
159
|
+
),
|
|
160
|
+
]
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
guard let json = AnnotationSerializer.serialize(original) else {
|
|
164
|
+
XCTFail("Failed to serialize")
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
guard let decoded = AnnotationSerializer.deserialize(from: json) else {
|
|
169
|
+
XCTFail("Failed to deserialize")
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
XCTAssertEqual(decoded.version, original.version)
|
|
174
|
+
XCTAssertEqual(decoded.annotations.count, original.annotations.count)
|
|
175
|
+
XCTAssertEqual(decoded.annotations[0].id, "rt-1")
|
|
176
|
+
XCTAssertEqual(decoded.annotations[1].id, "rt-2")
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
@testable internal import ExpoPdfMarkup
|
|
2
|
+
import PDFKit
|
|
3
|
+
import XCTest
|
|
4
|
+
|
|
5
|
+
final class AnnotationSerializerTests: XCTestCase {
|
|
6
|
+
// MARK: - PDFAnnotation creation
|
|
7
|
+
|
|
8
|
+
func testCreateInkAnnotation() throws {
|
|
9
|
+
let model = AnnotationModel(
|
|
10
|
+
id: "ink-1",
|
|
11
|
+
type: "ink",
|
|
12
|
+
page: 0,
|
|
13
|
+
color: "#FF0000",
|
|
14
|
+
lineWidth: 3.0,
|
|
15
|
+
paths: [[["x": 10, "y": 20], ["x": 30, "y": 40]]],
|
|
16
|
+
createdAt: 1_741_340_000
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
20
|
+
XCTAssertNotNil(annotation)
|
|
21
|
+
XCTAssertEqual(annotation?.type, "Ink")
|
|
22
|
+
XCTAssertEqual(annotation?.border?.lineWidth, 3.0)
|
|
23
|
+
XCTAssertTrue(try AnnotationSerializer.isModuleManaged(XCTUnwrap(annotation)))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
func testCreateHighlightAnnotation() {
|
|
27
|
+
let model = AnnotationModel(
|
|
28
|
+
id: "hl-1",
|
|
29
|
+
type: "highlight",
|
|
30
|
+
page: 0,
|
|
31
|
+
color: "#FFFF00",
|
|
32
|
+
alpha: 0.5,
|
|
33
|
+
bounds: AnnotationBounds(x: 72, y: 340, width: 200, height: 14),
|
|
34
|
+
createdAt: 1_741_340_000
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
38
|
+
XCTAssertNotNil(annotation)
|
|
39
|
+
XCTAssertEqual(annotation?.type, "Highlight")
|
|
40
|
+
XCTAssertEqual(annotation?.bounds, CGRect(x: 72, y: 340, width: 200, height: 14))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
func testCreateUnderlineAnnotation() {
|
|
44
|
+
let model = AnnotationModel(
|
|
45
|
+
id: "ul-1",
|
|
46
|
+
type: "underline",
|
|
47
|
+
page: 0,
|
|
48
|
+
color: "#0000FF",
|
|
49
|
+
bounds: AnnotationBounds(x: 50, y: 100, width: 150, height: 12)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
53
|
+
XCTAssertNotNil(annotation)
|
|
54
|
+
XCTAssertEqual(annotation?.type, "Underline")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
func testCreateFreeTextAnnotation() {
|
|
58
|
+
let model = AnnotationModel(
|
|
59
|
+
id: "txt-1",
|
|
60
|
+
type: "freeText",
|
|
61
|
+
page: 0,
|
|
62
|
+
color: "#00FF00",
|
|
63
|
+
bounds: AnnotationBounds(x: 100, y: 200, width: 200, height: 24),
|
|
64
|
+
contents: "My note",
|
|
65
|
+
fontSize: 16.0
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
69
|
+
XCTAssertNotNil(annotation)
|
|
70
|
+
XCTAssertEqual(annotation?.type, "FreeText")
|
|
71
|
+
XCTAssertEqual(annotation?.contents, "My note")
|
|
72
|
+
XCTAssertEqual(annotation?.font?.pointSize, 16.0)
|
|
73
|
+
XCTAssertEqual(annotation?.color, .clear)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func testUnknownTypeReturnsNil() {
|
|
77
|
+
let model = AnnotationModel(
|
|
78
|
+
id: "bad-1",
|
|
79
|
+
type: "unknown",
|
|
80
|
+
page: 0,
|
|
81
|
+
color: "#000000"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
85
|
+
XCTAssertNil(annotation)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// MARK: - Model extraction
|
|
89
|
+
|
|
90
|
+
func testExtractModelFromModuleManagedAnnotation() {
|
|
91
|
+
let model = AnnotationModel(
|
|
92
|
+
id: "ext-1",
|
|
93
|
+
type: "ink",
|
|
94
|
+
page: 0,
|
|
95
|
+
color: "#FF0000",
|
|
96
|
+
lineWidth: 2.0,
|
|
97
|
+
paths: [[["x": 10, "y": 20], ["x": 30, "y": 40]]],
|
|
98
|
+
createdAt: 1_741_340_000
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
let pdfAnnotation = try XCTUnwrap(AnnotationSerializer.toPDFAnnotation(model))
|
|
102
|
+
let extracted = AnnotationSerializer.toModel(from: pdfAnnotation, page: 0)
|
|
103
|
+
|
|
104
|
+
XCTAssertNotNil(extracted)
|
|
105
|
+
let result = try XCTUnwrap(extracted)
|
|
106
|
+
XCTAssertEqual(result.id, "ext-1")
|
|
107
|
+
XCTAssertEqual(result.type, "ink")
|
|
108
|
+
XCTAssertEqual(result.page, 0)
|
|
109
|
+
XCTAssertEqual(result.color, "#FF0000")
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
func testFreeTextColorRoundTrip() {
|
|
113
|
+
let model = AnnotationModel(
|
|
114
|
+
id: "ft-color-1",
|
|
115
|
+
type: "freeText",
|
|
116
|
+
page: 0,
|
|
117
|
+
color: "#FF0000",
|
|
118
|
+
bounds: AnnotationBounds(x: 10, y: 10, width: 100, height: 20),
|
|
119
|
+
contents: "Red text",
|
|
120
|
+
fontSize: 14.0,
|
|
121
|
+
createdAt: 1_741_340_000
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
let pdfAnnotation = try XCTUnwrap(AnnotationSerializer.toPDFAnnotation(model))
|
|
125
|
+
let extracted = AnnotationSerializer.toModel(from: pdfAnnotation, page: 0)
|
|
126
|
+
|
|
127
|
+
XCTAssertNotNil(extracted)
|
|
128
|
+
let result = try XCTUnwrap(extracted)
|
|
129
|
+
XCTAssertEqual(result.color, "#FF0000", "freeText color must survive a serialisation round-trip")
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
func testExtractModelFromNonManagedAnnotationReturnsNil() {
|
|
133
|
+
let annotation = PDFAnnotation(
|
|
134
|
+
bounds: CGRect(x: 0, y: 0, width: 100, height: 10),
|
|
135
|
+
forType: .highlight,
|
|
136
|
+
withProperties: nil
|
|
137
|
+
)
|
|
138
|
+
let extracted = AnnotationSerializer.toModel(from: annotation, page: 0)
|
|
139
|
+
XCTAssertNil(extracted)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// MARK: - Ownership filtering
|
|
143
|
+
|
|
144
|
+
func testOwnershipFiltering() {
|
|
145
|
+
let managed = PDFAnnotation(
|
|
146
|
+
bounds: CGRect(x: 0, y: 0, width: 100, height: 10),
|
|
147
|
+
forType: .highlight,
|
|
148
|
+
withProperties: nil
|
|
149
|
+
)
|
|
150
|
+
AnnotationSerializer.tagAsModuleManaged(managed, id: "owned-1", createdAt: 1_741_340_000)
|
|
151
|
+
|
|
152
|
+
let embedded = PDFAnnotation(
|
|
153
|
+
bounds: CGRect(x: 0, y: 0, width: 100, height: 10),
|
|
154
|
+
forType: .highlight,
|
|
155
|
+
withProperties: nil
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
XCTAssertTrue(AnnotationSerializer.isModuleManaged(managed))
|
|
159
|
+
XCTAssertFalse(AnnotationSerializer.isModuleManaged(embedded))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// MARK: - Color helpers
|
|
163
|
+
|
|
164
|
+
func testColorFromHex() {
|
|
165
|
+
let red = try XCTUnwrap(AnnotationSerializer.colorFromHex("#FF0000"))
|
|
166
|
+
|
|
167
|
+
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
168
|
+
red.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
169
|
+
XCTAssertEqual(r, 1.0, accuracy: 0.01)
|
|
170
|
+
XCTAssertEqual(g, 0.0, accuracy: 0.01)
|
|
171
|
+
XCTAssertEqual(b, 0.0, accuracy: 0.01)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
func testColorFromHexWithoutHash() {
|
|
175
|
+
let blue = try XCTUnwrap(AnnotationSerializer.colorFromHex("0000FF"))
|
|
176
|
+
|
|
177
|
+
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
178
|
+
blue.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
179
|
+
XCTAssertEqual(b, 1.0, accuracy: 0.01)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
func testColorFromInvalidHex() {
|
|
183
|
+
XCTAssertNil(AnnotationSerializer.colorFromHex("xyz"))
|
|
184
|
+
XCTAssertNil(AnnotationSerializer.colorFromHex("#GG0000"))
|
|
185
|
+
XCTAssertNil(AnnotationSerializer.colorFromHex(""))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
func testHexFromColor() {
|
|
189
|
+
let hex = AnnotationSerializer.hexFromColor(.red)
|
|
190
|
+
XCTAssertEqual(hex, "#FF0000")
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
func testHexRoundTrip() {
|
|
194
|
+
let original = "#3366CC"
|
|
195
|
+
guard let color = AnnotationSerializer.colorFromHex(original) else {
|
|
196
|
+
XCTFail("Failed to parse hex")
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
let result = AnnotationSerializer.hexFromColor(color)
|
|
200
|
+
XCTAssertEqual(result, original)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
@testable internal import ExpoPdfMarkup
|
|
2
|
+
import PDFKit
|
|
3
|
+
import XCTest
|
|
4
|
+
|
|
5
|
+
final class ExpoPdfMarkupViewTests: XCTestCase {
|
|
6
|
+
var view: ExpoPdfMarkupView!
|
|
7
|
+
|
|
8
|
+
override func setUp() {
|
|
9
|
+
super.setUp()
|
|
10
|
+
view = ExpoPdfMarkupView(appContext: nil)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override func tearDown() {
|
|
14
|
+
view = nil
|
|
15
|
+
super.tearDown()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// MARK: - Initial state
|
|
19
|
+
|
|
20
|
+
func testInitialState() {
|
|
21
|
+
XCTAssertNotNil(view)
|
|
22
|
+
XCTAssertTrue(view.clipsToBounds)
|
|
23
|
+
XCTAssertNil(view.pdfView.document)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// MARK: - loadPdf
|
|
27
|
+
|
|
28
|
+
func testLoadPdfWithValidFile() throws {
|
|
29
|
+
let bundle = Bundle(for: type(of: self))
|
|
30
|
+
guard let pdfURL = bundle.url(forResource: "test", withExtension: "pdf") else {
|
|
31
|
+
XCTFail("test.pdf not found in test bundle")
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
view.loadPdf(from: pdfURL.path)
|
|
36
|
+
XCTAssertNotNil(view.pdfView.document)
|
|
37
|
+
XCTAssertGreaterThan(try XCTUnwrap(view.pdfView.document?.pageCount), 0)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func testLoadPdfWithInvalidPath() {
|
|
41
|
+
view.loadPdf(from: "/nonexistent/path.pdf")
|
|
42
|
+
XCTAssertNil(view.pdfView.document)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
func testLoadPdfSkipsDuplicateSource() {
|
|
46
|
+
let bundle = Bundle(for: type(of: self))
|
|
47
|
+
guard let pdfURL = bundle.url(forResource: "test", withExtension: "pdf") else {
|
|
48
|
+
XCTFail("test.pdf not found in test bundle")
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
view.loadPdf(from: pdfURL.path)
|
|
53
|
+
let firstDoc = view.pdfView.document
|
|
54
|
+
|
|
55
|
+
// Loading same source again should be a no-op (same document instance)
|
|
56
|
+
view.loadPdf(from: pdfURL.path)
|
|
57
|
+
XCTAssertTrue(view.pdfView.document === firstDoc)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// MARK: - goToPage
|
|
61
|
+
|
|
62
|
+
func testGoToPageWithNoDocument() {
|
|
63
|
+
// Should not crash when no document is loaded
|
|
64
|
+
view.goToPage(0)
|
|
65
|
+
view.goToPage(5)
|
|
66
|
+
view.goToPage(-1)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
func testGoToPageWithValidIndex() {
|
|
70
|
+
let bundle = Bundle(for: type(of: self))
|
|
71
|
+
guard let pdfURL = bundle.url(forResource: "test", withExtension: "pdf"),
|
|
72
|
+
let doc = PDFDocument(url: pdfURL),
|
|
73
|
+
doc.pageCount > 0
|
|
74
|
+
else {
|
|
75
|
+
XCTFail("test.pdf not found or empty")
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
view.loadPdf(from: pdfURL.path)
|
|
80
|
+
view.goToPage(0)
|
|
81
|
+
|
|
82
|
+
let currentPage = view.pdfView.currentPage
|
|
83
|
+
XCTAssertNotNil(currentPage)
|
|
84
|
+
if let current = currentPage {
|
|
85
|
+
XCTAssertEqual(view.pdfView.document?.index(for: current), 0)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func testGoToPageRejectsNegativeIndex() {
|
|
90
|
+
let bundle = Bundle(for: type(of: self))
|
|
91
|
+
guard let pdfURL = bundle.url(forResource: "test", withExtension: "pdf") else {
|
|
92
|
+
XCTFail("test.pdf not found in test bundle")
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
view.loadPdf(from: pdfURL.path)
|
|
97
|
+
let pageBefore = view.pdfView.currentPage
|
|
98
|
+
|
|
99
|
+
view.goToPage(-1)
|
|
100
|
+
// Page should not change
|
|
101
|
+
XCTAssertEqual(view.pdfView.currentPage, pageBefore)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func testGoToPageRejectsOutOfBoundsIndex() throws {
|
|
105
|
+
let bundle = Bundle(for: type(of: self))
|
|
106
|
+
guard let pdfURL = bundle.url(forResource: "test", withExtension: "pdf") else {
|
|
107
|
+
XCTFail("test.pdf not found in test bundle")
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
view.loadPdf(from: pdfURL.path)
|
|
112
|
+
let pageCount = try XCTUnwrap(view.pdfView.document?.pageCount)
|
|
113
|
+
let pageBefore = view.pdfView.currentPage
|
|
114
|
+
|
|
115
|
+
view.goToPage(pageCount + 10)
|
|
116
|
+
XCTAssertEqual(view.pdfView.currentPage, pageBefore)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// MARK: - layoutSubviews
|
|
120
|
+
|
|
121
|
+
func testLayoutSubviewsSizesPdfView() {
|
|
122
|
+
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
|
|
123
|
+
view.layoutSubviews()
|
|
124
|
+
|
|
125
|
+
XCTAssertEqual(view.pdfView.frame, view.bounds)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tobyt/expo-pdf-markup",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "An Expo module for displaying and annotating PDFs",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -29,7 +29,11 @@
|
|
|
29
29
|
"expo-module": "expo-module",
|
|
30
30
|
"open:ios": "xed example/ios",
|
|
31
31
|
"open:android": "open -a \"Android Studio\" example/android",
|
|
32
|
-
"docs": "typedoc"
|
|
32
|
+
"docs": "typedoc",
|
|
33
|
+
"changeset": "changeset",
|
|
34
|
+
"changeset:add": "changeset add",
|
|
35
|
+
"changeset:version": "changeset version",
|
|
36
|
+
"release": "bash scripts/release.sh"
|
|
33
37
|
},
|
|
34
38
|
"keywords": [
|
|
35
39
|
"react-native",
|
|
@@ -48,6 +52,7 @@
|
|
|
48
52
|
"access": "public"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
55
|
+
"@changesets/cli": "^2.30.0",
|
|
51
56
|
"@types/react": "~19.2.10",
|
|
52
57
|
"@types/react-test-renderer": "^19.1.0",
|
|
53
58
|
"expo": "^55.0.5",
|