-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathindex.tsx
131 lines (126 loc) · 4.17 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as React from 'react';
import { useWindowDimensions } from 'react-native';
import {
Annotation,
Reader,
ReaderProvider,
useReader,
} from '@epubjs-react-native/core';
import { useFileSystem } from '@epubjs-react-native/expo-file-system';
import { BottomSheetModal } from '@gorhom/bottom-sheet';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { COLORS } from './AnnotationForm';
import { AnnotationsList } from './AnnotationsList';
import { Selection } from './utils';
function Book() {
const { width, height } = useWindowDimensions();
const { addAnnotation, removeAnnotation, annotations } = useReader();
const [selection, setSelection] = React.useState<Selection | null>(null);
const [selectedAnnotation, setSelectedAnnotation] = React.useState<
Annotation | undefined
>(undefined);
const [tempMark, setTempMark] = React.useState<Annotation | null>(null);
const annotationsListRef = React.useRef<BottomSheetModal>(null);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<Reader
src="https://s3.amazonaws.com/moby-dick/OPS/package.opf"
width={width}
height={height * 0.85}
fileSystem={useFileSystem}
initialLocation="introduction_001.xhtml"
initialAnnotations={[
// Chapter 1
{
cfiRange: 'epubcfi(/6/10!/4/2/4,/1:0,/1:319)',
data: {},
sectionIndex: 4,
styles: { color: '#23CE6B' },
cfiRangeText:
'The pale Usher—threadbare in coat, heart, body, and brain; I see him now. He was ever dusting his old lexicons and grammars, with a queer handkerchief, mockingly embellished with all the gay flags of all the known nations of the world. He loved to dust his old grammars; it somehow mildly reminded him of his mortality.',
type: 'highlight',
},
// Chapter 5
{
cfiRange: 'epubcfi(/6/22!/4/2/4,/1:80,/1:88)',
data: {},
sectionIndex: 3,
styles: { color: '#CBA135' },
cfiRangeText: 'landlord',
type: 'highlight',
},
]}
onAddAnnotation={(annotation) => {
if (annotation.type === 'highlight' && annotation.data?.isTemp) {
setTempMark(annotation);
}
}}
onPressAnnotation={(annotation) => {
setSelectedAnnotation(annotation);
annotationsListRef.current?.present();
}}
onChangeAnnotations={(annotation) => {
console.log('onChangeAnnotations', annotation);
}}
menuItems={[
{
label: '🟡',
action: (cfiRange) => {
addAnnotation('highlight', cfiRange, undefined, {
color: COLORS[2],
});
return true;
},
},
{
label: '🔴',
action: (cfiRange) => {
addAnnotation('highlight', cfiRange, undefined, {
color: COLORS[0],
});
return true;
},
},
{
label: '🟢',
action: (cfiRange) => {
addAnnotation('highlight', cfiRange, undefined, {
color: COLORS[3],
});
return true;
},
},
{
label: 'Add Note',
action: (cfiRange, text) => {
setSelection({ cfiRange, text });
addAnnotation('highlight', cfiRange, { isTemp: true });
annotationsListRef.current?.present();
return true;
},
},
]}
/>
<AnnotationsList
ref={annotationsListRef}
selection={selection}
selectedAnnotation={selectedAnnotation}
annotations={annotations}
onClose={() => {
setTempMark(null);
setSelection(null);
setSelectedAnnotation(undefined);
if (tempMark) removeAnnotation(tempMark);
annotationsListRef.current?.dismiss();
}}
/>
</GestureHandlerRootView>
);
}
export function Annotations() {
return (
<ReaderProvider>
<Book />
</ReaderProvider>
);
}