-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
337 lines (324 loc) · 10 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import * as React from 'react'
import { Routes, Link, Route, Navigate, useLocation } from 'react-router-dom'
import cx from 'classnames'
import { ifDev, ifProd, ifPrerender } from 'crd-client-utils'
const Giscus = require('@giscus/react')
import Menu from '../component/Menu'
import Icon from '../component/Icon'
import Affix from '../component/Affix'
import Header from '../component/Header'
import Footer from '../component/Footer'
import Tags from '../component/Tags'
import languageMap from '../language'
import { isMobile, ifAddPrefix } from '../utils'
import { getOpenSubMenuKeys } from './utils'
import logo from '../crd.logo.svg'
import styles from './index.less'
import '../style/mobile.less'
const { useState, useEffect, useMemo } = React
const SubMenu = Menu.SubMenu
function BasicLayout({
routeData,
menuSource
}) {
const location = useLocation()
const { pathname } = location
const {
user,
repo,
branch = 'main',
language = 'en',
menuOpenKeys,
tags,
comment
} = DOCSCONFIG || {}
const [inlineCollapsed, setInlineCollapsed] = useState(true)
const [selectedKey, setSelectedKey] = useState('')
const curOpenKeys = getOpenSubMenuKeys({
pathname,
menuSource,
menuOpenKeys
})
const defaultPath = (routeData.find(data => data.path === '/README')
&& routeData.find(data => data.path === '/README').mdconf
&& routeData.find(data => data.path === '/README').mdconf.abbrlink) || 'README'
useEffect(() => {
if (ifPrerender) {
scrollToTop()
INJECT?.inject?.()
}
}, [])
useEffect(() => {
INJECT?.injectWithPathname?.(pathname)
}, [pathname])
useEffect(() => {
const { pathname } = location
let newPathName = pathname
// fix https://github.com/MuYunyun/create-react-doc/issues/195
if (newPathName.endsWith('/')) {
newPathName = newPathName.slice(0, newPathName.length - 1)
}
if (newPathName.startsWith(`/${repo}`)) {
newPathName = newPathName.slice(`/${repo}`.length, newPathName.length)
}
setSelectedKey(newPathName || defaultPath)
}, location.pathname)
const scrollToTop = () => {
document.body.scrollTop = 0
document.documentElement.scrollTop = 0
window.scrollTo(0, 0)
}
const renderSubMenuItem = (menus) => {
return (
<>
{menus.map((item, index) => {
const { mdconf, routePath } = item || {}
const { abbrlink } = mdconf || {}
const path = abbrlink ? `/${abbrlink}` : routePath
// item.path carrys .md here.
return item.children && item.children.length > 0 ? (
<SubMenu
key={index}
keyValue={item.path}
title={item.name}
icon={<Icon type="folder" size={16} />}
>
{renderSubMenuItem(item.children)}
</SubMenu>
) : (
<Menu.Item
key={index}
icon={<Icon type="file" size={16} />}
keyValue={abbrlink ? `/${abbrlink}` : item.path}
title={
item &&
item.type === "directory" &&
item.props &&
item.props.isEmpty ? (
<span>
{(item.mdconf && item.mdconf.title) || item.name}
</span>
) : (
<Link
to={ifProd ? `/${repo}${path}` : `${path}`}
replace={pathname.indexOf(path) > -1}
>
{item && item.mdconf && item.mdconf.title
? item.mdconf.title
: item.title}
</Link>
)
}
/>
)
})}
</>
)
}
const renderMenu = (menus) => {
if (menus.length < 1) return null
return (
<Affix
offsetTop={0}
className={styles.affixPlaceholder}
wrapperClassName={styles.affixWrapper}
width={inlineCollapsed ? 0 : 240}
>
<Menu
inlineCollapsed={inlineCollapsed}
toggle={() => {
setInlineCollapsed(!inlineCollapsed)
}}
menuStyle={{
height: "100vh",
overflow: "auto",
}}
selectedKey={selectedKey}
onSelect={(keyValue) => {
setSelectedKey(keyValue)
}}
defaultOpenKeys={curOpenKeys}
>
{renderSubMenuItem(menus || [])}
</Menu>
</Affix>
)
}
/**
* This section is to show article's relevant information
* such as edit in github and so on.
*/
const renderPageHeader = () => {
const curMenuSource = routeData.filter(r => {
if (r.props.type === 'directory') return false
return pathname.indexOf(r.mdconf.abbrlink) > -1 || decodeURIComponent(pathname).indexOf(r.path) > -1
})
const editPathName = curMenuSource[0] && curMenuSource[0].props.path
const isNotTagPage = location.pathname.indexOf('/tags') === -1
return (
<div className={cx(styles.pageHeader)}>
{user && repo && isNotTagPage ? (
<a
href={`https://github.com/${user}/${
repo
}/edit/${branch}${editPathName}`}
target="_blank"
>
<Icon className={cx(styles.icon)} type="edit" size={13} />
<span>Edit in GitHub</span>
</a>
) : null}
</div>
)
}
/**
* This section is to render comment area.
* Every pathname should has its own comment module.
*/
const renderComment = useMemo(() => {
return <Giscus
key={pathname}
id="comments"
repo={`${user}/${repo}`}
category="General"
mapping="pathname"
strict="0"
reactionsEnabled="1"
emitMetadata="0"
inputPosition="top"
theme="preferred_color_scheme"
lang="en"
loading="lazy"
crossorigin="anonymous"
async
{...comment?.GiscusConfig}
/>
}, [pathname])
/**
* This section is to show article's relevant information
* such as edit in created time、edited time and so on.
*/
const renderPageFooter = () => {
// in local env, data.path is to be /READEME, however pathname may be /Users/mac/.../.crd-dist/READEME/index.html
const matchData = routeData.find((data) => pathname.indexOf(data.path) > -1)
const matchProps = matchData && matchData.props
return (
<div className={cx(styles.pageFooter)}>
{matchProps && matchProps.birthtime ? (
<span className={cx(styles.position)}>
<Icon className={cx(styles.icon)} type="create-time" size={13} />
{languageMap[language].create_tm}:
<span>{matchProps.birthtime}</span>
</span>
) : null}
{matchProps && matchProps.mtime ? (
<span className={cx(styles.position)}>
<Icon className={cx(styles.icon)} type="update-time" size={13} />
{languageMap[language].modify_tm}:
<span>
{routeData.find((data) => pathname.indexOf(data.path) > -1).props.mtime}
</span>
</span>
): null}
</div>
)
}
const isCurentChildren = () => {
const getRoute = routeData.filter((data) => pathname.indexOf(data.path) > -1)
const article = getRoute.length > 0 ? getRoute[0].article : null
const childs = menuSource.filter(
(data) =>
article === data.article && data.children && data.children.length > 1
)
return childs.length > 0
}
const isChild = isCurentChildren()
const renderMenuContainer = () => {
return (
<>
<nav
className={cx(styles.menuWrapper, {
[`${styles['menuWrapper-inlineCollapsed']}`]: inlineCollapsed,
})}
>
{renderMenu(menuSource)}
</nav>
<div
className={cx({
[`${styles.menuMask}`]: isMobile && !inlineCollapsed,
})}
onClick={(e) => {
e.stopPropagation()
setInlineCollapsed(true)
}}
/>
</>
)
}
const renderContent = () => {
return (
<div
className={cx(`${styles.content}`, {
[`${styles["content-fullpage"]}`]: inlineCollapsed || isMobile,
})}
>
<Routes>
{/* see https://reacttraining.com/react-router/web/api/Redirect/exact-bool */}
<Route
path={ifAddPrefix ? `/${repo}` : `/`}
element={<Navigate to={ifAddPrefix ? `/${repo}/${defaultPath}` : `/${defaultPath}`} replace />}
/>
{routeData.map((item) => {
const { path, mdconf, component } = item
const { abbrlink } = mdconf
const enhancePath = abbrlink ? `/${abbrlink}` : path
const Comp = component
return (
<Route
key={enhancePath}
path={ifAddPrefix ? `/${repo}${enhancePath}` : enhancePath}
element={<Comp {...item} />}
/>
)
})}
{
tags
? <>
<Route
key='/tags'
path={ifAddPrefix ? `/${repo}/tags` : '/tags'}
element={<Tags />}
/>
<Route
key='/tags/:name'
path={ifAddPrefix ? `/${repo}/tags/:name` : '/tags/:name'}
element={<Tags />}
/>
</>
: null
}
{/* Todo: follow up how to use Redirect to back up the rest of route. */}
{/* <Redirect path='/' to={ifAddPrefix ? `/${repo}/404` : `/404`} /> */}
</Routes>
{comment?.GiscusConfig ? renderComment : null}
{renderPageFooter()}
</div>
)
}
return (
<div className={styles.wrapper}>
<Header logo={logo} />
<div
className={cx(styles.wrapperContent, {
[styles.wrapperMobile]: isMobile,
})}
>
{renderPageHeader()}
{renderMenuContainer()}
{renderContent()}
<Footer inlineCollapsed={inlineCollapsed} />
</div>
</div>
)
}
export default BasicLayout