1
1
import * as React from 'react' ;
2
+ import { useCallback } from 'react' ;
3
+ import { useEffect } from 'react' ;
2
4
import { useDispatch , useSelector } from 'react-redux' ;
3
5
import { categories } from '../Common' ;
4
6
import { setStateFromClipboard , State } from '../reducer' ;
@@ -9,6 +11,28 @@ const CopyPasteSelection = (): JSX.Element =>
9
11
const dispatch = useDispatch ( ) ;
10
12
const state = useSelector < State , State > ( state => state ) ;
11
13
14
+ const performPaste = useCallback ( ( text : string ) => {
15
+ try {
16
+ const jsonState = JSON . parse ( text ) ;
17
+ dispatch ( setStateFromClipboard ( jsonState ) ) ;
18
+ } catch ( e ) {
19
+ alert ( "Sorry - couldn't parse pasted selection" ) ;
20
+ console . log ( "pasting state failed" , e ) ;
21
+ }
22
+ } , [ dispatch ] ) ;
23
+
24
+ useEffect ( ( ) => {
25
+ const eh = ( e : ClipboardEvent ) => {
26
+ e . preventDefault ( ) ;
27
+ const text = e . clipboardData ?. getData ( 'text/plain' ) ;
28
+ if ( text ) performPaste ( text ) ;
29
+ } ;
30
+ document . addEventListener ( "paste" , eh ) ;
31
+ return ( ) => {
32
+ document . removeEventListener ( "paste" , eh ) ;
33
+ }
34
+ } , [ performPaste ] ) ;
35
+
12
36
const copy = ( ) => {
13
37
const serializedState = {
14
38
frameworks : state . frameworks . filter ( f => state . selectedFrameworksDropDown . has ( f ) ) . map ( f => f . dir ) ,
@@ -20,26 +44,25 @@ const CopyPasteSelection = (): JSX.Element =>
20
44
navigator . clipboard . writeText ( json ) ;
21
45
window . location . hash = btoa ( json ) ;
22
46
} ;
23
- const paste = async ( ) => {
47
+ const paste = useCallback ( async ( ) => {
24
48
try {
25
49
const text = await navigator . clipboard . readText ( ) ;
26
- const jsonState = JSON . parse ( text ) ;
27
- dispatch ( setStateFromClipboard ( jsonState ) ) ;
50
+ performPaste ( text ) ;
28
51
} catch ( e ) {
29
52
alert ( "Sorry - couldn't parse pasted selection" ) ;
30
53
console . log ( "pasting state failed" , e ) ;
31
54
}
32
- } ;
55
+ } , [ performPaste ] ) ;
33
56
return < >
34
57
< p > Copy/paste current selection</ p >
35
58
< div className = "hspan" />
36
59
< button className = 'iconbutton' onClick = { copy } >
37
60
{ /* svg from https://ionic.io/ionicons */ }
38
- < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > < title > Copy</ title > < rect x = "128" y = "128" width = "336" height = "336" rx = "57" ry = "57" fill = "none" stroke = "currentColor" strokeLinejoin = "round" strokeWidth = "32" /> < path d = "M383.5 128l.5-24a56.16 56.16 0 00-56-56H112a64.19 64.19 0 00-64 64v216a56.16 56.16 0 0056 56h24" fill = "none" stroke = "currentColor" strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "32" /> </ svg >
61
+ < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > < title > Copy selected frameworks and benchmarks </ title > < rect x = "128" y = "128" width = "336" height = "336" rx = "57" ry = "57" fill = "none" stroke = "currentColor" strokeLinejoin = "round" strokeWidth = "32" /> < path d = "M383.5 128l.5-24a56.16 56.16 0 00-56-56H112a64.19 64.19 0 00-64 64v216a56.16 56.16 0 0056 56h24" fill = "none" stroke = "currentColor" strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "32" /> </ svg >
39
62
</ button >
40
63
< button className = 'iconbutton' onClick = { paste } >
41
64
{ /* svg from https://ionic.io/ionicons */ }
42
- < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > < title > Clipboard </ title > < path d = "M336 64h32a48 48 0 0148 48v320a48 48 0 01-48 48H144a48 48 0 01-48-48V112a48 48 0 0148-48h32" fill = "none" stroke = "currentColor" strokeLinejoin = "round" strokeWidth = "32" /> < rect x = "176" y = "32" width = "160" height = "64" rx = "26.13" ry = "26.13" fill = "none" stroke = "currentColor" strokeLinejoin = "round" strokeWidth = "32" /> </ svg >
65
+ < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > < title > Paste selected items (or use ctrl/cmd + v for firefox) </ title > < path d = "M336 64h32a48 48 0 0148 48v320a48 48 0 01-48 48H144a48 48 0 01-48-48V112a48 48 0 0148-48h32" fill = "none" stroke = "currentColor" strokeLinejoin = "round" strokeWidth = "32" /> < rect x = "176" y = "32" width = "160" height = "64" rx = "26.13" ry = "26.13" fill = "none" stroke = "currentColor" strokeLinejoin = "round" strokeWidth = "32" /> </ svg >
43
66
</ button >
44
67
</ >
45
68
}
0 commit comments