forked from gerhardsletten/react-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.tsx
More file actions
135 lines (130 loc) · 4.5 KB
/
Selection.tsx
File metadata and controls
135 lines (130 loc) · 4.5 KB
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
132
133
134
135
import { useState, useRef, useEffect, useCallback } from 'react'
import { ReactReader } from '../../lib/index'
import type { Contents, Rendition } from 'epubjs'
import { DEMO_URL, DEMO_NAME } from '../components/config'
import { Example } from '../components/Example'
type ITextSelection = {
text: string
cfiRange: string
}
/*
This example are trying out dynamic change color of epubjs hightlight, but its not working..
*/
export const Selection = () => {
const [selections, setSelections] = useState<ITextSelection[]>([])
const [rendition, setRendition] = useState<Rendition | undefined>(undefined)
const [location, setLocation] = useState<string | number>(0)
const [color, setColor] = useState('red')
useEffect(() => {
console.log('effect', color)
if (rendition) {
/*
rendition.annotations.each().forEach((annotation) => {
annotation.style = { fill: color }
})*/
console.log('set color', rendition)
}
}, [rendition, color])
useEffect(() => {
if (rendition) {
function setRenderSelection(cfiRange: string, contents: Contents) {
if (rendition) {
setSelections((list) =>
list.concat({
text: rendition.getRange(cfiRange).toString(),
cfiRange,
})
)
rendition.annotations.add(
'highlight',
cfiRange,
{},
(e: MouseEvent) => console.log('click on selection', cfiRange, e),
'my-class',
{ fill: color }
)
console.log('rendition', { rendition, contents })
const selection = contents.window.getSelection()
selection?.removeAllRanges()
}
}
rendition.on('selected', setRenderSelection)
return () => {
rendition?.off('selected', setRenderSelection)
}
}
}, [setSelections, rendition])
return (
<Example
title="Selection example"
above={
<div className="border border-stone-400 bg-white min-h-[100px] p-2 rounded">
<h2 className="font-bold mb-1">Selections</h2>
<ul className="grid grid-cols-1 divide-y divide-stone-400 border-t border-stone-400 -mx-2">
{selections.map(({ text, cfiRange }, i) => (
<li key={i} className="p-2">
<span>{text}</span>
<button
className="underline hover:no-underline text-sm mx-1"
onClick={() => {
rendition?.display(cfiRange)
}}
>
Show
</button>
<button
className="underline hover:no-underline text-sm mx-1"
onClick={() => {
rendition?.annotations.remove(cfiRange, 'highlight')
setSelections(selections.filter((item, j) => j !== i))
}}
>
Remove
</button>
</li>
))}
</ul>
<div className="flex items-center gap-2 flex-wrap mt-2">
{['red', 'pink'].map((item) => (
<button
key={item}
onClick={() => setColor(item)}
className="flex items-center size-10 rounded-full bg-(--color) border-2 border-transparent aria-[current]:border-black"
aria-current={item === color ? 'true' : undefined}
aria-label={`Change color to ${item}`}
style={{ '--color': item } as React.CSSProperties}
></button>
))}
</div>
</div>
}
>
<ReactReader
url={DEMO_URL}
title={DEMO_NAME}
location={location}
locationChanged={(loc: string) => setLocation(loc)}
getRendition={(_rendition: Rendition) => {
setRendition(_rendition)
_rendition.hooks.content.register((contents: Contents) => {
const document = contents.window.document
console.log('document', document)
if (document) {
const css = `
.epubjs-hl {
border: 1px solid black;
}
p {
border: 1px solid red;
}
`
const style = document.createElement('style')
style.appendChild(document.createTextNode(css))
document.head.appendChild(style)
}
})
}}
/>
</Example>
)
}