-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97a27b2
commit 45aeacd
Showing
27 changed files
with
736 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const PACKAGE_NAME = '@shuvi/platform-mp'; | ||
export const PACKAGE_ROUTER = '@shuvi/router-mp'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "@shuvi/router-mp", | ||
"version": "0.0.1-rc.32", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/shuvijs/shuvi.git", | ||
"directory": "packages/router-mp" | ||
}, | ||
"author": "liximomo", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"dev": "run-p watch:*", | ||
"watch:esm": "tsc -p tsconfig.build.esm.json -w", | ||
"watch:cjs": "tsc -p tsconfig.build.cjs.json -w", | ||
"prebuild": "rimraf lib esm", | ||
"build": "run-p build:*", | ||
"build:esm": "tsc -p tsconfig.build.esm.json", | ||
"build:cjs": "tsc -p tsconfig.build.cjs.json" | ||
}, | ||
"engines": { | ||
"node": ">= 12.0.0" | ||
}, | ||
"dependencies": { | ||
"@shuvi/router-react": "^0.0.1-rc.32", | ||
"@shuvi/router": "^0.0.1-rc.32", | ||
"@shuvi/types": "^0.0.1-rc.32" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^16.9.43" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
useResolvedPath, | ||
useCurrentRoute, | ||
useNavigate | ||
} from '@shuvi/router-react'; | ||
import { pathToString, State, PathRecord } from '@shuvi/router'; | ||
import { View } from '@tarojs/components'; | ||
import { __DEV__ } from './constants'; | ||
|
||
function isModifiedEvent(event: React.MouseEvent) { | ||
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey); | ||
} | ||
|
||
/** | ||
* The public API for rendering a history-aware <a>. | ||
*/ | ||
export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>( | ||
function LinkWithRef( | ||
{ onClick, replace: replaceProp = false, state, target, to, ...rest }, | ||
ref | ||
) { | ||
let navigate = useNavigate(); | ||
const location = useCurrentRoute(); | ||
let path = useResolvedPath(to); | ||
|
||
function handleClick(event: React.MouseEvent<HTMLAnchorElement>) { | ||
if (onClick) onClick(event); | ||
if ( | ||
!event.defaultPrevented && // onClick prevented default | ||
!isModifiedEvent(event) // Ignore clicks with modifier keys | ||
) { | ||
event.preventDefault(); | ||
|
||
// If the URL hasn't changed, a regular <a> will do a replace instead of | ||
// a push, so do the same here. | ||
let replace = | ||
!!replaceProp || | ||
pathToString(location) === pathToString(path) || | ||
!target || | ||
target === '_self'; | ||
|
||
navigate(to, { replace, state }); | ||
} | ||
} | ||
return ( | ||
// @ts-ignore | ||
<View {...rest} ref={ref} onClick={handleClick} /> | ||
); | ||
} | ||
); | ||
|
||
export interface LinkProps | ||
extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> { | ||
replace?: boolean; | ||
state?: State; | ||
to: PathRecord; | ||
} | ||
|
||
if (__DEV__) { | ||
Link.displayName = 'MpLink'; | ||
Link.propTypes = { | ||
onClick: PropTypes.func, | ||
replace: PropTypes.bool, | ||
state: PropTypes.object, | ||
target: PropTypes.string, | ||
to: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.shape({ | ||
pathname: PropTypes.string, | ||
search: PropTypes.string, | ||
hash: PropTypes.string | ||
}) | ||
]).isRequired | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const __DEV__ = process.env.NODE_ENV !== 'production'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { IRouteRecord } from '@shuvi/router'; | ||
|
||
const _globalRoutes: IRouteRecord[] = []; | ||
|
||
export function getGlobalRoutes() { | ||
return _globalRoutes; | ||
} | ||
|
||
export function addGlobalRoutes( | ||
routePath: string, | ||
routeComponent: React.ReactElement | ||
) { | ||
for (let i = 0; i < _globalRoutes.length; i++) { | ||
const { path, component } = _globalRoutes[i]; | ||
if (path === routePath && component === routeComponent) { | ||
return _globalRoutes; | ||
} | ||
} | ||
_globalRoutes.push({ | ||
path: routePath, | ||
component: routeComponent | ||
}); | ||
return _globalRoutes; | ||
} | ||
|
||
export function delGlobalRoutes( | ||
routePath: string, | ||
routeComponent: React.ReactElement | ||
) { | ||
for (let i = _globalRoutes.length - 1; i <= 0; i--) { | ||
const { path, component } = _globalRoutes[i]; | ||
if (path === routePath && component === routeComponent) { | ||
_globalRoutes.splice(i, 1); | ||
} | ||
} | ||
return _globalRoutes; | ||
} |
Oops, something went wrong.