-
Notifications
You must be signed in to change notification settings - Fork 15
/
App.tsx
216 lines (193 loc) · 7.61 KB
/
App.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import * as React from 'react'
import { useState, Suspense, useEffect } from 'react'
import './css/App.css'
import './css/Topbar.css'
import './css/Modal.css'
//import './css/dark-theme.css'
import PrivacyPolicy from './PrivacyPolicy'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faArrowRotateRight, faArrowUpRightFromSquare, faDownload, faBars, faXmark } from '@fortawesome/free-solid-svg-icons'
const Editor = React.lazy(() => import('./Editor'))
import { ReactComponent as Logo } from './assets/logo.svg'
import { saveAs } from 'file-saver';
import Settings from './Settings'
import Tools from './Tools'
import Examples from './Examples'
import LoadingMenu from './LoadingMenu'
import { config } from './config/config'
function formatArgs(args) {
let out = '#' + Object.entries(args).map(([key, val]) => (val ? `${key}=${val}` : null)).filter((x) => x).join('&')
if (out == '#') {
return ' '
}
return out
}
function parseArgs() {
let _args = window.location.hash.replace('#', '').split('&').map((s) => s.split('=')).filter(x => x[0])
return Object.fromEntries(_args)
}
const App: React.FC = () => {
const [restart, setRestart] = useState<(project?) => Promise<void>>()
const [navOpen, setNavOpen] = useState(false)
const menuRef = React.useRef<HTMLDivElement>()
const submenuRef = React.useRef<HTMLDivElement>()
// Open a submenu. We manage submenus here so that only one submenu can be open at any time.
const [submenu, setSubmenu] = useState<React.JSX.Element>(null)
function openSubmenu(ev: React.MouseEvent, component: React.JSX.Element) {
setNavOpen(true)
setSubmenu(component)
ev.stopPropagation()
}
function closeNav() {
setNavOpen(false)
}
/* Option to change themes */
const isBrowserDefaultDark = () => window.matchMedia('(prefers-color-scheme: dark)').matches
const [theme, setTheme] = React.useState(isBrowserDefaultDark() ? 'GithubDark' : 'lightPlus')
const [content, setContent] = useState<string>('')
const [url, setUrl] = useState<string>(null)
const [project, setProject] = useState<string>('mathlib-demo')
const [contentFromUrl, setContentFromUrl] = useState<string>(null)
const readHash = () => {
let args = parseArgs()
if (args.code) {setContent(decodeURIComponent(args.code))}
if (args.url) {setUrl(decodeURIComponent(args.url))}
if (args.project) {
console.log(`setting project to ${args.project}`)
setProject(args.project)
}
}
const onDidChangeContent = (newContent) => {
setContent(newContent)
}
const save = () => {
var blob = new Blob([content], {type: "text/plain;charset=utf-8"});
saveAs(blob, "Lean4WebDownload.lean");
}
const loadFromUrl = (url: string, project=null) => {
setUrl((oldUrl) => {
if (oldUrl === url) {
setContent(contentFromUrl)
}
return url
})
if (project) {
setProject(project)
}
}
useEffect(() => {
// Trigger onhashchange once in the beginning
readHash()
// Closing the dropdown menu or submenu when clicking outside it.
// Use `ev.stopPropagation()` or `ev.stopImmediatePropagation()` inside
// the menu to prevent.
document.body.addEventListener("click", (ev) => {
if (menuRef?.current) {
if (menuRef.current.contains(ev.target as HTMLElement)) {
if(submenuRef?.current && submenuRef.current.contains(ev.target as HTMLElement)) {
console.log('keeping submenu open')
} else {
// Close submenu when clicking inside the menu
setSubmenu(null)
console.log('closing submenu')
}
ev.stopImmediatePropagation()
} else {
// Close Nav on clicking somewhere outside the menu
setNavOpen(false)
console.log('closing nav')
}
}
})
}, [])
// // if ("onhashchange" in window) // does the browser support the hashchange event?
// // window.addEventListener('hashchange', readHash)
useEffect(() => {
//let args = parseArgs()
let _project = (project == 'mathlib-demo' ? null : project)
if (content === contentFromUrl) {
let args = {project: _project, url: encodeURIComponent(url), code: null}
history.replaceState(undefined, undefined, formatArgs(args))
} else if (content === "") {
let args = {project: _project, url: null, code: null}
history.replaceState(undefined, undefined, formatArgs(args))
} else {
let args = {project: _project, url: null, code: encodeURIComponent(content)}
history.replaceState(undefined, undefined, formatArgs(args))
}
}, [project, content])
useEffect(() => {
if (url !== null) {
setContent("Loading...")
setContentFromUrl("Loading...")
fetch(url)
.then((response) => response.text())
.then((content) => {
setContent(content)
setContentFromUrl(content)
})
.catch( err => {
setContent(err.toString())
setContentFromUrl(err.toString())
})
}
}, [url])
useEffect(() => {
if (restart) {
console.log(`changing Lean version to ${project}`)
restart(project)
}
}, [project])
return (
<div className={'app monaco-editor'}>
<div className='nav'>
<Logo className='logo' />
<div className='menu' ref={menuRef}>
{!config.verticalLayout && <>
{/* Buttons for desktop version */}
<Examples loadFromUrl={loadFromUrl} openSubmenu={openSubmenu} closeNav={closeNav}/>
<LoadingMenu loadFromUrl={loadFromUrl} setContent={setContent} openSubmenu={openSubmenu} closeNav={closeNav}/>
</>
}
<span className={"nav-link nav-icon"} onClick={(ev) => {setNavOpen(!navOpen)}}>
{navOpen ? <FontAwesomeIcon icon={faXmark} /> : <FontAwesomeIcon icon={faBars} />}
</span>
<div className={'dropdown' + (navOpen ? '' : ' hidden')}>
{config.verticalLayout && <>
{/* Buttons for mobile version */}
<Examples loadFromUrl={loadFromUrl} openSubmenu={openSubmenu} closeNav={closeNav}/>
<LoadingMenu loadFromUrl={loadFromUrl} setContent={setContent} openSubmenu={openSubmenu} closeNav={closeNav}/>
</>}
<Settings closeNav={closeNav} theme={theme} setTheme={setTheme}
project={project} setProject={setProject}/>
<span className="nav-link" onClick={restart}>
<FontAwesomeIcon icon={faArrowRotateRight} /> Restart server
</span>
<Tools />
<span className="nav-link" onClick={save}>
<FontAwesomeIcon icon={faDownload} /> Save file
</span>
<PrivacyPolicy />
<a className="nav-link" href="https://leanprover-community.github.io/" target="_blank">
<FontAwesomeIcon icon={faArrowUpRightFromSquare} /> Lean community
</a>
<a className="nav-link" href="https://leanprover.github.io/lean4/doc/" target="_blank">
<FontAwesomeIcon icon={faArrowUpRightFromSquare} /> Lean documentation
</a>
<a className="nav-link" href="https://github.com/hhu-adam/lean4web" target="_blank">
<FontAwesomeIcon icon={faArrowUpRightFromSquare} /> GitHub
</a>
<div className="submenu" ref={submenuRef}>
{submenu && submenu}
</div>
</div>
</div>
</div>
<Suspense fallback={<div className="loading-ring"></div>}>
<Editor setRestart={setRestart}
value={content} onDidChangeContent={onDidChangeContent} theme={theme} project={project}/>
</Suspense>
</div>
)
}
export default App