提交 11d3afa6 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat: 优化 pages.json 数据结构

上级 fdbfd8f5
import { pages } from '@/target-pages.json' import { pages, groups } from '@/target-pages.json'
type Group = {
id : string,
name : string,
children : (Group | null)[] | null
}
type PageGroup = { type PageGroup = {
id : string, id : string,
name : string, name : string,
index : number | null index : number
} }
type Page = { type Page = {
path : string, path : string,
style : UTSJSONObject, style : UTSJSONObject,
group ?: PageGroup[] | null group ?: string | null
} }
export type MenuItem = { export type MenuItem = {
...@@ -22,22 +28,31 @@ export type MenuItem = { ...@@ -22,22 +28,31 @@ export type MenuItem = {
export function generateMenu(tabBarType : string) : (MenuItem | null)[] { export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
let res : (MenuItem | null)[] = [] let res : (MenuItem | null)[] = []
const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group)) const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group))
const groupTree = JSON.parse<(Group | null)[]>(JSON.stringify(groups))
tabBarPages.forEach(page => { tabBarPages.forEach(page => {
let menuItemArr : (MenuItem | null)[] = res let menuItemArr : (MenuItem | null)[] = res
let currentMenu : MenuItem | null = null let currentMenu : MenuItem | null = null
const lastGroup = page.group![page.group!.length - 1] const groupIndexList = page.group!.split(',').map((index : string) : number => parseInt(index))
let currentGroups : (Group | null)[] | null = groupTree
const pageGroups : PageGroup[] = []
groupIndexList.forEach((groupIndex, index) => {
if (index > 0) {
pageGroups.push({ id: currentGroups![groupIndex]!.id, name: currentGroups![groupIndex]!.name, index: groupIndex } as PageGroup)
}
currentGroups = currentGroups![groupIndex]!.children
})
const lastGroup = pageGroups[pageGroups.length - 1]
let hasPageGroup = false let hasPageGroup = false
if (page.path.endsWith(camelToDash(lastGroup.name)) && page.group!.length > 1) { if (page.path.endsWith(camelToDash(lastGroup.name)) && pageGroups.length > 1) {
hasPageGroup = true hasPageGroup = true
page.group!.pop() pageGroups.pop()
} }
const groupLength = page.group!.length const groupLength = pageGroups.length
page.group!.forEach((group, groupIndex) => { pageGroups.forEach((group, groupIndex) => {
const { index, id, name } = group const { index, id, name } = group
const validIndex = isNumber(index) ? index! : menuItemArr.length const validIndex = index
fillMenuArrayWithNull(menuItemArr, validIndex) fillMenuArrayWithNull(menuItemArr, validIndex)
if (menuItemArr[validIndex] == null) { if (menuItemArr[validIndex] == null) {
...@@ -57,7 +72,7 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] { ...@@ -57,7 +72,7 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
} }
}) })
if (hasPageGroup) { if (hasPageGroup) {
const pageIndex = isNumber(lastGroup.index) ? lastGroup.index! : currentMenu!.pages.length const pageIndex = lastGroup.index
fillPageArrayWithNull(currentMenu!.pages, pageIndex) fillPageArrayWithNull(currentMenu!.pages, pageIndex)
if (currentMenu!.pages[pageIndex] == null) { if (currentMenu!.pages[pageIndex] == null) {
currentMenu!.pages[pageIndex] = page currentMenu!.pages[pageIndex] = page
...@@ -72,7 +87,7 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] { ...@@ -72,7 +87,7 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
return removeNullItem(res) return removeNullItem(res)
} }
function hasPageGroup(value : PageGroup[] | null) : boolean { function hasPageGroup(value : string | null) : boolean {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
return value !== null return value !== null
// #endif // #endif
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册