-
Notifications
You must be signed in to change notification settings - Fork 119
/
index.js
executable file
路55 lines (52 loc) 路 1.69 KB
/
index.js
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
import React, { useState, useRef } from 'react';
import styled from 'styled-components';
import { useProvided } from 'nonaction';
import { TextContainer } from '../../Container';
import Previewer from './Previewer';
import Editor from './Editor';
import DragBar from './DragBar.js';
import 'github-markdown-css';
import useDrop from '../../Container/Hooks/useDrop.js';
import uploadFile from '../../Lib/uploadFile.js';
import createEventTargetHook from 'create-event-target-hook';
const useWindowEvent = createEventTargetHook(window);
const Markdown = ({ className }) => {
const [text, setText] = useProvided(TextContainer);
const [isDrag, setDrag] = useState(false);
const [startX, setStartX] = useState(0);
const [width, setWidth] = useState(window.innerWidth / 2);
const markdownRef = useRef(null);
const [uploading, isOver] = useDrop(markdownRef, uploadFile);
// Partial fileText & text
useWindowEvent('mouseup', () => setDrag(false));
// The state `isDrag` must be false, when mouse up!
// So we listen it in window! (Seems ugly, but it just works ha.)
return (
<div
ref={markdownRef}
style={{ opacity: isOver || uploading ? 0.5 : 1 }}
className={className}
onMouseMove={e => {
if (!isDrag) return;
const pageX = e.nativeEvent.pageX;
setWidth(pageX - startX);
}}
>
<Editor className="no-print" width={width} setText={setText} />
<DragBar
className="no-print"
isDrag={isDrag}
setDrag={setDrag}
setStartX={setStartX}
/>
<Previewer>{text}</Previewer>
</div>
);
};
export default styled(Markdown)`
* {
box-sizing: border-box;
}
height: calc(100% - 40px);
display: flex;
`;