Skip to content

Commit

Permalink
feat: support nodes as input on $goto, $url and $isActive
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Jan 24, 2024
1 parent 6b5fc79 commit 4d96924
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
43 changes: 27 additions & 16 deletions lib/runtime/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,28 @@ export const getPath = (node1, node2) => {

/**
* @callback Goto
* @param {string} path relative, absolute or named URL
* @param {string|RNodeRuntime} pathOrNode relative, absolute or named URL
* @param {Object.<string, string>=} userParams
* @param {Partial<$UrlOptions & RouteState>=} options
* @type {Readable<Goto>} */
export const goto = {
subscribe: (run, invalidate) => {
const { router } = contexts
return derived(url, $url => (path, userParams, options) => {
const defaults = { mode: 'push', state: {} }
options = { ...defaults, ...options }
const newUrl = $url(path, userParams, options)
router.url[options.mode](newUrl, options.state)
return ''
}).subscribe(run, invalidate)

return derived(url, $url =>
/** @type {Goto} */
(pathOrNode, userParams, options) => {
const path =
typeof pathOrNode === 'string' ? pathOrNode : pathOrNode?.path

/** @type {options} */
const defaults = { mode: 'push', state: {} }
options = { ...defaults, ...options }
const newUrl = $url(path, userParams, options)
router.url[options.mode](newUrl, options.state)
return ''
},
).subscribe(run, invalidate)
},
}

Expand Down Expand Up @@ -90,7 +98,7 @@ export const goto = {

/**
* @typedef {((
* inputPath: string,
* pathOrNode: string|RNodeRuntime,
* userParams?: { [x: string]: string; },
* options?: Partial<$UrlOptions>
* ) => string)} UrlFromString
Expand Down Expand Up @@ -150,7 +158,8 @@ export const url = {
export const createUrl =
(fragment, router) =>
/** @type {UrlFromString} */
(_inputPath, userParams = {}, options = {}) => {
(pathOrNode, userParams = {}, options = {}) => {
let _inputPath = typeof pathOrNode === 'string' ? pathOrNode : pathOrNode?.path
const route = fragment.route

// in case we swapped the routes tree (rootNode), make sure we find
Expand Down Expand Up @@ -217,7 +226,7 @@ export const params = {

/**
* @callback IsActive
* @param {String=} path
* @param {String|RNodeRuntime=} pathOrNode
* @param {Object.<string,string>} [params]
* @param {IsActiveOptions} [options]
* @returns {Boolean}
Expand Down Expand Up @@ -317,7 +326,9 @@ export const isActiveUrl = renderContext => {
const { router, fragment } = renderContext

/** @type {IsActive} */
return (path, params = {}, options = {}) => {
return (pathOrNode, params = {}, options = {}) => {
let _path = typeof pathOrNode === 'string' ? pathOrNode : pathOrNode?.path

/**
* @type {{recursive: boolean, silent: TraverseOptions['silent']}}
*/
Expand All @@ -326,7 +337,7 @@ export const isActiveUrl = renderContext => {

// if we're using a custom rootNode, we need to strip it from the path
if (router.rootNode.path !== '/')
path = path.substring(router.rootNode.path.length)
_path = _path.substring(router.rootNode.path.length)

/**
* @type {TraverseOptions}
Expand All @@ -343,9 +354,9 @@ export const isActiveUrl = renderContext => {
)
if (!allWantedParamsAreInActiveChain) return false

const wantedNode = path.startsWith('.')
? fragment.node.traverse(path, chainOptions)
: router.rootNode.getChainTo(path, chainOptions).pop().node
const wantedNode = _path.startsWith('.')
? fragment.node.traverse(_path, chainOptions)
: router.rootNode.getChainTo(_path, chainOptions).pop().node

const actNodes = [...route.fragments.map(fragment => fragment.node)]

Expand Down
14 changes: 7 additions & 7 deletions typings/lib/runtime/helpers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getMRCA(node1: RNodeRuntime, node2: RNodeRuntime): {
export function getPath(node1: any, node2: any): string;
/**
* @callback Goto
* @param {string} path relative, absolute or named URL
* @param {string|RNodeRuntime} pathOrNode relative, absolute or named URL
* @param {Object.<string, string>=} userParams
* @param {Partial<$UrlOptions & RouteState>=} options
* @type {Readable<Goto>} */
Expand Down Expand Up @@ -47,7 +47,7 @@ export const goto: Readable<Goto>;
*/
/**
* @typedef {((
* inputPath: string,
* pathOrNode: string|RNodeRuntime,
* userParams?: { [x: string]: string; },
* options?: Partial<$UrlOptions>
* ) => string)} UrlFromString
Expand All @@ -69,7 +69,7 @@ export const params: Readable<{
}>;
/**
* @callback IsActive
* @param {String=} path
* @param {String|RNodeRuntime=} pathOrNode
* @param {Object.<string,string>} [params]
* @param {IsActiveOptions} [options]
* @returns {Boolean}
Expand All @@ -79,7 +79,7 @@ export const isActive: Readable<IsActive>;
export namespace isActiveFragment {
function subscribe(run: any, invalidate: any): import("svelte/store").Unsubscriber;
}
export function isActiveUrl(renderContext: RenderContext): (path: string, params?: {
export function isActiveUrl(renderContext: RenderContext): (pathOrNode: string | import("../Instance/RNodeRuntime.js").RNodeRuntime, params?: {
[x: string]: string;
}, options?: IsActiveOptions) => boolean;
export function resolveNode(path: string): import("../Instance/RNodeRuntime.js").RNodeRuntime;
Expand Down Expand Up @@ -108,7 +108,7 @@ export const beforeUrlChange: Readable<(arg0: BeforeUrlChangeCallback) => any>;
* @type {getDirectionCB & Readable<ReturnType<getDirectionCB>>}
*/
export const getDirection: getDirectionCB & Readable<ReturnType<getDirectionCB>>;
export type Goto = (path: string, userParams?: {
export type Goto = (pathOrNode: string | RNodeRuntime, userParams?: {
[x: string]: string;
} | undefined, options?: Partial<$UrlOptions & RouteState> | undefined) => any;
export type Readable<T> = import('svelte/store').Readable<T>;
Expand Down Expand Up @@ -144,10 +144,10 @@ export type RouteState = {
export type Url = <T extends string | HTMLAnchorElement>(inputPath: T, userParams?: {
[x: string]: string;
}, options?: Partial<$UrlOptions>) => T extends HTMLAnchorElement ? void : string;
export type UrlFromString = (inputPath: string, userParams?: {
export type UrlFromString = (pathOrNode: string | RNodeRuntime, userParams?: {
[x: string]: string;
}, options?: Partial<$UrlOptions>) => string;
export type IsActive = (path?: string | undefined, params?: {
export type IsActive = (pathOrNode?: (string | RNodeRuntime) | undefined, params?: {
[x: string]: string;
}, options?: IsActiveOptions) => boolean;
export type getDirectionCB = (boundary?: RNodeRuntime | undefined, newRoute?: Route | undefined, oldRoute?: Route | undefined) => 'first' | 'last' | 'same' | 'next' | 'prev' | 'higher' | 'lower' | 'na';

0 comments on commit 4d96924

Please sign in to comment.