1
+ import React , {
2
+ useState ,
3
+ useRef ,
4
+ useEffect ,
5
+ CSSProperties
6
+ } from 'react' ;
1
7
import useDocusaurusContext from '@docusaurus/useDocusaurusContext' ;
2
8
import useScripts from '@site/src/hooks/useScripts' ;
3
9
import Head from '@docusaurus/Head' ;
@@ -16,12 +22,50 @@ const example_code = `(define re #/<h1>([^>]+)<\\/h1>/)
16
22
(loop (- i 1))))
17
23
` ;
18
24
25
+ const WIDTH_MIN = 50 ;
26
+
19
27
export default function ScreenShotBox ( ) : JSX . Element {
28
+ const dragging = useRef < boolean > ( false ) ;
29
+ const dialogRef = useRef < HTMLDivElement > ( null ) ;
30
+ const boxRef = useRef < HTMLDivElement > ( null ) ;
31
+ const resizerRef = useRef < HTMLDivElement > ( null ) ;
32
+
20
33
const { siteConfig } = useDocusaurusContext ( ) ;
34
+
21
35
useScripts ( [
22
36
'https://cdn.jsdelivr.net/combine/npm/codemirror@5.58.3/lib/codemirror.js,npm/codemirror@5.58.3/mode/javascript/javascript.js,npm/codemirror@5.58.3/addon/edit/matchbrackets.js,gh/jcubic/lips@devel/lib/js/codemirror.js' ,
23
37
`${ siteConfig . baseUrl } js/screenshooter.js`
24
38
] ) ;
39
+
40
+ useEffect ( ( ) => {
41
+ function mouseMove ( event : MouseEvent ) {
42
+ if ( dragging . current ) {
43
+ const screenX = event . screenX ;
44
+ const rect = resizerRef . current . getBoundingClientRect ( ) ;
45
+ let diff = screenX - rect . left ;
46
+ let width = dialogRef . current . clientWidth + diff ;
47
+ if ( width < WIDTH_MIN ) {
48
+ width = WIDTH_MIN + 1 ;
49
+ }
50
+ boxRef . current . style . setProperty ( '--width' , `${ width } px` ) ;
51
+ }
52
+ }
53
+ function mouseUp ( ) {
54
+ dragging . current = false ;
55
+ }
56
+ document . addEventListener ( 'mousemove' , mouseMove ) ;
57
+ document . addEventListener ( 'mouseup' , mouseUp ) ;
58
+ return ( ) => {
59
+ document . removeEventListener ( 'mousemove' , mouseMove ) ;
60
+ document . removeEventListener ( 'mouseup' , mouseUp ) ;
61
+ }
62
+ } , [ ] ) ;
63
+
64
+
65
+ function dragStart ( ) {
66
+ dragging . current = true ;
67
+ }
68
+
25
69
return (
26
70
< >
27
71
< Head >
@@ -30,18 +74,21 @@ export default function ScreenShotBox(): JSX.Element {
30
74
< link href = "https://cdn.jsdelivr.net/npm/codemirror@5.58.3/addon/hint/show-hint.css" rel = "stylesheet" />
31
75
< script src = "https://cdn.jsdelivr.net/npm/html-to-image@1.10.4/dist/html-to-image.js" > </ script >
32
76
</ Head >
33
- < div className = "box" >
77
+ < div className = "box" ref = { boxRef } >
34
78
< pre className = "hidden" > { example_code } </ pre >
35
79
< div className = "wrapper" >
36
- < div className = "cm-dialog" >
37
- < header >
38
- < ul className = "cm-icons" >
39
- < li > </ li >
40
- < li > </ li >
41
- < li > </ li >
42
- </ ul >
43
- </ header >
44
- < div className = "cm-body" > </ div >
80
+ < div className = "resizable" >
81
+ < div className = "cm-dialog" ref = { dialogRef } >
82
+ < header >
83
+ < ul className = "cm-icons" >
84
+ < li > </ li >
85
+ < li > </ li >
86
+ < li > </ li >
87
+ </ ul >
88
+ </ header >
89
+ < div className = "cm-body" > </ div >
90
+ </ div >
91
+ < div className = "resizer" ref = { resizerRef } onMouseDown = { dragStart } > </ div >
45
92
</ div >
46
93
</ div >
47
94
< footer >
0 commit comments