Skip to content

Commit 52f3af5

Browse files
committed
(docs) add resizing to screenshooter jcubic#438
1 parent 70e8e95 commit 52f3af5

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed

docs/src/components/ScreenShotBox/index.tsx

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import React, {
2+
useState,
3+
useRef,
4+
useEffect,
5+
CSSProperties
6+
} from 'react';
17
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
28
import useScripts from '@site/src/hooks/useScripts';
39
import Head from '@docusaurus/Head';
@@ -16,12 +22,50 @@ const example_code = `(define re #/<h1>([^>]+)<\\/h1>/)
1622
(loop (- i 1))))
1723
`;
1824

25+
const WIDTH_MIN = 50;
26+
1927
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+
2033
const { siteConfig } = useDocusaurusContext();
34+
2135
useScripts([
2236
'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',
2337
`${siteConfig.baseUrl}js/screenshooter.js`
2438
]);
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+
2569
return (
2670
<>
2771
<Head>
@@ -30,18 +74,21 @@ export default function ScreenShotBox(): JSX.Element {
3074
<link href="https://cdn.jsdelivr.net/npm/codemirror@5.58.3/addon/hint/show-hint.css" rel="stylesheet"/>
3175
<script src="https://cdn.jsdelivr.net/npm/html-to-image@1.10.4/dist/html-to-image.js"></script>
3276
</Head>
33-
<div className="box">
77+
<div className="box" ref={boxRef}>
3478
<pre className="hidden">{example_code}</pre>
3579
<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>
4592
</div>
4693
</div>
4794
<footer>

docs/src/components/ScreenShotBox/style.css

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040
.wrapper {
4141
background: #ABB8C3;
4242
padding-block: 50px;
43+
--radius: 5px;
44+
}
45+
.resizable {
46+
display: flex;
47+
}
48+
.resizable .resizer {
49+
width: 5px;
50+
cursor: ew-resize;
51+
border-radius: 0 var(--radius) var(--radius) 0;
52+
}
53+
.resizable .resizer:hover {
54+
background: #bd6b18;
4355
}
4456
.position, .cm-shadow-box, .wrapper {
4557
display: flex;
@@ -50,13 +62,16 @@
5062
.cm-dialog {
5163
width: var(--width);
5264
height: auto;
53-
border-radius: 5px;
65+
border-radius: var(--radius);
5466
box-shadow: rgba(0, 0, 0, 0.55) 0px 20px 68px;
5567
--background: #151718;
5668
background: var(--background, white);
5769
z-index: 100;
5870
overflow: hidden;
5971
}
72+
.cm-dialog:has(+.resizer:hover) {
73+
border-radius: var(--radius) 0 0 var(--radius);
74+
}
6075
.cm-dialog header {
6176
padding: 10px;
6277
}

docs/static/js/screenshooter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ function adjust_height() {
4949
body.style.setProperty('--height', rect.height + 300);
5050
}
5151
}
52-
5352
})();

0 commit comments

Comments
 (0)