UNPKG

@alitajs/keep-alive

Version:
12 lines (9 loc) 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _default = function _default(absTmpPath) { return "\nimport React from 'react';\nimport { getRoutes } from '".concat(absTmpPath, "/core/routes';\nimport { setLayoutInstance } from './KeepAliveModel';\nimport pathToRegexp from '@umijs/deps/compiled/path-to-regexp';\nimport { matchRoutes } from 'react-router-config';\nimport * as app from '@/app';\n\nconst runTimeConfig = app;\nconst isKeepPath = (aliveList:any[],path:string)=>{\n let isKeep = false;\n aliveList.map(item=>{\n if(item === path){\n isKeep = true;\n }\n if(item instanceof RegExp && item.test(path)){\n isKeep = true;\n }\n if(typeof item === 'string' && item.toLowerCase() === path){\n isKeep = true;\n }\n })\n return isKeep;\n}\nconst getKeepAliveViewMap = (routeList:any[],aliveList:any[])=>{\n let keepAliveMap = {};\n function find(routess: any[], list:any[]) {\n if(!routess|| !list ){\n return routess;\n }\n return routess.map(element => {\n if (!Array.isArray(element.routes)&&isKeepPath(list,element.path?element.path.toLowerCase():'')) {\n element.recreateTimes = 0;\n keepAliveMap[element.path.toLowerCase()] = element;\n }else{\n element.routes = find(element.routes,aliveList);\n }\n return element;\n });\n }\n find(routeList,aliveList)\n return keepAliveMap;\n}\nconst getView = (\n pathname: string,\n keepAliveViewMap: { [key: string]: any },\n) => {\n let View;\n for (const key in keepAliveViewMap) {\n if (pathToRegexp(key).test(pathname)) {\n View = keepAliveViewMap[key]\n break;\n }\n }\n return View;\n};\ninterface PageProps {\n location: {\n pathname: string;\n };\n}\nexport default class BasicLayout extends React.PureComponent<PageProps> {\n constructor(props: any) {\n super(props);\n this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(), props.keepalive);\n const patchKeepAlive = async (func: (config: anyd[]) => any[]) => {\n const keepalive = await func(props.keepalive);\n this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(), keepalive);\n }\n this.patchKeepAlive = patchKeepAlive;\n this.state = { keepaliveConfig: props.keepalive, keepAliveViewMap: this.keepAliveViewMap };\n // TODO: \u4E34\u65F6\u652F\u6301\u52A8\u6001\u7684 keepalive\uFF0C\u5C06 map \u653E\u5230 state \u4E2D\uFF0C\u975E\u5E38\u4E0D\u53CB\u597D\n this.init();\n }\n async init() {\n if (runTimeConfig?.getKeepAlive) {\n try {\n const keepaliverumtime = await runTimeConfig?.getKeepAlive(this.state.keepaliveConfig);\n this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(), keepaliverumtime);\n this.setState({\n keepaliveConfig: keepaliverumtime,\n keepAliveViewMap: this.keepAliveViewMap\n })\n } catch (error) {\n console.error(error);\n }\n }\n }\n componentDidMount() {\n setLayoutInstance(this);\n }\n keepAliveViewMap = {};\n\n alivePathnames: string[] = [];\n\n render() {\n const {\n location: { pathname },\n } = this.props;\n const { keepAliveViewMap } = this.state;\n const showKeepAlive = !!getView(pathname, keepAliveViewMap);\n if (showKeepAlive) {\n const index = this.alivePathnames.findIndex(\n tPathname => tPathname === pathname.toLowerCase(),\n );\n if (index === -1) {\n this.alivePathnames.push(pathname.toLowerCase());\n }\n }\n return (\n <>\n {this.alivePathnames.map(curPathname => {\n const currentView = getView(curPathname, this.keepAliveViewMap);\n const { component: View, recreateTimes } = currentView;\n const matchRoute = matchRoutes([currentView], curPathname)[0];\n const pageProps: any = { ...this.props,...matchRoute };\n return View ? (\n <div\n id={`BasicLayout-${curPathname}`}\n key={\n curPathname + recreateTimes\n }\n style={{ height: '100%', width: '100%', position: 'relative', overflow: 'hidden auto' }}\n className=\"rumtime-keep-alive-layout\"\n hidden={curPathname !== pathname.toLowerCase()}\n >\n <View {...pageProps} />\n </div>\n ) : null;\n })}\n <div hidden={showKeepAlive} style={{ height: '100%', width: '100%', position: 'relative', overflow: 'hidden auto' }} className=\"rumtime-keep-alive-layout-no\">\n {!showKeepAlive && this.props.children}\n </div>\n </>\n )\n }\n}\n"); }; exports.default = _default;