Skip to content

Commit 3bb1ba2

Browse files
committed
fix: menu init open
1 parent 9360178 commit 3bb1ba2

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

packages/crd-seed/layout/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ function BasicLayout({
2222
menuSource,
2323
indexProps,
2424
}) {
25+
console.log('menuSource', menuSource)
26+
console.log('routeData', routeData)
27+
2528
const { pathname } = location
29+
console.log('location', location)
2630
const { user, repo, branch = 'main', language = 'en', menuOpenKeys } = DOCSCONFIG || {}
2731
const [inlineCollapsed, setInlineCollapsed] = useState(isMobile)
2832
const [selectedKey, setSelectedKey] = useState('')
2933
// console.log()
30-
const curOpenKeys = getOpenSubMenuKeys(pathname, menuOpenKeys)
34+
const curOpenKeys = getOpenSubMenuKeys({
35+
pathname,
36+
menuSource,
37+
menuOpenKeys
38+
})
3139
console.log('curOpenKeys', curOpenKeys)
3240
const defaultPath = (routeData.find(data => data.path === '/README')
3341
&& routeData.find(data => data.path === '/README').mdconf
@@ -113,6 +121,7 @@ function BasicLayout({
113121
}
114122
const renderMenu = (menus) => {
115123
if (menus.length < 1) return null
124+
116125
return (
117126
<Affix
118127
offsetTop={0}

packages/crd-seed/layout/utils.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,54 @@
11
/**
22
* get keys of open sub menu from pathname
3-
* input: /BasicSkill/basis/DOM
4-
* menuOpenKeys: means extra show open keys in config.yml
3+
* {
4+
* pathname: /BasicSkill/basis/DOM,
5+
* menuOpenKeys: means extra show open keys in config.yml
6+
* }
57
* output: ["/BasicSkill", "/BasicSkill/basis"]
68
*/
7-
function getOpenSubMenuKeys(pathname, menuOpenKeys) {
9+
// todo: optimize for depth-first traversal
10+
function getOpenSubMenuKeys({
11+
pathname,
12+
menuSource,
13+
menuOpenKeys
14+
}) {
815
const result = []
9-
if (menuOpenKeys) {
10-
result.push(...menuOpenKeys.split(','))
11-
}
16+
getOpenSubMenuKeysForAbbrLink(menuSource, pathname, result)
1217
/**
18+
* logic for not abbrLink
1319
* there is no pick item if the length of pathnameSplit is less than or equal to 2.
1420
* eg: /README => ["", "README"]
1521
*/
1622
const pathnameSplit = pathname.split('/')
1723
if (pathnameSplit.length <= 2) return result
1824
let recordValue = ''
19-
// eslint-disable-next-line no-plusplus
2025
for (let i = 1; i < pathnameSplit.length - 1; i++) {
2126
recordValue += `/${pathnameSplit[i]}`
2227
result.push(recordValue)
2328
}
29+
30+
/** default open menu from config.yml */
31+
if (menuOpenKeys) {
32+
result.push(...menuOpenKeys.split(','))
33+
}
34+
2435
return result
2536
}
2637

38+
function getOpenSubMenuKeysForAbbrLink(source, pathname, result) {
39+
for (let i = 0; i < source.length; i++) {
40+
const { type, path, mdconf } = source[i]
41+
if (type === 'directory') {
42+
result.push(path)
43+
const ifFind = getOpenSubMenuKeysForAbbrLink(source[i].children, pathname, result)
44+
if (ifFind) return true
45+
result.pop()
46+
} else {
47+
if (pathname.indexOf(mdconf.abbrlink) > -1) {
48+
return true
49+
}
50+
}
51+
}
52+
}
53+
2754
export { getOpenSubMenuKeys }

0 commit comments

Comments
 (0)