forked from microvoid/marktion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tsx
162 lines (135 loc) · 4.42 KB
/
main.tsx
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React, { useCallback, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom/client';
import { GitHubLogoIcon, SunIcon, MoonIcon } from '@radix-ui/react-icons';
import MarktionEditor, { MarktionProps, MarktionRef } from './src/marktion';
import { FloatButton, Segmented, Tooltip } from 'antd';
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
const INIT_MARKDOWN = [import.meta.env.VITE_README_ZH, import.meta.env.VITE_README_EN];
function App() {
const marktionRef = useRef<MarktionRef>(null);
const { isDarkMode, toggle } = useDarkMode();
const [lang, setLang] = useState(0);
const [tooltipOpen, setTooltipOpen] = useState(false);
const onUploadImage = useCallback<NonNullable<MarktionProps['onUploadImage']>>(file => {
return new Promise(resolve => {
const reader = new FileReader();
reader.onload = e => {
resolve(e.target?.result as string);
};
reader.readAsDataURL(file);
});
}, []);
const onExport = () => {
const content = marktionRef.current?.getMarkdown();
if (content) {
const filename = getMarktionTitle(content) || 'marktion';
downloadFile(`${filename}.md`, content);
}
};
useEffect(() => {
setTimeout(() => {
setTooltipOpen(true);
}, 2000);
}, []);
return (
<>
<header className="w-full p-4 dark:text-gray-100">
<div className="container flex justify-between items-center h-16 mx-auto">
<Tooltip open={tooltipOpen} title="Star on Github" placement="right" color="purple">
<a
href="https://github.com/microvoid/marktion"
className="rounded-lg cursor-pointer p-2 transition-colors duration-200 hover:bg-stone-100 hover:dark:text-black sm:bottom-auto sm:top-5"
>
<GitHubLogoIcon />
</a>
</Tooltip>
<div
onClick={toggle}
className="rounded-lg cursor-pointer p-2 transition-colors duration-200 hover:bg- hover:text-base sm:bottom-auto sm:top-5"
>
{isDarkMode ? <MoonIcon /> : <SunIcon />}
</div>
</div>
</header>
<div className="max-w-screen-lg w-full">
<div className="container flex justify-center">
<Segmented
options={[
{
label: '中文',
value: 0
},
{
label: 'English',
value: 1
}
]}
value={lang}
onChange={value => {
const index = Number(value);
marktionRef.current?.editor.commands.setMarkdwon(INIT_MARKDOWN[index]);
setLang(index);
}}
/>
</div>
<MarktionEditor
ref={marktionRef}
darkMode={isDarkMode}
markdown={INIT_MARKDOWN[lang]}
onUploadImage={onUploadImage}
>
<FloatButton tooltip="Export markdwon file" onClick={onExport} />
</MarktionEditor>
</div>
</>
);
}
function useDarkMode() {
const [isDarkMode, setIsDarkMode] = useState(() => {
let theme = getThemeFromStorage();
if (theme === null) {
theme = getThemeFromSystem();
}
return theme === 'dark';
});
useEffect(() => {
if (isDarkMode) {
document.documentElement.setAttribute('data-mode', 'dark');
} else {
document.documentElement.removeAttribute('data-mode');
}
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
}, [isDarkMode]);
return {
isDarkMode,
toggle: () => setIsDarkMode(!isDarkMode)
};
}
function getThemeFromStorage() {
const theme = localStorage.getItem('theme') || '';
if (!['dark', 'light'].includes(theme)) {
return null;
}
return theme;
}
function getThemeFromSystem() {
// const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// return isDark ? 'dark' : 'light';
return 'light';
}
async function downloadFile(filename: string, content: string) {
const FileSaver = (await import('file-saver')).default;
const blob = new Blob([content], {
type: 'text/plain;charset=utf-8'
});
return FileSaver.saveAs(blob, filename);
}
function getMarktionTitle(markdown: string) {
const paragraphs = markdown.split('\n');
const heading = paragraphs.find(item => item.startsWith('#')) || '';
return heading.replace(/#+\s/, '');
}