1549469775 4 years ago
parent
commit
2718e3951c
  1. 1
      .gitignore
  2. 5
      jest.config.js
  3. 1956
      package-lock.json
  4. 6
      package.json
  5. 50
      src/render/plugins/useRoute/index.ts
  6. 56
      src/render/route.tsx
  7. 0
      te22st.js

1
.gitignore

@ -4,3 +4,4 @@ out
.vscode
dist/electron
计划.md
coverage

5
jest.config.js

@ -0,0 +1,5 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

1956
package-lock.json

File diff suppressed because it is too large

6
package.json

@ -19,7 +19,8 @@
"packageaaa": "electron-forge package",
"make": "electron-forge make",
"build:vite": "vite build",
"serve": "vite preview"
"serve": "vite preview",
"test": "jest"
},
"keywords": [],
"author": "TopOne",
@ -37,6 +38,7 @@
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.2.3",
"@types/jest": "^26.0.24",
"@types/lodash": "^4.14.172",
"@types/markdown-it": "^12.0.3",
"@types/minimist": "^1.2.2",
@ -62,6 +64,7 @@
"execa": "^5.1.1",
"i18next": "^20.3.4",
"i18next-browser-languagedetector": "^6.1.2",
"jest": "^27.0.6",
"lodash": "^4.17.21",
"markdown-it": "^12.2.0",
"node-loader": "^2.0.0",
@ -75,6 +78,7 @@
"redux-saga": "^1.1.3",
"sass": "^1.35.1",
"styled-jsx": "^3.4.4",
"ts-jest": "^27.0.4",
"ts-loader": "^9.2.4",
"ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1",

50
src/render/plugins/useRoute/index.ts

@ -0,0 +1,50 @@
import { useLocation } from "react-router-dom"
export function foundRoute(route: any, name: string, arrIndex?: number[]): any {
if (!arrIndex) {
arrIndex = []
}
for (let i = 0; i < route.length; i++) {
const element = route[i]
if (element.root) {
arrIndex = []
}
arrIndex.push(i)
let lena = element.path.split("/").length
let lenb = name.split("/").length
if (element.path == name && lena == lenb) {
return arrIndex
} else if (element.children) {
let a = foundRoute(element.children, name, arrIndex)
if (a) {
return a
}
}
}
}
export default function useRoute(routes:any){
const { pathname } = useLocation()
let oroute = JSON.parse(JSON.stringify(routes))
let index = foundRoute(routes, pathname)
let isLayout = true
let curA = null
let curB = null
if (index && index.length) {
let res = oroute[index[0]]
let cur = res
for (let i = 1; i < index.length; i++) {
const element = index[i]
cur.children = [cur.children[element]]
cur = cur.children[element]
}
if (cur) {
cur.children = []
}
if (cur.layout != undefined) {
isLayout = !!cur.layout
}
curA = cur
curB = res
}
return [curA,curB]
}

56
src/render/route.tsx

@ -2,33 +2,10 @@ import Page404 from "@/views/Auth/Page404"
import Home from "@/views/Home"
import Login from "@/views/Login"
import React, { lazy } from "react"
import { useLocation } from "react-router-dom"
import useRouter from "@render/plugins/useRoute"
export const Loading = (props: any) => <div style={{ color: props.color }}>Lodeing.22..</div>
function foundRoute(route: any, name: string, arrIndex?: number[]): any {
if (!arrIndex) {
arrIndex = []
}
for (let i = 0; i < route.length; i++) {
const element = route[i]
if (element.root) {
arrIndex = []
}
arrIndex.push(i)
let lena = element.path.split("/").length
let lenb = name.split("/").length
if (element.path == name && lena == lenb) {
return arrIndex
} else if (element.children) {
let a = foundRoute(element.children, name, arrIndex)
if (a) {
return a
}
}
}
}
let delay =
(Comp: any, duration = 1000) =>
() =>
@ -46,7 +23,7 @@ let delay =
})
)
const routes = [
const routesArray = [
{
path: "/about",
component: lazy(() => import("@/views/About")),
@ -110,31 +87,6 @@ const routes = [
},
]
export function useRoute(){
const { pathname } = useLocation()
let oroute = JSON.parse(JSON.stringify(routes))
let index = foundRoute(routes, pathname)
let isLayout = true
let curA = null
let curB = null
if (index && index.length) {
let res = oroute[index[0]]
let cur = res
for (let i = 1; i < index.length; i++) {
const element = index[i]
cur.children = [cur.children[element]]
cur = cur.children[element]
}
if (cur) {
cur.children = []
}
if (cur.layout != undefined) {
isLayout = !!cur.layout
}
curA = cur
curB = res
}
return [curA,curB]
}
export const useRoute = useRouter.bind(null, routesArray)
export default routes
export default routesArray

0
test.js → te22st.js

Loading…
Cancel
Save